comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/DefaultServiceFactory.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultServiceFactory.java@a8d62eb93cd4
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifactdatabase;
10
11 import de.intevation.artifacts.common.utils.Config;
12
13 import de.intevation.artifacts.Service;
14 import de.intevation.artifacts.GlobalContext;
15 import de.intevation.artifacts.ServiceFactory;
16
17 import org.apache.log4j.Logger;
18
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Node;
21
22 /**
23 * Trivial implementation of the ServiceFactory interface.
24 * Name and an description are configured by the given Node given to
25 * #setup(Document, Node) via the 'name' and 'description' attributes.
26 * The name of the class that provides the concrete serice is configured
27 * by the 'service' attribute.
28 *
29 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
30 */
31 public class DefaultServiceFactory
32 implements ServiceFactory
33 {
34 private static Logger logger =
35 Logger.getLogger(DefaultServiceFactory.class);
36
37 /**
38 * XPath to access the name of the service.
39 */
40 public static final String XPATH_NAME = "@name";
41 /**
42 * XPath to access the description of the service.
43 */
44 public static final String XPATH_DESCRIPTION = "@description";
45 /**
46 * XPath to access the class name of the service to be build by
47 * this factory.
48 */
49 public static final String XPATH_SERVICE = "@service";
50
51 /**
52 * Default description if no description is given in configuration.
53 */
54 public static final String DEFAULT_DESCRIPTION =
55 "No description available";
56
57 /**
58 * Loaded service class if no class name is given in the configuration.
59 */
60 public static final String DEFAULT_SERVICE =
61 "de.intevation.artifactdatabase.DefaultService";
62
63 /**
64 * The name of the service factory.
65 */
66 protected String name;
67
68 /**
69 * The description of the service factory.
70 */
71 protected String description;
72
73 /**
74 * The loaded class used to build the concrete service.
75 */
76 protected Class serviceClass;
77
78 /**
79 * Default constructor.
80 */
81 public DefaultServiceFactory() {
82 }
83
84 @Override
85 public String getName() {
86 return name;
87 }
88
89 @Override
90 public String getDescription() {
91 return description;
92 }
93
94 @Override
95 public Service createService(GlobalContext globalContext) {
96 try {
97 Service service = (Service)serviceClass.newInstance();
98
99 service.setup(this, globalContext);
100
101 return service;
102 }
103 catch (InstantiationException ie) {
104 logger.error(ie.getLocalizedMessage(), ie);
105 }
106 catch (IllegalAccessException iae) {
107 logger.error(iae.getLocalizedMessage(), iae);
108 }
109 catch (ClassCastException cce) {
110 logger.error(cce.getLocalizedMessage(), cce);
111 }
112
113 return null;
114 }
115
116 @Override
117 public void setup(Document config, Node factoryNode) {
118
119 description = Config.getStringXPath(
120 factoryNode, XPATH_DESCRIPTION, DEFAULT_DESCRIPTION);
121
122 name = Config.getStringXPath(
123 factoryNode, XPATH_NAME, toString());
124
125 String service = Config.getStringXPath(
126 factoryNode, XPATH_SERVICE, DEFAULT_SERVICE);
127
128 try {
129 serviceClass = Class.forName(service);
130 }
131 catch (ClassNotFoundException cnfe) {
132 logger.error(cnfe.getLocalizedMessage(), cnfe);
133 }
134
135 if (serviceClass == null) {
136 serviceClass = DefaultService.class;
137 }
138 }
139 }
140 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org