001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.wsn.component;
018    
019    import java.net.URL;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import javax.jbi.servicedesc.ServiceEndpoint;
024    import javax.jms.ConnectionFactory;
025    import javax.wsdl.Definition;
026    import javax.wsdl.factory.WSDLFactory;
027    import javax.wsdl.xml.WSDLReader;
028    import javax.xml.namespace.QName;
029    
030    import org.w3c.dom.Document;
031    
032    import com.ibm.wsdl.Constants;
033    
034    import org.apache.servicemix.common.BaseComponent;
035    import org.apache.servicemix.common.BaseLifeCycle;
036    import org.apache.servicemix.common.BaseServiceUnitManager;
037    import org.apache.servicemix.common.Deployer;
038    import org.apache.servicemix.common.Endpoint;
039    import org.apache.servicemix.common.EndpointSupport;
040    import org.apache.servicemix.common.tools.wsdl.WSDLFlattener;
041    
042    public class WSNComponent extends BaseComponent {
043    
044        private WSDLFlattener flattener;
045    
046        private Map<QName, Document> descriptions;
047    
048        @Override
049        protected BaseLifeCycle createLifeCycle() {
050            return new WSNLifeCycle(this);
051        }
052    
053        @Override
054        public BaseServiceUnitManager createServiceUnitManager() {
055            Deployer[] deployers = new Deployer[] {new WSNDeployer(this) };
056            return new BaseServiceUnitManager(this, deployers);
057        }
058    
059        public ConnectionFactory getConnectionFactory() {
060            return ((WSNLifeCycle) lifeCycle).getConnectionFactory();
061        }
062    
063        public void setConnectionFactory(ConnectionFactory connectionFactory) {
064            ((WSNLifeCycle) lifeCycle).setConnectionFactory(connectionFactory);
065        }
066    
067        /*
068         * (non-Javadoc)
069         * 
070         * @see org.apache.servicemix.common.BaseComponent#getServiceDescription(javax.jbi.servicedesc.ServiceEndpoint)
071         */
072        @Override
073        public Document getServiceDescription(ServiceEndpoint endpoint) {
074            if (logger.isDebugEnabled()) {
075                logger.debug("Querying service description for " + endpoint);
076            }
077            String key = EndpointSupport.getKey(endpoint);
078            Endpoint ep = this.registry.getEndpoint(key);
079            if (ep != null) {
080                QName interfaceName = ep.getInterfaceName();
081                if (interfaceName == null) {
082                    if (logger.isDebugEnabled()) {
083                        logger.debug("Could not retrieve description for endpoint " + key + " (no interface defined)");
084                    }
085                    return null;
086                }
087                return getDescription(interfaceName);
088            } else {
089                if (logger.isDebugEnabled()) {
090                    logger.debug("No endpoint found for " + key);
091                }
092                return null;
093            }
094        }
095    
096        private synchronized Document getDescription(QName interfaceName) {
097            try {
098                if (descriptions == null) {
099                    descriptions = new HashMap<QName, Document>();
100                }
101                Document doc = descriptions.get(interfaceName);
102                if (doc == null) {
103                    if (flattener == null) {
104                        URL resource = getClass().getClassLoader().getResource("org/apache/servicemix/wsn/wsn.wsdl");
105                        WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
106                        reader.setFeature(Constants.FEATURE_VERBOSE, false);
107                        Definition definition = reader.readWSDL(null, resource.toString());
108                        flattener = new WSDLFlattener(definition);
109                    }
110                    Definition flatDef = flattener.getDefinition(interfaceName);
111                    doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(flatDef);
112                    descriptions.put(interfaceName, doc);
113                }
114                return doc;
115            } catch (Exception e) {
116                if (logger.isDebugEnabled()) {
117                    logger.debug("Error retrieving endpoint description", e);
118                }
119                return null;
120            }
121        }
122    
123    }