Base class for deriving custom AppConfig factories. This object provides
the core support for loading AppConfig objects from well-known set places.
All you have to do when subclassing this object is to pass in the
appropriate settings in the constructor. The rest is handled by this
object.
For example, here is the code for a singleton implementation:
package com.myapp;
import org.xorm.util.AppConfigFactory;
public final class MyConfigFactory extends AppConfigFactory {
private static MyConfigFactory singleton;
static {
singleton = new MyConfigFactory();
}
public static MyConfigFactory getInstance() {
return singleton;
}
private MyConfigFactory() {
super("myapp/config",
"com.myapp.configFile",
"/myapp.properties");
}
}
The example above would look for application configuration in the following
places:
- It looks for a
"myapp/config" JNDI resource, which you
could set up under your servlet engine (i.e. Tomcat) or webapp
configuration. If you choose to configure your application via JNDI,
you should make sure the resource points to an AppConfig object.
- It looks at the
"com.myapp.configFile" system property,
which would specify the resource or file where the application properties
live.
- It falls back on a default resource or file
"/myapp.properties" which can live either on the classpath
or on the file system. Generally you are encouraged to build a default
property file into your application's JAR or class files...in the case
above named myapp.properties.
Then, in your application code, whenever you need to access properties
or get a PersistenceManager instance, simply do this:
package com.myapp;
import javax.jdo.PersistenceManager;
import org.xorm.util.AppConfig;
public class Example {
public void main(String[] args) {
AppConfig appConfig = MyAppConfigFactory.getInstance().getAppConfig();
PersistenceManager mgr = appConfig.getPersistenceManager();
String prop = appConfig.getProperty("someProperty");
}
}
author: Dan Checkoway See Also: AppConfig |