You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by to...@apache.org on 2009/01/20 06:47:41 UTC
svn commit: r735939 [3/5] - in /harmony/enhanced/classlib/branches/java6: ./
modules/auth/META-INF/ modules/beans/src/main/java/java/beans/
modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/
modules/beans/src/test/support/java/org/a...
Modified: harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/Preferences.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/Preferences.java?rev=735939&r1=735938&r2=735939&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/Preferences.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/Preferences.java Mon Jan 19 21:47:38 2009
@@ -22,6 +22,7 @@
import java.net.MalformedURLException;
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.util.Locale;
import org.apache.harmony.prefs.internal.nls.Messages;
@@ -88,762 +89,672 @@
* @since 1.4
*/
public abstract class Preferences {
-
- /*
- * ---------------------------------------------------------
- * Class fields
- * ---------------------------------------------------------
- */
-
- /**
- * Maximum size in characters of preferences key
- */
- public static final int MAX_KEY_LENGTH = 80;
-
- /**
- * Maximum size in characters of preferences name
- */
- public static final int MAX_NAME_LENGTH = 80;
-
- /**
- * Maximum size in characters of preferences value
- */
- public static final int MAX_VALUE_LENGTH = 8192;
-
- //permission
- 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;
-
/**
- * ---------------------------------------------------------
- * Class initializer
- * ---------------------------------------------------------
- */
- static{
- String factoryClassName = AccessController.doPrivileged(new PrivilegedAction<String>() {
+ * Maximum size in characters of preferences key
+ */
+ public static final int MAX_KEY_LENGTH = 80;
+
+ /**
+ * Maximum size in characters of preferences name
+ */
+ public static final int MAX_NAME_LENGTH = 80;
+
+ /**
+ * Maximum size in characters of preferences value
+ */
+ public static final int MAX_VALUE_LENGTH = 8192;
+
+ //permission
+ 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;
+
+ // default provider factory name for Windows
+ private static final String DEFAULT_FACTORY_NAME_WIN = "java.util.prefs.RegistryPreferencesFactoryImpl"; //$NON-NLS-1$
+
+ // default provider factory name for Unix
+ private static final String DEFAULT_FACTORY_NAME_UNIX = "java.util.prefs.FilePreferencesFactoryImpl"; //$NON-NLS-1$
+
+ static {
+ String factoryClassName = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("java.util.prefs.PreferencesFactory"); //$NON-NLS-1$
}
});
- try {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if(loader == null){
- loader = ClassLoader.getSystemClassLoader();
- }
- Class<?> factoryClass = loader.loadClass(factoryClassName);
- factory = (PreferencesFactory) factoryClass.newInstance();
+
+ // set default provider
+ if (factoryClassName == null) {
+ String osName = AccessController.doPrivileged(new PrivilegedAction<String>() {
+ public String run() {
+ return System.getProperty("os.name"); //$NON-NLS-1$
+ }
+ });
+
+ // only comparing ASCII, so assume english locale
+ osName = (osName == null ? null : osName.toLowerCase(Locale.ENGLISH));
+
+ if (osName != null && osName.startsWith("windows")) {
+ factoryClassName = DEFAULT_FACTORY_NAME_WIN;
+ } else {
+ factoryClassName = DEFAULT_FACTORY_NAME_UNIX;
+ }
+ }
+ try {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if(loader == null){
+ loader = ClassLoader.getSystemClassLoader();
+ }
+ Class<?> factoryClass = loader.loadClass(factoryClassName);
+ factory = (PreferencesFactory) factoryClass.newInstance();
} catch (Exception e) {
// prefs.10=Cannot initiate PreferencesFactory: {0}. Caused by {1}
throw new InternalError(Messages.getString("prefs.10", factoryClassName, e)); //$NON-NLS-1$
}
- }
-
- /*
- * ---------------------------------------------------------
- * Constructors
- * ---------------------------------------------------------
- */
-
- /**
- * Default constructor, for use by subclasses only.
- */
- protected Preferences() {
+ }
+
+ /**
+ * Default constructor, for use by subclasses only.
+ */
+ protected Preferences() {
super();
- }
-
- /*
- * ---------------------------------------------------------
- * Methods
- * ---------------------------------------------------------
- */
-
- /**
- * Get this preference node's absolute path string.
- *
- * @return this preference node's absolute path string.
- */
- public abstract String absolutePath();
-
- /**
- * Return names of all children of this node, or empty string if this node
- * has no children.
- *
- * @return names of all children of this node
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract String[] childrenNames() throws BackingStoreException;
-
- /**
- * Remove all preferences of this node.
- *
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void clear() throws BackingStoreException;
-
- /**
- * Export all preferences of this node to the given output stream in XML
- * document.
- * <p>
- * This XML document has the following DOCTYPE declaration:
- * <pre>
- * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"></pre>
- * And the UTF-8 encoding will be used. Please note that this node is not
- * thread-safe, which is an exception of this class.
- * </p>
- * @param ostream
- * the output stream to export the XML
- * @throws IOException
- * if export operation caused an <code>IOException</code>
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void exportNode (OutputStream ostream) throws IOException, BackingStoreException;
-
- /**
- * Export all preferences of this node and its all descendants to the given
- * output stream in XML document.
- * <p>
- * This XML document has the following DOCTYPE declaration:
- * <pre>
- * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"></pre> *
- * And the UTF-8 encoding will be used. Please note that this node is not
- * thread-safe, which is an exception of this class.
- * </p>
- * @param ostream
- * the output stream to export the XML
- * @throws IOException
- * if export operation caused an <code>IOException</code>
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void exportSubtree (OutputStream ostream) throws IOException, BackingStoreException;
-
- /**
- * Force the updates to this node and its descendants to the backing store.
- * <p>
- * If this node has been removed, then the invocation of this method only
- * flush this node without descendants.
- * </p>
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- */
- public abstract void flush() throws BackingStoreException;
-
- /**
- * Return the string value mapped to the given key, or default value if no
- * value is mapped or backing store is unavailable.
- * <p>
- * Some implementations may store default values in backing stores. In this case,
- * if there is no value mapped to the given key, the stored default value is
- * returned.
- * </p>
- *
- * @param key the preference key
- * @param deflt the default value, which will be returned if no value is
- * mapped to the given key or backing store unavailable
- * @return the preference value mapped to the given key, or default value if
- * no value is mapped or backing store unavailable
- * @throws IllegalStateException
- * if this node has been removed
- * @throws NullPointerException
- * if parameter key is null
- */
- public abstract String get (String key, String deflt);
-
- /**
- * Return the boolean value mapped to the given key, or default value if no
- * value is mapped, backing store is unavailable, or the value is invalid.
- * <p>
- * The valid value is string equals "true", which represents true, or "false",
- * which represents false, case is ignored.
- * </p>
- * <p>
- * Some implementations may store default values in backing stores. In this case,
- * if there is no value mapped to the given key, the stored default value is
- * returned.
- * </p>
- *
- * @param key the preference key
- * @param deflt the default value, which will be returned if no value is
- * mapped to the given key, backing store unavailable or value
- * is invalid
- * @return the boolean value mapped to the given key, or default value if
- * no value is mapped, backing store unavailable or value is invalid
- * @throws IllegalStateException
- * if this node has been removed
- * @throws NullPointerException
- * if parameter key is null
- */
- public abstract boolean getBoolean (String key, boolean deflt);
-
- /**
- * Return the byte array value mapped to the given key, or default value if no
- * value is mapped, backing store is unavailable, or the value is invalid string.
- * <p>
- * The valid value string is Base64 encoded binary data. The Base64 encoding
- * is as defined in <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>,
- * section 6.8.
- * </p>
- * <p>
- * Some implementations may store default values in backing stores. In this case,
- * if there is no value mapped to the given key, the stored default value is
- * returned.
- * </p>
- *
- * @param key the preference key
- * @param deflt the default value, which will be returned if no value is
- * mapped to the given key, backing store unavailable or value
- * is invalid
- * @return the byte array value mapped to the given key, or default value if
- * no value is mapped, backing store unavailable or value is invalid
- * @throws IllegalStateException
- * if this node has been removed
- * @throws NullPointerException
- * if parameter key is null
- */
- public abstract byte[] getByteArray (String key, byte[] deflt);
-
- /**
- * Return the double value mapped to the given key, or default value if no
- * value is mapped, backing store is unavailable, or the value is invalid string.
- * <p>
- * The valid value string can be converted to double number by
- * {@link Double#parseDouble(String) Double.parseDouble(String)}.
- * </p>
- * <p>
- * Some implementations may store default values in backing stores. In this case,
- * if there is no value mapped to the given key, the stored default value is
- * returned.
- * </p>
- *
- * @param key the preference key
- * @param deflt the default value, which will be returned if no value is
- * mapped to the given key, backing store unavailable or value
- * is invalid
- * @return the double value mapped to the given key, or default value if
- * no value is mapped, backing store unavailable or value is invalid
- * @throws IllegalStateException
- * if this node has been removed
- * @throws NullPointerException
- * if parameter key is null
- */
- public abstract double getDouble (String key, double deflt);
-
- /**
- * Return the float value mapped to the given key, or default value if no
- * value is mapped, backing store is unavailable, or the value is invalid string.
- * <p>
- * The valid value string can be converted to float number by
- * {@link Float#parseFloat(String) Float.parseFloat(String)}.
- * </p>
- * <p>
- * Some implementations may store default values in backing stores. In this case,
- * if there is no value mapped to the given key, the stored default value is
- * returned.
- * </p>
- *
- * @param key the preference key
- * @param deflt the default value, which will be returned if no value is
- * mapped to the given key, backing store unavailable or value
- * is invalid
- * @return the float value mapped to the given key, or default value if
- * no value is mapped, backing store unavailable or value is invalid
- * @throws IllegalStateException
- * if this node has been removed
- * @throws NullPointerException
- * if parameter key is null
- */
- public abstract float getFloat (String key, float deflt);
-
- /**
- * Return the float value mapped to the given key, or default value if no
- * value is mapped, backing store is unavailable, or the value is invalid string.
- * <p>
- * The valid value string can be converted to integer by
- * {@link Integer#parseInt(String) Integer.parseInt(String)}.
- * </p>
- * <p>
- * Some implementations may store default values in backing stores. In this case,
- * if there is no value mapped to the given key, the stored default value is
- * returned.
- * </p>
- *
- * @param key the preference key
- * @param deflt the default value, which will be returned if no value is
- * mapped to the given key, backing store unavailable or value
- * is invalid
- * @return the integer value mapped to the given key, or default value if
- * no value is mapped, backing store unavailable or value is invalid
- * @throws IllegalStateException
- * if this node has been removed
- * @throws NullPointerException
- * if parameter key is null
- */
- public abstract int getInt (String key, int deflt);
-
- /**
- * Return the long value mapped to the given key, or default value if no
- * value is mapped, backing store is unavailable, or the value is invalid string.
- * <p>
- * The valid value string can be converted to long integer by
- * {@link Long#parseLong(String) Long.parseLong(String)}.
- * </p>
- * <p>
- * Some implementations may store default values in backing stores. In this case,
- * if there is no value mapped to the given key, the stored default value is
- * returned.
- * </p>
- *
- * @param key the preference key
- * @param deflt the default value, which will be returned if no value is
- * mapped to the given key, backing store unavailable or value
- * is invalid
- * @return the long value mapped to the given key, or default value if
- * no value is mapped, backing store unavailable or value is invalid
- * @throws IllegalStateException
- * if this node has been removed
- * @throws NullPointerException
- * if parameter key is null
- */
- public abstract long getLong (String key, long deflt);
-
- /**
- * Import all preferences from the given input stream in XML document.
- * <p>
- * This XML document has the following DOCTYPE declaration:
- * <pre>
- * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"></pre> *
- * Please note that this node is not thread-safe, which is an exception of
- * this class.
- * </p>
- *
- * @param istream
- * the given input stream to read data
- * @throws InvalidPreferencesFormatException
- * if the data read from given input stream is not valid XML
- * document
- * @throws IOException
- * if import operation caused an <code>IOException</code>
- * @throws SecurityException
- * if <code>RuntimePermission("preferences")</code> is denied
- * by a <code>SecurityManager</code>
- */
- public static void importPreferences (InputStream istream) throws InvalidPreferencesFormatException, IOException {
- checkSecurity();
- if(null == istream){
+ }
+
+ /**
+ * Get this preference node's absolute path string.
+ *
+ * @return this preference node's absolute path string.
+ */
+ public abstract String absolutePath();
+
+ /**
+ * Return names of all children of this node, or empty string if this node has no children.
+ *
+ * @return names of all children of this node
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract String[] childrenNames() throws BackingStoreException;
+
+ /**
+ * Remove all preferences of this node.
+ *
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void clear() throws BackingStoreException;
+
+ /**
+ * Export all preferences of this node to the given output stream in XML document.
+ * <p>
+ * This XML document has the following DOCTYPE declaration:
+ *
+ * <pre>
+ * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
+ * </pre>
+ *
+ * And the UTF-8 encoding will be used. Please note that this node is not thread-safe, which is
+ * an exception of this class.
+ * </p>
+ *
+ * @param ostream the output stream to export the XML
+ * @throws IOException if export operation caused an <code>IOException</code>
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void exportNode(OutputStream ostream) throws IOException, BackingStoreException;
+
+ /**
+ * Export all preferences of this node and its all descendants to the given output stream in XML
+ * document.
+ * <p>
+ * This XML document has the following DOCTYPE declaration:
+ *
+ * <pre>
+ * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
+ * </pre>
+ *
+ * And the UTF-8 encoding will be used. Please note that this node is not thread-safe, which is
+ * an exception of this class.
+ * </p>
+ *
+ * @param ostream the output stream to export the XML
+ * @throws IOException if export operation caused an <code>IOException</code>
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void exportSubtree(OutputStream ostream) throws IOException,
+ BackingStoreException;
+
+ /**
+ * Force the updates to this node and its descendants to the backing store.
+ * <p>
+ * If this node has been removed, then the invocation of this method only flush this node
+ * without descendants.
+ * </p>
+ *
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ */
+ public abstract void flush() throws BackingStoreException;
+
+ /**
+ * Return the string value mapped to the given key, or default value if no value is mapped or
+ * backing store is unavailable.
+ * <p>
+ * Some implementations may store default values in backing stores. In this case, if there is no
+ * value mapped to the given key, the stored default value is returned.
+ * </p>
+ *
+ * @param key the preference key
+ * @param deflt the default value, which will be returned if no value is mapped to the given key
+ * or backing store unavailable
+ * @return the preference value mapped to the given key, or default value if no value is mapped
+ * or backing store unavailable
+ * @throws IllegalStateException if this node has been removed
+ * @throws NullPointerException if parameter key is null
+ */
+ public abstract String get(String key, String deflt);
+
+ /**
+ * Return the boolean value mapped to the given key, or default value if no value is mapped,
+ * backing store is unavailable, or the value is invalid.
+ * <p>
+ * The valid value is string equals "true", which represents true, or "false", which represents
+ * false, case is ignored.
+ * </p>
+ * <p>
+ * Some implementations may store default values in backing stores. In this case, if there is no
+ * value mapped to the given key, the stored default value is returned.
+ * </p>
+ *
+ * @param key the preference key
+ * @param deflt the default value, which will be returned if no value is mapped to the given
+ * key, backing store unavailable or value is invalid
+ * @return the boolean value mapped to the given key, or default value if no value is mapped,
+ * backing store unavailable or value is invalid
+ * @throws IllegalStateException if this node has been removed
+ * @throws NullPointerException if parameter key is null
+ */
+ public abstract boolean getBoolean(String key, boolean deflt);
+
+ /**
+ * Return the byte array value mapped to the given key, or default value if no value is mapped,
+ * backing store is unavailable, or the value is invalid string.
+ * <p>
+ * The valid value string is Base64 encoded binary data. The Base64 encoding is as defined in <a
+ * href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>, section 6.8.
+ * </p>
+ * <p>
+ * Some implementations may store default values in backing stores. In this case, if there is no
+ * value mapped to the given key, the stored default value is returned.
+ * </p>
+ *
+ * @param key the preference key
+ * @param deflt the default value, which will be returned if no value is mapped to the given
+ * key, backing store unavailable or value is invalid
+ * @return the byte array value mapped to the given key, or default value if no value is mapped,
+ * backing store unavailable or value is invalid
+ * @throws IllegalStateException if this node has been removed
+ * @throws NullPointerException if parameter key is null
+ */
+ public abstract byte[] getByteArray(String key, byte[] deflt);
+
+ /**
+ * Return the double value mapped to the given key, or default value if no value is mapped,
+ * backing store is unavailable, or the value is invalid string.
+ * <p>
+ * The valid value string can be converted to double number by
+ * {@link Double#parseDouble(String) Double.parseDouble(String)}.
+ * </p>
+ * <p>
+ * Some implementations may store default values in backing stores. In this case, if there is no
+ * value mapped to the given key, the stored default value is returned.
+ * </p>
+ *
+ * @param key the preference key
+ * @param deflt the default value, which will be returned if no value is mapped to the given
+ * key, backing store unavailable or value is invalid
+ * @return the double value mapped to the given key, or default value if no value is mapped,
+ * backing store unavailable or value is invalid
+ * @throws IllegalStateException if this node has been removed
+ * @throws NullPointerException if parameter key is null
+ */
+ public abstract double getDouble(String key, double deflt);
+
+ /**
+ * Return the float value mapped to the given key, or default value if no value is mapped,
+ * backing store is unavailable, or the value is invalid string.
+ * <p>
+ * The valid value string can be converted to float number by {@link Float#parseFloat(String)
+ * Float.parseFloat(String)}.
+ * </p>
+ * <p>
+ * Some implementations may store default values in backing stores. In this case, if there is no
+ * value mapped to the given key, the stored default value is returned.
+ * </p>
+ *
+ * @param key the preference key
+ * @param deflt the default value, which will be returned if no value is mapped to the given
+ * key, backing store unavailable or value is invalid
+ * @return the float value mapped to the given key, or default value if no value is mapped,
+ * backing store unavailable or value is invalid
+ * @throws IllegalStateException if this node has been removed
+ * @throws NullPointerException if parameter key is null
+ */
+ public abstract float getFloat(String key, float deflt);
+
+ /**
+ * Return the float value mapped to the given key, or default value if no value is mapped,
+ * backing store is unavailable, or the value is invalid string.
+ * <p>
+ * The valid value string can be converted to integer by {@link Integer#parseInt(String)
+ * Integer.parseInt(String)}.
+ * </p>
+ * <p>
+ * Some implementations may store default values in backing stores. In this case, if there is no
+ * value mapped to the given key, the stored default value is returned.
+ * </p>
+ *
+ * @param key the preference key
+ * @param deflt the default value, which will be returned if no value is mapped to the given
+ * key, backing store unavailable or value is invalid
+ * @return the integer value mapped to the given key, or default value if no value is mapped,
+ * backing store unavailable or value is invalid
+ * @throws IllegalStateException if this node has been removed
+ * @throws NullPointerException if parameter key is null
+ */
+ public abstract int getInt(String key, int deflt);
+
+ /**
+ * Return the long value mapped to the given key, or default value if no value is mapped,
+ * backing store is unavailable, or the value is invalid string.
+ * <p>
+ * The valid value string can be converted to long integer by {@link Long#parseLong(String)
+ * Long.parseLong(String)}.
+ * </p>
+ * <p>
+ * Some implementations may store default values in backing stores. In this case, if there is no
+ * value mapped to the given key, the stored default value is returned.
+ * </p>
+ *
+ * @param key the preference key
+ * @param deflt the default value, which will be returned if no value is mapped to the given
+ * key, backing store unavailable or value is invalid
+ * @return the long value mapped to the given key, or default value if no value is mapped,
+ * backing store unavailable or value is invalid
+ * @throws IllegalStateException if this node has been removed
+ * @throws NullPointerException if parameter key is null
+ */
+ public abstract long getLong(String key, long deflt);
+
+ /**
+ * Import all preferences from the given input stream in XML document.
+ * <p>
+ * This XML document has the following DOCTYPE declaration:
+ *
+ * <pre>
+ * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
+ * </pre>
+ *
+ * Please note that this node is not thread-safe, which is an exception of this class.
+ * </p>
+ *
+ * @param istream the given input stream to read data
+ * @throws InvalidPreferencesFormatException if the data read from given input stream is not
+ * valid XML document
+ * @throws IOException if import operation caused an <code>IOException</code>
+ * @throws SecurityException if <code>RuntimePermission("preferences")</code> is denied by a
+ * <code>SecurityManager</code>
+ */
+ public static void importPreferences (InputStream istream) throws InvalidPreferencesFormatException, IOException {
+ checkSecurity();
+ if(null == istream){
// prefs.0=Inputstream cannot be null\!
- throw new MalformedURLException(Messages.getString("prefs.0")); //$NON-NLS-1$
- }
- XMLParser.importPrefs(istream);
- }
-
- /**
- * Return true if this is a user preferences, false if this is a system
- * preferences
- *
- * @return true if this is a user preferences, false if this is a
- * system preferences
- */
- public abstract boolean isUserNode();
-
- /**
- * Return all preferences keys stored in this node, or empty array if no
- * key is found.
- *
- * @return all preferences keys in this node
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract String[] keys() throws BackingStoreException;
-
- /**
- * Return name of this node.
- *
- * @return the name of this node
- */
- public abstract String name();
-
- /**
- * Return the preferences node with the given path name. The path name can
- * be relative or absolute. The dictated preferences and its ancestors will
- * be created if they do not exist.
- * <p>
- * The path is treated as relative to this node if it doesn't start with
- * slash, or as absolute otherwise.</p>
- *
- * @param path the path name of dictated preferences
- * @return the dictated preferences node
- * @throws IllegalStateException
- * if this node has been removed.
- * @throws IllegalArgumentException
- * if the path name is invalid.
- * @throws NullPointerException
- * if given path is null.
- */
- public abstract Preferences node (String path);
-
- /**
- * Return the preferences node with the given path name. The path is treated
- * as relative to this node if it doesn't start with slash, or as absolute
- * otherwise.
- * <p>
- * Please note that if this node has been removed, invocation of this node
- * will throw <code>IllegalStateException</code> except the given path is
- * empty string, which will return false.
- * </p>
- *
- * @param path the path name of dictated preferences
- * @return true if the dictated preferences node exists
- * @throws IllegalStateException
- * if this node has been removed and the path is not empty string.
- * @throws IllegalArgumentException
- * if the path name is invalid.
- * @throws NullPointerException
- * if given path is null.
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- */
- public abstract boolean nodeExists (String path) throws BackingStoreException;
-
- /**
- * Return the parent preferences node of this node, or null if this node is root.
- *
- * @return the parent preferences node of this node.
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract Preferences parent();
-
- /**
- * Add new preferences to this node using given key and value, or update
- * value if preferences with given key has already existed.
- *
- * @param key the preferences key to be added or be updated
- * @param value the preferences value for the given key
- * @throws NullPointerException
- * if the given key or value is null
- * @throws IllegalArgumentException
- * if the given key's length is bigger than
- * <code>MAX_KEY_LENGTH</code>, or the value's length is bigger
- * than <code>MAX_VALUE_LENGTH</code>
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void put (String key, String value);
-
- /**
- * Add new preferences to this node using given key and string form of given
- * value, or update value if preferences with given key has already existed.
- *
- * @param key the preferences key to be added or be updated
- * @param value the preferences value for the given key
- * @throws NullPointerException
- * if the given key is null
- * @throws IllegalArgumentException
- * if the given key's length is bigger than
- * <code>MAX_KEY_LENGTH</code>
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void putBoolean (String key, boolean value);
-
- /**
- * Add new preferences to this node using given key and string form of given
- * value, or update value if preferences with given key has already existed.
- * <p>
- * The string form of value is the Base64 encoded binary data of the given
- * byte array. The Base64 encoding is as defined in
- * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>, section 6.8.</p>
- *
- * @param key the preferences key to be added or be updated
- * @param value the preferences value for the given key
- * @throws NullPointerException
- * if the given key or value is null
- * @throws IllegalArgumentException
- * if the given key's length is bigger than
- * <code>MAX_KEY_LENGTH</code> or value's length is bigger than
- * three quarters of <code>MAX_KEY_LENGTH</code>
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void putByteArray (String key, byte[] value);
-
- /**
- * Add new preferences to this node using given key and string form of given
- * value, or update value if preferences with given key has already existed.
- * <p>
- * The string form of given value is the result of invoking
- * {@link Double#toString(double) Double.toString(double)}</p>
- *
- * @param key the preferences key to be added or be updated
- * @param value the preferences value for the given key
- * @throws NullPointerException
- * if the given key is null
- * @throws IllegalArgumentException
- * if the given key's length is bigger than
- * <code>MAX_KEY_LENGTH</code>
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void putDouble (String key, double value);
-
- /**
- * Add new preferences to this node using given key and string form of given
- * value, or update value if preferences with given key has already existed.
- * <p>
- * The string form of given value is the result of invoking
- * {@link Float#toString(float) Float.toString(float)}</p>
- *
- * @param key the preferences key to be added or be updated
- * @param value the preferences value for the given key
- * @throws NullPointerException
- * if the given key is null
- * @throws IllegalArgumentException
- * if the given key's length is bigger than
- * <code>MAX_KEY_LENGTH</code>
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void putFloat (String key, float value);
-
- /**
- * Add new preferences to this node using given key and string form of given
- * value, or update value if preferences with given key has already existed.
- * <p>
- * The string form of given value is the result of invoking
- * {@link Integer#toString(int) Integer.toString(int)}</p>
- *
- * @param key the preferences key to be added or be updated
- * @param value the preferences value for the given key
- * @throws NullPointerException
- * if the given key is null
- * @throws IllegalArgumentException
- * if the given key's length is bigger than
- * <code>MAX_KEY_LENGTH</code>
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void putInt (String key, int value);
-
- /**
- * Add new preferences to this node using given key and string form of given
- * value, or update value if preferences with given key has already existed.
- * <p>
- * The string form of given value is the result of invoking
- * {@link Long#toString(long) Long.toString(long)}</p>
- *
- * @param key the preferences key to be added or be updated
- * @param value the preferences value for the given key
- * @throws NullPointerException
- * if the given key is null
- * @throws IllegalArgumentException
- * if the given key's length is bigger than
- * <code>MAX_KEY_LENGTH</code>
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void putLong (String key, long value);
-
- /**
- * Remove the preferences mapped to the given key from this node.
- *
- * @param key the given preferences key to removed
- * @throws NullPointerException
- * if the given key is null
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void remove (String key);
-
- /**
- * Remove this preferences node and its all descendants. The removal maybe
- * won't be persisted until the <code>flush()</code> method is invoked.
- *
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- * @throws IllegalStateException
- * if this node has been removed
- * @throws UnsupportedOperationException
- * if this is a root node
- */
- public abstract void removeNode() throws BackingStoreException;
-
- /**
- * Register an <code>NodeChangeListener</code> instance for this node, which
- * will receive <code>NodeChangeEvent</code>. <code>NodeChangeEvent</code> will
- * be produced when direct child node is added to or removed from this node.
- *
- * @param ncl the given listener to be registered
- * @throws NullPointerException
- * if the given listener is null
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void addNodeChangeListener (NodeChangeListener ncl);
-
- /**
- * Register an <code>PreferenceChangeListener</code> instance for this node, which
- * will receive <code>PreferenceChangeEvent</code>. <code>PreferenceChangeEvent</code> will
- * be produced when preference is added to, removed from or updated for this node.
- *
- * @param pcl the given listener to be registered
- * @throws NullPointerException
- * if the given listener is null
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void addPreferenceChangeListener (PreferenceChangeListener pcl);
-
- /**
- * Remove the given <code>NodeChangeListener</code> instance from this node.
- *
- * @param ncl the given listener to be removed
- * @throws IllegalArgumentException
- * if the given listener
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void removeNodeChangeListener (NodeChangeListener ncl);
-
- /**
- * Remove the given <code>PreferenceChangeListener</code> instance from this node.
- *
- * @param pcl the given listener to be removed
- * @throws IllegalArgumentException
- * if the given listener
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void removePreferenceChangeListener (PreferenceChangeListener pcl);
-
- /**
- * Synchronize this preferences node and its descendants' data with the back
- * end preferences store. The changes of back end should be reflect by this
- * node and its descendants, meanwhile, the changes of this node and descendants
- * should be persisted.
- *
- * @throws BackingStoreException
- * if backing store is unavailable or causes operation failure
- * @throws IllegalStateException
- * if this node has been removed
- */
- public abstract void sync() throws BackingStoreException;
-
- /**
- * Return the system preference node for the package of given class. The
- * absolute path of the returned node is one slash followed by the given
- * class's full package name with replacing each period ('.') with slash.
- * For example, the preference's associated with class <code>Object<code>
+ throw new MalformedURLException(Messages.getString("prefs.0")); //$NON-NLS-1$
+ }
+ XMLParser.importPrefs(istream);
+ }
+
+ /**
+ * Return true if this is a user preferences, false if this is a system preferences
+ *
+ * @return true if this is a user preferences, false if this is a system preferences
+ */
+ public abstract boolean isUserNode();
+
+ /**
+ * Return all preferences keys stored in this node, or empty array if no key is found.
+ *
+ * @return all preferences keys in this node
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract String[] keys() throws BackingStoreException;
+
+ /**
+ * Return name of this node.
+ *
+ * @return the name of this node
+ */
+ public abstract String name();
+
+ /**
+ * Return the preferences node with the given path name. The path name can be relative or
+ * absolute. The dictated preferences and its ancestors will be created if they do not exist.
+ * <p>
+ * The path is treated as relative to this node if it doesn't start with slash, or as absolute
+ * otherwise.
+ * </p>
+ *
+ * @param path the path name of dictated preferences
+ * @return the dictated preferences node
+ * @throws IllegalStateException if this node has been removed.
+ * @throws IllegalArgumentException if the path name is invalid.
+ * @throws NullPointerException if given path is null.
+ */
+ public abstract Preferences node(String path);
+
+ /**
+ * Return the preferences node with the given path name. The path is treated as relative to this
+ * node if it doesn't start with slash, or as absolute otherwise.
+ * <p>
+ * Please note that if this node has been removed, invocation of this node will throw
+ * <code>IllegalStateException</code> except the given path is empty string, which will return
+ * false.
+ * </p>
+ *
+ * @param path the path name of dictated preferences
+ * @return true if the dictated preferences node exists
+ * @throws IllegalStateException if this node has been removed and the path is not empty string.
+ * @throws IllegalArgumentException if the path name is invalid.
+ * @throws NullPointerException if given path is null.
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ */
+ public abstract boolean nodeExists(String path) throws BackingStoreException;
+
+ /**
+ * Return the parent preferences node of this node, or null if this node is root.
+ *
+ * @return the parent preferences node of this node.
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract Preferences parent();
+
+ /**
+ * Add new preferences to this node using given key and value, or update value if preferences
+ * with given key has already existed.
+ *
+ * @param key the preferences key to be added or be updated
+ * @param value the preferences value for the given key
+ * @throws NullPointerException if the given key or value is null
+ * @throws IllegalArgumentException if the given key's length is bigger than
+ * <code>MAX_KEY_LENGTH</code>, or the value's length is bigger than
+ * <code>MAX_VALUE_LENGTH</code>
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void put(String key, String value);
+
+ /**
+ * Add new preferences to this node using given key and string form of given value, or update
+ * value if preferences with given key has already existed.
+ *
+ * @param key the preferences key to be added or be updated
+ * @param value the preferences value for the given key
+ * @throws NullPointerException if the given key is null
+ * @throws IllegalArgumentException if the given key's length is bigger than
+ * <code>MAX_KEY_LENGTH</code>
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void putBoolean(String key, boolean value);
+
+ /**
+ * Add new preferences to this node using given key and string form of given value, or update
+ * value if preferences with given key has already existed.
+ * <p>
+ * The string form of value is the Base64 encoded binary data of the given byte array. The
+ * Base64 encoding is as defined in <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>,
+ * section 6.8.
+ * </p>
+ *
+ * @param key the preferences key to be added or be updated
+ * @param value the preferences value for the given key
+ * @throws NullPointerException if the given key or value is null
+ * @throws IllegalArgumentException if the given key's length is bigger than
+ * <code>MAX_KEY_LENGTH</code> or value's length is bigger than three quarters of
+ * <code>MAX_KEY_LENGTH</code>
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void putByteArray(String key, byte[] value);
+
+ /**
+ * Add new preferences to this node using given key and string form of given value, or update
+ * value if preferences with given key has already existed.
+ * <p>
+ * The string form of given value is the result of invoking {@link Double#toString(double)
+ * Double.toString(double)}
+ * </p>
+ *
+ * @param key the preferences key to be added or be updated
+ * @param value the preferences value for the given key
+ * @throws NullPointerException if the given key is null
+ * @throws IllegalArgumentException if the given key's length is bigger than
+ * <code>MAX_KEY_LENGTH</code>
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void putDouble(String key, double value);
+
+ /**
+ * Add new preferences to this node using given key and string form of given value, or update
+ * value if preferences with given key has already existed.
+ * <p>
+ * The string form of given value is the result of invoking {@link Float#toString(float)
+ * Float.toString(float)}
+ * </p>
+ *
+ * @param key the preferences key to be added or be updated
+ * @param value the preferences value for the given key
+ * @throws NullPointerException if the given key is null
+ * @throws IllegalArgumentException if the given key's length is bigger than
+ * <code>MAX_KEY_LENGTH</code>
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void putFloat(String key, float value);
+
+ /**
+ * Add new preferences to this node using given key and string form of given value, or update
+ * value if preferences with given key has already existed.
+ * <p>
+ * The string form of given value is the result of invoking {@link Integer#toString(int)
+ * Integer.toString(int)}
+ * </p>
+ *
+ * @param key the preferences key to be added or be updated
+ * @param value the preferences value for the given key
+ * @throws NullPointerException if the given key is null
+ * @throws IllegalArgumentException if the given key's length is bigger than
+ * <code>MAX_KEY_LENGTH</code>
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void putInt(String key, int value);
+
+ /**
+ * Add new preferences to this node using given key and string form of given value, or update
+ * value if preferences with given key has already existed.
+ * <p>
+ * The string form of given value is the result of invoking {@link Long#toString(long)
+ * Long.toString(long)}
+ * </p>
+ *
+ * @param key the preferences key to be added or be updated
+ * @param value the preferences value for the given key
+ * @throws NullPointerException if the given key is null
+ * @throws IllegalArgumentException if the given key's length is bigger than
+ * <code>MAX_KEY_LENGTH</code>
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void putLong(String key, long value);
+
+ /**
+ * Remove the preferences mapped to the given key from this node.
+ *
+ * @param key the given preferences key to removed
+ * @throws NullPointerException if the given key is null
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void remove(String key);
+
+ /**
+ * Remove this preferences node and its all descendants. The removal maybe won't be persisted
+ * until the <code>flush()</code> method is invoked.
+ *
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ * @throws IllegalStateException if this node has been removed
+ * @throws UnsupportedOperationException if this is a root node
+ */
+ public abstract void removeNode() throws BackingStoreException;
+
+ /**
+ * Register an <code>NodeChangeListener</code> instance for this node, which will receive
+ * <code>NodeChangeEvent</code>. <code>NodeChangeEvent</code> will be produced when direct child
+ * node is added to or removed from this node.
+ *
+ * @param ncl the given listener to be registered
+ * @throws NullPointerException if the given listener is null
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void addNodeChangeListener(NodeChangeListener ncl);
+
+ /**
+ * Register an <code>PreferenceChangeListener</code> instance for this node, which will receive
+ * <code>PreferenceChangeEvent</code>. <code>PreferenceChangeEvent</code> will be produced when
+ * preference is added to, removed from or updated for this node.
+ *
+ * @param pcl the given listener to be registered
+ * @throws NullPointerException if the given listener is null
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void addPreferenceChangeListener (PreferenceChangeListener pcl);
+
+ /**
+ * Remove the given <code>NodeChangeListener</code> instance from this node.
+ *
+ * @param ncl the given listener to be removed
+ * @throws IllegalArgumentException if the given listener
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void removeNodeChangeListener (NodeChangeListener ncl);
+
+ /**
+ * Remove the given <code>PreferenceChangeListener</code> instance from this node.
+ *
+ * @param pcl the given listener to be removed
+ * @throws IllegalArgumentException if the given listener
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void removePreferenceChangeListener (PreferenceChangeListener pcl);
+
+ /**
+ * Synchronize this preferences node and its descendants' data with the back end preferences
+ * store. The changes of back end should be reflect by this node and its descendants, meanwhile,
+ * the changes of this node and descendants should be persisted.
+ *
+ * @throws BackingStoreException if backing store is unavailable or causes operation failure
+ * @throws IllegalStateException if this node has been removed
+ */
+ public abstract void sync() throws BackingStoreException;
+
+ /**
+ * <p>Return the system preference node for the package of given class. The absolute path of the
+ * returned node is one slash followed by the given class's full package name with replacing
+ * each period ('.') with slash. For example, the preference's associated with class
+ * <code>Object<code>
* has absolute path like "/java/lang". As a special case, the unnamed
- * package is associated with preference node "/<unnamed>".
+ * package is associated with preference node "/<unnamed>".</p>
*
- * This method will create node and its ancestors if needed, and the new
- * created nodes maybe won't be persisted until the <code>flush()</code>
- * is invoked.
- *
- * @param c the given class
- * @return the system preference node for the package of given class.
- * @throws NullPointerException
- * if the given class is null
- * @throws SecurityException
- * if <code>RuntimePermission("preferences")</code> is denied
- * by a <code>SecurityManager</code>
- */
- public static Preferences systemNodeForPackage (Class<?> c) {
- checkSecurity();
- return factory.systemRoot().node(getNodeName(c));
- }
-
- /**
- * Return the root node for system preference hierarchy.
- *
- * @return the root node for system preference hierarchy
- * @throws SecurityException
- * if <code>RuntimePermission("preferences")</code> is denied
- * by a <code>SecurityManager</code>
- */
- public static Preferences systemRoot() {
- checkSecurity();
- return factory.systemRoot();
- }
-
- //check the RuntimePermission("preferences")
+ * <p>This method will create node and its ancestors if needed, and the new
+ * created nodes maybe won't be persisted until the <code>flush()</code> is invoked.</p>
+ *
+ * @param c the given class
+ * @return the system preference node for the package of given class.
+ * @throws NullPointerException if the given class is null
+ * @throws SecurityException if <code>RuntimePermission("preferences")</code> is denied by a
+ * <code>SecurityManager</code>
+ */
+ public static Preferences systemNodeForPackage (Class<?> c) {
+ checkSecurity();
+ return factory.systemRoot().node(getNodeName(c));
+ }
+
+ /**
+ * Return the root node for system preference hierarchy.
+ *
+ * @return the root node for system preference hierarchy
+ * @throws SecurityException if <code>RuntimePermission("preferences")</code> is denied by a
+ * <code>SecurityManager</code>
+ */
+ public static Preferences systemRoot() {
+ checkSecurity();
+ return factory.systemRoot();
+ }
+
+ //check the RuntimePermission("preferences")
private static void checkSecurity() {
SecurityManager manager = System.getSecurityManager();
if(null != manager){
manager.checkPermission(PREFS_PERM);
}
-
+
}
/**
- * Return the user preference node for the package of given class. The
- * absolute path of the returned node is one slash followed by the given
- * class's full package name with replacing each period ('.') with slash.
- * For example, the preference's associated with class <code>Object<code>
+ * <p>Return the user preference node for the package of given class. The absolute path of the
+ * returned node is one slash followed by the given class's full package name with replacing
+ * each period ('.') with slash. For example, the preference's associated with class
+ * <code>Object<code>
* has absolute path like "/java/lang". As a special case, the unnamed
- * package is associated with preference node "/<unnamed>".
+ * package is associated with preference node "/<unnamed>".</p>
*
- * This method will create node and its ancestors if needed, and the new
- * created nodes maybe won't be persisted until the <code>flush()</code>
- * is invoked.
- *
- * @param c the given class
- * @return the user preference node for the package of given class.
- * @throws NullPointerException
- * if the given class is null
- * @throws SecurityException
- * if <code>RuntimePermission("preferences")</code> is denied
- * by a <code>SecurityManager</code>
- */
- public static Preferences userNodeForPackage (Class<?> c) {
- checkSecurity();
- return factory.userRoot().node(getNodeName(c));
- }
-
- //parse node's absolute path from class instance
- private static String getNodeName(Class<?> c){
- Package p = c.getPackage();
- if(null == p){
- return "/<unnamed>"; //$NON-NLS-1$
- }
- return "/"+p.getName().replace('.', '/'); //$NON-NLS-1$
- }
-
- /**
- * Return the root node for user preference hierarchy.
- *
- * @return the root node for user preference hierarchy
- * @throws SecurityException
- * if <code>RuntimePermission("preferences")</code> is denied
- * by a <code>SecurityManager</code>
- */
- public static Preferences userRoot() {
- checkSecurity();
- return factory.userRoot();
- }
-
- /**
- * Return a string description of this node. The format is "User/System
- * Preference Node: " followed by this node's absolute path.
- *
- * @return a string description of this node
- *
- */
- @Override
+ * <p>This method will create node and its ancestors if needed, and the new
+ * created nodes maybe won't be persisted until the <code>flush()</code> is invoked.</p>
+ *
+ * @param c the given class
+ * @return the user preference node for the package of given class.
+ * @throws NullPointerException if the given class is null
+ * @throws SecurityException if <code>RuntimePermission("preferences")</code> is denied by a
+ * <code>SecurityManager</code>
+ */
+ public static Preferences userNodeForPackage (Class<?> c) {
+ checkSecurity();
+ return factory.userRoot().node(getNodeName(c));
+ }
+
+ //parse node's absolute path from class instance
+ private static String getNodeName(Class<?> c){
+ Package p = c.getPackage();
+ if(null == p){
+ return "/<unnamed>"; //$NON-NLS-1$
+ }
+ return "/"+p.getName().replace('.', '/'); //$NON-NLS-1$
+ }
+
+ /**
+ * Return the root node for user preference hierarchy.
+ *
+ * @return the root node for user preference hierarchy
+ * @throws SecurityException if <code>RuntimePermission("preferences")</code> is denied by a
+ * <code>SecurityManager</code>
+ */
+ public static Preferences userRoot() {
+ checkSecurity();
+ return factory.userRoot();
+ }
+
+ /**
+ * Return a string description of this node. The format is "User/System
+ * Preference Node: " followed by this node's absolute path.
+ *
+ * @return a string description of this node
+ */
+ @Override
public abstract String toString();
}
Modified: harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/PreferencesFactory.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/PreferencesFactory.java?rev=735939&r1=735938&r2=735939&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/PreferencesFactory.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/PreferencesFactory.java Mon Jan 19 21:47:38 2009
@@ -14,35 +14,29 @@
* limitations under the License.
*/
-
package java.util.prefs;
/**
- * This interface is used by {@link Preferences} class
- * as factory class to create Preferences instance. This interface can be implemented
- * and installed to replace the default preferences implementation.
+ * This interface is used by {@link Preferences} class as factory class to create Preferences
+ * instance. This interface can be implemented and installed to replace the default preferences
+ * implementation.
*
* @see java.util.prefs.Preferences
*
* @since 1.4
*/
public interface PreferencesFactory {
- /**
- * Returns the root of the preferences hierarchy for the calling user
- * context.
- *
- * @return The user root preferences node.
- */
- Preferences userRoot();
-
- /**
- * Returns the root of the system preferences hierarchy.
- *
- * @return The root of the system preferences hierarchy.
- */
- Preferences systemRoot();
+ /**
+ * Returns the root of the preferences hierarchy for the calling user context.
+ *
+ * @return The user root preferences node.
+ */
+ Preferences userRoot();
+
+ /**
+ * Returns the root of the system preferences hierarchy.
+ *
+ * @return The root of the system preferences hierarchy.
+ */
+ Preferences systemRoot();
}
-
-
-
-
Modified: harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java?rev=735939&r1=735938&r2=735939&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java Mon Jan 19 21:47:38 2009
@@ -20,20 +20,20 @@
import java.util.prefs.PreferencesFactory;
/**
- * Default implementation of <code>PreferencesFactory</code> for windows
- * platform, using windows Registry as back end.
+ * 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
+ // user root preferences
private static final Preferences USER_ROOT = new RegistryPreferencesImpl(true);
- //system root preferences
+ // system root preferences
private static final Preferences SYSTEM_ROOT = new RegistryPreferencesImpl(false);
-
+
public RegistryPreferencesFactoryImpl() {
- super();
+ super();
}
public Preferences userRoot() {
@@ -44,7 +44,3 @@
return SYSTEM_ROOT;
}
}
-
-
-
-
Modified: harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java?rev=735939&r1=735938&r2=735939&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java Mon Jan 19 21:47:38 2009
@@ -14,35 +14,29 @@
* limitations under the License.
*/
-
package java.util.prefs;
import org.apache.harmony.prefs.internal.nls.Messages;
/**
- * Default implementation of <code>AbstractPreferences</code> for windows platform,
- * using windows registry as back end.
+ * Default implementation of <code>AbstractPreferences</code> for windows platform, using windows
+ * registry as back end.
*
* @since 1.4
*/
class RegistryPreferencesImpl extends AbstractPreferences {
-
+
static {
- System.loadLibrary("hyprefs"); //$NON-NLS-1$
- }
+ System.loadLibrary("hyprefs"); //$NON-NLS-1$
+ }
- /*
- * --------------------------------------------------------------
- * Class fields
- * --------------------------------------------------------------
- */
- //registry path for root preferences
+ // registry path for root preferences
private static final String ROOT_PATH = "SOFTWARE\\JavaSoft\\Prefs"; //$NON-NLS-1$
- //index for returned error code
+ // index for returned error code
private static final int ERROR_CODE = 0;
- //error code for registry access
+ // error code for registry access
private static final int RETURN_SUCCESS = 0;
@SuppressWarnings("unused")
@@ -53,22 +47,11 @@
@SuppressWarnings("unused")
private static final int RETURN_UNKNOWN_ERROR = 3;
- /*
- * --------------------------------------------------------------
- * Instance fields
- * --------------------------------------------------------------
- */
- //registry path for this preferences, default value is the root path
+ // registry path for this preferences, default value is the root path
private byte[] path = ROOT_PATH.getBytes();
- /*
- * --------------------------------------------------------------
- * Constructors
- * --------------------------------------------------------------
- */
/**
- * Construct <code>RegistryPreferencesImpl</code> instance using given parent
- * and given name
+ * Construct <code>RegistryPreferencesImpl</code> instance using given parent and given name
*/
public RegistryPreferencesImpl(AbstractPreferences parent, String name) {
super(parent, name);
@@ -77,8 +60,8 @@
}
/**
- * Construct root <code>RegistryPreferencesImpl</code> instance, construct
- * user root if userNode is true, system root otherwise
+ * Construct root <code>RegistryPreferencesImpl</code> instance, construct user root if userNode
+ * is true, system root otherwise
*/
public RegistryPreferencesImpl(boolean userNode) {
super(null, ""); //$NON-NLS-1$
@@ -91,7 +74,7 @@
byte[][] names = getChildNames(path, userNode, error);
if (error[ERROR_CODE] != RETURN_SUCCESS) {
// prefs.B=Enumerate child nodes error\!
- throw new BackingStoreException(Messages.getString("prefs.B")); //$NON-NLS-1$
+ throw new BackingStoreException(Messages.getString("prefs.B")); //$NON-NLS-1$
}
String[] result = new String[names.length];
for (int i = 0; i < result.length; i++) {
@@ -104,7 +87,7 @@
protected AbstractPreferences childSpi(String name) {
int[] error = new int[1];
RegistryPreferencesImpl result = new RegistryPreferencesImpl(this, name);
- //FIXME: is it right thing to set newNode here?
+ // FIXME: is it right thing to set newNode here?
result.newNode = getNode(path, encodeWindowsStr(name).getBytes(), result.userNode, error);
if (error[ERROR_CODE] == RETURN_ACCESS_DENIED) {
throw new SecurityException();
@@ -118,7 +101,7 @@
flushPrefs(path, userNode, error);
if (error[ERROR_CODE] != RETURN_SUCCESS) {
// prefs.C=Flush error\!
- throw new BackingStoreException(Messages.getString("prefs.C")); //$NON-NLS-1$
+ throw new BackingStoreException(Messages.getString("prefs.C")); //$NON-NLS-1$
}
}
@@ -138,7 +121,7 @@
byte[][] keys = keys(path, userNode, errorCode);
if (errorCode[ERROR_CODE] != RETURN_SUCCESS) {
// prefs.D=Enumerate keys error\!
- throw new BackingStoreException(Messages.getString("prefs.D")); //$NON-NLS-1$
+ throw new BackingStoreException(Messages.getString("prefs.D")); //$NON-NLS-1$
}
String[] result = new String[keys.length];
for (int i = 0; i < result.length; i++) {
@@ -153,18 +136,18 @@
putValue(path, encodeWindowsStr(name).getBytes(), value.getBytes(), userNode, errorCode);
if (errorCode[ERROR_CODE] == RETURN_ACCESS_DENIED) {
// prefs.E=Access denied\!
- throw new SecurityException(Messages.getString("prefs.E")); //$NON-NLS-1$
+ throw new SecurityException(Messages.getString("prefs.E")); //$NON-NLS-1$
}
}
@Override
protected void removeNodeSpi() throws BackingStoreException {
int[] error = new int[1];
- removeNode(((RegistryPreferencesImpl) parent()).path,
- encodeWindowsStr(name()).getBytes(), userNode, error);
+ removeNode(((RegistryPreferencesImpl) parent()).path, encodeWindowsStr(name()).getBytes(),
+ userNode, error);
if (error[ERROR_CODE] != RETURN_SUCCESS) {
// prefs.F=Remove node error\!
- throw new BackingStoreException(Messages.getString("prefs.F")); //$NON-NLS-1$
+ throw new BackingStoreException(Messages.getString("prefs.F")); //$NON-NLS-1$
}
}
@@ -174,7 +157,7 @@
removeKey(path, encodeWindowsStr(key).getBytes(), userNode, errorCode);
if (errorCode[ERROR_CODE] == RETURN_ACCESS_DENIED) {
// prefs.E=Access denied\!
- throw new SecurityException(Messages.getString("prefs.E")); //$NON-NLS-1$
+ throw new SecurityException(Messages.getString("prefs.E")); //$NON-NLS-1$
}
}
@@ -183,71 +166,70 @@
flushSpi();
}
- //handle the lower/upper case pitfall
- private static String encodeWindowsStr(String str){
+ // handle the lower/upper case pitfall
+ private static String encodeWindowsStr(String str) {
char[] chars = str.toCharArray();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
- if(c == '/'){
+ if (c == '/') {
buffer.append("\\"); //$NON-NLS-1$
- }else if(c == '\\'){
+ } else if (c == '\\') {
buffer.append("//"); //$NON-NLS-1$
- }else if((c >= 'A') && (c <= 'Z')){
+ } else if ((c >= 'A') && (c <= 'Z')) {
buffer.append('/').append(c);
- }else{
+ } else {
buffer.append(c);
}
}
return buffer.toString();
}
-
- private static String decodeWindowsStr(String str){
+
+ private static String decodeWindowsStr(String str) {
StringBuffer buffer = new StringBuffer();
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
- char c= chars[i];
- if(c == '\\'){
+ char c = chars[i];
+ if (c == '\\') {
buffer.append('/');
- }else if(c == '/'){
- if((c = chars[++i]) == '/'){
+ } else if (c == '/') {
+ if ((c = chars[++i]) == '/') {
buffer.append('\\');
- }else{
+ } else {
buffer.append(c);
}
- }else{
+ } else {
buffer.append(c);
}
}
return buffer.toString();
}
-
+
/*
- * --------------------------------------------------------------
- * Native methods declaration
+ * --------------------------------------------------------------
+ * Native method declarations
* --------------------------------------------------------------
*/
- private native byte[] getValue(byte[] registryPath, byte[] key,
- boolean isUserNode, int[] errorCode);
- private native void putValue(byte[] registryPath, byte[] key, byte[] value,
- boolean isUserNode, int[] errorCode);
+ private native byte[] getValue(byte[] registryPath, byte[] key, boolean isUserNode,
+ int[] errorCode);
- private native void removeKey(byte[] registryPath, byte[] key,
- boolean isUserNode, int[] errorCode);
+ private native void putValue(byte[] registryPath, byte[] key, byte[] value, boolean isUserNode,
+ int[] errorCode);
- private native byte[][] keys(byte[] registryPath, boolean isUserNode,
+ private native void removeKey(byte[] registryPath, byte[] key, boolean isUserNode,
int[] errorCode);
- private native void removeNode(byte[] registryPath, byte[] name,
- boolean isUserNode, int[] errorCode);
+ private native byte[][] keys(byte[] registryPath, boolean isUserNode, int[] errorCode);
- private native boolean getNode(byte[] registryPath, byte[] name,
- boolean isUserNode, int[] errorCode);
+ private native void removeNode(byte[] registryPath, byte[] name, boolean isUserNode,
+ int[] errorCode);
+
+ private native boolean getNode(byte[] registryPath, byte[] name, boolean isUserNode,
+ int[] errorCode);
- private native byte[][] getChildNames(byte[] registryPath,
- boolean isUserNode, int[] errorCode);
+ private native byte[][] getChildNames(byte[] registryPath, boolean isUserNode, int[] errorCode);
- private native void flushPrefs(byte[] registryPath, boolean isUserNode,
- int[] errorCode) throws SecurityException;
+ private native void flushPrefs(byte[] registryPath, boolean isUserNode, int[] errorCode)
+ throws SecurityException;
}
Modified: harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/XMLParser.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/XMLParser.java?rev=735939&r1=735938&r2=735939&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/XMLParser.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/prefs/src/main/java/java/util/prefs/XMLParser.java Mon Jan 19 21:47:38 2009
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-
package java.util.prefs;
import java.io.BufferedInputStream;
@@ -27,6 +26,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
+import java.io.Writer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.security.AccessController;
@@ -55,7 +55,6 @@
/**
* Utility class for the Preferences import/export from XML file.
- *
*/
class XMLParser {
@@ -68,15 +67,15 @@
* Constant - the DTD string
*/
static final String PREFS_DTD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" //$NON-NLS-1$
- + " <!ELEMENT preferences (root)>" //$NON-NLS-1$
- + " <!ATTLIST preferences EXTERNAL_XML_VERSION CDATA \"0.0\" >" //$NON-NLS-1$
- + " <!ELEMENT root (map, node*) >" //$NON-NLS-1$
- + " <!ATTLIST root type (system|user) #REQUIRED >" //$NON-NLS-1$
- + " <!ELEMENT node (map, node*) >" //$NON-NLS-1$
- + " <!ATTLIST node name CDATA #REQUIRED >" //$NON-NLS-1$
- + " <!ELEMENT map (entry*) >" //$NON-NLS-1$
- + " <!ELEMENT entry EMPTY >" //$NON-NLS-1$
- + " <!ATTLIST entry key CDATA #REQUIRED value CDATA #REQUIRED >"; //$NON-NLS-1$
+ + " <!ELEMENT preferences (root)>" //$NON-NLS-1$
+ + " <!ATTLIST preferences EXTERNAL_XML_VERSION CDATA \"0.0\" >" //$NON-NLS-1$
+ + " <!ELEMENT root (map, node*) >" //$NON-NLS-1$
+ + " <!ATTLIST root type (system|user) #REQUIRED >" //$NON-NLS-1$
+ + " <!ELEMENT node (map, node*) >" //$NON-NLS-1$
+ + " <!ATTLIST node name CDATA #REQUIRED >" //$NON-NLS-1$
+ + " <!ELEMENT map (entry*) >" //$NON-NLS-1$
+ + " <!ELEMENT entry EMPTY >" //$NON-NLS-1$
+ + " <!ATTLIST entry key CDATA #REQUIRED value CDATA #REQUIRED >"; //$NON-NLS-1$
/*
* Constant - the specified header
@@ -92,7 +91,7 @@
* empty string array constant
*/
private static final String[] EMPTY_SARRAY = new String[0];
-
+
/*
* Constant - used by FilePreferencesImpl, which is default implementation of Linux platform
*/
@@ -102,7 +101,7 @@
* Constant - specify the DTD version
*/
private static final float XML_VERSION = 1.0f;
-
+
/*
* DOM builder
*/
@@ -126,7 +125,7 @@
}
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId)
- throws SAXException, IOException {
+ throws SAXException, IOException {
if (systemId.equals(PREFS_DTD_NAME)) {
InputSource result = new InputSource(new StringReader(
PREFS_DTD));
@@ -181,7 +180,7 @@
flushEmptyElement("map", out); //$NON-NLS-1$
StringTokenizer ancestors = new StringTokenizer(prefs.absolutePath(),
- "/"); //$NON-NLS-1$
+ "/"); //$NON-NLS-1$
exportNode(ancestors, prefs, withSubTree, out);
flushEndTag("root", out); //$NON-NLS-1$
@@ -192,7 +191,7 @@
private static void exportNode(StringTokenizer ancestors,
Preferences prefs, boolean withSubTree, BufferedWriter out)
- throws IOException, BackingStoreException {
+ throws IOException, BackingStoreException {
if (ancestors.hasMoreTokens()) {
String name = ancestors.nextToken();
flushStartTag(
@@ -211,7 +210,7 @@
}
private static void exportSubTree(Preferences prefs, BufferedWriter out)
- throws BackingStoreException, IOException {
+ throws BackingStoreException, IOException {
String[] names = prefs.childrenNames();
if (names.length > 0) {
for (int i = 0; i < names.length; i++) {
@@ -226,7 +225,7 @@
}
private static void exportEntries(Preferences prefs, BufferedWriter out)
- throws BackingStoreException, IOException {
+ throws BackingStoreException, IOException {
String[] keys = prefs.keys();
String[] values = new String[keys.length];
for (int i = 0; i < keys.length; i++) {
@@ -252,7 +251,7 @@
}
private static void flushEndTag(String tagName, BufferedWriter out)
- throws IOException {
+ throws IOException {
flushIndent(indent--, out);
out.write("</"); //$NON-NLS-1$
out.write(tagName);
@@ -261,7 +260,7 @@
}
private static void flushEmptyElement(String tagName, BufferedWriter out)
- throws IOException {
+ throws IOException {
flushIndent(++indent, out);
out.write("<"); //$NON-NLS-1$
out.write(tagName);
@@ -293,7 +292,7 @@
}
private static void flushIndent(int ind, BufferedWriter out)
- throws IOException {
+ throws IOException {
for (int i = 0; i < ind; i++) {
out.write(" "); //$NON-NLS-1$
}
@@ -310,7 +309,7 @@
}
private static void flushStartTag(String tagName, BufferedWriter out)
- throws IOException {
+ throws IOException {
flushIndent(++indent, out);
out.write("<"); //$NON-NLS-1$
out.write(tagName);
@@ -350,7 +349,7 @@
* utilities for Preferences import
**************************************************************************/
static void importPrefs(InputStream in) throws IOException,
- InvalidPreferencesFormatException {
+ InvalidPreferencesFormatException {
try {
// load XML document
Document doc = builder.parse(new InputSource(in));
@@ -367,7 +366,7 @@
// check preferences root's type
Element root = (Element) preferences
- .getElementsByTagName("root").item(0); //$NON-NLS-1$
+ .getElementsByTagName("root").item(0); //$NON-NLS-1$
Preferences prefsRoot = null;
String type = root.getAttribute("type"); //$NON-NLS-1$
if (type.equals("user")) { //$NON-NLS-1$
@@ -388,7 +387,7 @@
}
private static void loadNode(Preferences prefs, Element node)
- throws TransformerException {
+ throws TransformerException {
// load preferences
NodeList children = XPathAPI.selectNodeList(node, "node"); //$NON-NLS-1$
NodeList entries = XPathAPI.selectNodeList(node, "map/entry"); //$NON-NLS-1$
@@ -435,14 +434,6 @@
return loadFilePrefsImpl(file);
}
});
-
- // try {
- // //FIXME: lines below can be deleted, because it is not required to
- // persistent at the very beginning
- // flushFilePrefs(file, result);
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
}
static Properties loadFilePrefsImpl(final File file) {
@@ -453,7 +444,6 @@
InputStream in = null;
FileLock lock = null;
try {
-
FileInputStream istream = new FileInputStream(file);
in = new BufferedInputStream(istream);
FileChannel channel = istream.getChannel();
@@ -469,17 +459,14 @@
result.setProperty(key, value);
}
return result;
- } catch (Exception e) {
- e.printStackTrace();
+ } catch (IOException e) {
+ } catch (SAXException e) {
+ } catch (TransformerException e) {
+ // transform shouldn't fail for xpath call
+ throw new AssertionError(e);
} finally {
- try {
- lock.release();
- } catch (Exception e) {//ignore
- }
- try {
- in.close();
- } catch (Exception e) {//ignore
- }
+ releaseQuietly(lock);
+ closeQuietly(in);
}
} else {
file.delete();
@@ -501,7 +488,7 @@
}
});
}
-
+
static void flushFilePrefsImpl(File file, Properties prefs) throws IOException {
BufferedWriter out = null;
FileLock lock = null;
@@ -527,18 +514,35 @@
}
out.flush();
} finally {
- try {
- lock.release();
- } catch (Exception e) {//ignore
- }
- try {
- if (null != out) {
- out.close();
- }
- } catch (Exception e) {//ignore
- }
+ releaseQuietly(lock);
+ closeQuietly(out);
}
}
+
+ private static void releaseQuietly(FileLock lock) {
+ if (lock == null) {
+ return;
+ }
+ try {
+ lock.release();
+ } catch (IOException e) {}
+ }
+
+ private static void closeQuietly(Writer out) {
+ if (out == null) {
+ return;
+ }
+ try {
+ out.close();
+ } catch (IOException e) {}
+ }
+
+ private static void closeQuietly(InputStream in) {
+ if (in == null) {
+ return;
+ }
+ try {
+ in.close();
+ } catch (IOException e) {}
+ }
}
-
-
Modified: harmony/enhanced/classlib/branches/java6/modules/prefs/src/test/java/org/apache/harmony/prefs/tests/AllTests.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/prefs/src/test/java/org/apache/harmony/prefs/tests/AllTests.java?rev=735939&r1=735938&r2=735939&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/prefs/src/test/java/org/apache/harmony/prefs/tests/AllTests.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/prefs/src/test/java/org/apache/harmony/prefs/tests/AllTests.java Mon Jan 19 21:47:38 2009
@@ -25,15 +25,15 @@
*/
public class AllTests {
- public static void main(String[] args) {
- junit.textui.TestRunner.run(AllTests.suite());
- }
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(AllTests.suite());
+ }
- public static Test suite() {
- TestSuite suite = new TestSuite("All Prefs test suites");
- // $JUnit-BEGIN$
- suite.addTest(org.apache.harmony.prefs.tests.java.util.prefs.AllTests.suite());
- // $JUnit-END$
- return suite;
- }
+ public static Test suite() {
+ TestSuite suite = new TestSuite("All Prefs test suites");
+ // $JUnit-BEGIN$
+ suite.addTest(org.apache.harmony.prefs.tests.java.util.prefs.AllTests.suite());
+ // $JUnit-END$
+ return suite;
+ }
}