KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > ws > addressing > VersionTransformer


1 package org.objectweb.celtix.bus.ws.addressing;
2
3
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import javax.xml.bind.JAXBContext;
8 import javax.xml.bind.JAXBException;
9 import javax.xml.namespace.QName JavaDoc;
10
11 // importation convention: if the same class name is used for
12
// 2005/08 and 2004/08, then the former version is imported
13
// and the latter is fully qualified when used
14
import org.objectweb.celtix.ws.addressing.AttributedURIType;
15 import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
16 import org.objectweb.celtix.ws.addressing.ReferenceParametersType;
17 import org.objectweb.celtix.ws.addressing.RelatesToType;
18 import org.objectweb.celtix.ws.addressing.v200408.AttributedQName;
19 import org.objectweb.celtix.ws.addressing.v200408.AttributedURI;
20 import org.objectweb.celtix.ws.addressing.v200408.ObjectFactory;
21 import org.objectweb.celtix.ws.addressing.v200408.Relationship;
22 import org.objectweb.celtix.ws.addressing.v200408.ServiceNameType;
23 import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
24
25
26 /**
27  * This class is responsible for transforming between the native
28  * WS-Addressing schema version (i.e. 2005/08) and exposed
29  * version (currently may be 2005/08 or 2004/08).
30  * <p>
31  * The native version is that used throughout the stack, were the
32  * WS-A types are represented via the JAXB generated types for the
33  * 2005/08 schema.
34  * <p>
35  * The exposed version is that used when the WS-A types are
36  * externalized, i.e. are encoded in the headers of outgoing
37  * messages. For outgoing requests, the exposed version is
38  * determined from configuration. For outgoing responses, the
39  * exposed version is determined by the exposed version of
40  * the corresponding request.
41  * <p>
42  * The motivation for using different native and exposed types
43  * is usually to facilitate a WS-* standard based on an earlier
44  * version of WS-Adressing (for example WS-RM depends on the
45  * 2004/08 version).
46  */

