KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.hivemind.ErrorHandler;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.definition.ConfigurationParserDefinition;
24 import org.apache.hivemind.definition.ConfigurationPointDefinition;
25 import org.apache.hivemind.definition.ContributionDefinition;
26 import org.apache.hivemind.definition.DefinitionMessages;
27 import org.apache.hivemind.definition.ModuleDefinition;
28 import org.apache.hivemind.definition.RegistryDefinition;
29 import org.apache.hivemind.definition.ImplementationDefinition;
30 import org.apache.hivemind.definition.InterceptorDefinition;
31 import org.apache.hivemind.definition.ServicePointDefinition;
32 import org.apache.hivemind.definition.UnresolvedExtension;
33 import org.apache.hivemind.definition.Visibility;
34 import org.apache.hivemind.util.IdUtils;
35
36 /**
37  * Resolves the {@link UnresolvedExtension unresolved extensions} in all
38  * modules of a {@link RegistryDefinition} during
39  * the construction of a registry by {@link RegistryBuilder}.
40  * Every unresolved extension (e.g. contribution, interceptor) references
41  * an extension point by its fully qualified id. This class looks for these
42  * extension points in the registry definition and adds the formerly unresolved
43  * extension directly to the extension points.
44  * The error handling is delegated to an instance of {@link ErrorHandler}.
45  *
46  * @author Achim Huegen
47  */

