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.mina;
019    
020    import org.apache.camel.Producer;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.impl.DefaultProducer;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.apache.mina.common.ConnectFuture;
026    import org.apache.mina.common.IoConnector;
027    import org.apache.mina.common.IoHandler;
028    import org.apache.mina.common.IoHandlerAdapter;
029    import org.apache.mina.common.IoSession;
030    
031    import java.net.SocketAddress;
032    
033    /**
034     * A {@link Producer} implementation for MINA
035     *
036     * @version $Revision: 534145 $
037     */
038    public class MinaProducer extends DefaultProducer {
039        private static final transient Log log = LogFactory.getLog(MinaProducer.class);
040        private IoSession session;
041        private MinaEndpoint endpoint;
042    
043        public MinaProducer(MinaEndpoint endpoint) {
044            super(endpoint);
045            this.endpoint = endpoint;
046        }
047    
048        public void process(Exchange exchange) {
049            if (session == null) {
050                throw new IllegalStateException("Not started yet!");
051            }
052            Object body = exchange.getIn().getBody();
053            if (body == null) {
054                log.warn("No payload for exchange: " + exchange);
055            }
056            else {
057                session.write(body);
058            }
059        }
060    
061        @Override
062        protected void doStart() throws Exception {
063            SocketAddress address = endpoint.getAddress();
064            IoConnector connector = endpoint.getConnector();
065            if (log.isDebugEnabled()) {
066                log.debug("Creating connector to address: " + address + " using connector: " + connector);
067            }
068            IoHandler ioHandler = new IoHandlerAdapter() {
069                @Override
070                public void messageReceived(IoSession ioSession, Object object) throws Exception {
071                    super.messageReceived(ioSession, object);    /** TODO */
072                }
073            };
074            ConnectFuture future = connector.connect(address, ioHandler, endpoint.getConfig());
075            future.join();
076            session = future.getSession();
077        }
078    
079        @Override
080        protected void doStop() throws Exception {
081            if (session != null) {
082                session.close().join(2000);
083            }
084        }
085    }