org.xorm.util
Class AppConfigFactory

java.lang.Object
  |
  +--org.xorm.util.AppConfigFactory

public class AppConfigFactory
extends Object

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:

  1. 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.
  2. It looks at the "com.myapp.configFile" system property, which would specify the resource or file where the application properties live.
  3. 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

Field Summary
protected  Class appConfigClass
           
protected  AppConfig config
           
protected  String configFileSystemProperty
           
protected  String defaultConfigFile
           
protected  String jndiConfigResource
           
protected  Logger logger
           
 
Constructor Summary
protected AppConfigFactory(String jndiConfigResource, String configFileSystemProperty, String defaultConfigFile)
          Constructor
protected AppConfigFactory(String jndiConfigResource, String configFileSystemProperty, String defaultConfigFile, Class appConfigClass)
           
 
Method Summary
 AppConfig getAppConfig()
          Get the AppConfig associated with this factory.
protected  AppConfig loadAppConfig()
          Load the AppConfig from the specified locations.
protected  AppConfig loadAppConfigFromJNDI()
          Load the AppConfig as a JNDI resource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

jndiConfigResource

protected String jndiConfigResource

configFileSystemProperty

protected String configFileSystemProperty

defaultConfigFile

protected String defaultConfigFile

appConfigClass

protected Class appConfigClass

logger

protected Logger logger

config

protected AppConfig config
Constructor Detail

AppConfigFactory

protected AppConfigFactory(String jndiConfigResource,
                           String configFileSystemProperty,
                           String defaultConfigFile)
Constructor

Parameters:
jndiConfigResource - the resource path (under "jndi:comp/env") where the first attempt to load the configuration should look
configFileSystemProperty - the name of a system property that may have been defined as a second-level fallback, pointing to a file or resource where the configuration should be loaded
defaultConfigFile - the path of a file or resource where the last ditch attempt to find the configuration will look

AppConfigFactory

protected AppConfigFactory(String jndiConfigResource,
                           String configFileSystemProperty,
                           String defaultConfigFile,
                           Class appConfigClass)
Method Detail

getAppConfig

public AppConfig getAppConfig()
Get the AppConfig associated with this factory. This factory loads a single instance of AppConfig and keeps it around for subsequent calls. Only that one instance is ever created.

Returns:
the respective AppConfig object
See Also:
AppConfig

loadAppConfig

protected AppConfig loadAppConfig()
Load the AppConfig from the specified locations. This method will first try to load the config from the specified JNDI resource. Then it will look at the specified system property value, falling back on the default path.

Returns:
a new instance of a freshly loaded AppConfig

loadAppConfigFromJNDI

protected AppConfig loadAppConfigFromJNDI()
Load the AppConfig as a JNDI resource

Returns:
any AppConfig instance configured under the specified resource path, if found; otherwise null


$Header: /cvsroot/xorm/xorm/docs/api/org/xorm/util/AppConfigFactory.html,v 1.2 2004/05/30 08:55:10 wbiggs Exp $