KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > wsn > component > WSNComponent


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.wsn.component;
18
19 import java.net.URL JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.jbi.servicedesc.ServiceEndpoint;
24 import javax.jms.ConnectionFactory JavaDoc;
25 import javax.wsdl.Definition;
26 import javax.wsdl.factory.WSDLFactory;
27 import javax.wsdl.xml.WSDLReader;
28 import javax.xml.namespace.QName JavaDoc;
29
30 import org.apache.servicemix.common.BaseComponent;
31 import org.apache.servicemix.common.BaseLifeCycle;
32 import org.apache.servicemix.common.BaseServiceUnitManager;
33 import org.apache.servicemix.common.Deployer;
34 import org.apache.servicemix.common.Endpoint;
35 import org.apache.servicemix.common.EndpointSupport;
36 import org.apache.servicemix.common.tools.wsdl.WSDLFlattener;
37 import org.w3c.dom.Document JavaDoc;
38
39 import com.ibm.wsdl.Constants;
40
41 public class WSNComponent extends BaseComponent {
42
43     private WSDLFlattener flattener;
44     private Map JavaDoc descriptions;
45     
46     @Override JavaDoc
47     protected BaseLifeCycle createLifeCycle() {
48         return new WSNLifeCycle(this);
49     }
50
51     @Override JavaDoc
52     public BaseServiceUnitManager createServiceUnitManager() {
53         Deployer[] deployers = new Deployer[] { new WSNDeployer(this) };
54         return new BaseServiceUnitManager(this, deployers);
55     }
56
57     public ConnectionFactory JavaDoc getConnectionFactory() {
58         return ((WSNLifeCycle) lifeCycle).getConnectionFactory();
59     }
60
61     public void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
62         ((WSNLifeCycle) lifeCycle).setConnectionFactory(connectionFactory);
63     }
64
65     /* (non-Javadoc)
66      * @see org.apache.servicemix.common.BaseComponent#getServiceDescription(javax.jbi.servicedesc.ServiceEndpoint)
67      */

68     @Override JavaDoc
69     public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
70         if (logger.isDebugEnabled()) {
71             logger.debug("Querying service description for " + endpoint);
72         }
73         String JavaDoc key = EndpointSupport.getKey(endpoint);
74         Endpoint ep = this.registry.getEndpoint(key);
75         if (ep != null) {
76             QName JavaDoc interfaceName = ep.getInterfaceName();
77             if (interfaceName == null) {
78                 if (logger.isDebugEnabled()) {
79                     logger.debug("Could not retrieve description for endpoint " + key + " (no interface defined)");
80                 }
81                 return null;
82             }
83             Document JavaDoc doc = getDescription(interfaceName);
84             return doc;
85         } else {
86             if (logger.isDebugEnabled()) {
87                 logger.debug("No endpoint found for " + key);
88             }
89             return null;
90         }
91     }
92
93     private synchronized Document JavaDoc getDescription(QName JavaDoc interfaceName) {
94         try {
95             if (descriptions == null) {
96                 descriptions = new HashMap JavaDoc();
97             }
98             Document JavaDoc doc = (Document JavaDoc) descriptions.get(interfaceName);
99             if (doc == null) {
100                 if (flattener == null) {
101                     URL JavaDoc resource = getClass().getClassLoader().getResource("org/apache/servicemix/wsn/wsn.wsdl");
102                     WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
103                     reader.setFeature(Constants.FEATURE_VERBOSE, false);
104                     Definition definition = reader.readWSDL(null, resource.toString());
105                     flattener = new WSDLFlattener(definition);
106                 }
107                 Definition flatDef = flattener.getDefinition(interfaceName);
108                 doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(flatDef);
109                 descriptions.put(interfaceName, doc);
110             }
111             return doc;
112         } catch (Exception JavaDoc e) {
113             if (logger.isDebugEnabled()) {
114                 logger.debug("Error retrieving endpoint description", e);
115             }
116             return null;
117         }
118     }
119     
120 }
121
Popular Tags