KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hivemind.ApplicationRuntimeException;
25 import org.apache.hivemind.ErrorHandler;
26 import org.apache.hivemind.Location;
27 import org.apache.hivemind.definition.ConfigurationParserDefinition;
28 import org.apache.hivemind.definition.ConfigurationPointDefinition;
29 import org.apache.hivemind.definition.ModuleDefinition;
30 import org.apache.hivemind.definition.RegistryDefinition;
31 import org.apache.hivemind.definition.ServicePointDefinition;
32 import org.apache.hivemind.definition.UnresolvedExtension;
33 import org.apache.hivemind.definition.impl.ConfigurationParserDefinitionImpl;
34 import org.apache.hivemind.definition.impl.ConfigurationPointDefinitionImpl;
35 import org.apache.hivemind.schema.Schema;
36 import org.apache.hivemind.util.IdUtils;
37 import org.apache.hivemind.util.UniqueHashMap;
38 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParser;
39 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParserConstructor;
40 import org.apache.hivemind.xml.definition.impl.XmlModuleDefinitionImpl;
41 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl;
42
43 /**
44  * Resolves the {@link UnresolvedExtension unresolved extensions} in all
45  * xml modules of a {@link RegistryDefinition}.
46
47  * @author Achim Huegen
48  */

49 public class XmlExtensionResolver
50 {
51     private static final Log LOG = LogFactory.getLog(XmlExtensionResolver.class);
52
53     private ErrorHandler _errorHandler;
54     
55     private RegistryDefinition _definition;
56     
57     /**
58      * Global map of all schemas defined in any model. Key is the qualified schema id, value
59      * is an instance of {@link Schema}.
60      */

61     private Map JavaDoc _schemas = new HashMap JavaDoc();
62
63     public XmlExtensionResolver(RegistryDefinition definition, ErrorHandler errorHandler)
64     {
65         _errorHandler = errorHandler;
66         _definition = definition;
67         buildGlobalSchemaMap();
68     }
69
70     /**
71      * Resolves schema references via id from configuration points and services.
72      * All added module descriptors are processed.
73      */

74     public void resolveSchemas()
75     {
76         Collection JavaDoc modules = _definition.getModules();
77         
78         for (Iterator JavaDoc iterModules = modules.iterator(); iterModules.hasNext();)
79         {
80             ModuleDefinition module = (ModuleDefinition) iterModules.next();
81             if (module instanceof XmlModuleDefinitionImpl) {
82                 XmlModuleDefinitionImpl xmlModule = (XmlModuleDefinitionImpl) module;
83                 resolveServicePointSchemas(xmlModule);
84                 resolveSchemaAssignments(xmlModule);
85             }
86         }
87     }
88     
89     /**
90      * Builds a map that contains all schemas of all defined xml modules.
91      */

92     public void buildGlobalSchemaMap()
93     {
94         Collection JavaDoc modules = _definition.getModules();
95         for (Iterator JavaDoc iterModules = modules.iterator(); iterModules.hasNext();)
96         {
97             ModuleDefinition module = (ModuleDefinition) iterModules.next();
98             if (module instanceof XmlModuleDefinitionImpl) {
99                 XmlModuleDefinitionImpl xmlModule = (XmlModuleDefinitionImpl) module;
100                 Collection JavaDoc schemas = xmlModule.getSchemas();
101                 for (Iterator JavaDoc iterSchemas = schemas.iterator(); iterSchemas.hasNext();)
102                 {
103                     Schema schema = (Schema) iterSchemas.next();
104                     String JavaDoc schemaId = IdUtils.qualify(module.getId(), schema.getId());
105                     _schemas.put(schemaId, schema);
106                 }
107             }
108         }
109     }
110
111     private void resolveServicePointSchemas(ModuleDefinition sourceModule)
112     {
113         Collection JavaDoc points = sourceModule.getServicePoints();
114
115         for (Iterator JavaDoc iterSp = points.iterator(); iterSp.hasNext();)
116         {
117             ServicePointDefinition spd = (ServicePointDefinition) iterSp.next();
118             
119             // Check type. In case of the hivemind core module non xml service points exist
120
if (spd instanceof XmlServicePointDefinitionImpl) {
121                 XmlServicePointDefinitionImpl xspd = (XmlServicePointDefinitionImpl) spd;
122     
123                 if (xspd.getParametersSchemaId() != null) {
124                     ServicePointDefinition point = sourceModule.getServicePoint(xspd.getId());
125     
126                     XmlServicePointDefinitionImpl xmlServicePoint = (XmlServicePointDefinitionImpl) point;
127                     Schema schema = findSchema(sourceModule, xspd.getParametersSchemaId(), xspd.getLocation());
128                     if (schema == null) {
129                         // Error-Handling has been done in findSchema already
130
} else {
131                         xmlServicePoint.setParametersSchema(schema);
132                         xmlServicePoint.setParametersCount(xspd.getParametersCount());
133                     }
134                 }
135             }
136         }
137     }
138     
139     /**
140      * Adds schemas to configuration points for schemas that were assigned
141      * to the configuration point subsequently by use of the "schema-assignment" attribute.
142      * This is used if the original configuration point was defined in a non-xml module.
143      */

144     private void resolveSchemaAssignments(XmlModuleDefinitionImpl sourceModule)
145     {
146         Collection JavaDoc schemaAssignments = sourceModule.getSchemaAssignments();
147
148         for (Iterator JavaDoc iterSa = schemaAssignments.iterator(); iterSa.hasNext();)
149         {
150             SchemaAssignment schemaAssignment = (SchemaAssignment) iterSa.next();
151
152             String JavaDoc configurationPointId = IdUtils.qualify(sourceModule.getId(), schemaAssignment.getConfigurationId());
153             ConfigurationPointDefinition cpd = _definition.getConfigurationPoint(configurationPointId);
154             if (cpd == null) {
155                 throw new ApplicationRuntimeException(XmlImplMessages.unknownConfigurationPointOfSchemaAssignment(configurationPointId, schemaAssignment));
156             }
157             
158             String JavaDoc schemaId = IdUtils.qualify(sourceModule.getId(), schemaAssignment.getSchemaId());
159             Schema schema = getSchema(schemaId, sourceModule.getId(), schemaAssignment.getLocation());
160             
161             if (schema != null) {
162                 // TODO: check if schema has already been set
163
// TODO: more type related error handling, check root type of schema
164

165                 // Add parser constructor with direct reference to schema
166
ConfigurationParserDefinition parserDef = new ConfigurationParserDefinitionImpl(
167                         sourceModule, schemaAssignment.getLocation(), HiveMindSchemaParser.INPUT_FORMAT_NAME,
168                         new HiveMindSchemaParserConstructor(schema));
169                 
170                 cpd.addParser(parserDef);
171                 
172                 // For backward compatibility change the configuration to Map if the schema uses a map too
173
if (HashMap JavaDoc.class.getName().equals(schema.getRootElementClassName())
174                         || UniqueHashMap.class.getName().equals(schema.getRootElementClassName())) {
175                     // The schema assignments are mainly used for backward compatibility so we can
176
// expect to deal here with a configuration point from the core or xml module.
177
// The cast prevents us from putting the setter into the public api of the ConfigurationPointDefinition
178
if (cpd instanceof ConfigurationPointDefinitionImpl) {
179                         ((ConfigurationPointDefinitionImpl) cpd).setConfigurationTypeName(Map JavaDoc.class.getName());
180                     }
181                 }
182                 
183             }
184         }
185     }
186     
187     private Schema findSchema(ModuleDefinition module, String JavaDoc schemaId, Location location)
188     {
189         if (schemaId == null)
190             return null;
191
192         String JavaDoc moduleId = module.getId();
193         String JavaDoc qualifiedId = IdUtils.qualify(moduleId, schemaId);
194
195         return getSchema(qualifiedId, moduleId, location);
196     }
197     
198     /**
199      * @param qualifiedSchemaId
200      * @param referencingModule the id of the module referencing the schema
201      * @param location the location the schema is referenced from
202      * @return null if schema wasn't found or is not visible
203      */

204     private Schema getSchema(String JavaDoc qualifiedSchemaId, String JavaDoc referencingModule, Location location)
205     {
206         Schema schema = (Schema) _schemas.get(qualifiedSchemaId);
207
208         if (schema == null)
209             _errorHandler
210                     .error(LOG, XmlImplMessages.unableToResolveSchema(qualifiedSchemaId), location, null);
211         else if (!schema.visibleToModule(referencingModule))
212         {
213             _errorHandler.error(
214                     LOG,
215                     XmlImplMessages.schemaNotVisible(qualifiedSchemaId, referencingModule),
216                     location,
217                     null);
218             schema = null;
219         }
220
221         return schema;
222     }
223     
224 }
225
Popular Tags