1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.ontotext.wsmo4j.factory;
20
21 import org.wsmo.common.*;
22 import org.wsmo.locator.Locator;
23 import org.wsmo.common.exception.SynchronisationException;
24
25 import java.util.*;
26 import java.lang.reflect.Proxy;
27
28
29
30
31
32
33
34
35
36
37 final class EntityRegistry implements Locator {
38
39 private static WeakHashMap <Identifier, WeakHashMap> weakMap = new WeakHashMap <Identifier, WeakHashMap>();
40
41
42
43 private static EntityRegistry theOnlyInstance = new EntityRegistry();
44
45 private EntityRegistry() {
46
47 }
48
49 static EntityRegistry get() {
50 return theOnlyInstance;
51 }
52
53
54
55
56
57
58
59
60 static Entity isRegistered(Identifier uri, Class clazz) {
61 if (!clazz.isInterface()) {
62 throw new RuntimeException("supply an Interface as clazz parameter!");
63 }
64
65 WeakHashMap innerMap = null;
66 synchronized(weakMap) {
67 try {
68 innerMap = weakMap.get(uri);
69 }
70 catch (ClassCastException e) {
71 throw new RuntimeException("Invalid entry type. WeakHashMap expected!");
72 }
73
74 if (innerMap != null) {
75 Object entity = null;
76 Set keySet = innerMap.keySet();
77 for (Iterator i = keySet.iterator(); i.hasNext();) {
78 entity = i.next();
79
80
81 if (entity instanceof Proxy) {
82 Object invocHandler = Proxy.getInvocationHandler(entity);
83 if (invocHandler instanceof IDReference) {
84 IDReference ref = (IDReference) invocHandler;
85 Class[] supportedIfaces = ref.getSupportedInterfaces();
86 for (int j = 0; j < supportedIfaces.length; j++) {
87 if (supportedIfaces[j].isAssignableFrom(clazz)) {
88 return (Entity) entity;
89 }
90 }
91 }
92 }
93
94
95 if (clazz.isInstance(entity)) {
96 return (Entity) entity;
97 }
98 }
99 }
100 }
101
102 return null;
103 }
104
105 static void registerEntity(Entity entity) {
106 synchronized (weakMap) {
107 WeakHashMap innerMap = null;
108 try {
109 innerMap = weakMap.get(entity.getIdentifier());
110 }
111 catch (ClassCastException e) {
112 throw new RuntimeException("Invalid entry type. WeakHashMap expected!");
113 }
114 if (innerMap == null) {
115 innerMap = new WeakHashMap();
116 weakMap.put(entity.getIdentifier(), innerMap);
117 }
118
119
120
121
122 else if (entity instanceof TopEntity) {
123 Iterator iterator = innerMap.keySet().iterator();
124 if (iterator.hasNext()) {
125 StringBuffer errorMsg = getEntityType((Entity) innerMap.keySet().iterator().next());
126 throw new RuntimeException("IRI " + entity.getIdentifier().toString() +
127 " is already registered as [" + errorMsg + "]. " +
128 "Metamodeling is disallowed for TopEntity objects!");
129 }
130 }
131
132
133 innerMap.put(entity, null);
134 }
135 }
136
137
138 private static StringBuffer getEntityType(Entity entity) {
139 StringBuffer resultStr = new StringBuffer();
140 if (entity instanceof Proxy == false) {
141 resultStr.append(entity.getClass().getName());
142 return resultStr;
143 }
144 if (Proxy.getInvocationHandler(entity) instanceof IDReference) {
145 IDReference ref = (IDReference) Proxy.getInvocationHandler(entity);
146 for(int i = 0; i < ref.getSupportedInterfaces().length; i++) {
147 resultStr.append(ref.getSupportedInterfaces()[i].getName());
148 if (i+1 < ref.getSupportedInterfaces().length) {
149 resultStr.append(", ");
150 }
151 }
152 }
153 return resultStr;
154 }
155
156
157
158
159
160
161
162
163 public Entity lookup(Identifier id, Class clazz) throws SynchronisationException {
164 Entity temp = isRegistered(id, clazz);
165 if (temp != null) {
166 if (temp instanceof Proxy) {
167 Object ref = Proxy.getInvocationHandler(temp);
168 if (ref instanceof IDReference) {
169 if (((IDReference)ref).isResolved())
170 return temp;
171 }
172 }
173 else {
174 return temp;
175 }
176 }
177 return null;
178 }
179
180 public Set <Entity> lookup(Identifier id) throws SynchronisationException {
181 throw new UnsupportedOperationException("Use lookup(Identifier, Class) instead!");
182 }
183 }