FileDocCategorySizeDatePackage
FilePreferencesImpl.javaAPI DocAndroid 1.5 API7657Wed May 06 22:41:04 BST 2009java.util.prefs

FilePreferencesImpl

public class FilePreferencesImpl extends AbstractPreferences
The default implementation of AbstractPreferences for the Linux platform, using the file system as its back end. TODO some sync mechanism with backend, Performance - check file edit date
since
Android 1.0

Fields Summary
private static final String
PREFS_FILE_NAME
private static String
USER_HOME
private static String
SYSTEM_HOME
private String
path
private Properties
prefs
private File
prefsFile
private File
dir
private Set
removed
private Set
updated
Constructors Summary
FilePreferencesImpl(boolean userNode)
Construct root FilePreferencesImpl instance, construct user root if userNode is true, system root otherwise


    /*
     * --------------------------------------------------------------
     * Constructors
     * --------------------------------------------------------------
     */
    
                        
      
        super(null, ""); //$NON-NLS-1$
        this.userNode = userNode;
        path = userNode ? USER_HOME : SYSTEM_HOME;
        initPrefs();
    
private FilePreferencesImpl(AbstractPreferences parent, String name)
Construct a prefs using given parent and given name

        super(parent, name);
        path = ((FilePreferencesImpl) parent).path + File.separator + name;
        initPrefs();
    
Methods Summary
protected java.util.prefs.AbstractPreferenceschildSpi(java.lang.String name)

        FilePreferencesImpl child = new FilePreferencesImpl(this, name);
        return child;
    
protected java.lang.String[]childrenNamesSpi()

        String[] names = AccessController
                .doPrivileged(new PrivilegedAction<String[]>() {
                    public String[] run() {
                        return dir.list(new FilenameFilter() {
                            public boolean accept(File parent, String name) {
                                return new File(path + File.separator + name).isDirectory(); 
                            }
                        });

                    }
                });
        if (null == names) {// file is not a directory, exception case
            // prefs.3=Cannot get children names for {0}!
            throw new BackingStoreException(
                    Messages.getString("prefs.3", toString()));  //$NON-NLS-1$
        }
        return names;
    
protected voidflushSpi()

        try {
            //if removed, return
            if(isRemoved()){
                return;
            }
            // reload
            Properties currentPrefs = XMLParser.loadFilePrefs(prefsFile);
            // merge
            Iterator<String> it = removed.iterator();
            while (it.hasNext()) {
                currentPrefs.remove(it.next());
            }
            removed.clear();
            it = updated.iterator();
            while (it.hasNext()) {
                Object key = it.next();
                currentPrefs.put(key, prefs.get(key));
            }
            updated.clear();
            // flush
            prefs = currentPrefs;
            XMLParser.flushFilePrefs(prefsFile, prefs);
        } catch (Exception e) {
            throw new BackingStoreException(e);
        }
    
protected java.lang.StringgetSpi(java.lang.String key)

        try {
            if (null == prefs) {
                prefs = XMLParser.loadFilePrefs(prefsFile);
            }
            return prefs.getProperty(key);
        } catch (Exception e) {// if Exception happened, return null
            return null;
        }
    
private voidinitPrefs()

        dir = new File(path);
        newNode = (AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
            public Boolean run() {
                return Boolean.valueOf(!dir.exists());
            }
        })).booleanValue();
        prefsFile = new File(path + File.separator + PREFS_FILE_NAME);
        prefs = XMLParser.loadFilePrefs(prefsFile);
    
protected java.lang.String[]keysSpi()

        return prefs.keySet().toArray(new String[0]);
    
protected voidputSpi(java.lang.String name, java.lang.String value)

        prefs.setProperty(name, value);
        updated.add(name);
    
protected voidremoveNodeSpi()

        boolean removeSucceed = (AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
            public Boolean run() {
                prefsFile.delete();
                return Boolean.valueOf(dir.delete());
            }
        })).booleanValue();
        if (!removeSucceed) {
            // prefs.4=Cannot remove {0}!
            throw new BackingStoreException(Messages.getString("prefs.4", toString()));  //$NON-NLS-1$
        }
    
protected voidremoveSpi(java.lang.String key)

        prefs.remove(key);
        updated.remove(key);
        removed.add(key);
    
protected voidsyncSpi()

        flushSpi();