public class FilePersistenceManager extends Object implements PersistenceManager
FilePersistenceManager
class 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 the
FilePersistenceManager(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
org.apache.felix.cm.impl.ConfigurationManager#CM_CONFIG_DIR
bundle
context property. That is the Configuration Admin Service creates an instance
of this class calling
new FilePersistenceManager(bundleContext, bundleContext.getProperty(CM_CONFIG_DIR))
.
If the location is not set, the config
directory in the current
working directory (as set in the user.dir
system 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 .config
to the PID of the configuration. The PID
is converted into a relative path name by replacing enclosed dots to slashes.
Non-symbolic-name
characters in the PID are encoded with their
Unicode character code in hexadecimal.
Examples of PID to name conversion: | |
PID | Configuration File Name |
---|---|
sample | sample.config |
org.apache.felix.log.LogService | org/apache/felix/log/LogService.config |
sample.fläche | sample/fl%00e8che.config |
Mulithreading Issues
In a multithreaded environment the store(String, Dictionary)
and
#load(File)
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:
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.#load(File)
method is completeley synchronized on the
FilePersistenceManager instance such that the store(java.lang.String, java.util.Dictionary)
method might
inadvertantly try to replace the file while it is being read.Iterator
returned by getDictionaries()
is implemented such that any temporary configuration file is just ignored.Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_CONFIG_DIR
The default configuration data directory if no location is configured
(value is "config").
|
Constructor and Description |
---|
FilePersistenceManager(org.osgi.framework.BundleContext bundleContext,
String location)
Creates an instance of this persistence manager using the given location
as the directory to store and retrieve the configuration files.
|
FilePersistenceManager(String location)
Creates an instance of this persistence manager using the given location
as the directory to store and retrieve the configuration files.
|
Modifier and Type | Method and Description |
---|---|
void |
delete(String pid)
Deletes the file for the given identifier.
|
boolean |
exists(String pid)
Returns
true if a (configuration) file exists for the given
identifier. |
Enumeration |
getDictionaries()
Loads configuration data from the configuration location and returns
it as
Dictionary objects. |
File |
getLocation()
Returns the directory in which the configuration files are written as
a
File object. |
Dictionary |
load(String pid)
Reads the (configuration) for the given identifier into a
Dictionary object. |
void |
store(String pid,
Dictionary props)
Stores the contents of the
Dictionary in a file denoted
by the given identifier. |
public static final String DEFAULT_CONFIG_DIR
public FilePersistenceManager(String location)
This constructor resolves the configuration file location as follows:
location
is null
, the config
directory in the current working directory as specified in the
user.dir
system property is assumed.File.getAbsoluteFile()
method.IllegalArgumentException
is thrown.
This constructor is equivalent to calling
FilePersistenceManager(BundleContext, String)
with a
null
BundleContext
.
location
- The configuration file location. If this is
null
the config
directory below the current
working directory is used.IllegalArgumentException
- If the location
exists but
is not a directory or does not exist and cannot be created.public FilePersistenceManager(org.osgi.framework.BundleContext bundleContext, String location)
This constructor resolves the configuration file location as follows:
location
is null
, the config
directory in the persistent storage area of the bundle identified by
bundleContext
is used.bundleContext
is null
,
the config
directory in the current working directory as
specified in the user.dir
system property is assumed.bundleContext
is 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 the
File.getAbsoluteFile()
method.IllegalArgumentException
is thrown.bundleContext
- The BundleContext
to optionally get
the data location for the configuration files. This may be
null
, in which case this constructor acts exactly the
same as calling FilePersistenceManager(String)
.location
- The configuration file location. If this is
null
the config
directory below the current
working directory is used.IllegalArgumentException
- If the location exists but is not a
directory or does not exist and cannot be created.IllegalStateException
- If the bundleContext
is not
valid.public File getLocation()
File
object.public Enumeration getDictionaries()
Dictionary
objects.
This method is a lazy implementation, which is just one configuration file ahead of the current enumeration location.
getDictionaries
in interface PersistenceManager
Dictionary
class.public void delete(String pid)
delete
in interface PersistenceManager
pid
- The identifier of the configuration file to delete.public boolean exists(String pid)
true
if a (configuration) file exists for the given
identifier.exists
in interface PersistenceManager
pid
- The identifier of the configuration file to check.true
if the file existspublic Dictionary load(String pid) throws IOException
Dictionary
object.load
in interface PersistenceManager
pid
- The identifier of the configuration file to delete.Dictionary
may be empty if the file contains no configuration information
or is not properly formatted.IOException
- If an error occurrs loading the dictionary. An
IOException
must also be thrown if no dictionary
exists for the given identifier.public void store(String pid, Dictionary props) throws IOException
Dictionary
in a file denoted
by the given identifier.store
in interface PersistenceManager
pid
- The identifier of the configuration file to which to write
the configuration contents.props
- The configuration data to write.IOException
- If an error occurrs writing the configuration data.Copyright © 2006–2013 The Apache Software Foundation. All rights reserved.