sascha@5: package de.intevation.artifactdatabase; sascha@5: sascha@5: import org.w3c.dom.Document; sascha@5: import org.w3c.dom.NodeList; sascha@5: sascha@5: import java.util.ArrayList; sascha@5: sascha@5: import de.intevation.artifacts.ArtifactFactory; sascha@5: import de.intevation.artifacts.ArtifactContextFactory; sascha@5: sascha@5: /** sascha@5: * Bootstrap facility for the global context and the artifact factories. sascha@5: * sascha@5: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@5: */ sascha@5: public class FactoryBootstrap sascha@5: { sascha@5: public static final String CONTEXT_FACTORY = sascha@5: "/artifact-database/factories/context-factory/text()"; sascha@5: sascha@5: public static final String DEFAULT_CONTEXT_FACTORY = sascha@5: "de.intevation.artifactdatabase.DefaultArtifactContextFactory"; sascha@5: sascha@5: public static final String ARTIFACT_FACTORIES = sascha@5: "/artifact-database/factories/artifact-factories/artifact-factory/text()"; sascha@5: sascha@5: protected Object context; sascha@5: sascha@5: protected ArtifactFactory [] artifactFactories; sascha@5: sascha@5: public FactoryBootstrap() { sascha@5: } sascha@5: sascha@5: void buildContext() { sascha@5: String className = Config.getStringXPath( sascha@5: CONTEXT_FACTORY, DEFAULT_CONTEXT_FACTORY); sascha@5: sascha@5: ArtifactContextFactory factory = null; sascha@5: sascha@5: try { sascha@5: Class clazz = Class.forName(className); sascha@5: factory = (ArtifactContextFactory)clazz.newInstance(); sascha@5: } sascha@5: catch (ClassNotFoundException cnfe) { sascha@5: cnfe.printStackTrace(System.err); sascha@5: } sascha@5: catch (InstantiationException ie) { sascha@5: ie.printStackTrace(System.err); sascha@5: } sascha@5: catch (ClassCastException cce) { sascha@5: cce.printStackTrace(System.err); sascha@5: } sascha@5: catch (IllegalAccessException iae) { sascha@5: iae.printStackTrace(System.err); sascha@5: } sascha@5: sascha@5: if (factory == null) { sascha@5: factory = new DefaultArtifactContextFactory(); sascha@5: } sascha@5: sascha@5: context = factory.createArtifactContext(Config.getConfig()); sascha@5: } sascha@5: sascha@5: protected void loadArtifactFactories() { sascha@5: ArrayList loadedFactories = new ArrayList(); sascha@5: sascha@5: NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES); sascha@5: sascha@5: Document config = Config.getConfig(); sascha@5: sascha@5: for (int i = 0, N = nodes.getLength(); i < N; ++i) { sascha@5: String className = nodes.item(i).getTextContent(); sascha@5: sascha@5: ArtifactFactory factory = null; sascha@5: sascha@5: try { sascha@5: Class clazz = Class.forName(className); sascha@5: factory = (ArtifactFactory)clazz.newInstance(); sascha@5: } sascha@5: catch (ClassNotFoundException cnfe) { sascha@5: cnfe.printStackTrace(System.err); sascha@5: } sascha@5: catch (InstantiationException ie) { sascha@5: ie.printStackTrace(System.err); sascha@5: } sascha@5: catch (ClassCastException cce) { sascha@5: cce.printStackTrace(System.err); sascha@5: } sascha@5: catch (IllegalAccessException iae) { sascha@5: iae.printStackTrace(System.err); sascha@5: } sascha@5: sascha@5: if (factory != null) { tim@7: factory.setup(config,nodes.item(i)); sascha@5: loadedFactories.add(factory); sascha@5: } sascha@5: } sascha@5: sascha@5: artifactFactories = (ArtifactFactory [])loadedFactories.toArray( sascha@5: new ArtifactFactory[loadedFactories.size()]); sascha@5: } sascha@5: sascha@5: public void boot() { sascha@5: buildContext(); sascha@5: loadArtifactFactories(); sascha@5: } sascha@5: sascha@5: public ArtifactFactory [] getArtifactFactories() { sascha@5: return artifactFactories; sascha@5: } sascha@5: sascha@5: public Object getContext() { sascha@5: return context; sascha@5: } sascha@5: } sascha@5: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: