You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/12/24 07:38:03 UTC
svn commit: r489990 - in
/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs:
FilePreferencesFactoryImpl.java FilePreferencesImpl.java Preferences.java
RegistryPreferencesFactoryImpl.java RegistryPreferencesImpl.java
Author: ndbeyer
Date: Sat Dec 23 22:38:02 2006
New Revision: 489990
URL: http://svn.apache.org/viewvc?view=rev&rev=489990
Log:
Cleanup and refactor code
* Add missing annotations
* move user/system root instances to factory classes
Modified:
harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java
harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java
harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/Preferences.java
harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java
harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java
Modified: harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java?view=diff&rev=489990&r1=489989&r2=489990
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java Sat Dec 23 22:38:02 2006
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-
package java.util.prefs;
/**
@@ -24,20 +23,22 @@
* @since 1.4
*/
class FilePreferencesFactoryImpl implements PreferencesFactory {
-
- /**
- * Default constructor
- */
+ // user root preferences
+ private static final Preferences USER_ROOT = new FilePreferencesImpl(true);
+
+ // system root preferences
+ private static final Preferences SYSTEM_ROOT = new FilePreferencesImpl(false);
+
public FilePreferencesFactoryImpl() {
- super();
+ super();
}
public Preferences userRoot() {
- return FilePreferencesImpl.userRoot;
+ return USER_ROOT;
}
public Preferences systemRoot() {
- return FilePreferencesImpl.systemRoot;
+ return SYSTEM_ROOT;
}
}
Modified: harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java?view=diff&rev=489990&r1=489989&r2=489990
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java Sat Dec 23 22:38:02 2006
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-
package java.util.prefs;
import java.io.File;
@@ -29,11 +28,10 @@
import org.apache.harmony.prefs.internal.nls.Messages;
/**
- * TODO some sync mechanism with backend, Performance - check file edit date
- */
-/*
* Default implementation of <code>AbstractPreferences</code> for Linux platform,
- * using file system as back end.
+ * using file system as back end.
+ *
+ * TODO some sync mechanism with backend, Performance - check file edit date
*
* @since 1.4
*/
@@ -41,59 +39,52 @@
/*
* --------------------------------------------------------------
- * Constants
+ * Class fields
* --------------------------------------------------------------
*/
- // user root preferences
- static final Preferences userRoot;
-
- // system root preferences
- static final Preferences systemRoot;
//prefs file name
- private static final String prefsFileName = "prefs.xml"; //$NON-NLS-1$
+ private static final String PREFS_FILE_NAME = "prefs.xml"; //$NON-NLS-1$
//home directory for user prefs
- static String USER_HOME;
+ private static String USER_HOME;
//home directory for system prefs
- static String SYSTEM_HOME;
+ private static String SYSTEM_HOME;
/*
* --------------------------------------------------------------
- * static init
+ * Class initializer
* --------------------------------------------------------------
*/
static {
- AccessController.doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {
+ public Void run() {
USER_HOME = System.getProperty("user.home") + "/.java/.userPrefs";//$NON-NLS-1$ //$NON-NLS-2$
SYSTEM_HOME = System.getProperty("java.home") + "/.systemPrefs";//$NON-NLS-1$//$NON-NLS-2$
return null;
}
});
- userRoot = new FilePreferencesImpl(true);
- systemRoot = new FilePreferencesImpl(false);
}
/*
* --------------------------------------------------------------
- * Variables
+ * Instance fields
* --------------------------------------------------------------
*/
//file path for this preferences node
- String path;
+ private String path;
//internal cache for prefs key-value pair
private Properties prefs;
//file represents this preferences node
- File prefsFile;
+ private File prefsFile;
//parent dir for this preferences node
- File dir;
+ private File dir;
//cache for removed prefs key-value pair
private Set<String> removed = new HashSet<String>();
@@ -106,7 +97,19 @@
* Constructors
* --------------------------------------------------------------
*/
- /*
+
+ /**
+ * Construct root <code>FilePreferencesImpl</code> instance, construct
+ * user root if userNode is true, system root otherwise
+ */
+ FilePreferencesImpl(boolean userNode) {
+ super(null, ""); //$NON-NLS-1$
+ this.userNode = userNode;
+ path = userNode ? USER_HOME : SYSTEM_HOME;
+ initPrefs();
+ }
+
+ /**
* Construct a prefs using given parent and given name
*/
private FilePreferencesImpl(AbstractPreferences parent, String name) {
@@ -122,31 +125,11 @@
return Boolean.valueOf(!dir.exists());
}
})).booleanValue();
- prefsFile = new File(path + File.separator + prefsFileName);
+ prefsFile = new File(path + File.separator + PREFS_FILE_NAME);
prefs = XMLParser.loadFilePrefs(prefsFile);
}
- /*
- * Construct root <code>FilePreferencesImpl</code> instance, construct
- * user root if userNode is true, system root otherwise
- */
- private FilePreferencesImpl(boolean userNode) {
- super(null, ""); //$NON-NLS-1$
- this.userNode = userNode;
- path = userNode ? USER_HOME : SYSTEM_HOME;
- initPrefs();
- }
-
- /*
- * --------------------------------------------------------------
- * Methods implement AbstractPreferences
- * --------------------------------------------------------------
- */
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#childrenNamesSpi()
- */
+ @Override
protected String[] childrenNamesSpi() throws BackingStoreException {
String[] names = AccessController
.doPrivileged(new PrivilegedAction<String[]>() {
@@ -167,21 +150,13 @@
return names;
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#childSpi()
- */
+ @Override
protected AbstractPreferences childSpi(String name) {
FilePreferencesImpl child = new FilePreferencesImpl(this, name);
return child;
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#flushSpi()
- */
+ @Override
protected void flushSpi() throws BackingStoreException {
try {
//if removed, return
@@ -210,11 +185,7 @@
}
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#getSpi(java.lang.String)
- */
+ @Override
protected String getSpi(String key) {
try {
if (null == prefs) {
@@ -226,31 +197,18 @@
}
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#keysSpi()
- */
+ @Override
protected String[] keysSpi() throws BackingStoreException {
return prefs.keySet().toArray(new String[0]);
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#putSpi(java.lang.String,
- * java.lang.String)
- */
+ @Override
protected void putSpi(String name, String value) {
prefs.setProperty(name, value);
updated.add(name);
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#removeNodeSpi()
- */
+ @Override
protected void removeNodeSpi() throws BackingStoreException {
boolean removeSucceed = (AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
@@ -264,24 +222,15 @@
}
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#removeSpi(java.lang.String)
- */
+ @Override
protected void removeSpi(String key) {
prefs.remove(key);
updated.remove(key);
removed.add(key);
}
- /*
- * (non-Javadoc)
- *
- * @see java.util.prefs.AbstractPreferences#syncSpi()
- */
+ @Override
protected void syncSpi() throws BackingStoreException {
flushSpi();
}
-
}
Modified: harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/Preferences.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/Preferences.java?view=diff&rev=489990&r1=489989&r2=489990
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/Preferences.java (original)
+++ harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/Preferences.java Sat Dec 23 22:38:02 2006
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-
package java.util.prefs;
import java.io.IOException;
@@ -26,8 +25,6 @@
import org.apache.harmony.prefs.internal.nls.Messages;
-
-
/**
* <code>Preferences</code> instance represents one node in preferences tree,
* which provide a mechanisms to store and access configuration data in a
@@ -92,11 +89,12 @@
*/
public abstract class Preferences {
- /**
+ /*
* ---------------------------------------------------------
- * Constants
+ * Class fields
* ---------------------------------------------------------
*/
+
/**
* Maximum size in characters of preferences key
*/
@@ -113,13 +111,14 @@
public static final int MAX_VALUE_LENGTH = 8192;
//permission
- private static final RuntimePermission perm = new RuntimePermission("preferences"); //$NON-NLS-1$
- //factory used to get user/system prefs root
+ private static final RuntimePermission PREFS_PERM = new RuntimePermission("preferences"); //$NON-NLS-1$
+
+ //factory used to get user/system prefs root
private static final PreferencesFactory factory;
/**
* ---------------------------------------------------------
- * Static init
+ * Class initializer
* ---------------------------------------------------------
*/
static{
@@ -139,25 +138,27 @@
// prefs.10=Cannot initiate PreferencesFactory: {0}. Caused by {1}
throw new InternalError(Messages.getString("prefs.10", factoryClassName, e)); //$NON-NLS-1$
}
-
}
- /**
+ /*
* ---------------------------------------------------------
* Constructors
* ---------------------------------------------------------
- */
+ */
+
/**
- * Default constructor, just for using by subclass.
+ * Default constructor, for use by subclasses only.
*/
- protected Preferences() {//empty constructor
+ protected Preferences() {
+ super();
}
- /**
+ /*
* ---------------------------------------------------------
* Methods
* ---------------------------------------------------------
- */
+ */
+
/**
* Get this preference node's absolute path string.
*
@@ -784,7 +785,7 @@
private static void checkSecurity() {
SecurityManager manager = System.getSecurityManager();
if(null != manager){
- manager.checkPermission(perm);
+ manager.checkPermission(PREFS_PERM);
}
}
@@ -843,5 +844,6 @@
* @return a string description of this node
*
*/
- public abstract String toString();
+ @Override
+ public abstract String toString();
}
Modified: harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java?view=diff&rev=489990&r1=489989&r2=489990
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java Sat Dec 23 22:38:02 2006
@@ -14,41 +14,35 @@
* limitations under the License.
*/
-
package java.util.prefs;
import java.util.prefs.Preferences;
import java.util.prefs.PreferencesFactory;
-/*
+/**
* Default implementation of <code>PreferencesFactory</code> for windows
* platform, using windows Registry as back end.
*
* @since 1.4
*/
class RegistryPreferencesFactoryImpl implements PreferencesFactory {
+ //user root preferences
+ private static final Preferences USER_ROOT = new RegistryPreferencesImpl(true);
+
+ //system root preferences
+ private static final Preferences SYSTEM_ROOT = new RegistryPreferencesImpl(false);
- /*
- * Default constructor
- */
public RegistryPreferencesFactoryImpl() {
super();
}
- /* (non-Javadoc)
- * @see java.util.prefs.PreferencesFactory#userRoot()
- */
public Preferences userRoot() {
- return RegistryPreferencesImpl.USER_ROOT;
+ return USER_ROOT;
}
- /* (non-Javadoc)
- * @see java.util.prefs.PreferencesFactory#systemRoot()
- */
public Preferences systemRoot() {
- return RegistryPreferencesImpl.SYSTEM_ROOT;
+ return SYSTEM_ROOT;
}
-
}
Modified: harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java?view=diff&rev=489990&r1=489989&r2=489990
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java Sat Dec 23 22:38:02 2006
@@ -19,7 +19,7 @@
import org.apache.harmony.prefs.internal.nls.Messages;
-/*
+/**
* Default implementation of <code>AbstractPreferences</code> for windows platform,
* using windows registry as back end.
*
@@ -32,36 +32,33 @@
}
/*
- * -------------------------------------------------------------- Constants
+ * --------------------------------------------------------------
+ * Class fields
* --------------------------------------------------------------
*/
//registry path for root preferences
- private final static String ROOT_PATH = "SOFTWARE\\JavaSoft\\Prefs"; //$NON-NLS-1$
+ private static final String ROOT_PATH = "SOFTWARE\\JavaSoft\\Prefs"; //$NON-NLS-1$
//index for returned error code
- private final static int ERROR_CODE = 0;
+ private static final int ERROR_CODE = 0;
//error code for registry access
- final static int RETURN_SUCCESS = 0;
+ private static final int RETURN_SUCCESS = 0;
- final static int RETURN_FILE_NOT_FOUND = 1;
+ @SuppressWarnings("unused")
+ private static final int RETURN_FILE_NOT_FOUND = 1;
- final static int RETURN_ACCESS_DENIED = 2;
+ private static final int RETURN_ACCESS_DENIED = 2;
- final static int RETURN_UNKNOWN_ERROR = 3;
-
- //user root preferences
- final static Preferences USER_ROOT = new RegistryPreferencesImpl(true);
-
- //system root preferences
- final static Preferences SYSTEM_ROOT = new RegistryPreferencesImpl(false);
+ @SuppressWarnings("unused")
+ private static final int RETURN_UNKNOWN_ERROR = 3;
/*
* --------------------------------------------------------------
- * Variables
+ * Instance fields
* --------------------------------------------------------------
*/
- //registry path for this preferences, default value is root's path
+ //registry path for this preferences, default value is the root path
private byte[] path = ROOT_PATH.getBytes();
/*
@@ -69,7 +66,7 @@
* Constructors
* --------------------------------------------------------------
*/
- /*
+ /**
* Construct <code>RegistryPreferencesImpl</code> instance using given parent
* and given name
*/
@@ -79,7 +76,7 @@
path = (ROOT_PATH + encodeWindowsStr(absolutePath())).getBytes();
}
- /*
+ /**
* Construct root <code>RegistryPreferencesImpl</code> instance, construct
* user root if userNode is true, system root otherwise
*/
@@ -88,11 +85,7 @@
this.userNode = userNode;
}
- /*
- * --------------------------------------------------------------
- * Methods implement AbstractPreferences
- * --------------------------------------------------------------
- */
+ @Override
protected String[] childrenNamesSpi() throws BackingStoreException {
int[] error = new int[1];
byte[][] names = getChildNames(path, userNode, error);
@@ -107,6 +100,7 @@
return result;
}
+ @Override
protected AbstractPreferences childSpi(String name) {
int[] error = new int[1];
RegistryPreferencesImpl result = new RegistryPreferencesImpl(this, name);
@@ -118,6 +112,7 @@
return result;
}
+ @Override
protected void flushSpi() throws BackingStoreException {
int[] error = new int[1];
flushPrefs(path, userNode, error);
@@ -127,6 +122,7 @@
}
}
+ @Override
protected String getSpi(String key) {
int[] error = new int[1];
byte[] result = getValue(path, encodeWindowsStr(key).getBytes(), userNode, error);
@@ -136,6 +132,7 @@
return new String(result);
}
+ @Override
protected String[] keysSpi() throws BackingStoreException {
int[] errorCode = new int[1];
byte[][] keys = keys(path, userNode, errorCode);
@@ -150,6 +147,7 @@
return result;
}
+ @Override
protected void putSpi(String name, String value) {
int[] errorCode = new int[1];
putValue(path, encodeWindowsStr(name).getBytes(), value.getBytes(), userNode, errorCode);
@@ -159,6 +157,7 @@
}
}
+ @Override
protected void removeNodeSpi() throws BackingStoreException {
int[] error = new int[1];
removeNode(((RegistryPreferencesImpl) parent()).path,
@@ -169,6 +168,7 @@
}
}
+ @Override
protected void removeSpi(String key) {
int[] errorCode = new int[1];
removeKey(path, encodeWindowsStr(key).getBytes(), userNode, errorCode);
@@ -178,6 +178,7 @@
}
}
+ @Override
protected void syncSpi() throws BackingStoreException {
flushSpi();
}
@@ -200,6 +201,7 @@
}
return buffer.toString();
}
+
private static String decodeWindowsStr(String str){
StringBuffer buffer = new StringBuffer();
char[] chars = str.toCharArray();
@@ -249,5 +251,3 @@
private native void flushPrefs(byte[] registryPath, boolean isUserNode,
int[] errorCode) throws SecurityException;
}
-
-