0
|
1 /*
|
|
2 * (c) Copyright IBM Corp 2001, 2005
|
|
3 */
|
|
4
|
|
5 package javax.wadls.extensions;
|
|
6
|
|
7 import java.util.*;
|
|
8
|
|
9 import javax.wadls.*;
|
|
10 import javax.xml.namespace.*;
|
|
11
|
|
12 /**
|
|
13 * This class is used to associate serializers, deserializers, and
|
|
14 * Java implementation types with extensibility elements.
|
|
15 *
|
|
16 * @author Matthew J. Duftler (duftler@us.ibm.com)
|
|
17 */
|
|
18 public class ExtensionRegistry implements java.io.Serializable
|
|
19 {
|
|
20 public static final long serialVersionUID = 1;
|
|
21
|
|
22 /**
|
|
23 * Creates the extension registry, and sets the defaultSerializer
|
|
24 * and defaultDeserializer properties to instances of an
|
|
25 * UnknownExtensionSerializer, and an UnknownExtensionDeserializer,
|
|
26 * respectively.
|
|
27 */
|
|
28 public ExtensionRegistry()
|
|
29 {
|
|
30 setDefaultSerializer(new UnknownExtensionSerializer());
|
|
31 setDefaultDeserializer(new UnknownExtensionDeserializer());
|
|
32 }
|
|
33
|
|
34 /*
|
|
35 This is a Map of Maps. The top-level Map is keyed by (Class)parentType,
|
|
36 and the inner Maps are keyed by (QName)elementType.
|
|
37 */
|
|
38 protected Map serializerReg = new Hashtable();
|
|
39 /*
|
|
40 This is a Map of Maps. The top-level Map is keyed by (Class)parentType,
|
|
41 and the inner Maps are keyed by (QName)elementType.
|
|
42 */
|
|
43 protected Map deserializerReg = new Hashtable();
|
|
44 /*
|
|
45 This is a Map of Maps. The top-level Map is keyed by (Class)parentType,
|
|
46 and the inner Maps are keyed by (QName)elementType.
|
|
47 */
|
|
48 protected Map extensionTypeReg = new Hashtable();
|
|
49 protected ExtensionSerializer defaultSer = null;
|
|
50 protected ExtensionDeserializer defaultDeser = null;
|
|
51 /*
|
|
52 This is a Map of Maps. The top-level Map is keyed by (Class)parentType,
|
|
53 and the inner Maps are keyed by (QName)attrName.
|
|
54 */
|
|
55 protected Map extensionAttributeTypeReg = new Hashtable();
|
|
56
|
|
57 /**
|
|
58 * Set the serializer to be used when none is found for an extensibility
|
|
59 * element. Set this to null to have an exception thrown when
|
|
60 * unexpected extensibility elements are encountered. Default value is
|
|
61 * an instance of UnknownExtensionSerializer.
|
|
62 *
|
|
63 * @see UnknownExtensionSerializer
|
|
64 */
|
|
65 public void setDefaultSerializer(ExtensionSerializer defaultSer)
|
|
66 {
|
|
67 this.defaultSer = defaultSer;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Get the serializer to be used when none is found for an extensibility
|
|
72 * element. Default value is an instance of UnknownExtensionSerializer.
|
|
73 *
|
|
74 * @see UnknownExtensionSerializer
|
|
75 */
|
|
76 public ExtensionSerializer getDefaultSerializer()
|
|
77 {
|
|
78 return defaultSer;
|
|
79 }
|
|
80
|
|
81 /**
|
|
82 * Set the deserializer to be used when none is found for an encountered
|
|
83 * element. Set this to null to have an exception thrown when
|
|
84 * unexpected extensibility elements are encountered. Default value is
|
|
85 * an instance of UnknownExtensionDeserializer.
|
|
86 *
|
|
87 * @see UnknownExtensionDeserializer
|
|
88 */
|
|
89 public void setDefaultDeserializer(ExtensionDeserializer defaultDeser)
|
|
90 {
|
|
91 this.defaultDeser = defaultDeser;
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Get the deserializer to be used when none is found for an encountered
|
|
96 * element. Default value is an instance of UnknownExtensionDeserializer.
|
|
97 *
|
|
98 * @see UnknownExtensionDeserializer
|
|
99 */
|
|
100 public ExtensionDeserializer getDefaultDeserializer()
|
|
101 {
|
|
102 return defaultDeser;
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * Declare that the specified serializer should be used to serialize
|
|
107 * all extensibility elements with a qname matching elementType, when
|
|
108 * encountered as children of the specified parentType.
|
|
109 *
|
|
110 * @param parentType a class object indicating where in the WSDL
|
|
111 * definition this extension was encountered. For
|
|
112 * example, javax.wsdls.Binding.class would be used to indicate
|
|
113 * this extensibility element was found in the list of
|
|
114 * extensibility elements belonging to a javax.wsdls.Binding.
|
|
115 * @param elementType the qname of the extensibility element
|
|
116 * @param es the extension serializer to use
|
|
117 *
|
|
118 * @see #querySerializer(Class, QName)
|
|
119 */
|
|
120 public void registerSerializer(Class parentType,
|
|
121 QName elementType,
|
|
122 ExtensionSerializer es)
|
|
123 {
|
|
124 Map innerSerializerReg = (Map)serializerReg.get(parentType);
|
|
125
|
|
126 if (innerSerializerReg == null)
|
|
127 {
|
|
128 innerSerializerReg = new Hashtable();
|
|
129
|
|
130 serializerReg.put(parentType, innerSerializerReg);
|
|
131 }
|
|
132
|
|
133 innerSerializerReg.put(elementType, es);
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Declare that the specified deserializer should be used to deserialize
|
|
138 * all extensibility elements with a qname matching elementType, when
|
|
139 * encountered as immediate children of the element represented by the
|
|
140 * specified parentType.
|
|
141 *
|
|
142 * @param parentType a class object indicating where in the WSDL
|
|
143 * document this extensibility element was encountered. For
|
|
144 * example, javax.wsdls.Binding.class would be used to indicate
|
|
145 * this element was encountered as an immediate child of
|
|
146 * a <wsdl:binding> element.
|
|
147 * @param elementType the qname of the extensibility element
|
|
148 * @param ed the extension deserializer to use
|
|
149 *
|
|
150 * @see #queryDeserializer(Class, QName)
|
|
151 */
|
|
152 public void registerDeserializer(Class parentType,
|
|
153 QName elementType,
|
|
154 ExtensionDeserializer ed)
|
|
155 {
|
|
156 Map innerDeserializerReg = (Map)deserializerReg.get(parentType);
|
|
157
|
|
158 if (innerDeserializerReg == null)
|
|
159 {
|
|
160 innerDeserializerReg = new Hashtable();
|
|
161
|
|
162 deserializerReg.put(parentType, innerDeserializerReg);
|
|
163 }
|
|
164
|
|
165 innerDeserializerReg.put(elementType, ed);
|
|
166 }
|
|
167
|
|
168 /**
|
|
169 * Look up the serializer to use for the extensibility element with
|
|
170 * the qname elementType, which was encountered as a child of the
|
|
171 * specified parentType.
|
|
172 *
|
|
173 * @param parentType a class object indicating where in the WSDL
|
|
174 * definition this extension was encountered. For
|
|
175 * example, javax.wsdls.Binding.class would be used to indicate
|
|
176 * this extensibility element was found in the list of
|
|
177 * extensibility elements belonging to a javax.wsdls.Binding.
|
|
178 * @param elementType the qname of the extensibility element
|
|
179 *
|
|
180 * @return the extension serializer, if one was found. If none was
|
|
181 * found, the behavior depends on the value of the defaultSerializer
|
|
182 * property. If the defaultSerializer property is set to a non-null
|
|
183 * value, that value is returned; otherwise, a WSDLSException is
|
|
184 * thrown.
|
|
185 *
|
|
186 * @see #registerSerializer(Class, QName, ExtensionSerializer)
|
|
187 * @see #setDefaultSerializer(ExtensionSerializer)
|
|
188 */
|
|
189 public ExtensionSerializer querySerializer(Class parentType,
|
|
190 QName elementType)
|
|
191 throws WADLSException
|
|
192 {
|
|
193 Map innerSerializerReg = (Map)serializerReg.get(parentType);
|
|
194 ExtensionSerializer es = null;
|
|
195
|
|
196 if (innerSerializerReg != null)
|
|
197 {
|
|
198 es = (ExtensionSerializer)innerSerializerReg.get(elementType);
|
|
199 }
|
|
200
|
|
201 if (es == null)
|
|
202 {
|
|
203 es = defaultSer;
|
|
204 }
|
|
205
|
|
206 if (es == null)
|
|
207 {
|
|
208 throw new WADLSException(WADLSException.CONFIGURATION_ERROR,
|
|
209 "No ExtensionSerializer found " +
|
|
210 "to serialize a '" + elementType +
|
|
211 "' element in the context of a '" +
|
|
212 parentType.getName() + "'.");
|
|
213 }
|
|
214
|
|
215 return es;
|
|
216 }
|
|
217
|
|
218 /**
|
|
219 * Look up the deserializer for the extensibility element with the
|
|
220 * qname elementType, which was encountered as an immediate child
|
|
221 * of the element represented by the specified parentType.
|
|
222 *
|
|
223 * @param parentType a class object indicating where in the WSDL
|
|
224 * document this extensibility element was encountered. For
|
|
225 * example, javax.wsdls.Binding.class would be used to indicate
|
|
226 * this element was encountered as an immediate child of
|
|
227 * a <wsdl:binding> element.
|
|
228 * @param elementType the qname of the extensibility element
|
|
229 *
|
|
230 * @return the extension deserializer, if one was found. If none was
|
|
231 * found, the behavior depends on the value of the defaultDeserializer
|
|
232 * property. If the defaultDeserializer property is set to a non-null
|
|
233 * value, that value is returned; otherwise, a WSDLSException is thrown.
|
|
234 *
|
|
235 * @see #registerDeserializer(Class, QName, ExtensionDeserializer)
|
|
236 * @see #setDefaultDeserializer(ExtensionDeserializer)
|
|
237 */
|
|
238 public ExtensionDeserializer queryDeserializer(Class parentType,
|
|
239 QName elementType)
|
|
240 throws WADLSException
|
|
241 {
|
|
242
|
|
243 System.out.println("%%%% Inside Query Deserializer");
|
|
244 Map innerDeserializerReg = (Map)deserializerReg.get(parentType);
|
|
245 System.out.println("%%%% inner DeserializerReg Size:"+innerDeserializerReg.size());
|
|
246 ExtensionDeserializer ed = null;
|
|
247
|
|
248 if (innerDeserializerReg != null)
|
|
249 {
|
|
250 System.out.println("%%%% Inner Deserializer not null");
|
|
251 ed = (ExtensionDeserializer)innerDeserializerReg.get(elementType);
|
|
252 System.out.println("%%%% Ed string "+ed.toString());
|
|
253 }
|
|
254
|
|
255 if (ed == null)
|
|
256 {
|
|
257 ed = defaultDeser;
|
|
258 System.out.println("%%%% Default Deser String:"+defaultDeser.toString());
|
|
259 }
|
|
260
|
|
261 if (ed == null)
|
|
262 {
|
|
263 throw new WADLSException(WADLSException.CONFIGURATION_ERROR,
|
|
264 "No ExtensionDeserializer found " +
|
|
265 "to deserialize a '" + elementType +
|
|
266 "' element in the context of a '" +
|
|
267 parentType.getName() + "'.");
|
|
268 }
|
|
269
|
|
270 return ed;
|
|
271 }
|
|
272
|
|
273 /**
|
|
274 * Returns a set of QNames representing the extensibility elements
|
|
275 * that are allowed as children of the specified parent type.
|
|
276 * Basically, this method returns the keys associated with the set
|
|
277 * of extension deserializers registered for this parent type.
|
|
278 * Returns null if no extension deserializers are registered for
|
|
279 * this parent type.
|
|
280 */
|
|
281 public Set getAllowableExtensions(Class parentType)
|
|
282 {
|
|
283 Map innerDeserializerReg = (Map)deserializerReg.get(parentType);
|
|
284
|
|
285 return (innerDeserializerReg != null)
|
|
286 ? innerDeserializerReg.keySet()
|
|
287 : null;
|
|
288 }
|
|
289
|
|
290 /**
|
|
291 * Declare that the specified extensionType is the concrete
|
|
292 * class which should be used to represent extensibility elements
|
|
293 * with qnames matching elementType, that are intended to exist as
|
|
294 * children of the specified parentType.
|
|
295 *
|
|
296 * @param parentType a class object indicating where in the WSDL
|
|
297 * definition this extension would exist. For example,
|
|
298 * javax.wsdls.Binding.class would be used to indicate
|
|
299 * this extensibility element would be added to the list of
|
|
300 * extensibility elements belonging to a javax.wsdls.Binding,
|
|
301 * after being instantiated.
|
|
302 * @param elementType the qname of the extensibility element
|
|
303 * @param extensionType the concrete class which should be instantiated
|
|
304 *
|
|
305 * @see #createExtension(Class, QName)
|
|
306 */
|
|
307 public void mapExtensionTypes(Class parentType,
|
|
308 QName elementType,
|
|
309 Class extensionType)
|
|
310 {
|
|
311 Map innerExtensionTypeReg = (Map)extensionTypeReg.get(parentType);
|
|
312
|
|
313 if (innerExtensionTypeReg == null)
|
|
314 {
|
|
315 innerExtensionTypeReg = new Hashtable();
|
|
316
|
|
317 extensionTypeReg.put(parentType, innerExtensionTypeReg);
|
|
318 }
|
|
319
|
|
320 innerExtensionTypeReg.put(elementType, extensionType);
|
|
321 }
|
|
322
|
|
323 /**
|
|
324 * Create an instance of the type which was declared to be used to
|
|
325 * represent extensibility elements with qnames matching elementType,
|
|
326 * when intended to exist as children of the specified parentType.
|
|
327 * This method allows a user to instantiate an extensibility element
|
|
328 * without having to know the implementing type.
|
|
329 *
|
|
330 * @param parentType a class object indicating where in the WSDL
|
|
331 * definition this extension will exist. For example,
|
|
332 * javax.wsdls.Binding.class would be used to indicate
|
|
333 * this extensibility element is going to be added to the list of
|
|
334 * extensibility elements belonging to a javax.wsdls.Binding,
|
|
335 * after being instantiated.
|
|
336 * @param elementType the qname of the extensibility element
|
|
337 *
|
|
338 * @return a new instance of the type used to represent the
|
|
339 * specified extension
|
|
340 *
|
|
341 * @see #mapExtensionTypes(Class, QName, Class)
|
|
342 */
|
|
343 public ExtensibilityElement createExtension(Class parentType,
|
|
344 QName elementType)
|
|
345 throws WADLSException
|
|
346 {
|
|
347 Map innerExtensionTypeReg = (Map)extensionTypeReg.get(parentType);
|
|
348 Class extensionType = null;
|
|
349
|
|
350 if (innerExtensionTypeReg != null)
|
|
351 {
|
|
352 extensionType = (Class)innerExtensionTypeReg.get(elementType);
|
|
353 }
|
|
354
|
|
355 if (extensionType == null)
|
|
356 {
|
|
357 throw new WADLSException(WADLSException.CONFIGURATION_ERROR,
|
|
358 "No Java extensionType found " +
|
|
359 "to represent a '" + elementType +
|
|
360 "' element in the context of a '" +
|
|
361 parentType.getName() + "'.");
|
|
362 }
|
|
363 else if (!(ExtensibilityElement.class.isAssignableFrom(extensionType)))
|
|
364 {
|
|
365 throw new WADLSException(WADLSException.CONFIGURATION_ERROR,
|
|
366 "The Java extensionType '" +
|
|
367 extensionType.getName() + "' does " +
|
|
368 "not implement the ExtensibilityElement " +
|
|
369 "interface.");
|
|
370 }
|
|
371
|
|
372 try
|
|
373 {
|
|
374 ExtensibilityElement ee = (ExtensibilityElement)extensionType.newInstance();
|
|
375
|
|
376 if (ee.getElementType() == null)
|
|
377 {
|
|
378 ee.setElementType(elementType);
|
|
379 }
|
|
380
|
|
381 return ee;
|
|
382 }
|
|
383 catch (Exception e)
|
|
384 {
|
|
385 /*
|
|
386 Catches:
|
|
387 InstantiationException
|
|
388 IllegalAccessException
|
|
389 */
|
|
390 throw new WADLSException(WADLSException.CONFIGURATION_ERROR,
|
|
391 "Problem instantiating Java " +
|
|
392 "extensionType '" + extensionType.getName() +
|
|
393 "'.",
|
|
394 e);
|
|
395 }
|
|
396 }
|
|
397
|
|
398 /**
|
|
399 * Declare that the type of the specified extension attribute, when it occurs
|
|
400 * as an attribute of the specified parent type, should be assumed to be
|
|
401 * attrType.
|
|
402 *
|
|
403 * @param parentType a class object indicating where in the WSDL
|
|
404 * document this extensibility attribute was encountered. For
|
|
405 * example, javax.wsdls.Binding.class would be used to indicate
|
|
406 * this attribute was defined on a <wsdl:binding> element.
|
|
407 * @param attrName the qname of the extensibility attribute
|
|
408 * @param attrType one of the constants defined on the AttributeExtensible
|
|
409 * class
|
|
410 *
|
|
411 * @see #queryExtensionAttributeType(Class, QName)
|
|
412 * @see AttributeExtensible
|
|
413 */
|
|
414 public void registerExtensionAttributeType(Class parentType,
|
|
415 QName attrName,
|
|
416 int attrType)
|
|
417 {
|
|
418 Map innerExtensionAttributeTypeReg =
|
|
419 (Map)extensionAttributeTypeReg.get(parentType);
|
|
420
|
|
421 if (innerExtensionAttributeTypeReg == null)
|
|
422 {
|
|
423 innerExtensionAttributeTypeReg = new Hashtable();
|
|
424
|
|
425 extensionAttributeTypeReg.put(parentType, innerExtensionAttributeTypeReg);
|
|
426 }
|
|
427
|
|
428 innerExtensionAttributeTypeReg.put(attrName, new Integer(attrType));
|
|
429 }
|
|
430
|
|
431 /**
|
|
432 * Look up the type of the extensibility attribute with the qname attrName,
|
|
433 * which was defined on an element represented by the specified parentType.
|
|
434 *
|
|
435 * @param parentType a class object indicating where in the WSDL
|
|
436 * document this extensibility attribute was encountered. For
|
|
437 * example, javax.wsdls.Binding.class would be used to indicate
|
|
438 * this attribute was defined on a <wsdl:binding> element.
|
|
439 * @param attrName the qname of the extensibility attribute
|
|
440 *
|
|
441 * @return one of the constants defined on the AttributeExtensible class
|
|
442 *
|
|
443 * @see #registerExtensionAttributeType(Class, QName, int)
|
|
444 * @see AttributeExtensible
|
|
445 */
|
|
446 public int queryExtensionAttributeType(Class parentType, QName attrName)
|
|
447 {
|
|
448 Map innerExtensionAttributeTypeReg =
|
|
449 (Map)extensionAttributeTypeReg.get(parentType);
|
|
450 Integer attrType = null;
|
|
451
|
|
452 if (innerExtensionAttributeTypeReg != null)
|
|
453 {
|
|
454 attrType = (Integer)innerExtensionAttributeTypeReg.get(attrName);
|
|
455 }
|
|
456
|
|
457 if (attrType != null)
|
|
458 {
|
|
459 return attrType.intValue();
|
|
460 }
|
|
461 else
|
|
462 {
|
|
463 return AttributeExtensible.NO_DECLARED_TYPE;
|
|
464 }
|
|
465 }
|
|
466 } |