Parameter selection API
Document catalog JD018 Cherami
Cherami@163.net
Overview applications require parameters and configuration data to accommodate different users and environments.
The java.util.prefs package provides the application to store and restore user and system parameters and configuration data. The data is constantly deposited in a stand-alone implemented backup storage. There are two discrete parameter node trees, one for user parameters, and another for system parameters.
All methods for modifying parameter data allow asynchronous operations. They may return immediately and modified will eventually be delivered to the backup storage. The Flush method can be used to force refresh backup storage.
The method in the Preferences class can be used in parallel with multiple threads without external synchronization under single JVM. If this class is used by multiple JVMs that store parameter data using the same backup area, the data storage will not be damaged, but the data consistency cannot be guaranteed.
For more details, please refer to the link below:
Precautions for comparison of API and other mechanisms design FAQ
Parameter Selection API and other mechanisms The developer may select a special way to manage parameters and configuration data before introducing parameters: Use the parameters API or JNDI API described below. Typically, the parameters and configuration data are stored in the configuration file, which can be accessed through the Java.util.Properties API. However, these documents should be placed or what they should call anything. Use this mechanism, back up user data or pass them from a machine to another machine. As the application increases, the file name conflict may increase. Moreover, this mechanism is not used under the platform without the local disk, or requires data to store data on an external data store (eg, an enterprise-wide LDAP directory service). In a few cases, the developer uses the JNDI (Java Name and Directory Interface) API access to the directory service to store user parameters and configuration data. (Unlike parameter API, JNDI allows for any backup storage (rear end neutral). Although JNDI is an abnormal power, it is also huge, it consists of 5 packs and 83. JNDI does not provide parameter data should be stored Regular policies in the directory name space. Both parameters API and JNDI provide a simple, universal, back-end neutral parameter management tool. Parameter selection API provides such a tool, which combines the convenient selection of the API And the rear end of JNDI. It provides a sufficient built-in policy to prevent name conflicts in difficult backup data storage, encouraging consistency and promotes robustness.
See: Design FAQ
Precautions This section contains materials that are not part of the parameter selection API specification, which just provides examples of how to use parameters to select API.
Get the Preferences object in the class to get the status of the Preferences object atomization correction detection backup storage in the static method.
Getting the example object below in the class clarifies how to get the Preferences object (system and user) in the class. This example can only work in an instance method to pay attention to Static Final members instead of its literal string to use as the name of the key (Num_Rows and Num_cols). This reduces the possibility of runtime errors due to the wrong runtime in printing of keyword names.
At the same time, pay attention to the reasonable default value provided by each parameter. These default values are returned if the parameter value is not set or the reserve storage is not arriving.
Package com.acme.widget;
Import java.util.prefs. *;
Public class gadget {
// Preference Keys for this package
Private static final string num_rows = "num_rows";
Private static final string num_cols = "num_cols";
Void foo () {
Preferences prefs = preferences.Usernodeforpackage (this);
Int nuMrows = prefs.getint (Num_Rows, 40);
Int numcols = prefs.getint (Num_cols, 80);
...
}
}
The above example got a parameter for each user, if you want to get the system value, the first line in the foo should be replaced by the following program:
Preferences prefs = preferences.systemnodeforpackage (this);
Examples of the previous section of the Preferences object in a static method clarified that the Preferences object is acquired in the class, in the instance method. In a static method or static initializer, you need to clear the package name:
Static string urbename = "/ com / acme / widget";
STATIC void foo () {
Preferences prefs = preferences.userroot (). Node (outodename);
...
}
It is acceptable to get a system parameter in a static initializer when you need system parameters:
Static preferences prefs = preferences.systemroot (). Node (outodename);
It is usually the same thing for a user parameter to be acceptable, but if the code is used for the server, multiple users may concurrent or running the program in a row, so that it will not work. Under a such system, UserRoot returns a suitable node for the caller, so calling UserNodeForPackage or UserRoot in an appropriate time is very dangerous. In such a server environment, the finalized code snippet should be to obtain the user parameter object before using them, which is a good, conservative practice. Just like
The previous example did it.
Atomic correction parameters Select the API does not provide multiple parameter atoms like "transaction" to be modified database operations. Sometimes we need to modify two or more parameters as a unit. For example, assume that the X and Y coordinates of the storage window, the only way to complete the atomic modification is to store two values simultaneously in a parameter. Many encoding methods are possible, the following is a simple example:
INT X, Y;
...
Prefs.Put (position, x "," y);
When reading a "composite parameter" like this, you need to decode. For robustness, it should be allowed to be destroyed (non-resoluntary) value being read:
Static int x_default = 50, y_default = 25;
Void baz () {
String Position = prefs.get (position, x_default "," y_default);
INT X, Y;
Try {
INT i = position.indexof (',');
X = integer.parseint (Coordinates.Substring (0, i));
Y = integer.parseint (position.substring (i 1));
} catch (exception e) {
// Value Was Corrupt, Just Use Defaults
X = x_default;
Y = y_default;
}
...
}
The state of the probe backup store usually does not need to know if the backup storage is available. It should always be available, but if it is not available, the code will use the default value instead of the parameter value in the backup store to continue execution. In the very rare case, some advanced programs may want to change its behavior when the backup storage is not available (or simple refusal to continue running). Below is a method of detecting whether the backup storage is available by trying to modify the parameter value and force the result to write back the backup store. Private static final string backing_store_avail = "backingstoreavail";
Private static boolean backingstoreavailable () {
Preferences prefs = preferences.userroot (). Node ("
");
Try {
Boolean OldValue = prefs.getBoolean (backing_store_avail, false);
Prefs.putBoolean (Backing_Store_avail,! OldValue);
Prefs.flush ();
} catch (backingstoreexception e) {
Return False;
}
Return True;
}
Design FAQ The following is the relevant parameter selection API design of FAQ.
Parameter Selection API involves parameter API? It is intentionally replaces the parameters of the usual use of most people, correcting its shortcomings while maintaining its simpleness. When using the parameter API, the programmer must specify the path to each parameter file, but there is no standard positioning or naming agreement. The parameter file is "fragile" because they can be edited by manual but easily destroyed. Parameter files are not easy to use for other continuing mechanisms outside the file system. In short, the parameter API tool is not suitable. The parameter selection API involves JNDI? Like JNDI, parameter selection API provides a lasting keyword-value to data access. However, JNDI is far from powerful and relatively large. JNDI is suitable for enterprises that require its capabilities. The parameter selection API is intended to be simple, universally, the backend parameter management tool, so that any Java application can be easily customized according to user parameters and maintain a small status parameter from one process to another process. Why do all GET methods need to call programs to pass in a default? This mandatory application author provides a reasonable default, and there is also a reasonable possible value run even when the memory is not available. How to decide what is needed to throw a backingstoreException? Only those methods that are absolutely needed in semantics need to throw this exception. Usually the application does not need to call these methods. As long as these methods are avoided, the application can run even in the case where the backup storage is not available, this is a clear data object. Why is this API to provide stronger guarantees on multi-virtual machine parallel access? Similar, why this API allows multiple Preferences to update merge into a single "transaction", all or no semantic concepts? Although this API provides Basic continuous data storage, but it has no intention to replace the database. It is dangerous on standard parameter / configuration storage, most of which does not provide guarantees and functions like databases. Such a desired purpose that stores this API has been proven to be appropriate. Why this API is sensitive to keywords and node names, while other APIs with the same function (such as Win32 registry and LDAP) are not? In the Java language, large-mode-sensitive string keywords are very common. In particular, they are provided by the Properties class that will be replaced by this API. Use Properties in case sensitive ways to use Properties to be unusual. For example, Java's package name (also case sensitive) is sometimes used as a keyword. This design makes the work of system programmers that implement Preferences on the backup storage in case storage, but this is a considered price, because more programmers will use parameter selection API is not realizing it. Why does this API do not use a collection structure of Java 2? This API is designed for a very accurate purpose and optimized for this purpose. This API will be inconvenient for ordinary users due to the lack of normal types (see JSR-14). If it is forced to comply with the MAP API, it will make it lack the type of security at compile. It is also not expected to use other MAPs (although if this assumption is wrong, you can also implement an adapter class to correct this assumption). The parameter selection API design and MAP are so similar that the programmers who are familiar with the latter should have no difficulties. Why does the PUT and REMOVE methods do not return previous values? Both methods are expected to be executed even if the backup storage is not available. If they need to return the previous value, then this expectation is impossible. Further, if this API is implemented on some universal backend data storage, it will give a negative impact on performance. Why this API allows but not required to store default values? This feature is required in the enterprise settings as a standard, the cost-effective management of the parameter selection runs through the entire enterprise, but this requirement is too much on the self-managed single user settings. .
Why does this API do not include methods of reading and writing a serialized object? Some of the serialized objects are somewhat fragile: if the version of the program is read and the program version written by writing this feature is different, the object may be incomplete continuous (or It is not continuous at all). It is not impossible to use this API to store string objects, but we do not encourage this, and there is no convenient way. Why preferences is an abstract class rather than an interface? The value of the ability to add new methods in conjunction with more than preferences cannot be used as a "mixin" (that is, not any class can serve as a Preferences object) Disadvantages. At the same time, this also avoids the needs of individual classes on static methods. (The interface cannot include static methods.) CopyRight © 2001 Sun Microsystems, Inc. All Rights RESERVED. Java Software