KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Constructor JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map 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.ErrorHandler;
27 import org.apache.hivemind.Location;
28 import org.apache.hivemind.internal.RegistryInfrastructure;
29 import org.apache.hivemind.schema.Translator;
30 import org.apache.hivemind.schema.rules.ClassTranslator;
31 import org.apache.hivemind.schema.rules.InstanceTranslator;
32 import org.apache.hivemind.schema.rules.ServiceTranslator;
33 import org.apache.hivemind.schema.rules.SmartTranslator;
34
35 /**
36  * Manages translators for {@link org.apache.hivemind.impl.RegistryInfrastructureImpl}.
37  *
38  * @author Howard Lewis Ship
39  */

40 public class TranslatorManager
41 {
42     static final Log LOG = LogFactory.getLog(TranslatorManager.class);
43
44     public static final String JavaDoc TRANSLATORS_CONFIGURATION_ID = "hivemind.Translators";
45
46     private ErrorHandler _errorHandler;
47
48     private RegistryInfrastructure _registry;
49
50     /**
51      * Map of Class, keyed on translator name, used to instantiate new
52      * {@link org.apache.hivemind.schema.Translator}s. Loaded from the
53      * <code>hivemind.Translators</code> configuration point;
54      */

55     private Map JavaDoc _translatorClasses = new HashMap JavaDoc();
56
57     private Map JavaDoc _translatorsCache = new HashMap JavaDoc();
58
59     private boolean _translatorsLoaded;
60
61     public TranslatorManager(RegistryInfrastructure registry, ErrorHandler errorHandler)
62     {
63         _registry = registry;
64         _errorHandler = errorHandler;
65
66         // Seed the basic translators used to "bootstrap" the
67
// processing of the hivemind.Translators configuration point.
68

69         _translatorsCache.put("class", new ClassTranslator());
70         _translatorsCache.put("service", new ServiceTranslator());
71         _translatorsCache.put("smart", new SmartTranslator());
72         _translatorsCache.put("instance", new InstanceTranslator());
73
74         // smart may take an initializer, so we need to put it into the classes as
75
// well.
76

77         _translatorClasses.put("smart", SmartTranslator.class);
78
79     }
80
81     public synchronized Translator getTranslator(String JavaDoc constructor)
82     {
83         // The cache is preloaded with the hardcoded translators.
84

85         if (!_translatorsLoaded && !_translatorsCache.containsKey(constructor))
86             loadTranslators();
87
88         Translator result = (Translator) _translatorsCache.get(constructor);
89
90         if (result == null)
91         {
92             result = constructTranslator(constructor);
93             _translatorsCache.put(constructor, result);
94         }
95
96         return result;
97     }
98
99     private Translator constructTranslator(String JavaDoc constructor)
100     {
101         String JavaDoc name = constructor;
102         String JavaDoc initializer = null;
103
104         int commax = constructor.indexOf(',');
105
106         if (commax > 0)
107         {
108             name = constructor.substring(0, commax);
109             initializer = constructor.substring(commax + 1);
110         }
111
112         Class JavaDoc translatorClass = findTranslatorClass(name);
113
114         // TODO: check for null class, meaning that the translator is a service.
115

116         return createTranslator(translatorClass, initializer);
117     }
118
119     private Translator createTranslator(Class JavaDoc translatorClass, String JavaDoc initializer)
120     {
121         try
122         {
123
124             if (initializer == null)
125                 return (Translator) translatorClass.newInstance();
126
127             Constructor JavaDoc c = translatorClass.getConstructor(new Class JavaDoc[]
128             { String JavaDoc.class });
129
130             return (Translator) c.newInstance(new Object JavaDoc[]
131             { initializer });
132         }
133         catch (Exception JavaDoc ex)
134         {
135             throw new ApplicationRuntimeException(ImplMessages.translatorInstantiationFailure(
136                     translatorClass,
137                     ex), ex);
138         }
139     }
140
141     private Class JavaDoc findTranslatorClass(String JavaDoc translatorName)
142     {
143         Class JavaDoc result = (Class JavaDoc) _translatorClasses.get(translatorName);
144
145         if (result == null)
146             throw new ApplicationRuntimeException(ImplMessages.unknownTranslatorName(
147                     translatorName,
148                     TRANSLATORS_CONFIGURATION_ID));
149
150         return result;
151     }
152
153     private void loadTranslators()
154     {
155         // Prevent endless recursion!
156

157         _translatorsLoaded = true;
158
159         List JavaDoc contributions = _registry.getConfiguration(TRANSLATORS_CONFIGURATION_ID, null);
160
161         Map JavaDoc locations = new HashMap JavaDoc();
162         locations.put("class", null);
163
164         Iterator JavaDoc i = contributions.iterator();
165         while (i.hasNext())
166         {
167             TranslatorContribution c = (TranslatorContribution) i.next();
168
169             String JavaDoc name = c.getName();
170             Location oldLocation = (Location) locations.get(name);
171
172             if (oldLocation != null)
173             {
174                 _errorHandler.error(LOG, ImplMessages.duplicateTranslatorName(name, oldLocation), c
175                         .getLocation(), null);
176
177                 continue;
178             }
179
180             locations.put(name, c.getLocation());
181
182             Translator t = c.getTranslator();
183
184             if (t != null)
185             {
186                 _translatorsCache.put(name, t);
187                 continue;
188             }
189
190             Class JavaDoc tClass = c.getTranslatorClass();
191
192             if (tClass == null)
193             {
194                 _errorHandler.error(
195                         LOG,
196                         ImplMessages.incompleteTranslator(c),
197                         c.getLocation(),
198                         null);
199                 continue;
200             }
201
202             _translatorClasses.put(name, tClass);
203         }
204
205     }
206
207 }
Popular Tags