48 public class ExtensionResolver
49 {
50     private static final Log LOG = LogFactory.getLog(ExtensionResolver.class);
51
52     private ErrorHandler _errorHandler;
53     
54     private RegistryDefinition _definition;
55
56     public ExtensionResolver(RegistryDefinition definition, ErrorHandler errorHandler)
57     {
58         _errorHandler = errorHandler;
59         _definition = definition;
60     }
61
62     /**
63      * Resolves all unresolved extensions in the registry definition passed in the constructor.
64      * During this process the object graph represented by the registry definition is changed,
65      * so that afterwards it doesn't contain unresolved extensions any longer if anything went ok.
66      * If errors occur it depends on the assigned {@link ErrorHandler} whether an exceptions
67      * is raised or errors are logged only. In the latter case all extensions that couldn't
68      * be resolved will remain in the module definitions.
69      */

70     public void resolveExtensions()
71     {
72         for (Iterator JavaDoc iterModules = _definition.getModules().iterator(); iterModules.hasNext();)
73         {
74             ModuleDefinition module = (ModuleDefinition) iterModules.next();
75         
76             resolveImplementations(module);
77             resolveInterceptors(module);
78             resolveContributions(module);
79             resolveConfigurationParsers(module);
80         }
81     }
82
83     private void resolveImplementations(ModuleDefinition module)
84     {
85         for (Iterator JavaDoc iter = module.getImplementations().iterator(); iter.hasNext();)
86         {
87             UnresolvedExtension unresolved = (UnresolvedExtension) iter.next();
88             String JavaDoc servicePointId = unresolved.getExtensionPointId();
89             if (LOG.isDebugEnabled()) {
90                 LOG.debug("Trying to resolve service point " + servicePointId + " referenced by" +
91                         " implementation" + logLocation(unresolved.getExtension().getLocation()));
92             }
93             ServicePointDefinition servicePoint = _definition.getServicePoint(servicePointId);
94             if (servicePoint == null)
95             {
96                 _errorHandler.error(
97                         LOG,
98                         DefinitionMessages.unknownServicePoint(
99                                 IdUtils.extractModule(servicePointId),
100                                 IdUtils.stripModule(servicePointId)),
101                         unresolved.getExtension().getLocation(),
102                         null);
103             } else {
104                 servicePoint.addImplementation((ImplementationDefinition) unresolved.getExtension());
105             }
106             iter.remove();
107         }
108     }
109     
110     private String JavaDoc logLocation(Location location)
111     {
112         if (location == null) {
113             return "";
114         } else {
115             return " at " + location.toString();
116         }
117     }
118     
119     private void resolveInterceptors(ModuleDefinition module)
120     {
121         for (Iterator JavaDoc iter = module.getInterceptors().iterator(); iter.hasNext();)
122         {
123             UnresolvedExtension unresolved = (UnresolvedExtension) iter.next();
124             String JavaDoc servicePointId = unresolved.getExtensionPointId();
125             if (LOG.isDebugEnabled()) {
126                 LOG.debug("Trying to resolve service point " + servicePointId + " referenced by" +
127                         " interceptor" + logLocation(unresolved.getExtension().getLocation()));
128             }
129             ServicePointDefinition servicePoint = _definition.getServicePoint(servicePointId);
130             if (servicePoint == null)
131             {
132                 _errorHandler.error(
133                         LOG,
134                         DefinitionMessages.unknownServicePoint(
135                                 IdUtils.extractModule(servicePointId),
136                                 IdUtils.stripModule(servicePointId)),
137                         unresolved.getExtension().getLocation(),
138                         null);
139             } else {
140                 servicePoint.addInterceptor((InterceptorDefinition) unresolved.getExtension());
141             }
142             iter.remove();
143         }
144     }
145     
146     private void resolveContributions(ModuleDefinition module)
147     {
148         for (Iterator JavaDoc iter = module.getContributions().iterator(); iter.hasNext();)
149         {
150             UnresolvedExtension unresolved = (UnresolvedExtension) iter.next();
151             String JavaDoc configurationPointId = unresolved.getExtensionPointId();
152             if (LOG.isDebugEnabled()) {
153                 LOG.debug("Trying to resolve configuration point " + configurationPointId + " referenced by" +
154                         " contribution " + logLocation(unresolved.getExtension().getLocation()));
155             }
156             ConfigurationPointDefinition configurationPoint = _definition.getConfigurationPoint(configurationPointId);
157             if (configurationPoint == null)
158             {
159                 _errorHandler.error(
160                         LOG,
161                         DefinitionMessages.unknownConfigurationPoint(
162                                 IdUtils.extractModule(configurationPointId),
163                                 IdUtils.stripModule(configurationPointId)),
164                         unresolved.getExtension().getLocation(),
165                         null);
166             } else {
167                 configurationPoint.addContribution((ContributionDefinition) unresolved.getExtension());
168             }
169             iter.remove();
170         }
171     }
172     
173     private void resolveConfigurationParsers(ModuleDefinition module)
174     {
175         for (Iterator JavaDoc iter = module.getConfigurationParsers().iterator(); iter.hasNext();)
176         {
177             UnresolvedExtension unresolved = (UnresolvedExtension) iter.next();
178             String JavaDoc configurationPointId = unresolved.getExtensionPointId();
179             if (LOG.isDebugEnabled()) {
180                 LOG.debug("Trying to resolve configuration point " + configurationPointId + " referenced by" +
181                         " ConfigurationParser " + logLocation(unresolved.getExtension().getLocation()));
182             }
183             ConfigurationPointDefinition configurationPoint = _definition.getConfigurationPoint(configurationPointId);
184             if (configurationPoint == null)
185             {
186                 _errorHandler.error(
187                         LOG,
188                         DefinitionMessages.unknownConfigurationPoint(
189                                 IdUtils.extractModule(configurationPointId),
190                                 IdUtils.stripModule(configurationPointId)),
191                         unresolved.getExtension().getLocation(),
192                         null);
193             } else {
194                 if (Visibility.PRIVATE.equals(configurationPoint.getVisibility())
195                    && !module.getId().equals(IdUtils.extractModule(configurationPointId))) {
196                     _errorHandler.error(
197                             LOG,
198                             DefinitionMessages.configurationPointNotVisible(
199                                     configurationPoint,
200                                     module),
201                             unresolved.getExtension().getLocation(),
202                             null);
203                     
204                 }
205                 
206                 configurationPoint.addParser((ConfigurationParserDefinition) unresolved.getExtension());
207             }
208             iter.remove();
209         }
210     }
211
212 }
213
Popular Tags