Class FilePersistenceManager
- java.lang.Object
-
- org.apache.felix.cm.file.FilePersistenceManager
-
- All Implemented Interfaces:
PersistenceManager
public class FilePersistenceManager extends java.lang.Object implements PersistenceManager
TheFilePersistenceManagerclass stores configuration data in properties-like files inside a given directory. All configuration files are located in the same directory.The configuration directory is set by either the
FilePersistenceManager(String)constructor or theFilePersistenceManager(BundleContext, String)constructor. Refer to the respective JavaDocs for more information.When this persistence manager is used by the Configuration Admin Service, the location may be configured using the
ConfigurationManagerbundle context property.If the location is not set, the
configdirectory in the current working directory (as set in theuser.dirsystem property) is used. If the the location is set but, no such directory exists, the directory and any missing parent directories are created. If a file exists at the given location, the constructor fails.Configuration files are created in the configuration directory by appending the extension
.configto the PID of the configuration. The PID is converted into a relative path name by replacing enclosed dots to slashes. Non-symbolic-namecharacters in the PID are encoded with their Unicode character code in hexadecimal.Examples of PID to name conversion: PID Configuration File Name samplesample.configorg.apache.felix.log.LogServiceorg/apache/felix/log/LogService.configsample.flächesample/fl%00e8che.configMulithreading Issues
In a multithreaded environment the
store(String, Dictionary)andload(String)methods may be called at the the quasi-same time for the same configuration PID. It may no happen, that the store method starts writing the file and the load method might at the same time read from the file currently being written and thus loading corrupt data (if data is available at all).To prevent this situation from happening, the methods use synchronization and temporary files as follows:
- The
store(String, Dictionary)method writes a temporary file with file extension.tmp. When done, the file is renamed to actual configuration file name as implied by the PID. This last step of renaming the file is synchronized on the FilePersistenceManager instance. - The
load(String)method is completeley synchronized on the FilePersistenceManager instance such that thestore(java.lang.String, java.util.Dictionary)method might inadvertantly try to replace the file while it is being read. - Finally the
Iteratorreturned bygetDictionaries()is implemented such that any temporary configuration file is just ignored.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_CONFIG_DIRThe default configuration data directory if no location is configured (value is "config").static java.lang.StringDEFAULT_PERSISTENCE_MANAGER_NAMEThe name of this persistence manager when registered in the service registry.-
Fields inherited from interface org.apache.felix.cm.PersistenceManager
PROPERTY_NAME
-
-
Constructor Summary
Constructors Constructor Description FilePersistenceManager(java.lang.String location)Creates an instance of this persistence manager using the given location as the directory to store and retrieve the configuration files.FilePersistenceManager(org.osgi.framework.BundleContext bundleContext, java.lang.String location)Creates an instance of this persistence manager using the given location as the directory to store and retrieve the configuration files.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddelete(java.lang.String pid)Deletes the file for the given identifier.booleanexists(java.lang.String pid)Returnstrueif a (configuration) file exists for the given identifier.java.util.EnumerationgetDictionaries()Loads configuration data from the configuration location and returns it asDictionaryobjects.java.io.FilegetLocation()Returns the directory in which the configuration files are written as aFileobject.java.util.Dictionaryload(java.lang.String pid)Reads the (configuration) for the given identifier into aDictionaryobject.voidstore(java.lang.String pid, java.util.Dictionary props)Stores the contents of theDictionaryin a file denoted by the given identifier.
-
-
-
Field Detail
-
DEFAULT_CONFIG_DIR
public static final java.lang.String DEFAULT_CONFIG_DIR
The default configuration data directory if no location is configured (value is "config").- See Also:
- Constant Field Values
-
DEFAULT_PERSISTENCE_MANAGER_NAME
public static final java.lang.String DEFAULT_PERSISTENCE_MANAGER_NAME
The name of this persistence manager when registered in the service registry. (value is "file").- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FilePersistenceManager
public FilePersistenceManager(java.lang.String location)
Creates an instance of this persistence manager using the given location as the directory to store and retrieve the configuration files.This constructor resolves the configuration file location as follows:
- If
locationisnull, theconfigdirectory in the current working directory as specified in theuser.dirsystem property is assumed. - Otherwise the named directory is used.
- If the directory name resolved in the first or second step is not an
absolute path, it is resolved to an absolute path calling the
File.getAbsoluteFile()method. - If a non-directory file exists as the location found in the previous
step or the named directory (including any parent directories) cannot be
created, an
IllegalArgumentExceptionis thrown.
This constructor is equivalent to calling
FilePersistenceManager(BundleContext, String)with anullBundleContext.- Parameters:
location- The configuration file location. If this isnulltheconfigdirectory below the current working directory is used.- Throws:
java.lang.IllegalArgumentException- If thelocationexists but is not a directory or does not exist and cannot be created.
- If
-
FilePersistenceManager
public FilePersistenceManager(org.osgi.framework.BundleContext bundleContext, java.lang.String location)Creates an instance of this persistence manager using the given location as the directory to store and retrieve the configuration files.This constructor resolves the configuration file location as follows:
- If
locationisnull, theconfigdirectory in the persistent storage area of the bundle identified bybundleContextis used. - If the framework does not support persistent storage area for bundles
in the filesystem or if
bundleContextisnull, theconfigdirectory in the current working directory as specified in theuser.dirsystem property is assumed. - Otherwise the named directory is used.
- If the directory name resolved in the first, second or third step is
not an absolute path and a
bundleContextis provided which provides access to persistent storage area, the directory name is resolved as being inside the persistent storage area. Otherwise the directory name is resolved to an absolute path calling theFile.getAbsoluteFile()method. - If a non-directory file exists as the location found in the previous
step or the named directory (including any parent directories) cannot be
created, an
IllegalArgumentExceptionis thrown.
- Parameters:
bundleContext- TheBundleContextto optionally get the data location for the configuration files. This may benull, in which case this constructor acts exactly the same as callingFilePersistenceManager(String).location- The configuration file location. If this isnulltheconfigdirectory below the current working directory is used.- Throws:
java.lang.IllegalArgumentException- If the location exists but is not a directory or does not exist and cannot be created.java.lang.IllegalStateException- If thebundleContextis not valid.
- If
-
-
Method Detail
-
getLocation
public java.io.File getLocation()
Returns the directory in which the configuration files are written as aFileobject.- Returns:
- The configuration file location.
-
getDictionaries
public java.util.Enumeration getDictionaries()
Loads configuration data from the configuration location and returns it asDictionaryobjects.This method is a lazy implementation, which is just one configuration file ahead of the current enumeration location.
- Specified by:
getDictionariesin interfacePersistenceManager- Returns:
- an enumeration of configuration data returned as instances of
the
Dictionaryclass.
-
delete
public void delete(java.lang.String pid)
Deletes the file for the given identifier.- Specified by:
deletein interfacePersistenceManager- Parameters:
pid- The identifier of the configuration file to delete.
-
exists
public boolean exists(java.lang.String pid)
Returnstrueif a (configuration) file exists for the given identifier.- Specified by:
existsin interfacePersistenceManager- Parameters:
pid- The identifier of the configuration file to check.- Returns:
trueif the file exists
-
load
public java.util.Dictionary load(java.lang.String pid) throws java.io.IOExceptionReads the (configuration) for the given identifier into aDictionaryobject.- Specified by:
loadin interfacePersistenceManager- Parameters:
pid- The identifier of the configuration file to delete.- Returns:
- The configuration read from the file. This
Dictionarymay be empty if the file contains no configuration information or is not properly formatted. - Throws:
java.io.IOException- If an error occurs loading the dictionary. AnIOExceptionmust also be thrown if no dictionary exists for the given identifier.
-
store
public void store(java.lang.String pid, java.util.Dictionary props) throws java.io.IOExceptionStores the contents of theDictionaryin a file denoted by the given identifier.- Specified by:
storein interfacePersistenceManager- Parameters:
pid- The identifier of the configuration file to which to write the configuration contents.props- The configuration data to write.- Throws:
java.io.IOException- If an error occurrs writing the configuration data.
-
-