KickJava   Java API By Example, From Geeks To Geeks.

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


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.TranslatorManager;
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  * Implementation of {@link TranslatorManager}
37  */

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

51     private Map JavaDoc _translatorClasses = new HashMap JavaDoc();
52
53     private Map JavaDoc _translatorsCache = new HashMap JavaDoc();
54
55     private List JavaDoc _contributedTranslators;
56     
57     private boolean _translatorsLoaded;
58     
59     public TranslatorManagerImpl(List JavaDoc translators)
60     {
61         this(translators, new DefaultErrorHandler());
62     }
63
64     public TranslatorManagerImpl(List JavaDoc translators, ErrorHandler errorHandler)
65     {
66         _contributedTranslators = translators;
67         _errorHandler = errorHandler;
68
69         // Seed the basic translators used to "bootstrap" the
70
// processing of the hivemind.Translators configuration point.
71

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

80         _translatorClasses.put("smart", SmartTranslator.class);
81
82     }
83
84     /**
85      * @see TranslatorManager#getTranslator(java.lang.String)
86      */

87     public synchronized Translator getTranslator(String JavaDoc constructor)
88     {
89         // The cache is preloaded with the hardcoded translators.
90

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

122         return createTranslator(translatorClass, initializer);
123     }
124
125     private Translator createTranslator(Class JavaDoc translatorClass, String JavaDoc initializer)
126     {
127         try
128         {
129
130             if (initializer == null)
131                 return (Translator) translatorClass.newInstance();
132
133             Constructor JavaDoc c = translatorClass.getConstructor(new Class JavaDoc[]
134             { String JavaDoc.class });
135
136             return (Translator) c.newInstance(new Object JavaDoc[]
137             { initializer });
138         }
139         catch (Exception JavaDoc ex)
140         {
141             throw new ApplicationRuntimeException(XmlImplMessages.translatorInstantiationFailure(
142                     translatorClass,
143                     ex), ex);
144         }
145     }
146
147     private Class JavaDoc findTranslatorClass(String JavaDoc translatorName)
148     {
149         Class JavaDoc result = (Class JavaDoc) _translatorClasses.get(translatorName);
150
151         if (result == null)
152             throw new ApplicationRuntimeException(XmlImplMessages.unknownTranslatorName(
153                     translatorName,
154                     TRANSLATORS_CONFIGURATION_ID));
155
156         return result;
157     }
158
159     /**
160      * Load the contributed translators. This is done as late as possible since the
161      * parsing of the xml contributes will trigger a recursive call. The configuration
162      * is lazy so that parsing is executed on first element access.
163      */

164     private void loadContributedTranslators()
165     {
166         // Prevent endless recursion!
167

168         _translatorsLoaded = true;
169
170         Map JavaDoc locations = new HashMap JavaDoc();
171         locations.put("class", null);
172
173         Iterator JavaDoc i = _contributedTranslators.iterator();
174         while (i.hasNext())
175         {
176             TranslatorContribution c = (TranslatorContribution) i.next();
177
178             String JavaDoc name = c.getName();
179             Location oldLocation = (Location) locations.get(name);
180
181             if (oldLocation != null)
182             {
183                 _errorHandler.error(LOG, XmlImplMessages.duplicateTranslatorName(name, oldLocation), c
184                         .getLocation(), null);
185
186                 continue;
187             }
188
189             locations.put(name, c.getLocation());
190
191             Translator t = c.getTranslator();
192
193             if (t != null)
194             {
195                 _translatorsCache.put(name, t);
196                 continue;
197             }
198
199             Class JavaDoc tClass = c.getTranslatorClass();
200
201             if (tClass == null)
202             {
203                 _errorHandler.error(
204                         LOG,
205                         XmlImplMessages.incompleteTranslator(c),
206                         c.getLocation(),
207                         null);
208                 continue;
209             }
210
211             _translatorClasses.put(name, tClass);
212         }
213
214     }
215
216 }
Popular Tags