KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > soap > SoapEndpoint


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.soap;
18
19 import java.net.URI JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.jbi.component.ComponentContext;
27 import javax.jbi.messaging.MessageExchange.Role;
28 import javax.jbi.servicedesc.ServiceEndpoint;
29 import javax.wsdl.Definition;
30 import javax.wsdl.Import;
31 import javax.wsdl.Types;
32 import javax.wsdl.WSDLException;
33 import javax.wsdl.extensions.ExtensibilityElement;
34 import javax.wsdl.extensions.ExtensionRegistry;
35 import javax.wsdl.extensions.schema.Schema;
36 import javax.wsdl.extensions.schema.SchemaImport;
37 import javax.wsdl.factory.WSDLFactory;
38 import javax.wsdl.xml.WSDLReader;
39 import javax.wsdl.xml.WSDLWriter;
40 import javax.xml.namespace.QName JavaDoc;
41
42 import org.apache.servicemix.common.Endpoint;
43 import org.apache.servicemix.common.ExchangeProcessor;
44 import org.apache.servicemix.common.wsdl1.JbiExtension;
45 import org.apache.servicemix.common.xbean.XBeanServiceUnit;
46 import org.apache.servicemix.jbi.security.auth.AuthenticationService;
47 import org.apache.servicemix.jbi.security.keystore.KeystoreManager;
48 import org.apache.servicemix.soap.handlers.addressing.AddressingHandler;
49 import org.springframework.core.io.Resource;
50 import org.w3c.dom.Document JavaDoc;
51
52 import com.ibm.wsdl.Constants;
53
54 public abstract class SoapEndpoint extends Endpoint {
55
56     protected ServiceEndpoint activated;
57     protected ExchangeProcessor processor;
58     protected Role role;
59     protected URI JavaDoc defaultMep;
60     protected boolean soap;
61     protected String JavaDoc soapVersion;
62     protected Resource wsdlResource;
63     protected QName JavaDoc defaultOperation;
64     protected QName JavaDoc targetInterfaceName;
65     protected QName JavaDoc targetService;
66     protected String JavaDoc targetEndpoint;
67     protected List JavaDoc policies;
68     protected Map JavaDoc wsdls = new HashMap JavaDoc();
69     
70     public SoapEndpoint() {
71         policies = Collections.singletonList(new AddressingHandler());
72     }
73     
74     public AuthenticationService getAuthenticationService() {
75         return null;
76     }
77     
78     public KeystoreManager getKeystoreManager() {
79         return null;
80     }
81     
82     /**
83      * @return the policies
84      */

85     public List JavaDoc getPolicies() {
86         return policies;
87     }
88     /**
89      * @param policies the policies to set
90      */

91     public void setPolicies(List JavaDoc policies) {
92         this.policies = policies;
93     }
94     /**
95      * @return Returns the defaultMep.
96      */

97     public URI JavaDoc getDefaultMep() {
98         return defaultMep;
99     }
100     /**
101      * @param defaultMep The defaultMep to set.
102      */

103     public void setDefaultMep(URI JavaDoc defaultMep) {
104         this.defaultMep = defaultMep;
105     }
106     /**
107      * @return Returns the defaultOperation.
108      */

109     public QName JavaDoc getDefaultOperation() {
110         return defaultOperation;
111     }
112     /**
113      * @param defaultOperation The defaultOperation to set.
114      */

115     public void setDefaultOperation(QName JavaDoc defaultOperation) {
116         this.defaultOperation = defaultOperation;
117     }
118     /**
119      * @return Returns the role.
120      */

121     public Role getRole() {
122         return role;
123     }
124     /**
125      * @param role The role to set.
126      */

127     public void setRole(Role role) {
128         this.role = role;
129     }
130     /**
131      * @return Returns the soap.
132      */

133     public boolean isSoap() {
134         return soap;
135     }
136     /**
137      * @param soap The soap to set.
138      */

139     public void setSoap(boolean soap) {
140         this.soap = soap;
141     }
142     /**
143      * @return Returns the soapVersion.
144      */

145     public String JavaDoc getSoapVersion() {
146         return soapVersion;
147     }
148     /**
149      * @param soapVersion The soapVersion to set.
150      */

151     public void setSoapVersion(String JavaDoc soapVersion) {
152         this.soapVersion = soapVersion;
153     }
154     /**
155      * @return Returns the targetEndpoint.
156      */

157     public String JavaDoc getTargetEndpoint() {
158         return targetEndpoint;
159     }
160     /**
161      * @param targetEndpoint The targetEndpoint to set.
162      */

163     public void setTargetEndpoint(String JavaDoc targetEndpoint) {
164         this.targetEndpoint = targetEndpoint;
165     }
166     /**
167      * @return Returns the targetInterfaceName.
168      */

169     public QName JavaDoc getTargetInterfaceName() {
170         return targetInterfaceName;
171     }
172     /**
173      * @param targetInterfaceName The targetInterfaceName to set.
174      */

175     public void setTargetInterfaceName(QName JavaDoc targetInterfaceName) {
176         this.targetInterfaceName = targetInterfaceName;
177     }
178     /**
179      * @return Returns the targetServiceName.
180      */

181     public QName JavaDoc getTargetService() {
182         return targetService;
183     }
184     /**
185      * @param targetServiceName The targetServiceName to set.
186      */

187     public void setTargetService(QName JavaDoc targetServiceName) {
188         this.targetService = targetServiceName;
189     }
190     /**
191      * @return Returns the wsdlResource.
192      */

193     public Resource getWsdlResource() {
194         return wsdlResource;
195     }
196     /**
197      * @param wsdlResource The wsdlResource to set.
198      */

199     public void setWsdlResource(Resource wsdlResource) {
200         this.wsdlResource = wsdlResource;
201     }
202     /**
203      * @org.apache.xbean.Property alias="role"
204      * @param role
205      */

206     public void setRoleAsString(String JavaDoc role) {
207         if (role == null) {
208             throw new IllegalArgumentException JavaDoc("Role must be specified");
209         } else if (JbiExtension.ROLE_CONSUMER.equals(role)) {
210             setRole(Role.CONSUMER);
211         } else if (JbiExtension.ROLE_PROVIDER.equals(role)) {
212             setRole(Role.PROVIDER);
213         } else {
214             throw new IllegalArgumentException JavaDoc("Unrecognized role: " + role);
215         }
216     }
217
218     /**
219      * Load the wsdl for this endpoint.
220      */

221     protected void loadWsdl() {
222         // Load WSDL from the resource
223
if (description == null && wsdlResource != null) {
224             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
225             try {
226                 if (serviceUnit instanceof XBeanServiceUnit) {
227                     XBeanServiceUnit su = (XBeanServiceUnit) serviceUnit;
228                     Thread.currentThread().setContextClassLoader(su.getKernel().getClassLoaderFor(su.getConfiguration()));
229                 }
230                 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
231                 reader.setFeature(Constants.FEATURE_VERBOSE, false);
232                 Definition def = reader.readWSDL(wsdlResource.getURL().toString());
233                 overrideDefinition(def);
234             } catch (Exception JavaDoc e) {
235                 logger.warn("Could not load description from resource", e);
236             } finally {
237                 Thread.currentThread().setContextClassLoader(cl);
238             }
239         }
240         // If the endpoint is a consumer, try to find
241
// the proxied endpoint description
242
if (description == null && definition == null && getRole() == Role.CONSUMER) {
243             retrieveProxiedEndpointDefinition();
244         }
245         // If the wsdl definition is provided,
246
// convert it to a DOM document
247
if (description == null && definition != null) {
248             try {
249                 description = WSDLFactory.newInstance().newWSDLWriter().getDocument(definition);
250             } catch (Exception JavaDoc e) {
251                 logger.warn("Could not create document from wsdl description", e);
252             }
253         }
254         // If the dom description is provided
255
// convert it to a WSDL definition
256
if (definition == null && description != null) {
257             try {
258                 definition = WSDLFactory.newInstance().newWSDLReader().readWSDL(null, description);
259             } catch (Exception JavaDoc e) {
260                 logger.warn("Could not create wsdl definition from dom document", e);
261             }
262         }
263         if (definition != null) {
264             try {
265                 mapDefinition(definition);
266             } catch (Exception JavaDoc e) {
267                 logger.warn("Could not map wsdl definition to documents", e);
268             }
269         }
270     }
271
272     /**
273      * Create a wsdl definition for a consumer endpoint.
274      * Loads the target endpoint definition and add http binding
275      * informations to it.
276      */

277     protected void retrieveProxiedEndpointDefinition() {
278         if (logger.isDebugEnabled()) {
279             logger.debug("Retrieving proxied endpoint definition");
280         }
281         try {
282             ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
283             ServiceEndpoint ep = null;
284             if (targetService != null && targetEndpoint != null) {
285                 ep = ctx.getEndpoint(targetService, targetEndpoint);
286                 if (ep == null && logger.isDebugEnabled()) {
287                     logger.debug("Could not retrieve endpoint targetService/targetEndpoint");
288                 }
289             }
290             if (ep == null && targetService != null) {
291                 ServiceEndpoint[] eps = ctx.getEndpointsForService(targetService);
292                 if (eps != null && eps.length > 0) {
293                     ep = eps[0];
294                 }
295                 if (ep == null && logger.isDebugEnabled()) {
296                     logger.debug("Could not retrieve endpoint for targetService");
297                 }
298             }
299             if (ep == null && targetInterfaceName != null) {
300                 ServiceEndpoint[] eps = ctx.getEndpoints(targetInterfaceName);
301                 if (eps != null && eps.length > 0) {
302                     ep = eps[0];
303                 }
304                 if (ep == null && logger.isDebugEnabled()) {
305                     logger.debug("Could not retrieve endpoint for targetInterfaceName");
306                 }
307             }
308             if (ep == null && service != null && endpoint != null) {
309                 ep = ctx.getEndpoint(service, endpoint);
310                 if (ep == null && logger.isDebugEnabled()) {
311                     logger.debug("Could not retrieve endpoint for service/endpoint");
312                 }
313             }
314             if (ep != null) {
315                 Document JavaDoc doc = ctx.getEndpointDescriptor(ep);
316                 if (doc != null) {
317                     WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
318                     reader.setFeature(Constants.FEATURE_VERBOSE, false);
319                     Definition def = reader.readWSDL(null, doc);
320                     if (def != null) {
321                         overrideDefinition(def);
322                     }
323                 }
324             }
325         } catch (Exception JavaDoc e) {
326             logger.debug("Unable to retrieve target endpoint descriptor", e);
327         }
328     }
329     
330     /* (non-Javadoc)
331      * @see org.apache.servicemix.common.Endpoint#getProcessor()
332      */

333     public ExchangeProcessor getProcessor() {
334         return this.processor;
335     }
336
337     /* (non-Javadoc)
338      * @see org.servicemix.common.Endpoint#activate()
339      */

340     public void activate() throws Exception JavaDoc {
341         ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
342         loadWsdl();
343         if (getRole() == Role.PROVIDER) {
344             activated = ctx.activateEndpoint(service, endpoint);
345             processor = createProviderProcessor();
346         } else {
347             activated = createExternalEndpoint();
348             ctx.registerExternalEndpoint(activated);
349             processor = createConsumerProcessor();
350         }
351         processor.start();
352     }
353     
354     public void activateDynamic() throws Exception JavaDoc {
355         if (getRole() == Role.PROVIDER) {
356             processor = createProviderProcessor();
357         } else {
358             processor = createConsumerProcessor();
359         }
360         processor.start();
361     }
362
363     /* (non-Javadoc)
364      * @see org.servicemix.common.Endpoint#deactivate()
365      */

366     public void deactivate() throws Exception JavaDoc {
367         if (activated != null) {
368             ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
369             if (getRole() == Role.PROVIDER) {
370                 ServiceEndpoint ep = activated;
371                 activated = null;
372                 ctx.deactivateEndpoint(ep);
373             } else {
374                 ServiceEndpoint ep = activated;
375                 activated = null;
376                 ctx.deregisterExternalEndpoint(ep);
377             }
378         }
379         processor.stop();
380     }
381
382     protected abstract void overrideDefinition(Definition def) throws Exception JavaDoc;
383     
384     protected abstract ExchangeProcessor createProviderProcessor();
385     
386     protected abstract ExchangeProcessor createConsumerProcessor();
387     
388     protected abstract ServiceEndpoint createExternalEndpoint();
389
390     protected WSDLReader createWsdlReader() throws WSDLException {
391         WSDLFactory factory = WSDLFactory.newInstance();
392         ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();
393         registerExtensions(registry);
394         WSDLReader reader = factory.newWSDLReader();
395         reader.setFeature(Constants.FEATURE_VERBOSE, false);
396         reader.setExtensionRegistry(registry);
397         return reader;
398     }
399     
400     protected WSDLWriter createWsdlWriter() throws WSDLException {
401         WSDLFactory factory = WSDLFactory.newInstance();
402         ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();
403         registerExtensions(registry);
404         WSDLWriter writer = factory.newWSDLWriter();
405         //writer.setExtensionRegistry(registry);
406
return writer;
407     }
408     
409     protected void registerExtensions(ExtensionRegistry registry) {
410         JbiExtension.register(registry);
411     }
412
413     
414     protected void mapDefinition(Definition def) throws WSDLException {
415         wsdls.put("main.wsdl", createWsdlWriter().getDocument(def));
416         mapImports(def);
417     }
418
419     protected void mapImports(Definition def) throws WSDLException {
420         // Add other imports to mapping
421
Map JavaDoc imports = def.getImports();
422         for (Iterator JavaDoc iter = imports.values().iterator(); iter.hasNext();) {
423             List JavaDoc imps = (List JavaDoc) iter.next();
424             for (Iterator JavaDoc iterator = imps.iterator(); iterator.hasNext();) {
425                 Import imp = (Import) iterator.next();
426                 Definition impDef = imp.getDefinition();
427                 String JavaDoc impLoc = imp.getLocationURI();
428                 if (impDef != null && impLoc != null && !URI.create(impLoc).isAbsolute()) {
429                     wsdls.put(impLoc, createWsdlWriter().getDocument(impDef));
430                     mapImports(impDef);
431                 }
432             }
433         }
434         // Add schemas to mapping
435
Types types = def.getTypes();
436         if (types != null) {
437             for (Iterator JavaDoc it = types.getExtensibilityElements().iterator(); it.hasNext();) {
438                 ExtensibilityElement ee = (ExtensibilityElement) it.next();
439                 if (ee instanceof Schema) {
440                     Schema schema = (Schema) ee;
441                     Map JavaDoc schemaImports = schema.getImports();
442                     for (Iterator JavaDoc iter = schemaImports.values().iterator(); iter.hasNext();) {
443                         List JavaDoc imps = (List JavaDoc) iter.next();
444                         for (Iterator JavaDoc iterator = imps.iterator(); iterator.hasNext();) {
445                             SchemaImport schemaImport = (SchemaImport) iterator.next();
446                             Schema schemaImp = schemaImport.getReferencedSchema();
447                             String JavaDoc schemaLoc = schemaImport.getSchemaLocationURI();
448                             if (schemaLoc != null && schemaImp != null && schemaImp.getElement() != null && !URI.create(schemaLoc).isAbsolute()) {
449                                 wsdls.put(schemaLoc, schemaImp.getElement());
450                             }
451                         }
452                     }
453                 }
454             }
455         }
456     }
457
458     /**
459      * @return Returns the wsdls.
460      */

461     public Map JavaDoc getWsdls() {
462         return wsdls;
463     }
464     
465 }
466
Popular Tags