ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@69: package de.intevation.artifactdatabase; sascha@69: sascha@207: import de.intevation.artifacts.common.utils.Config; sascha@207: sascha@93: import de.intevation.artifacts.Service; sascha@93: import de.intevation.artifacts.ServiceFactory; sascha@93: sascha@93: import org.apache.log4j.Logger; sascha@93: sascha@69: import org.w3c.dom.Document; sascha@69: import org.w3c.dom.Node; sascha@69: sascha@69: /** sascha@89: * Trivial implementation of the ServiceFactory interface. sascha@89: * Name and an description are configured by the given Node given to sascha@89: * #setup(Document, Node) via the 'name' and 'description' attributes. sascha@89: * The name of the class that provides the concrete serice is configured sascha@89: * by the 'service' attribute. sascha@90: * sascha@77: * @author Sascha L. Teichmann sascha@69: */ sascha@69: public class DefaultServiceFactory sascha@69: implements ServiceFactory sascha@69: { sascha@69: private static Logger logger = sascha@69: Logger.getLogger(DefaultServiceFactory.class); sascha@69: sascha@89: /** sascha@89: * XPath to access the name of the service. sascha@89: */ sascha@69: public static final String XPATH_NAME = "@name"; sascha@89: /** sascha@89: * XPath to access the description of the service. sascha@89: */ sascha@69: public static final String XPATH_DESCRIPTION = "@description"; sascha@89: /** sascha@89: * XPath to access the class name of the service to be build by sascha@89: * this factory. sascha@89: */ sascha@69: public static final String XPATH_SERVICE = "@service"; sascha@69: sascha@89: /** sascha@89: * Default description if no description is given in configuration. sascha@89: */ sascha@69: public static final String DEFAULT_DESCRIPTION = sascha@69: "No description available"; sascha@69: sascha@89: /** sascha@89: * Loaded service class if no class name is given in the configuration. sascha@89: */ sascha@69: public static final String DEFAULT_SERVICE = sascha@69: "de.intevation.artifactdatabase.DefaultService"; sascha@69: sascha@89: /** sascha@89: * The name of the service factory. sascha@89: */ sascha@69: protected String name; sascha@69: sascha@89: /** sascha@89: * The description of the service factory. sascha@89: */ sascha@69: protected String description; sascha@69: sascha@89: /** sascha@89: * The loaded class used to build the concrete service. sascha@89: */ sascha@69: protected Class serviceClass; sascha@69: sascha@89: /** sascha@89: * Default constructor. sascha@89: */ sascha@69: public DefaultServiceFactory() { sascha@69: } sascha@69: sascha@69: public String getName() { sascha@69: return name; sascha@69: } sascha@69: sascha@69: public String getDescription() { sascha@69: return description; sascha@69: } sascha@69: sascha@69: public Service createService(Object globalContext) { sascha@69: try { sascha@69: Service service = (Service)serviceClass.newInstance(); sascha@69: sascha@69: service.setup(this, globalContext); sascha@69: sascha@69: return service; sascha@69: } sascha@69: catch (InstantiationException ie) { sascha@69: logger.error(ie.getLocalizedMessage(), ie); sascha@69: } sascha@69: catch (IllegalAccessException iae) { sascha@69: logger.error(iae.getLocalizedMessage(), iae); sascha@69: } sascha@69: catch (ClassCastException cce) { sascha@69: logger.error(cce.getLocalizedMessage(), cce); sascha@69: } sascha@69: sascha@69: return null; sascha@69: } sascha@69: sascha@69: public void setup(Document config, Node factoryNode) { sascha@69: sascha@69: description = Config.getStringXPath( sascha@69: factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION); sascha@69: sascha@69: name = Config.getStringXPath( sascha@69: factoryNode, XPATH_NAME, toString()); sascha@69: sascha@69: String service = Config.getStringXPath( sascha@69: factoryNode, XPATH_SERVICE, DEFAULT_SERVICE); sascha@69: sascha@69: try { sascha@69: serviceClass = Class.forName(service); sascha@69: } sascha@69: catch (ClassNotFoundException cnfe) { sascha@69: logger.error(cnfe.getLocalizedMessage(), cnfe); sascha@69: } sascha@69: sascha@69: if (serviceClass == null) { sascha@69: serviceClass = DefaultService.class; sascha@69: } sascha@69: } sascha@69: } sascha@69: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :