KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > XmlModuleDescriptorProcessor


1 // Copyright 2007 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import java.util.Collection JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hivemind.ErrorHandler;
26 import org.apache.hivemind.Location;
27 import org.apache.hivemind.conditional.EvaluationContextImpl;
28 import org.apache.hivemind.conditional.Node;
29 import org.apache.hivemind.conditional.Parser;
30 import org.apache.hivemind.definition.ConfigurationParserDefinition;
31 import org.apache.hivemind.definition.ImplementationDefinition;
32 import org.apache.hivemind.definition.RegistryDefinition;
33 import org.apache.hivemind.definition.impl.ConfigurationParserDefinitionImpl;
34 import org.apache.hivemind.definition.impl.ConfigurationPointDefinitionImpl;
35 import org.apache.hivemind.definition.impl.ContributionDefinitionImpl;
36 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
37 import org.apache.hivemind.definition.impl.ImplementationDefinitionImpl;
38 import org.apache.hivemind.definition.impl.InterceptorDefinitionImpl;
39 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl;
40 import org.apache.hivemind.parse.ConfigurationPointDescriptor;
41 import org.apache.hivemind.parse.ContributionDescriptor;
42 import org.apache.hivemind.parse.DependencyDescriptor;
43 import org.apache.hivemind.parse.ImplementationDescriptor;
44 import org.apache.hivemind.parse.InstanceBuilder;
45 import org.apache.hivemind.parse.InterceptorDescriptor;
46 import org.apache.hivemind.parse.ModuleDescriptor;
47 import org.apache.hivemind.parse.SchemaAssignmentDescriptor;
48 import org.apache.hivemind.parse.ServicePointDescriptor;
49 import org.apache.hivemind.schema.impl.SchemaImpl;
50 import org.apache.hivemind.util.IdUtils;
51 import org.apache.hivemind.util.UniqueHashMap;
52 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParser;
53 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParserConstructor;
54 import org.apache.hivemind.xml.definition.impl.XmlContributionImpl;
55 import org.apache.hivemind.xml.definition.impl.XmlModuleDefinitionImpl;
56 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl;
57
58 /**
59  * Adds the modules held by instances of {@link org.apache.hivemind.parse.ModuleDescriptor}
60  * to a {@link org.apache.hivemind.definition.RegistryDefinition}.
61  * That is the xml specific module definition is converted to the structure
62  * defined in package {@link org.apache.hivemind.definition}.
63  *
64  * @author Achim Huegen
65  */

66 public class XmlModuleDescriptorProcessor
67 {
68     private static final Log _log = LogFactory.getLog(XmlModuleDescriptorProcessor.class);
69
70     private ErrorHandler _errorHandler;
71
72     private RegistryDefinition _registryDefinition;
73     
74     private Parser _conditionalExpressionParser;
75
76     /**
77      * Map of {@link ModuleDescriptor} keyed on module id.
78      */

79     private Map JavaDoc _moduleDescriptors = new HashMap JavaDoc();
80     
81     public XmlModuleDescriptorProcessor(RegistryDefinition registryDefinition, ErrorHandler errorHandler)
82     {
83         _registryDefinition = registryDefinition;
84         _errorHandler = errorHandler;
85     }
86
87     /**
88      * Adds all elements to the registry definition that are defined in a module descriptor.
89      */

90     public void processModuleDescriptor(ModuleDescriptor md)
91     {
92         String JavaDoc id = md.getModuleId();
93         
94         if (_log.isDebugEnabled())
95             _log.debug("Processing module " + id);
96
97         _moduleDescriptors.put(id, md);
98
99         XmlModuleDefinitionImpl module = new XmlModuleDefinitionImpl(id, md.getLocation(), md.getClassResolver(), md.getPackageName());
100         
101         addSchemas(module, md);
102         
103         addSchemaAssignments(module, md);
104
105         addServicePoints(module, md);
106
107         addConfigurationPoints(module, md);
108         
109         addImplementations(module, md);
110
111         addContributions(module, md);
112
113         addDependencies(module, md);
114         
115         _registryDefinition.addModule(module);
116         
117     }
118     
119     /**
120      * Adds all module dependencies to the module definition.
121      */

122     private void addDependencies(ModuleDefinitionImpl module, ModuleDescriptor md)
123     {
124         int count = size(md.getDependencies());
125         for (int i = 0; i < count; i++)
126         {
127             DependencyDescriptor dependency = (DependencyDescriptor) md.getDependencies().get(i);
128             // TODO: DependencyDefinition mit Location , dependency.getLocation()
129
module.addDependency(dependency.getModuleId());
130         }
131     }
132
133     /**
134      * Adds all schemas to the module definition.
135      */

136     private void addSchemas(XmlModuleDefinitionImpl module, ModuleDescriptor md)
137     {
138         for (Iterator JavaDoc schemas = md.getSchemas().iterator(); schemas.hasNext();)
139         {
140             SchemaImpl schema = (SchemaImpl) schemas.next();
141
142             module.addSchema(schema.getId(), schema);
143         }
144     }
145     
146     /**
147      * Adds all schema assignments to the module definition.
148      */

149     private void addSchemaAssignments(XmlModuleDefinitionImpl module, ModuleDescriptor md)
150     {
151         if (md.getSchemaAssignment() == null)
152             return;
153         
154         for (Iterator JavaDoc assignments = md.getSchemaAssignment().iterator(); assignments.hasNext();)
155         {
156             SchemaAssignmentDescriptor sad = (SchemaAssignmentDescriptor) assignments.next();
157
158             SchemaAssignment schemaAssignment = new SchemaAssignment(sad.getConfigurationId(), sad.getSchemaId(), sad.getLocation());
159             module.addSchemaAssignment(schemaAssignment);
160         }
161     }
162     
163     /**
164      * Adds all service points to the module definition.
165      */

166     private void addServicePoints(XmlModuleDefinitionImpl module, ModuleDescriptor md)
167     {
168         List JavaDoc services = md.getServicePoints();
169         int count = size(services);
170
171         for (int i = 0; i < count; i++)
172         {
173             ServicePointDescriptor sd = (ServicePointDescriptor) services.get(i);
174             XmlServicePointDefinitionImpl servicePoint = new XmlServicePointDefinitionImpl(module, sd.getId(), sd.getLocation(),
175                     sd.getVisibility(), sd.getInterfaceClassName());
176
177             // Store the schema if embedded,
178
// Schemas are for service factories only
179
if (sd.getParametersSchema() != null) {
180                 servicePoint.setParametersSchema(sd.getParametersSchema());
181                 servicePoint.setParametersCount(sd.getParametersCount());
182             } else if (sd.getParametersSchemaId() != null ) {
183                 // referenced schemas are resolved in post processing
184
servicePoint.setParametersSchemaId(sd.getParametersSchemaId());
185             }
186                 
187             
188             module.addServicePoint(servicePoint);
189                 
190             addInternalImplementations(module, servicePoint, sd);
191         }
192     }
193
194     /**
195      * Adds all service implementations to the module definition.
196      */

197     private void addImplementations(ModuleDefinitionImpl module, ModuleDescriptor md)
198     {
199         String JavaDoc moduleId = md.getModuleId();
200
201         List JavaDoc implementations = md.getImplementations();
202         int count = size(implementations);
203
204         for (int i = 0; i < count; i++)
205         {
206             ImplementationDescriptor impl = (ImplementationDescriptor) implementations.get(i);
207
208             if (!includeContribution(impl.getConditionalExpression(), module, impl
209                     .getLocation()))
210                 continue;
211
212             String JavaDoc pointId = impl.getServiceId();
213             String JavaDoc qualifiedId = IdUtils.qualify(moduleId, pointId);
214
215             addImplementationAndInterceptors(module, qualifiedId, impl);
216         }
217
218     }
219
220     /**
221      * Adds one implementation and its interceptors to a module definition.
222      */

223     private void addImplementationAndInterceptors(ModuleDefinitionImpl sourceModule, String JavaDoc qualifiedPointId, ImplementationDescriptor id)
224     {
225         InstanceBuilder builder = id.getInstanceBuilder();
226         List JavaDoc interceptors = id.getInterceptors();
227
228         if (builder != null) {
229             ImplementationDefinitionImpl implementation = new ImplementationDefinitionImpl(
230                     sourceModule, builder.getLocation(), builder.createConstructor(sourceModule.getId()),
231                     builder.getServiceModel(), false);
232             sourceModule.addImplementation(qualifiedPointId, implementation);
233         }
234         
235         int count = size(interceptors);
236         for (int i = 0; i < count; i++)
237         {
238             InterceptorDescriptor ind = (InterceptorDescriptor) interceptors.get(i);
239
240             addInterceptor(sourceModule, qualifiedPointId, ind);
241         }
242     }
243
244     /**
245      * Adds internal service contributions; the contributions provided inplace with the service
246      * definition.
247      */

248     private void addInternalImplementations(ModuleDefinitionImpl module, ServicePointDefinitionImpl point,
249             ServicePointDescriptor spd)
250     {
251         InstanceBuilder builder = spd.getInstanceBuilder();
252         List JavaDoc interceptors = spd.getInterceptors();
253         String JavaDoc pointId = point.getId();
254
255         if (builder == null && interceptors == null)
256             return;
257
258         if (builder != null) {
259             ImplementationDefinition implementation = new ImplementationDefinitionImpl(
260                     module, builder.getLocation(), builder.createConstructor(module.getId()),
261                     builder.getServiceModel(), false);
262             point.addImplementation(implementation);
263         }
264         if (interceptors == null)
265             return;
266
267         int count = size(interceptors);
268
269         for (int i = 0; i < count; i++)
270         {
271             InterceptorDescriptor id = (InterceptorDescriptor) interceptors.get(i);
272             String JavaDoc qualifiedId = IdUtils.qualify(module.getId(), pointId);
273             addInterceptor(module, qualifiedId, id);
274         }
275     }
276
277     /**
278      * Adds all configuratin points to a module definition.
279      */

280     private void addConfigurationPoints(XmlModuleDefinitionImpl module, ModuleDescriptor md)
281     {
282         List JavaDoc points = md.getConfigurationPoints();
283         int count = size(points);
284
285         for (int i = 0; i < count; i++)
286         {
287             ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(i);
288
289             ConfigurationPointDefinitionImpl configurationPoint = new ConfigurationPointDefinitionImpl(
290                     module, cpd.getId(), cpd.getLocation(), cpd.getVisibility(),
291                     cpd.getType(), cpd.getCount());
292             module.addConfigurationPoint(configurationPoint);
293             
294             // If schema is embedded we can add a parser now, otherwise it must
295
// be resolved and processed later
296
if (cpd.getContributionsSchema() != null) {
297                 // TODO: compare container class name and rootElementClassName
298
if (HashMap JavaDoc.class.getName().equals(cpd.getContributionsSchema().getRootElementClassName())
299                     || UniqueHashMap.class.getName().equals(cpd.getContributionsSchema().getRootElementClassName())) {
300                     
301                     configurationPoint.setConfigurationTypeName(Map JavaDoc.class.getName());
302                 }
303
304                 // Add parser constructor with direct reference to schema
305
ConfigurationParserDefinition parserDef = new ConfigurationParserDefinitionImpl(
306                         module, cpd.getContributionsSchema().getLocation(), HiveMindSchemaParser.INPUT_FORMAT_NAME,
307                         new HiveMindSchemaParserConstructor(cpd.getContributionsSchema()));
308                 
309                 configurationPoint.addParser(parserDef);
310             } else if (cpd.getContributionsSchemaId() != null) {
311                 // Add schema assignment and resolve in post processing
312
String JavaDoc qualifiedId = IdUtils.qualify(module.getId(), cpd.getId());
313                 SchemaAssignment schemaAssignment = new SchemaAssignment(qualifiedId,
314                         cpd.getContributionsSchemaId(), cpd.getLocation());
315                 module.addSchemaAssignment(schemaAssignment);
316             }
317         }
318     }
319
320     /**
321      * Adds all contributions to a module definition.
322      */

323     private void addContributions(ModuleDefinitionImpl module, ModuleDescriptor md)
324     {
325         String JavaDoc moduleId = md.getModuleId();
326
327         List JavaDoc contributions = md.getContributions();
328         int count = size(contributions);
329
330         for (int i = 0; i < count; i++)
331         {
332             ContributionDescriptor cd = (ContributionDescriptor) contributions.get(i);
333
334             if (!includeContribution(cd.getConditionalExpression(), module, cd.getLocation()))
335                 continue;
336
337             String JavaDoc pointId = cd.getConfigurationId();
338             String JavaDoc qualifiedId = IdUtils.qualify(moduleId, pointId);
339             
340             ContributionDefinitionImpl contribution = new ContributionDefinitionImpl(module, cd.getLocation(),
341                     new XmlContributionImpl(moduleId, cd.getElements()), false);
342             module.addContribution(qualifiedId, contribution);
343
344         }
345     }
346
347     /**
348      * Adds all interceptors to a module definition.
349      */

350     private void addInterceptor(ModuleDefinitionImpl module, String JavaDoc qualifiedPointId, InterceptorDescriptor id)
351     {
352         if (_log.isDebugEnabled())
353             _log.debug("Adding " + id + " to service extension point " + qualifiedPointId);
354         
355         InvokeFactoryInterceptorConstructor constructor = new InvokeFactoryInterceptorConstructor(id.getLocation());
356         constructor.setFactoryServiceId(id.getFactoryServiceId());
357         constructor.setParameters(id.getParameters());
358         constructor.setPrecedingInterceptorIds(id.getAfter());
359         constructor.setFollowingInterceptorIds(id.getBefore());
360         InterceptorDefinitionImpl interceptor = new InterceptorDefinitionImpl(
361                 module, id.getName(), id.getLocation(), constructor);
362         module.addInterceptor(qualifiedPointId, interceptor);
363     }
364
365     /**
366      * Filters a contribution based on an expression. Returns true if the expression is null, or
367      * evaluates to true. Returns false if the expression if non-null and evaluates to false, or an
368      * exception occurs evaluating the expression.
369      *
370      * @param expression
371      * to parse and evaluate
372      * @param location
373      * of the expression (used if an error is reported)
374      */

375
376     private boolean includeContribution(String JavaDoc expression, ModuleDefinitionImpl module, Location location)
377     {
378         if (expression == null)
379             return true;
380
381         if (_conditionalExpressionParser == null)
382             _conditionalExpressionParser = new Parser();
383
384         try
385         {
386             Node node = _conditionalExpressionParser.parse(expression);
387
388             return node.evaluate(new EvaluationContextImpl(module.getClassResolver()));
389         }
390         catch (RuntimeException JavaDoc ex)
391         {
392             _errorHandler.error(_log, ex.getMessage(), location, ex);
393
394             return false;
395         }
396     }
397     
398     private static int size(Collection JavaDoc c)
399     {
400         return c == null ? 0 : c.size();
401     }
402
403 }
404
Popular Tags