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