001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.component.mina; 018 019 import java.net.SocketAddress; 020 021 import org.apache.camel.Processor; 022 import org.apache.camel.impl.DefaultConsumer; 023 import org.apache.commons.logging.Log; 024 import org.apache.commons.logging.LogFactory; 025 import org.apache.mina.common.IoAcceptor; 026 import org.apache.mina.common.IoHandler; 027 import org.apache.mina.common.IoHandlerAdapter; 028 import org.apache.mina.common.IoSession; 029 030 /** 031 * A 032 * 033 * @{link Consumer} for MINA 034 * @version $Revision: 563665 $ 035 */ 036 public class MinaConsumer extends DefaultConsumer<MinaExchange> { 037 private static final transient Log LOG = LogFactory.getLog(MinaConsumer.class); 038 039 private final MinaEndpoint endpoint; 040 private final SocketAddress address; 041 private final IoAcceptor acceptor; 042 043 public MinaConsumer(final MinaEndpoint endpoint, Processor processor) { 044 super(endpoint, processor); 045 this.endpoint = endpoint; 046 address = endpoint.getAddress(); 047 acceptor = endpoint.getAcceptor(); 048 } 049 050 @Override 051 protected void doStart() throws Exception { 052 053 if (LOG.isDebugEnabled()) { 054 LOG.debug("Binding to server address: " + address + " using acceptor: " + acceptor); 055 } 056 057 IoHandler handler = new IoHandlerAdapter() { 058 @Override 059 public void messageReceived(IoSession session, Object object) throws Exception { 060 getProcessor().process(endpoint.createExchange(session, object)); 061 } 062 }; 063 064 acceptor.bind(address, handler, endpoint.getConfig()); 065 } 066 067 @Override 068 protected void doStop() throws Exception { 069 acceptor.unbind(address); 070 super.doStop(); 071 } 072 }