47 public class VersionTransformer {
48
49     protected static final String JavaDoc NATIVE_VERSION = Names.WSA_NAMESPACE_NAME;
50         
51     /**
52      * Constructor.
53      */

54     public VersionTransformer() {
55     }
56     
57     /**
58      * @param namespace a namspace URI to consider
59      * @return true if th WS-Addressing version specified by the namespace
60      * URI is supported
61      */

62     public boolean isSupported(String JavaDoc namespace) {
63         return NATIVE_VERSION.equals(namespace)
64                || Names200408.WSA_NAMESPACE_NAME.equals(namespace);
65     }
66     
67     /**
68      * Convert from 2005/08 AttributedURI to 2004/08 AttributedURI.
69      *
70      * @param internal the 2005/08 AttributedURIType
71      * @return an equivalent 2004/08 AttributedURI
72      */

73     public static AttributedURI convert(AttributedURIType internal) {
74         AttributedURI exposed =
75             Names200408.WSA_OBJECT_FACTORY.createAttributedURI();
76         String JavaDoc exposedValue =
77             Names.WSA_ANONYMOUS_ADDRESS.equals(internal.getValue())
78             ? Names200408.WSA_ANONYMOUS_ADDRESS
79             : Names.WSA_NONE_ADDRESS.equals(internal.getValue())
80               ? Names200408.WSA_NONE_ADDRESS
81               : internal.getValue();
82         exposed.setValue(exposedValue);
83         putAll(exposed.getOtherAttributes(), internal.getOtherAttributes());
84         return exposed;
85     }
86
87     /**
88      * Convert from 2004/08 AttributedURI to 2005/08 AttributedURI.
89      *
90      * @param exposed the 2004/08 AttributedURI
91      * @return an equivalent 2005/08 AttributedURIType
92      */

93     public static AttributedURIType convert(AttributedURI exposed) {
94         AttributedURIType internal =
95             ContextUtils.WSA_OBJECT_FACTORY.createAttributedURIType();
96         String JavaDoc internalValue =
97             Names200408.WSA_ANONYMOUS_ADDRESS.equals(exposed.getValue())
98             ? Names.WSA_ANONYMOUS_ADDRESS
99             : Names200408.WSA_NONE_ADDRESS.equals(exposed.getValue())
100               ? Names.WSA_NONE_ADDRESS
101               : exposed.getValue();
102         internal.setValue(internalValue);
103         putAll(internal.getOtherAttributes(), exposed.getOtherAttributes());
104         return internal;
105     }
106     
107     /**
108      * Convert from 2005/08 EndpointReferenceType to 2004/08
109      * EndpointReferenceType.
110      *
111      * @param internal the 2005/08 EndpointReferenceType
112      * @return an equivalent 2004/08 EndpointReferenceType
113      */

114     public static org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType convert(
115             EndpointReferenceType internal) {
116         org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType exposed =
117             Names200408.WSA_OBJECT_FACTORY.createEndpointReferenceType();
118         exposed.setAddress(convert(internal.getAddress()));
119         exposed.setReferenceParameters(
120                             convert(internal.getReferenceParameters()));
121         QName JavaDoc serviceQName = EndpointReferenceUtils.getServiceName(internal);
122         if (serviceQName != null) {
123             ServiceNameType serviceName =
124                 Names200408.WSA_OBJECT_FACTORY.createServiceNameType();
125             serviceName.setValue(serviceQName);
126             exposed.setServiceName(serviceName);
127         }
128         String JavaDoc portLocalName = EndpointReferenceUtils.getPortName(internal);
129         if (portLocalName != null) {
130             String JavaDoc namespace = serviceQName.getNamespaceURI() != null
131                                ? serviceQName.getNamespaceURI()
132                                : Names.WSDL_INSTANCE_NAMESPACE_NAME;
133             QName JavaDoc portQName =
134                 new QName JavaDoc(namespace, portLocalName);
135             AttributedQName portType =
136                 Names200408.WSA_OBJECT_FACTORY.createAttributedQName();
137             portType.setValue(portQName);
138             exposed.setPortType(portType);
139         }
140         // no direct analogue for Metadata
141
addAll(exposed.getAny(), internal.getAny());
142         putAll(exposed.getOtherAttributes(), internal.getOtherAttributes());
143         return exposed;
144     }
145     
146     /**
147      * Convert from 2004/08 EndpointReferenceType to 2005/08
148      * EndpointReferenceType.
149      *
150      * @param exposed the 2004/08 EndpointReferenceType
151      * @return an equivalent 2005/08 EndpointReferenceType
152      */

153     public static EndpointReferenceType convert(
154             org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType exposed) {
155         EndpointReferenceType internal =
156             ContextUtils.WSA_OBJECT_FACTORY.createEndpointReferenceType();
157         internal.setAddress(convert(exposed.getAddress()));
158         internal.setReferenceParameters(
159                             convert(exposed.getReferenceParameters()));
160         ServiceNameType serviceName = exposed.getServiceName();
161         AttributedQName portName = exposed.getPortType();
162         if (serviceName != null && portName != null) {
163             EndpointReferenceUtils.setServiceAndPortName(internal,
164                                                   serviceName.getValue(),
165                                                   portName.getValue().getLocalPart());
166         }
167
168         // no direct analogue for ReferenceProperties
169
addAll(internal.getAny(), exposed.getAny());
170         putAll(internal.getOtherAttributes(), exposed.getOtherAttributes());
171         return internal;
172     }
173
174     /**
175      * Convert from 2005/08 ReferenceParametersType to 2004/08
176      * ReferenceParametersType.
177      *
178      * @param internal the 2005/08 ReferenceParametersType
179      * @return an equivalent 2004/08 ReferenceParametersType
180      */

181     public static org.objectweb.celtix.ws.addressing.v200408.ReferenceParametersType convert(
182             ReferenceParametersType internal) {
183         org.objectweb.celtix.ws.addressing.v200408.ReferenceParametersType exposed =
184             null;
185         if (internal != null) {
186             exposed =
187                 Names200408.WSA_OBJECT_FACTORY.createReferenceParametersType();
188             addAll(exposed.getAny(), internal.getAny());
189         }
190         return exposed;
191     }
192     
193     /**
194      * Convert from 2004/08 ReferenceParametersType to 2005/08
195      * ReferenceParametersType.
196      *
197      * @param exposed the 2004/08 ReferenceParametersType
198      * @return an equivalent 2005/08 ReferenceParametersType
199      */

200     public static ReferenceParametersType convert(
201             org.objectweb.celtix.ws.addressing.v200408.ReferenceParametersType exposed) {
202         ReferenceParametersType internal = null;
203         if (exposed != null) {
204             internal =
205                 ContextUtils.WSA_OBJECT_FACTORY.createReferenceParametersType();
206             addAll(internal.getAny(), exposed.getAny());
207         }
208         return internal;
209     }
210
211     /**
212      * Convert from 2005/08 RelatesToType to 2004/08 Relationship.
213      *
214      * @param internal the 2005/08 RelatesToType
215      * @return an equivalent 2004/08 Relationship
216      */

217     public static Relationship convert(RelatesToType internal) {
218         Relationship exposed = null;
219         if (internal != null) {
220             exposed = Names200408.WSA_OBJECT_FACTORY.createRelationship();
221             exposed.setValue(internal.getValue());
222             String JavaDoc internalRelationshipType = internal.getRelationshipType();
223             if (internalRelationshipType != null) {
224                 QName JavaDoc exposedRelationshipType =
225                     Names.WSA_RELATIONSHIP_REPLY.equals(
226                                                     internalRelationshipType)
227                     ? new QName JavaDoc(Names200408.WSA_NAMESPACE_NAME,
228                                 Names.WSA_REPLY_NAME)
229                     : new QName JavaDoc(internalRelationshipType);
230                 exposed.setRelationshipType(exposedRelationshipType);
231             }
232             putAll(exposed.getOtherAttributes(), internal.getOtherAttributes());
233         }
234         return exposed;
235     }
236
237     /**
238      * Convert from 2004/08 Relationship to 2005/08 RelatesToType.
239      *
240      * @param exposed the 2004/08 Relationship
241      * @return an equivalent 2005/08 RelatesToType
242      */

243     public static RelatesToType convert(Relationship exposed) {
244         RelatesToType internal = null;
245         if (exposed != null) {
246             internal = ContextUtils.WSA_OBJECT_FACTORY.createRelatesToType();
247             internal.setValue(exposed.getValue());
248             QName JavaDoc exposedRelationshipType = exposed.getRelationshipType();
249             if (exposedRelationshipType != null) {
250                 String JavaDoc internalRelationshipType =
251                     Names.WSA_REPLY_NAME.equals(
252                                       exposedRelationshipType.getLocalPart())
253                     ? Names.WSA_RELATIONSHIP_REPLY
254                     : exposedRelationshipType.toString();
255                 internal.setRelationshipType(internalRelationshipType);
256             }
257             internal.getOtherAttributes().putAll(exposed.getOtherAttributes());
258         }
259         return internal;
260     }
261     
262     /**
263      * @param exposedURI specifies the version WS-Addressing
264      * @return JABXContext for the exposed namespace URI
265      */

266     public static JAXBContext getExposedJAXBContext(String JavaDoc exposedURI)
267         throws JAXBException {
268         return NATIVE_VERSION.equals(exposedURI)
269                ? ContextUtils.getJAXBContext()
270                : Names200408.getJAXBContext();
271     }
272
273     /**
274      * Put all entries from one map into another.
275      *
276      * @param to target map
277      * @param from source map
278      */

279     private static void putAll(Map JavaDoc<QName JavaDoc, String JavaDoc> to, Map JavaDoc<QName JavaDoc, String JavaDoc> from) {
280         if (from != null) {
281             to.putAll(from);
282         }
283     }
284
285     /**
286      * Add all entries from one list into another.
287      *
288      * @param to target list
289      * @param from source list
290      */

291     private static void addAll(List JavaDoc<Object JavaDoc> to, List JavaDoc<Object JavaDoc> from) {
292         if (from != null) {
293             to.addAll(from);
294         }
295     }
296
297     /**
298      * Holder for 2004/08 Names
299      */

300     public static class Names200408 {
301         public static final String JavaDoc WSA_NAMESPACE_NAME =
302             "http://schemas.xmlsoap.org/ws/2004/08/addressing";
303         public static final String JavaDoc WSA_ANONYMOUS_ADDRESS =
304             WSA_NAMESPACE_NAME + "/anonymous";
305         public static final String JavaDoc WSA_NONE_ADDRESS =
306             WSA_NAMESPACE_NAME + "/none";
307         public static final ObjectFactory WSA_OBJECT_FACTORY =
308             new ObjectFactory();
309         public static final String JavaDoc WS_ADDRESSING_PACKAGE =
310             AttributedURI.class.getPackage().getName();
311         public static final Class JavaDoc<org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType>
312         EPR_TYPE =
313             org.objectweb.celtix.ws.addressing.v200408.EndpointReferenceType.class;
314         
315         private static JAXBContext jaxbContext;
316         
317         protected Names200408() {
318         }
319         
320         /**
321          * Retrieve a JAXBContext for marshalling and unmarshalling JAXB generated
322          * types for the 2004/08 version.
323          *
324          * @return a JAXBContext
325          */

326         public static JAXBContext getJAXBContext() throws JAXBException {
327             synchronized (Names200408.class) {
328                 if (jaxbContext == null) {
329                     jaxbContext =
330                         JAXBContext.newInstance(WS_ADDRESSING_PACKAGE);
331                 }
332             }
333             return jaxbContext;
334         }
335         
336         /**
337          * Set the encapsulated JAXBContext (used by unit tests).
338          *
339          * @param ctx JAXBContext
340          */

341         public static void setJAXBContext(JAXBContext ctx) throws JAXBException {
342             synchronized (Names200408.class) {
343                 jaxbContext = ctx;
344             }
345         }
346     }
347 }
348
Popular Tags