KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2004, 2005 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 import java.util.Locale JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.hivemind.ErrorHandler;
22 import org.apache.hivemind.ShutdownCoordinator;
23 import org.apache.hivemind.definition.ConfigurationPointDefinition;
24 import org.apache.hivemind.definition.ModuleDefinition;
25 import org.apache.hivemind.definition.RegistryDefinition;
26 import org.apache.hivemind.definition.ServicePointDefinition;
27 import org.apache.hivemind.internal.Module;
28 import org.apache.hivemind.internal.RegistryInfrastructure;
29
30 /**
31  * Fed a {@link org.apache.hivemind.definition.RegistryDefinition}s, this class will assemble
32  * them into a final {@link org.apache.hivemind.internal.RegistryInfrastructure} as well as perform
33  * some validations.
34  * <p>
35  * This class was extracted from {@link org.apache.hivemind.impl.RegistryBuilder}.
36  *
37  * @author Howard M. Lewis Ship
38  * @since 1.1
39  */

40 public class RegistryInfrastructureConstructor
41 {
42     private ErrorHandler _errorHandler;
43
44     private Log _log;
45
46     private RegistryInfrastructureImpl _infrastructure;
47
48     public RegistryInfrastructureConstructor(ErrorHandler errorHandler, Log log, Locale JavaDoc locale)
49     {
50         _errorHandler = errorHandler;
51         _log = log;
52         
53         _infrastructure = new RegistryInfrastructureImpl(_errorHandler, locale);
54     }
55
56     /**
57      * Shutdown coordinator shared by all objects.
58      */

59
60     private ShutdownCoordinator _shutdownCoordinator = new ShutdownCoordinatorImpl();
61
62     /**
63      * Constructs the registry infrastructure, based on a blueprint defined by a {@link RegistryDefinition}.
64      * Expects that all extension resolving has already occured.
65      */

66     public RegistryInfrastructure constructRegistryInfrastructure(RegistryDefinition definition)
67     {
68         addModules(definition);
69         
70         _infrastructure.setShutdownCoordinator(_shutdownCoordinator);
71
72         // The caller is responsible for invoking startup().
73

74         return _infrastructure;
75     }
76
77     private void addModules(RegistryDefinition definition)
78     {
79         // Add each module to the registry.
80

81         Iterator JavaDoc i = definition.getModules().iterator();
82         while (i.hasNext())
83         {
84             ModuleDefinition module = (ModuleDefinition) i.next();
85
86             if (_log.isDebugEnabled())
87                 _log.debug("Adding module " + module.getId() + " to registry");
88
89             addModule(module);
90         }
91     }
92
93     private void addModule(ModuleDefinition moduleDefinition)
94     {
95         String JavaDoc id = moduleDefinition.getId();
96
97         if (_log.isDebugEnabled())
98             _log.debug("Processing module " + id);
99
100         if (_infrastructure.getModule(id) != null)
101         {
102             Module existing = _infrastructure.getModule(id);
103
104             _errorHandler.error(_log, ImplMessages.duplicateModuleId(id, existing.getLocation(), moduleDefinition
105                     .getLocation()), null, null);
106
107             // Ignore the duplicate module descriptor.
108
return;
109         }
110
111         ModuleImpl module = new ModuleImpl();
112
113         module.setLocation(moduleDefinition.getLocation());
114         module.setModuleId(id);
115         module.setPackageName(moduleDefinition.getPackageName());
116         module.setClassResolver(moduleDefinition.getClassResolver());
117
118         addServicePoints(moduleDefinition, module);
119         
120         addConfigurationPoints(moduleDefinition, module);
121         
122         module.setRegistry(_infrastructure);
123         _infrastructure.addModule(module);
124
125     }
126
127     private void addServicePoints(ModuleDefinition md, Module module)
128     {
129         String JavaDoc moduleId = md.getId();
130
131         for (Iterator JavaDoc services = md.getServicePoints().iterator(); services.hasNext();)
132         {
133             ServicePointDefinition sd = (ServicePointDefinition) services.next();
134
135             String JavaDoc pointId = moduleId + "." + sd.getId();
136
137             if (_log.isDebugEnabled())
138                 _log.debug("Creating service point " + pointId);
139
140             // Choose which class to instantiate based on
141
// whether the service is create-on-first-reference
142
// or create-on-first-use (deferred).
143

144             ServicePointImpl point = new ServicePointImpl(module, sd);
145
146             point.setShutdownCoordinator(_shutdownCoordinator);
147
148             _infrastructure.addServicePoint(point);
149         }
150     }
151
152     private void addConfigurationPoints(ModuleDefinition md, Module module)
153     {
154         String JavaDoc moduleId = md.getId();
155         for (Iterator JavaDoc points = md.getConfigurationPoints().iterator(); points.hasNext();)
156         {
157             ConfigurationPointDefinition cpd = (ConfigurationPointDefinition) points.next();
158
159             String JavaDoc pointId = moduleId + "." + cpd.getId();
160
161             if (_log.isDebugEnabled())
162                 _log.debug("Creating configuration point " + pointId);
163
164             ConfigurationPointImpl point = new ConfigurationPointImpl(module, cpd);
165
166             point.setShutdownCoordinator(_shutdownCoordinator);
167
168             _infrastructure.addConfigurationPoint(point);
169
170         }
171     }
172
173 }
Popular Tags