001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.cxf;
019    
020    import org.apache.camel.RuntimeCamelException;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.impl.DefaultProducer;
023    import org.apache.cxf.endpoint.Client;
024    import org.apache.cxf.frontend.ClientFactoryBean;
025    
026    import java.util.List;
027    
028    /**
029     * Sends messages from Camel into the CXF endpoint
030     *
031     * @version $Revision: 534156 $
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            }                                                           
054            catch (Exception e) {
055                throw new RuntimeCamelException(e);
056            }
057    
058            CxfBinding binding = endpoint.getBinding();
059            binding.storeCxfResponse(exchange, response);
060        }
061    
062        @Override
063        protected void doStart() throws Exception {
064            // TODO Add support for sending message inputstream.  Currently, we only 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                cfBean.setServiceClass(Class.forName(endpoint.getProperty(CxfConstants.SEI)));
073                client = cfBean.create();
074            }
075        }
076    
077        @Override
078        protected void doStop() throws Exception {
079            if (client != null) {
080                client.getConduit().close();
081                client = null;
082            }
083    
084            super.doStop();
085        }
086    }
087    
088