|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TranslatorManager.java | 94.4% | 94.4% | 100% | 94.9% |
|
1 |
// Copyright 2004 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;
|
|
18 |
import java.util.HashMap;
|
|
19 |
import java.util.Iterator;
|
|
20 |
import java.util.List;
|
|
21 |
import java.util.Map;
|
|
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.Registry;
|
|
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.RegistryImpl}.
|
|
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 TRANSLATORS_CONFIGURATION_ID = "hivemind.Translators"; |
|
45 |
|
|
46 |
private ErrorHandler _errorHandler;
|
|
47 |
private Registry _registry;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Map of Class, keyed on translator name, used to instantiate
|
|
51 |
* new {@link org.apache.hivemind.schema.Translator}s. Loaded
|
|
52 |
* from the <code>hivemind.Translators</code> configuration point;
|
|
53 |
*/
|
|
54 |
private Map _translatorClasses = new HashMap(); |
|
55 |
private Map _translatorsCache = new HashMap(); |
|
56 |
private boolean _translatorsLoaded; |
|
57 |
|
|
58 | 85 |
public TranslatorManager(Registry registry, ErrorHandler errorHandler)
|
59 |
{ |
|
60 | 85 |
_registry = registry; |
61 | 85 |
_errorHandler = errorHandler; |
62 |
|
|
63 |
// Seed the basic translators used to "bootstrap" the
|
|
64 |
// processing of the hivemind.Translators configuration point.
|
|
65 |
|
|
66 | 85 |
_translatorsCache.put("class", new ClassTranslator()); |
67 | 85 |
_translatorsCache.put("service", new ServiceTranslator()); |
68 | 85 |
_translatorsCache.put("smart", new SmartTranslator()); |
69 | 85 |
_translatorsCache.put("instance", new InstanceTranslator()); |
70 |
|
|
71 |
// smart may take an initializer, so we need to put it into the classes as
|
|
72 |
// well.
|
|
73 |
|
|
74 | 85 |
_translatorClasses.put("smart", SmartTranslator.class); |
75 |
|
|
76 |
} |
|
77 |
|
|
78 | 3583 |
public synchronized Translator getTranslator(String constructor) |
79 |
{ |
|
80 |
// The cache is preloaded with the hardcoded translators.
|
|
81 |
|
|
82 | 3583 |
if (!_translatorsLoaded && !_translatorsCache.containsKey(constructor))
|
83 | 84 |
loadTranslators(); |
84 |
|
|
85 | 3583 |
Translator result = (Translator) _translatorsCache.get(constructor); |
86 |
|
|
87 | 3583 |
if (result == null) |
88 |
{ |
|
89 | 175 |
result = constructTranslator(constructor); |
90 | 174 |
_translatorsCache.put(constructor, result); |
91 |
} |
|
92 |
|
|
93 | 3582 |
return result;
|
94 |
} |
|
95 |
|
|
96 | 175 |
private Translator constructTranslator(String constructor)
|
97 |
{ |
|
98 | 175 |
String name = constructor; |
99 | 175 |
String initializer = null;
|
100 |
|
|
101 | 175 |
int commax = constructor.indexOf(',');
|
102 |
|
|
103 | 175 |
if (commax > 0)
|
104 |
{ |
|
105 | 84 |
name = constructor.substring(0, commax); |
106 | 84 |
initializer = constructor.substring(commax + 1); |
107 |
} |
|
108 |
|
|
109 | 175 |
Class translatorClass = findTranslatorClass(name); |
110 |
|
|
111 |
// TODO: check for null class, meaning that the translator is a service.
|
|
112 |
|
|
113 | 174 |
return createTranslator(translatorClass, initializer);
|
114 |
} |
|
115 |
|
|
116 | 174 |
private Translator createTranslator(Class translatorClass, String initializer)
|
117 |
{ |
|
118 | 174 |
try
|
119 |
{ |
|
120 |
|
|
121 | 174 |
if (initializer == null) |
122 | 90 |
return (Translator) translatorClass.newInstance();
|
123 |
|
|
124 | 84 |
Constructor c = translatorClass.getConstructor(new Class[] { String.class }); |
125 |
|
|
126 | 84 |
return (Translator) c.newInstance(new Object[] { initializer }); |
127 |
} |
|
128 |
catch (Exception ex)
|
|
129 |
{ |
|
130 | 0 |
throw new ApplicationRuntimeException( |
131 |
ImplMessages.translatorInstantiationFailure(translatorClass, ex), |
|
132 |
ex); |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 | 175 |
private Class findTranslatorClass(String translatorName)
|
137 |
{ |
|
138 | 175 |
Class result = (Class) _translatorClasses.get(translatorName); |
139 |
|
|
140 | 175 |
if (result == null) |
141 | 1 |
throw new ApplicationRuntimeException( |
142 |
ImplMessages.unknownTranslatorName(translatorName, TRANSLATORS_CONFIGURATION_ID)); |
|
143 |
|
|
144 | 174 |
return result;
|
145 |
} |
|
146 |
|
|
147 | 84 |
private void loadTranslators() |
148 |
{ |
|
149 |
// Prevent endless recursion!
|
|
150 |
|
|
151 | 84 |
_translatorsLoaded = true;
|
152 |
|
|
153 | 84 |
List contributions = _registry.getConfiguration(TRANSLATORS_CONFIGURATION_ID); |
154 |
|
|
155 | 84 |
Map locations = new HashMap();
|
156 | 84 |
locations.put("class", null); |
157 |
|
|
158 | 84 |
Iterator i = contributions.iterator(); |
159 | 84 |
while (i.hasNext())
|
160 |
{ |
|
161 | 914 |
TranslatorContribution c = (TranslatorContribution) i.next(); |
162 |
|
|
163 | 914 |
String name = c.getName(); |
164 | 914 |
Location oldLocation = (Location) locations.get(name); |
165 |
|
|
166 | 914 |
if (oldLocation != null) |
167 |
{ |
|
168 | 0 |
_errorHandler.error( |
169 |
LOG, |
|
170 |
ImplMessages.duplicateTranslatorName(name, oldLocation), |
|
171 |
c.getLocation(), |
|
172 |
null);
|
|
173 |
|
|
174 | 0 |
continue;
|
175 |
} |
|
176 |
|
|
177 | 914 |
locations.put(name, c.getLocation()); |
178 |
|
|
179 | 914 |
Translator t = c.getTranslator(); |
180 |
|
|
181 | 914 |
if (t != null) |
182 |
{ |
|
183 | 83 |
_translatorsCache.put(name, t); |
184 | 83 |
continue;
|
185 |
} |
|
186 |
|
|
187 | 831 |
Class tClass = c.getTranslatorClass(); |
188 |
|
|
189 | 831 |
if (tClass == null) |
190 |
{ |
|
191 | 1 |
_errorHandler.error( |
192 |
LOG, |
|
193 |
ImplMessages.incompleteTranslator(c), |
|
194 |
c.getLocation(), |
|
195 |
null);
|
|
196 | 1 |
continue;
|
197 |
} |
|
198 |
|
|
199 | 830 |
_translatorClasses.put(name, tClass); |
200 |
} |
|
201 |
|
|
202 |
} |
|
203 |
|
|
204 |
} |
|
205 |
|
|