Package org.servicemix.components.xfire

Source Code of org.servicemix.components.xfire.XFireServiceUnit

/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.components.xfire;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.transport.Transport;
import org.servicemix.components.xbean.EndpointSpec;
import org.servicemix.components.xbean.XBeanServiceUnit;
import org.servicemix.components.xfire.XFireComponent.JbiTransport;
import org.servicemix.jbi.jaxp.SourceTransformer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.factory.WSDLFactory;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XFireServiceUnit extends XBeanServiceUnit {

    private static final Log logger = LogFactory.getLog(XFireServiceUnit.class);
   
    private Map xfServices = new ConcurrentHashMap();
   
    public XFireServiceUnit(XFireComponent component, String serviceUnitName, String serviceUnitRootPath) throws Exception {
        super(component, serviceUnitName, serviceUnitRootPath);
        for (Iterator iter = services.iterator(); iter.hasNext();) {
            EndpointSpec es = (EndpointSpec) iter.next();
            Service svc = component.getFactory().create(es.getPojo().getClass());
            component.getXFire().getServiceRegistry().register(svc);
            component.getXFire().getTransportManager().disableAll(svc.getName());
            Transport jbiT = component.getXFire().getTransportManager().getTransport(JbiTransport.JBI_BINDING);
            component.getXFire().getTransportManager().enable(jbiT, svc.getName());
           
            QName serviceName = svc.getServiceInfo().getName();
            String endpointName = null;
           
            if (es.getServiceName() == null) {
                es.setServiceName(serviceName);
            } else if (!es.getServiceName().equals(serviceName)) {
                logger.warn("The service name defined in the wsdl (" + serviceName +
                            ") does not match the service name defined in the endpoint spec (" + es.getServiceName() +
                            "). WSDL description may be unusable.");
            }
           
            // put service description
            try {
                SourceTransformer st = new SourceTransformer();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                component.getXFire().generateWSDL(svc.getName(), baos);
                Node node = st.toDOMNode(new StreamSource(new ByteArrayInputStream(baos.toByteArray())));
                Definition d = WSDLFactory.newInstance().newWSDLReader().readWSDL(null, (Document) node);
                javax.wsdl.Service service = d.getService(serviceName);
                if (service != null) {
                    if (service.getPorts().values().size() == 1) {
                        endpointName = ((Port) service.getPorts().values().iterator().next()).getName();
                        // Check if this is the same as defined in endpoint spec
                        if (es.getEndpointName() == null) {
                            es.setEndpointName(endpointName);
                        } else if (!es.getEndpointName().equals(endpointName)) {
                            logger.warn("The endpoint name defined in the wsdl (" + endpointName +
                                    ") does not match the endpoint name defined in the endpoint spec (" + es.getEndpointName() +
                            "). WSDL description may be unusable.");
                        }
                    }
                }
                if (es.getEndpointName() == null) {
                    throw new IllegalArgumentException("endpointName should be provided on the endpointSpec");
                }
                component.setServiceDescription(es.getServiceName(), es.getEndpointName(), (Document) node);
                if (logger.isDebugEnabled()) {
                    logger.debug("WSDL: " + st.toString(node));
                }
            } catch (Exception e) {
                logger.warn("Could not set endpoint description", e);
            }

           
            xfServices.put(getKey(es), svc);
        }
    }

    public Service getService(QName service, String endpoint) {
        return (Service) xfServices.get(getKey(service, endpoint));
    }

}
TOP

Related Classes of org.servicemix.components.xfire.XFireServiceUnit

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.