Coverage Report - org.apache.camel.component.mina.MinaProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
MinaProducer
78% 
100% 
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.mina;
 18  
 
 19  
 import java.net.SocketAddress;
 20  
 
 21  
 import org.apache.camel.Exchange;
 22  
 import org.apache.camel.impl.DefaultProducer;
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 import org.apache.mina.common.ConnectFuture;
 26  
 import org.apache.mina.common.IoConnector;
 27  
 import org.apache.mina.common.IoHandler;
 28  
 import org.apache.mina.common.IoHandlerAdapter;
 29  
 import org.apache.mina.common.IoSession;
 30  
 
 31  
 /**
 32  
  * A {@link Producer} implementation for MINA
 33  
  * 
 34  
  * @version $Revision: 563665 $
 35  
  */
 36  
 public class MinaProducer extends DefaultProducer {
 37  1
     private static final transient Log LOG = LogFactory.getLog(MinaProducer.class);
 38  
     private IoSession session;
 39  
     private MinaEndpoint endpoint;
 40  
 
 41  
     public MinaProducer(MinaEndpoint endpoint) {
 42  2
         super(endpoint);
 43  2
         this.endpoint = endpoint;
 44  2
     }
 45  
 
 46  
     public void process(Exchange exchange) {
 47  2
         if (session == null) {
 48  0
             throw new IllegalStateException("Not started yet!");
 49  
         }
 50  2
         Object body = exchange.getIn().getBody();
 51  2
         if (body == null) {
 52  0
             LOG.warn("No payload for exchange: " + exchange);
 53  0
         } else {
 54  2
             session.write(body);
 55  
         }
 56  2
     }
 57  
 
 58  
     @Override
 59  
     protected void doStart() throws Exception {
 60  2
         SocketAddress address = endpoint.getAddress();
 61  2
         IoConnector connector = endpoint.getConnector();
 62  2
         if (LOG.isDebugEnabled()) {
 63  0
             LOG.debug("Creating connector to address: " + address + " using connector: " + connector);
 64  
         }
 65  2
         IoHandler ioHandler = new IoHandlerAdapter() {
 66  
             @Override
 67  2
             public void messageReceived(IoSession ioSession, Object object) throws Exception {
 68  0
                 super.messageReceived(ioSession, object);
 69  
                 /** TODO */
 70  0
             }
 71  
         };
 72  2
         ConnectFuture future = connector.connect(address, ioHandler, endpoint.getConfig());
 73  2
         future.join();
 74  2
         session = future.getSession();
 75  2
     }
 76  
 
 77  
     @Override
 78  
     protected void doStop() throws Exception {
 79  2
         if (session != null) {
 80  2
             session.close().join(2000);
 81  
         }
 82  2
     }
 83  
 }