1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.nuiton.eugene.models.object.xml;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.nuiton.eugene.GeneratorUtil;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32
33
34
35
36
37
38
39 public class ExternalCacheExtension {
40
41 private static final Log log = LogFactory.getLog(ExternalCacheExtension.class);
42
43 public static final String OBJECTMODEL_EXTENSION = "externalClassifiers";
44
45 protected Map<String, ObjectModelClassifierImpl> cache;
46
47 public ExternalCacheExtension() {
48 cache = new HashMap<>();
49 }
50
51 @SuppressWarnings("unchecked")
52 public <C extends ObjectModelClassifierImpl> C getCache(
53 ObjectModelImplRef reference, Class<C> classifierClass)
54 throws RuntimeException {
55 ObjectModelClassifierImpl classifier = cache.get(reference.getName());
56 C result;
57 if (classifier != null &&
58 !classifierClass.isAssignableFrom(classifier.getClass())) {
59 throw new ClassCastException(
60 "Invalid cast for " + classifierClass.getName());
61 }
62 if (classifier == null) {
63 try {
64 result = classifierClass.newInstance();
65 addClassifierToCache(reference, result);
66 if (log.isDebugEnabled()) {
67 log.debug("Add '" + reference.getName() +
68 "' to external cache");
69 }
70 } catch (Exception eee) {
71
72 throw new RuntimeException(
73 "Unable to add new '" + classifierClass.getName() +
74 "' to cache for '" + reference.getName() + "'", eee);
75 }
76 } else {
77 if (log.isDebugEnabled()) {
78 log.debug("Get '" + reference.getName() +
79 "' from external cache");
80 }
81 result = (C) classifier;
82 }
83 return result;
84 }
85
86 protected void addClassifierToCache(ObjectModelImplRef reference,
87 ObjectModelClassifierImpl classifier) {
88 String fqn = reference.getName();
89 String packageName = GeneratorUtil.getParentPackageName(fqn);
90 String name = GeneratorUtil.getClassNameFromQualifiedName(fqn);
91 classifier.setName(name);
92 classifier.setPackage(packageName);
93 classifier.postInit();
94 classifier.setExtern(true);
95 cache.put(reference.getName(), classifier);
96 }
97
98
99 }