KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.ErrorHandler;
25 import org.apache.hivemind.SymbolExpander;
26 import org.apache.hivemind.SymbolSource;
27 import org.apache.hivemind.TranslatorManager;
28 import org.apache.hivemind.definition.ConfigurationPointDefinition;
29 import org.apache.hivemind.definition.Contribution;
30 import org.apache.hivemind.definition.ContributionContext;
31 import org.apache.hivemind.definition.ImplementationConstructionContext;
32 import org.apache.hivemind.definition.ImplementationConstructor;
33 import org.apache.hivemind.definition.ModuleDefinition;
34 import org.apache.hivemind.definition.RegistryDefinition;
35 import org.apache.hivemind.definition.RegistryDefinitionPostProcessor;
36 import org.apache.hivemind.definition.ServicePointDefinition;
37 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper;
38 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
39 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor;
40 import org.apache.hivemind.internal.ServiceModel;
41
42 /**
43  * Implementation of {@link RegistryProvider} that defines core services
44  * needed by the xml implementation. Theses services can not be defined in
45  * xml because that would cause recursive behaviour.
46  */

47 public class XmlCoreServicesProvider implements RegistryProvider
48 {
49     private static final Log LOG = LogFactory.getLog(XmlCoreServicesProvider.class);
50     private ModuleDefinitionHelper helper;
51
52     public void process(RegistryDefinition registryDefinition, ErrorHandler errorHandler)
53     {
54         if (LOG.isDebugEnabled()) {
55             LOG.debug("Adding xml core services to registry");
56         }
57         
58         // The "hivemind" module is available here only if the HivemoduleProvider
59
// has been executed before. This order is defined in the MANIFEST.MF file of the xml module
60
// The cast is safe since the module is defined in the core.
61
ModuleDefinitionImpl moduleDefinition = (ModuleDefinitionImpl) registryDefinition.getModule("hivemind");
62         if (moduleDefinition == null) {
63             throw new ApplicationRuntimeException("Module 'hivemind' not found.");
64         }
65         
66         helper = new ModuleDefinitionHelper(moduleDefinition);
67         
68         addTranslatorManager(moduleDefinition, errorHandler);
69         
70         addSymbolSourcesConfiguration(moduleDefinition);
71         
72         // SymbolSource implementation driven by the FactoryDefaults configuration point.
73
addSymbolSource(moduleDefinition, "FactoryDefaultsSymbolSource", "FactoryDefaults");
74         
75         // SymbolSource implementation driven by the ApplicationDefaults configuration point.
76
addSymbolSource(moduleDefinition, "ApplicationDefaultsSymbolSource", "ApplicationDefaults");
77         
78         addSymbolExpander(moduleDefinition, errorHandler);
79         
80         registryDefinition.addPostProcessor(new XmlPostProcessor());
81     }
82     
83     /**
84      * @see TranslatorManager
85      */

86     private void addTranslatorManager(ModuleDefinition md, final ErrorHandler errorHandler)
87     {
88         ServicePointDefinition spd = helper.addServicePoint("TranslationManager", TranslatorManager.class.getName());
89
90         // Define inline implementation constructor, that wires the Translators configuration
91
ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation())
92         {
93             public Object JavaDoc constructCoreServiceImplementation(ImplementationConstructionContext context)
94             {
95                 List JavaDoc translators = (List JavaDoc) context.getConfiguration("Translators");
96                 TranslatorManager result = new TranslatorManagerImpl(translators, errorHandler);
97                 return result;
98             }
99         };
100         helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE);
101     }
102     
103     /**
104      * Provides a list of sources (SymbolSource) for values of substitution symbols.
105      */

106     private void addSymbolSourcesConfiguration(ModuleDefinition md)
107     {
108         ConfigurationPointDefinition cpd = helper.addConfigurationPoint("SymbolSources", List JavaDoc.class.getName());
109
110         helper.addContributionDefinition(cpd, new Contribution()
111         {
112
113             public void contribute(ContributionContext context)
114             {
115                 List JavaDoc contribution = new ArrayList JavaDoc();
116                 
117                 // Add the default symbol sources FactoryDefaults and
118
// ApplicationDefaults
119
SymbolSource factoryDefaults = (SymbolSource) context.getService(
120                         "FactoryDefaultsSymbolSource", SymbolSource.class);
121                 SymbolSourceContribution factoryDefaultsContrib = new SymbolSourceContribution(factoryDefaults,
122                         "hivemind.FactoryDefaults", null, null);
123                 contribution.add(factoryDefaultsContrib);
124
125                 SymbolSource applicationDefaults = (SymbolSource) context.getService(
126                         "ApplicationDefaultsSymbolSource", SymbolSource.class);
127                 SymbolSourceContribution applicationDefaultsContrib = new SymbolSourceContribution(applicationDefaults,
128                         "hivemind.ApplicationDefaults", null, "hivemind.FactoryDefaults");
129                 contribution.add(applicationDefaultsContrib);
130                 
131                 context.mergeContribution(contribution);
132             }
133         });
134     }
135     
136     /**
137      * Adds a service that implements the symbol source interface and a
138      * corresponding configuration point that holds the symbol values.
139      */

140     private void addSymbolSource(ModuleDefinition md, final String JavaDoc servicePointId, final String JavaDoc configurationId)
141     {
142         ServicePointDefinition spd = helper.addServicePoint(servicePointId, SymbolSource.class.getName());
143
144         // Define inline implementation constructor, that wires the corresponding configuration
145
ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation())
146         {
147             public Object JavaDoc constructCoreServiceImplementation(ImplementationConstructionContext context)
148             {
149                 DefaultsSymbolSource result = new DefaultsSymbolSource();
150                 result.setDefaults(((Map JavaDoc) context.getConfiguration(configurationId)).values());
151                 result.initializeService();
152                 return result;
153             }
154         };
155         helper.addServiceImplementation(spd, constructor, ServiceModel.SINGLETON);
156         
157         // Configuration point for setting defaults for symbol values.
158

159         helper.addConfigurationPoint(configurationId, Map JavaDoc.class.getName());
160     }
161     
162     /**
163      * @see SymbolExpander
164      */

165     private void addSymbolExpander(ModuleDefinition md, final ErrorHandler errorHandler)
166     {
167         ServicePointDefinition spd = helper.addServicePoint("SymbolExpander", SymbolExpander.class.getName());
168
169         // Define inline implementation constructor, that wires ErrorHandler and SymbolSources
170
ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation())
171         {
172             public Object JavaDoc constructCoreServiceImplementation(ImplementationConstructionContext context)
173             {
174                 List JavaDoc symbolSources = (List JavaDoc) context.getConfiguration("SymbolSources");
175
176                 SymbolExpander result = new SymbolExpanderImpl(errorHandler, symbolSources);
177                 return result;
178             }
179         };
180         helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE);
181         
182     }
183
184 }
185
186 class XmlPostProcessor implements RegistryDefinitionPostProcessor
187 {
188     public void postprocess(RegistryDefinition registryDefinition, ErrorHandler errorHandler)
189     {
190         XmlExtensionResolver extensionResolver = new XmlExtensionResolver(registryDefinition, errorHandler);
191         extensionResolver.resolveSchemas();
192     }
193 }
194
Popular Tags