KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hivemind.ApplicationRuntimeException;
26 import org.apache.hivemind.ClassResolver;
27 import org.apache.hivemind.ErrorHandler;
28 import org.apache.hivemind.HiveMind;
29 import org.apache.hivemind.Resource;
30 import org.apache.hivemind.definition.RegistryDefinition;
31 import org.apache.hivemind.parse.ModuleDescriptor;
32 import org.apache.hivemind.parse.SubModuleDescriptor;
33 import org.apache.hivemind.parse.XmlResourceProcessor;
34 import org.apache.hivemind.util.ClasspathResource;
35 import org.apache.hivemind.util.URLResource;
36
37 /**
38  * Reads a hivemind xml module into a {@link RegistryDefinition}. Thus the defined
39  * services configurations and contributions are added to the registry.
40  * Contains convenience methods for reading xml modules from file system and classpath.
41  *
42  * The module files are parsed by {@link XmlResourceProcessor} and handed over
43  * to {@link XmlModuleDescriptorProcessor} for interpretation and addition
44  * to the registry definition.
45  *
46  * @author Achim Huegen
47  */

48 public class XmlModuleReader
49 {
50     private static final Log LOG = LogFactory.getLog(XmlModuleReader.class);
51
52     private RegistryDefinition _registryDefinition;
53     
54     /**
55      * Parser instance used by all parsing of module descriptors.
56      */

57     private XmlResourceProcessor _parser;
58     
59     private XmlModuleDescriptorProcessor _processor;
60     
61     private ErrorHandler _errorHandler;
62
63     private ClassResolver _classResolver;
64
65     public XmlModuleReader(RegistryDefinition registryDefinition)
66     {
67         this(registryDefinition, new DefaultClassResolver(), new DefaultErrorHandler());
68     }
69     
70     public XmlModuleReader(RegistryDefinition registryDefinition, ClassResolver classResolver,
71             ErrorHandler errorHandler)
72     {
73         _registryDefinition = registryDefinition;
74         _classResolver = classResolver;
75         _errorHandler = errorHandler;
76         _processor = new XmlModuleDescriptorProcessor(_registryDefinition, _errorHandler);
77         _parser = new XmlResourceProcessor(_classResolver, _errorHandler);
78     }
79     
80     /**
81      * Reads a module specified by a {@link Resource}
82      *
83      * @param moduleResource the resource specifying the location of the module
84      * @throws ApplicationRuntimeException if resource wasn't found
85      */

86    public void readModule(Resource moduleResource)
87     {
88         if (moduleResource.getResourceURL() == null)
89             throw new ApplicationRuntimeException(XmlImplMessages.unableToFindModuleResource(moduleResource));
90         
91         processResource(moduleResource);
92     }
93     
94     /**
95      * Reads the first module named <code>moduleResourceFileName</code> found in the classpath.
96      * Uses the instance of {@link ClassResolver} provided during construction or the {@link DefaultClassResolver}
97      * for searching the classpath.
98      *
99      * @param moduleFileName filename of module. For format see {@link ClassLoader#getResource(String)}
100      * @throws ApplicationRuntimeException if module wasn't found
101      */

102     public void readClassPathModule(String JavaDoc moduleFileName)
103     {
104         readModule(new ClasspathResource(_classResolver, moduleFileName));
105     }
106     
107     /**
108      * Reads all modules named <code>moduleResourceFileName</code> found in the classpath.
109      * Uses the instance of {@link ClassResolver} provided during construction or the {@link DefaultClassResolver}
110      * for searching the classpath.
111      *
112      * @param moduleFileName filename of modules. For format see {@link ClassLoader#getResource(String)}
113      * @throws ApplicationRuntimeException if no modules were found
114      */

115     public void readClassPathModules(String JavaDoc moduleFileName)
116     {
117         Enumeration JavaDoc foundResources = null;
118         ClassLoader JavaDoc loader = _classResolver.getClassLoader();
119
120         try
121         {
122             foundResources = loader.getResources(moduleFileName);
123         }
124         catch (IOException JavaDoc ex)
125         {
126             throw new ApplicationRuntimeException(XmlImplMessages.unableToFindModulesError(_classResolver, ex),
127                     ex);
128         }
129         
130         if (!foundResources.hasMoreElements())
131             throw new ApplicationRuntimeException(XmlImplMessages.unableToFindModuleResource(new ClasspathResource(_classResolver, moduleFileName)));
132
133         while (foundResources.hasMoreElements())
134         {
135             URL JavaDoc descriptorURL = (URL JavaDoc) foundResources.nextElement();
136
137             readModule(new URLResource(descriptorURL));
138         }
139     }
140    
141     private void processResource(Resource resource)
142     {
143         try
144         {
145             ModuleDescriptor md = _parser.processResource(resource);
146
147             _processor.processModuleDescriptor(md);
148             
149             // After parsing a module, parse any additional modules identified
150
// within the module (using the <sub-module> element) recursively.
151
processSubModules(md);
152         }
153         catch (RuntimeException JavaDoc ex)
154         {
155             _errorHandler.error(LOG, ex.getMessage(), HiveMind.getLocation(ex), ex);
156         }
157     }
158
159     private void processSubModules(ModuleDescriptor moduleDescriptor)
160     {
161         List JavaDoc subModules = moduleDescriptor.getSubModules();
162
163         if (subModules == null)
164             return;
165
166         for (Iterator JavaDoc i = subModules.iterator(); i.hasNext();)
167         {
168             SubModuleDescriptor smd = (SubModuleDescriptor) i.next();
169
170             Resource descriptorResource = smd.getDescriptor();
171
172             if (descriptorResource.getResourceURL() == null)
173             {
174                 _errorHandler.error(
175                         LOG,
176                         XmlImplMessages.subModuleDoesNotExist(descriptorResource),
177                         smd.getLocation(),
178                         null);
179                 continue;
180             }
181
182             processResource(smd.getDescriptor());
183         }
184     }
185     
186 }
187
Popular Tags