Coverage Report - org.apache.camel.component.cxf.CxfProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
CxfProducer
0% 
0% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.cxf;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.util.concurrent.CountDownLatch;
 21  
 
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.RuntimeCamelException;
 24  
 import org.apache.camel.impl.DefaultProducer;
 25  
 import org.apache.cxf.message.ExchangeImpl;
 26  
 import org.apache.cxf.message.Message;
 27  
 import org.apache.cxf.message.MessageImpl;
 28  
 import org.apache.cxf.service.model.EndpointInfo;
 29  
 import org.apache.cxf.transport.Conduit;
 30  
 import org.apache.cxf.transport.Destination;
 31  
 import org.apache.cxf.transport.MessageObserver;
 32  
 import org.apache.cxf.transport.local.LocalConduit;
 33  
 import org.apache.cxf.transport.local.LocalTransportFactory;
 34  
 
 35  
 /**
 36  
  * Sends messages from Camel into the CXF endpoint
 37  
  * 
 38  
  * @version $Revision: 563665 $
 39  
  */
 40  
 public class CxfProducer extends DefaultProducer {
 41  
     private CxfEndpoint endpoint;
 42  
     private final LocalTransportFactory transportFactory;
 43  
     private Destination destination;
 44  
     private Conduit conduit;
 45  0
     private ResultFuture future = new ResultFuture();
 46  
 
 47  
     public CxfProducer(CxfEndpoint endpoint, LocalTransportFactory transportFactory) {
 48  0
         super(endpoint);
 49  0
         this.endpoint = endpoint;
 50  0
         this.transportFactory = transportFactory;
 51  0
     }
 52  
 
 53  
     public void process(Exchange exchange) {
 54  0
         CxfExchange cxfExchange = endpoint.toExchangeType(exchange);
 55  0
         process(cxfExchange);
 56  0
     }
 57  
 
 58  
     public void process(CxfExchange exchange) {
 59  
         try {
 60  0
             CxfBinding binding = endpoint.getBinding();
 61  0
             MessageImpl m = binding.createCxfMessage(exchange);
 62  0
             ExchangeImpl e = new ExchangeImpl();
 63  0
             e.setInMessage(m);
 64  0
             m.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
 65  0
             m.setDestination(destination);
 66  0
             synchronized (conduit) {
 67  0
                 conduit.prepare(m);
 68  
 
 69  
                 // now lets wait for the response
 70  0
                 if (endpoint.isInOut()) {
 71  0
                     Message response = future.getResponse();
 72  
 
 73  
                     // TODO - why do we need to ignore the returned message and
 74  
                     // get the out message from the exchange!
 75  0
                     response = e.getOutMessage();
 76  0
                     binding.storeCxfResponse(exchange, response);
 77  
                 }
 78  0
             }
 79  0
         } catch (IOException e) {
 80  0
             throw new RuntimeCamelException(e);
 81  0
         }
 82  0
     }
 83  
 
 84  
     @Override
 85  
     protected void doStart() throws Exception {
 86  0
         super.doStart();
 87  0
         EndpointInfo endpointInfo = endpoint.getEndpointInfo();
 88  0
         destination = transportFactory.getDestination(endpointInfo);
 89  
 
 90  
         // Set up a listener for the response
 91  0
         conduit = transportFactory.getConduit(endpointInfo);
 92  0
         conduit.setMessageObserver(future);
 93  0
     }
 94  
 
 95  
     @Override
 96  
     protected void doStop() throws Exception {
 97  0
         super.doStop();
 98  
 
 99  0
         if (conduit != null) {
 100  0
             conduit.close();
 101  
         }
 102  0
     }
 103  
 
 104  0
     protected class ResultFuture implements MessageObserver {
 105  
         Message response;
 106  0
         CountDownLatch latch = new CountDownLatch(1);
 107  
 
 108  
         public Message getResponse() {
 109  0
             while (response == null) {
 110  
                 try {
 111  0
                     latch.await();
 112  0
                 } catch (InterruptedException e) {
 113  
                     // ignore
 114  0
                 }
 115  0
             }
 116  0
             return response;
 117  
         }
 118  
 
 119  
         public synchronized void onMessage(Message message) {
 120  
             try {
 121  0
                 message.remove(LocalConduit.DIRECT_DISPATCH);
 122  0
                 this.response = message;
 123  
             } finally {
 124  0
                 latch.countDown();
 125  0
             }
 126  0
         }
 127  
     }
 128  
 }