|
|||||||||||||||||||
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, 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;
|
|
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.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 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 _translatorClasses = new HashMap(); |
|
56 |
|
|
57 |
private Map _translatorsCache = new HashMap(); |
|
58 |
|
|
59 |
private boolean _translatorsLoaded; |
|
60 |
|
|
61 | 128 |
public TranslatorManager(RegistryInfrastructure registry, ErrorHandler errorHandler)
|
62 |
{ |
|
63 | 128 |
_registry = registry; |
64 | 128 |
_errorHandler = errorHandler; |
65 |
|
|
66 |
// Seed the basic translators used to "bootstrap" the
|
|
67 |
// processing of the hivemind.Translators configuration point.
|
|
68 |
|
|
69 | 128 |
_translatorsCache.put("class", new ClassTranslator()); |
70 | 128 |
_translatorsCache.put("service", new ServiceTranslator()); |
71 | 128 |
_translatorsCache.put("smart", new SmartTranslator()); |
72 | 128 |
_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 | 128 |
_translatorClasses.put("smart", SmartTranslator.class); |
78 |
|
|
79 |
} |
|
80 |
|
|
81 | 6684 |
public synchronized Translator getTranslator(String constructor) |
82 |
{ |
|
83 |
// The cache is preloaded with the hardcoded translators.
|
|
84 |
|
|
85 | 6684 |
if (!_translatorsLoaded && !_translatorsCache.containsKey(constructor))
|
86 | 109 |
loadTranslators(); |
87 |
|
|
88 | 6684 |
Translator result = (Translator) _translatorsCache.get(constructor); |
89 |
|
|
90 | 6684 |
if (result == null) |
91 |
{ |
|
92 | 225 |
result = constructTranslator(constructor); |
93 | 224 |
_translatorsCache.put(constructor, result); |
94 |
} |
|
95 |
|
|
96 | 6683 |
return result;
|
97 |
} |
|
98 |
|
|
99 | 225 |
private Translator constructTranslator(String constructor)
|
100 |
{ |
|
101 | 225 |
String name = constructor; |
102 | 225 |
String initializer = null;
|
103 |
|
|
104 | 225 |
int commax = constructor.indexOf(',');
|
105 |
|
|
106 | 225 |
if (commax > 0)
|
107 |
{ |
|
108 | 109 |
name = constructor.substring(0, commax); |
109 | 109 |
initializer = constructor.substring(commax + 1); |
110 |
} |
|
111 |
|
|
112 | 225 |
Class translatorClass = findTranslatorClass(name); |
113 |
|
|
114 |
// TODO: check for null class, meaning that the translator is a service.
|
|
115 |
|
|
116 | 224 |
return createTranslator(translatorClass, initializer);
|
117 |
} |
|
118 |
|
|
119 | 224 |
private Translator createTranslator(Class translatorClass, String initializer)
|
120 |
{ |
|
121 | 224 |
try
|
122 |
{ |
|
123 |
|
|
124 | 224 |
if (initializer == null) |
125 | 115 |
return (Translator) translatorClass.newInstance();
|
126 |
|
|
127 | 109 |
Constructor c = translatorClass.getConstructor(new Class[]
|
128 |
{ String.class });
|
|
129 |
|
|
130 | 109 |
return (Translator) c.newInstance(new Object[] |
131 |
{ initializer }); |
|
132 |
} |
|
133 |
catch (Exception ex)
|
|
134 |
{ |
|
135 | 0 |
throw new ApplicationRuntimeException(ImplMessages.translatorInstantiationFailure( |
136 |
translatorClass, |
|
137 |
ex), ex); |
|
138 |
} |
|
139 |
} |
|
140 |
|
|
141 | 225 |
private Class findTranslatorClass(String translatorName)
|
142 |
{ |
|
143 | 225 |
Class result = (Class) _translatorClasses.get(translatorName); |
144 |
|
|
145 | 225 |
if (result == null) |
146 | 1 |
throw new ApplicationRuntimeException(ImplMessages.unknownTranslatorName( |
147 |
translatorName, |
|
148 |
TRANSLATORS_CONFIGURATION_ID)); |
|
149 |
|
|
150 | 224 |
return result;
|
151 |
} |
|
152 |
|
|
153 | 109 |
private void loadTranslators() |
154 |
{ |
|
155 |
// Prevent endless recursion!
|
|
156 |
|
|
157 | 109 |
_translatorsLoaded = true;
|
158 |
|
|
159 | 109 |
List contributions = _registry.getConfiguration(TRANSLATORS_CONFIGURATION_ID, null);
|
160 |
|
|
161 | 109 |
Map locations = new HashMap();
|
162 | 109 |
locations.put("class", null); |
163 |
|
|
164 | 109 |
Iterator i = contributions.iterator(); |
165 | 109 |
while (i.hasNext())
|
166 |
{ |
|
167 | 1189 |
TranslatorContribution c = (TranslatorContribution) i.next(); |
168 |
|
|
169 | 1189 |
String name = c.getName(); |
170 | 1189 |
Location oldLocation = (Location) locations.get(name); |
171 |
|
|
172 | 1189 |
if (oldLocation != null) |
173 |
{ |
|
174 | 0 |
_errorHandler.error(LOG, ImplMessages.duplicateTranslatorName(name, oldLocation), c |
175 |
.getLocation(), null);
|
|
176 |
|
|
177 | 0 |
continue;
|
178 |
} |
|
179 |
|
|
180 | 1189 |
locations.put(name, c.getLocation()); |
181 |
|
|
182 | 1189 |
Translator t = c.getTranslator(); |
183 |
|
|
184 | 1189 |
if (t != null) |
185 |
{ |
|
186 | 108 |
_translatorsCache.put(name, t); |
187 | 108 |
continue;
|
188 |
} |
|
189 |
|
|
190 | 1081 |
Class tClass = c.getTranslatorClass(); |
191 |
|
|
192 | 1081 |
if (tClass == null) |
193 |
{ |
|
194 | 1 |
_errorHandler.error( |
195 |
LOG, |
|
196 |
ImplMessages.incompleteTranslator(c), |
|
197 |
c.getLocation(), |
|
198 |
null);
|
|
199 | 1 |
continue;
|
200 |
} |
|
201 |
|
|
202 | 1080 |
_translatorClasses.put(name, tClass); |
203 |
} |
|
204 |
|
|
205 |
} |
|
206 |
|
|
207 |
} |
|