comparison artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 71:c99da6837be0

Load service factories and services during bootstrap. artifacts/trunk@598 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Jan 2010 11:27:57 +0000
parents ce488c1d3fc4
children 48d1a9a082c2
comparison
equal deleted inserted replaced
70:ce488c1d3fc4 71:c99da6837be0
26 "de.intevation.artifactdatabase.DefaultArtifactContextFactory"; 26 "de.intevation.artifactdatabase.DefaultArtifactContextFactory";
27 27
28 public static final String ARTIFACT_FACTORIES = 28 public static final String ARTIFACT_FACTORIES =
29 "/artifact-database/factories/artifact-factories/artifact-factory"; 29 "/artifact-database/factories/artifact-factories/artifact-factory";
30 30
31 public static final String SERVICE_FACTORIES =
32 "/artifact-database/factories/service-factories/service-factory";
33
31 protected Object context; 34 protected Object context;
32 35
33 protected ArtifactFactory [] artifactFactories; 36 protected ArtifactFactory [] artifactFactories;
37
38 protected ServiceFactory [] serviceFactories;
34 39
35 public FactoryBootstrap() { 40 public FactoryBootstrap() {
36 } 41 }
37 42
38 void buildContext() { 43 void buildContext() {
67 72
68 context = factory.createArtifactContext(Config.getConfig()); 73 context = factory.createArtifactContext(Config.getConfig());
69 } 74 }
70 75
71 protected void loadArtifactFactories() { 76 protected void loadArtifactFactories() {
77
78 logger.info("loading artifact factories");
79
72 ArrayList loadedFactories = new ArrayList(); 80 ArrayList loadedFactories = new ArrayList();
73 81
74 NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES); 82 NodeList nodes = Config.getNodeSetXPath(ARTIFACT_FACTORIES);
75 83
76 if (nodes == null) { 84 if (nodes == null) {
78 } 86 }
79 87
80 Document config = Config.getConfig(); 88 Document config = Config.getConfig();
81 89
82 for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) { 90 for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) {
83 String className = nodes.item(i).getTextContent(); 91 String className = nodes.item(i).getTextContent().trim();
84 92
85 ArtifactFactory factory = null; 93 ArtifactFactory factory = null;
86 94
87 try { 95 try {
88 Class clazz = Class.forName(className); 96 Class clazz = Class.forName(className);
110 118
111 artifactFactories = (ArtifactFactory [])loadedFactories.toArray( 119 artifactFactories = (ArtifactFactory [])loadedFactories.toArray(
112 new ArtifactFactory[loadedFactories.size()]); 120 new ArtifactFactory[loadedFactories.size()]);
113 } 121 }
114 122
123 protected void loadServiceFactories() {
124
125 logger.info("loading service factories");
126
127 ArrayList loadedFactories = new ArrayList();
128
129 NodeList nodes = Config.getNodeSetXPath(SERVICE_FACTORIES);
130
131 if (nodes == null) {
132 logger.warn("No factories found");
133 }
134
135 Document config = Config.getConfig();
136
137 for (int i = 0, N = nodes != null ? nodes.getLength() : 0; i < N; ++i) {
138 String className = nodes.item(i).getTextContent().trim();
139
140 ServiceFactory factory = null;
141
142 try {
143 Class clazz = Class.forName(className);
144 factory = (ServiceFactory)clazz.newInstance();
145 }
146 catch (ClassNotFoundException cnfe) {
147 logger.error(cnfe.getLocalizedMessage(), cnfe);
148 }
149 catch (InstantiationException ie) {
150 logger.error(ie.getLocalizedMessage(), ie);
151 }
152 catch (ClassCastException cce) {
153 logger.error(cce.getLocalizedMessage(), cce);
154 }
155 catch (IllegalAccessException iae) {
156 logger.error(iae.getLocalizedMessage(), iae);
157 }
158
159 if (factory != null) {
160 factory.setup(config, nodes.item(i));
161 loadedFactories.add(factory);
162 logger.info("Registering '" + factory.getName() + "' as service factory.");
163 }
164 }
165
166 serviceFactories = (ServiceFactory [])loadedFactories.toArray(
167 new ServiceFactory[loadedFactories.size()]);
168 }
169
115 public void boot() { 170 public void boot() {
116 buildContext(); 171 buildContext();
117 loadArtifactFactories(); 172 loadArtifactFactories();
173 loadServiceFactories();
118 } 174 }
119 175
120 public ArtifactFactory [] getArtifactFactories() { 176 public ArtifactFactory [] getArtifactFactories() {
121 return artifactFactories; 177 return artifactFactories;
122 } 178 }
123 179
124 public ServiceFactory [] getServiceFactories() { 180 public ServiceFactory [] getServiceFactories() {
125 logger.warn("Service factories are not supported yet"); 181 return serviceFactories;
126 return new ServiceFactory[0];
127 } 182 }
128 183
129 public Object getContext() { 184 public Object getContext() {
130 return context; 185 return context;
131 } 186 }

http://dive4elements.wald.intevation.org