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.camel.component.cxf.transport;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Producer;
025    import org.apache.camel.ProducerTemplate;
026    import org.apache.camel.RuntimeCamelException;
027    import org.apache.camel.spi.HeaderFilterStrategy;
028    import org.apache.camel.util.ObjectHelper;
029    import org.apache.cxf.Bus;
030    import org.apache.cxf.common.logging.LogUtils;
031    import org.apache.cxf.configuration.Configurable;
032    import org.apache.cxf.configuration.Configurer;
033    import org.apache.cxf.message.Message;
034    import org.apache.cxf.service.model.EndpointInfo;
035    import org.apache.cxf.transport.AbstractConduit;
036    import org.apache.cxf.ws.addressing.EndpointReferenceType;
037    import org.slf4j.Logger;
038    import org.slf4j.LoggerFactory;
039    
040    /**
041     * @version 
042     */
043    public class CamelConduit extends AbstractConduit implements Configurable {
044        protected static final String BASE_BEAN_NAME_SUFFIX = ".camel-conduit";
045        private static final Logger LOG = LoggerFactory.getLogger(CamelConduit.class);
046        // used for places where CXF requires JUL
047        private static final java.util.logging.Logger JUL_LOG = LogUtils.getL7dLogger(CamelConduit.class);
048    
049        private CamelContext camelContext;
050        private EndpointInfo endpointInfo;
051        private String targetCamelEndpointUri;
052        private Producer producer;
053        private ProducerTemplate camelTemplate;
054        private Bus bus;
055        private HeaderFilterStrategy headerFilterStrategy;
056    
057        public CamelConduit(CamelContext context, Bus b, EndpointInfo endpointInfo) {
058            this(context, b, endpointInfo, null);
059        }
060    
061        public CamelConduit(CamelContext context, Bus b, EndpointInfo epInfo, EndpointReferenceType targetReference) {
062            this(context, b, epInfo, targetReference, null);
063        }
064    
065        public CamelConduit(CamelContext context, Bus b, EndpointInfo epInfo, EndpointReferenceType targetReference,
066                HeaderFilterStrategy headerFilterStrategy) {
067            super(getTargetReference(epInfo, targetReference, b));
068            String address = epInfo.getAddress();
069            if (address != null) {
070                targetCamelEndpointUri = address.substring(CamelTransportConstants.CAMEL_TRANSPORT_PREFIX.length());
071                if (targetCamelEndpointUri.startsWith("//")) {
072                    targetCamelEndpointUri = targetCamelEndpointUri.substring(2);
073                }
074            }
075            camelContext = context;
076            endpointInfo = epInfo;
077            bus = b;
078            initConfig();
079            this.headerFilterStrategy = headerFilterStrategy;
080            Endpoint target = getCamelContext().getEndpoint(targetCamelEndpointUri);
081            try {
082                producer = target.createProducer();
083                producer.start();
084            } catch (Exception e) {
085                throw new RuntimeCamelException("Cannot create the producer rightly", e);
086            }
087        }
088    
089        public void setCamelContext(CamelContext context) {
090            camelContext = context;
091        }
092    
093        public CamelContext getCamelContext() {
094            ObjectHelper.notNull(camelContext, "CamelContext", this);
095            return camelContext;
096        }
097    
098        // prepare the message for send out , not actually send out the message
099        public void prepare(Message message) throws IOException {
100            LOG.trace("CamelConduit send message");
101            CamelOutputStream os = new CamelOutputStream(this.targetCamelEndpointUri, 
102                                                         this.producer, 
103                                                         this.headerFilterStrategy, 
104                                                         this.getMessageObserver(), 
105                                                         message);
106            message.setContent(OutputStream.class, os);
107        }
108    
109        public void close() {
110            LOG.trace("CamelConduit closed ");
111            // shutdown the producer
112            try {
113                producer.stop();
114            } catch (Exception e) {
115                LOG.warn("CamelConduit producer stop with the exception", e);
116            }
117        }
118    
119        protected java.util.logging.Logger getLogger() {
120            return JUL_LOG;
121        }
122    
123        public String getBeanName() {
124            if (endpointInfo == null || endpointInfo.getName() == null) {
125                return "default" + BASE_BEAN_NAME_SUFFIX;
126            }
127            return endpointInfo.getName().toString() + BASE_BEAN_NAME_SUFFIX;
128        }
129    
130        private void initConfig() {
131            // we could configure the camel context here
132            if (bus != null) {
133                Configurer configurer = bus.getExtension(Configurer.class);
134                if (null != configurer) {
135                    configurer.configureBean(this);
136                }
137            }
138        }
139    
140        @Deprecated
141        public ProducerTemplate getCamelTemplate() throws Exception {
142            if (camelTemplate == null) {
143                camelTemplate = getCamelContext().createProducerTemplate();
144            }
145            return camelTemplate;
146        }
147    
148        @Deprecated
149        public void setCamelTemplate(ProducerTemplate template) {
150            camelTemplate = template;
151        }
152    
153    }