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