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;
018    
019    import java.util.List;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.RuntimeCamelException;
023    import org.apache.camel.impl.DefaultProducer;
024    import org.apache.camel.util.ObjectHelper;
025    import org.apache.cxf.endpoint.Client;
026    import org.apache.cxf.frontend.ClientFactoryBean;
027    
028    /**
029     * Sends messages from Camel into the CXF endpoint
030     * 
031     * @version $Revision: 563665 $
032     */
033    public class CxfInvokeProducer extends DefaultProducer {
034        private CxfInvokeEndpoint endpoint;
035        private Client client;
036    
037        public CxfInvokeProducer(CxfInvokeEndpoint endpoint) {
038            super(endpoint);
039            this.endpoint = endpoint;
040        }
041    
042        public void process(Exchange exchange) {
043            CxfExchange cxfExchange = endpoint.toExchangeType(exchange);
044            process(cxfExchange);
045            exchange.copyFrom(cxfExchange);
046        }
047    
048        public void process(CxfExchange exchange) {
049            List params = exchange.getIn().getBody(List.class);
050            Object[] response = null;
051            try {
052                response = client.invoke(endpoint.getProperty(CxfConstants.METHOD), params.toArray());
053            } catch (Exception e) {
054                throw new RuntimeCamelException(e);
055            }
056    
057            CxfBinding binding = endpoint.getBinding();
058            binding.storeCxfResponse(exchange, response);
059        }
060    
061        @Override
062        protected void doStart() throws Exception {
063            // TODO Add support for sending message inputstream. Currently, we only
064            // handle
065            // method invocation with pojo.
066    
067            // TODO Add support for endpoints associated with a WSDL
068            if (client == null) {
069                ClientFactoryBean cfBean = new ClientFactoryBean();
070                cfBean.setAddress(getEndpoint().getEndpointUri());
071                cfBean.setBus(endpoint.getBus());
072                String className = endpoint.getProperty(CxfConstants.SEI);
073                Class type = ObjectHelper.loadClass(className);
074                cfBean.setServiceClass(type);
075                client = cfBean.create();
076            }
077        }
078    
079        @Override
080        protected void doStop() throws Exception {
081            if (client != null) {
082                client.getConduit().close();
083                client = null;
084            }
085    
086            super.doStop();
087        }
088    }