This example shows how to load a registry. On a load
method, the class reads the configuration file (adapters.txt) and
dynamically instantiates the repository given within the repository-config tag. In this case, the
adapters.txt file is a flat properties file, so we can use the standard
PropertyRepository
class.
Sample config file:
<registry-config> <repositories> <repository> <repository-name>adapter</repository-name> <repository-class>org.jvending.registry.PropertyRepository</repository-class> <repository-config>${basedir}/adapters.txt</repository-config> <init-param> <param-name>add</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>delete</param-name> <param-value>false</param-value> </init-param> </repository> </repositories> </registry-config>
RepositoryRegistry repositoryRegistry = RepositoryRegistry.Factory.create(); repositoryRegistry.loadFromFile("./sample-config.xml"); PropertyRepository repository = (PropertyRepository) repositoryRegistry.find("adapter"); `
You may invoke the
loadFromFile
method multiple times with different registry-config.xml files
and the
RepositoryRegistry
instance will cumulatively load the repositories. As of version 1.2.0, each
RepositoryRegistry
is no longer a singleton, so you can also create
and load
multiple
RepositoryRegistry
instances, each containing a different set of repositories.
Keep in mind that if you have loaded other repositories into the registry and need to access them through the find method, you need to cast to the appropriate type.
You don't need a registry-config file to load repositories. Consider the following example. Create
a
PropertyRepository
, load it and add to the
RepositoryRegistry
PropertyRepository repo = new ProperyRepository(); FileInputStream fileInputStream = new FileInputStream("myprops.txt"); repo.load(fileInputStream, null); repositoryRegistry.addRepository("my-props", repo);