1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.dem; |
7 | |
|
8 | |
import java.util.HashMap; |
9 | |
import java.util.HashSet; |
10 | |
import java.util.Map; |
11 | |
import java.util.Set; |
12 | |
|
13 | |
import pt.digitalis.dif.dem.interfaces.IApplication; |
14 | |
import pt.digitalis.dif.dem.interfaces.IDEMRegistry; |
15 | |
import pt.digitalis.dif.dem.interfaces.IEntity; |
16 | |
import pt.digitalis.dif.dem.interfaces.IProvider; |
17 | |
import pt.digitalis.dif.dem.interfaces.IService; |
18 | |
import pt.digitalis.dif.dem.interfaces.IStage; |
19 | |
import pt.digitalis.dif.utils.logging.DIFLogger; |
20 | |
import pt.digitalis.utils.bytecode.holders.ClassHolder; |
21 | |
import pt.digitalis.utils.inspection.exception.ResourceNotFoundException; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class DEMRegistryImpl implements IDEMRegistry { |
38 | |
|
39 | |
|
40 | 0 | static private Map<String, IApplication> applications = new HashMap<String, IApplication>(); |
41 | |
|
42 | |
|
43 | |
static private String defaultProvider; |
44 | |
|
45 | |
|
46 | 0 | static private Set<String> overridenStages = new HashSet<String>(); |
47 | |
|
48 | |
|
49 | 0 | static private Map<String, IProvider> providers = new HashMap<String, IProvider>(); |
50 | |
|
51 | |
|
52 | 0 | static private DEMRegistryImpl registry = new DEMRegistryImpl(); |
53 | |
|
54 | |
|
55 | 0 | static private Map<String, IService> services = new HashMap<String, IService>(); |
56 | |
|
57 | |
|
58 | 0 | static private Map<String, IStage> stages = new HashMap<String, IStage>(); |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
static public IApplication addApplication(ClassHolder clazz) |
68 | |
{ |
69 | |
IApplication application; |
70 | |
|
71 | |
try |
72 | |
{ |
73 | 0 | application = (IApplication) clazz.getClassInstance(); |
74 | 0 | applications.put(application.getID(), application); |
75 | |
|
76 | 0 | return application; |
77 | |
|
78 | |
} |
79 | 0 | catch (ClassNotFoundException e) |
80 | |
{ |
81 | 0 | e.printStackTrace(); |
82 | |
|
83 | 0 | return null; |
84 | |
} |
85 | 0 | catch (InstantiationException e) |
86 | |
{ |
87 | 0 | e.printStackTrace(); |
88 | |
|
89 | 0 | return null; |
90 | |
} |
91 | 0 | catch (IllegalAccessException e) |
92 | |
{ |
93 | 0 | e.printStackTrace(); |
94 | |
|
95 | 0 | return null; |
96 | |
} |
97 | 0 | catch (ResourceNotFoundException e) |
98 | |
{ |
99 | 0 | e.printStackTrace(); |
100 | |
|
101 | 0 | return null; |
102 | |
} |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
static public IEntity addEntity(Entity entityType, ClassHolder clazz) |
115 | |
{ |
116 | |
|
117 | 0 | if (entityType.equals(Entity.PROVIDER)) |
118 | 0 | return addProvider(clazz); |
119 | 0 | if (entityType.equals(Entity.APPLICATION)) |
120 | 0 | return addApplication(clazz); |
121 | 0 | if (entityType.equals(Entity.SERVICE)) |
122 | 0 | return addService(clazz); |
123 | 0 | if (entityType.equals(Entity.STAGE)) |
124 | 0 | return addStage(clazz); |
125 | |
|
126 | 0 | return null; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
static public IProvider addProvider(ClassHolder clazz) |
139 | |
{ |
140 | |
IProvider provider; |
141 | |
|
142 | |
try |
143 | |
{ |
144 | 0 | provider = (IProvider) clazz.getClassInstance(); |
145 | 0 | providers.put(provider.getID(), provider); |
146 | |
|
147 | 0 | return provider; |
148 | |
|
149 | |
} |
150 | 0 | catch (ClassNotFoundException e) |
151 | |
{ |
152 | 0 | e.printStackTrace(); |
153 | |
|
154 | 0 | return null; |
155 | |
} |
156 | 0 | catch (InstantiationException e) |
157 | |
{ |
158 | 0 | e.printStackTrace(); |
159 | |
|
160 | 0 | return null; |
161 | |
} |
162 | 0 | catch (IllegalAccessException e) |
163 | |
{ |
164 | 0 | e.printStackTrace(); |
165 | |
|
166 | 0 | return null; |
167 | |
} |
168 | 0 | catch (ResourceNotFoundException e) |
169 | |
{ |
170 | 0 | e.printStackTrace(); |
171 | |
|
172 | 0 | return null; |
173 | |
} |
174 | |
|
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
static public IService addService(ClassHolder clazz) |
185 | |
{ |
186 | |
IService service; |
187 | |
|
188 | |
try |
189 | |
{ |
190 | 0 | service = (IService) clazz.getClassInstance(); |
191 | 0 | services.put(service.getID(), service); |
192 | |
|
193 | 0 | return service; |
194 | |
|
195 | |
} |
196 | 0 | catch (ClassNotFoundException e) |
197 | |
{ |
198 | 0 | e.printStackTrace(); |
199 | |
|
200 | 0 | return null; |
201 | |
} |
202 | 0 | catch (InstantiationException e) |
203 | |
{ |
204 | 0 | e.printStackTrace(); |
205 | |
|
206 | 0 | return null; |
207 | |
} |
208 | 0 | catch (IllegalAccessException e) |
209 | |
{ |
210 | 0 | e.printStackTrace(); |
211 | |
|
212 | 0 | return null; |
213 | |
} |
214 | 0 | catch (ResourceNotFoundException e) |
215 | |
{ |
216 | 0 | e.printStackTrace(); |
217 | |
|
218 | 0 | return null; |
219 | |
} |
220 | |
|
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
static public IStage addStage(ClassHolder clazz) |
231 | |
{ |
232 | |
IStage stage; |
233 | |
|
234 | |
try |
235 | |
{ |
236 | 0 | stage = (IStage) clazz.getClassInstance(); |
237 | |
|
238 | |
|
239 | |
|
240 | 0 | if (!overridenStages.contains(stage.getID())) |
241 | 0 | stages.put(stage.getID(), stage); |
242 | |
|
243 | |
|
244 | 0 | String overrideStageList = stage.getOverridesStageID(); |
245 | 0 | if (overrideStageList != null) |
246 | |
{ |
247 | 0 | String[] overrideStageArray = overrideStageList.split(","); |
248 | |
|
249 | 0 | for (String overrideStage: overrideStageArray) |
250 | |
{ |
251 | 0 | overrideStage = overrideStage.trim().toLowerCase(); |
252 | |
|
253 | 0 | DIFLogger.getLogger().debug( |
254 | 0 | "Stage \"" + stage.getName() + " (" + stage.getID() + ")\" has overriden stage \"" |
255 | 0 | + overrideStage + "\""); |
256 | |
|
257 | 0 | stages.put(overrideStage, stage); |
258 | 0 | overridenStages.add(overrideStage); |
259 | |
} |
260 | |
} |
261 | |
|
262 | 0 | return stage; |
263 | |
|
264 | |
} |
265 | 0 | catch (ClassNotFoundException e) |
266 | |
{ |
267 | 0 | e.printStackTrace(); |
268 | |
|
269 | 0 | return null; |
270 | |
} |
271 | 0 | catch (InstantiationException e) |
272 | |
{ |
273 | 0 | e.printStackTrace(); |
274 | |
|
275 | 0 | return null; |
276 | |
} |
277 | 0 | catch (IllegalAccessException e) |
278 | |
{ |
279 | 0 | e.printStackTrace(); |
280 | |
|
281 | 0 | return null; |
282 | |
} |
283 | 0 | catch (ResourceNotFoundException e) |
284 | |
{ |
285 | 0 | e.printStackTrace(); |
286 | |
|
287 | 0 | return null; |
288 | |
} |
289 | |
|
290 | |
} |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public static Map<String, IApplication> getApplications() |
298 | |
{ |
299 | 0 | return applications; |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
static public IProvider getDefaultProvider() |
308 | |
{ |
309 | 0 | return providers.get(getDefaultProviderID()); |
310 | |
} |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
static public String getDefaultProviderID() |
318 | |
{ |
319 | 0 | if (defaultProvider == null) |
320 | |
|
321 | 0 | defaultProvider = providers.keySet().iterator().next(); |
322 | |
|
323 | 0 | return defaultProvider; |
324 | |
} |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public static Map<String, IProvider> getProviders() |
332 | |
{ |
333 | 0 | return providers; |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
static public DEMRegistryImpl getRegistry() |
342 | |
{ |
343 | 0 | return registry; |
344 | |
} |
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
public static Map<String, IService> getServices() |
352 | |
{ |
353 | 0 | return services; |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
static public Map<String, IStage> getStages() |
364 | |
{ |
365 | 0 | return stages; |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
static public void setDefaultProvider(String providerID) |
375 | |
{ |
376 | 0 | defaultProvider = providerID; |
377 | 0 | } |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | 0 | private DEMRegistryImpl() |
383 | 0 | {} |
384 | |
|
385 | |
|
386 | |
public IApplication getApplication(String applicationID) |
387 | |
{ |
388 | 0 | if (applicationID == null) |
389 | 0 | return null; |
390 | |
else |
391 | 0 | return applications.get(applicationID.toLowerCase()); |
392 | |
} |
393 | |
|
394 | |
|
395 | |
public IProvider getProvider(String providerID) |
396 | |
{ |
397 | 0 | if (providerID == null) |
398 | 0 | return null; |
399 | |
else |
400 | 0 | return providers.get((providerID.toLowerCase())); |
401 | |
} |
402 | |
|
403 | |
|
404 | |
public IService getService(String serviceID) |
405 | |
{ |
406 | 0 | if (serviceID == null) |
407 | 0 | return null; |
408 | |
else |
409 | 0 | return services.get(serviceID.toLowerCase()); |
410 | |
} |
411 | |
|
412 | |
|
413 | |
public IStage getStage(String stageID) |
414 | |
{ |
415 | 0 | if (stageID == null) |
416 | 0 | return null; |
417 | |
else |
418 | 0 | return stages.get(stageID.toLowerCase()); |
419 | |
} |
420 | |
} |