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.Consumer;
022 import org.apache.camel.Exchange;
023 import org.apache.camel.MultipleConsumersSupport;
024 import org.apache.camel.Processor;
025 import org.apache.camel.Producer;
026 import org.apache.camel.impl.DefaultEndpoint;
027 import org.apache.camel.util.ObjectHelper;
028 import org.apache.mina.common.IoAcceptor;
029 import org.apache.mina.common.IoAcceptorConfig;
030 import org.apache.mina.common.IoConnector;
031 import org.apache.mina.common.IoConnectorConfig;
032 import org.apache.mina.common.IoSession;
033
034 /**
035 * Endpoint for Camel MINA.
036 *
037 * @version $Revision: 16508 $
038 */
039 public class MinaEndpoint extends DefaultEndpoint implements MultipleConsumersSupport {
040
041 /** The key of the IoSession which is stored in the message header*/
042 public static final transient String HEADER_MINA_IOSESSION = "CamelMinaIoSession";
043 /** The socket address of local machine that received the message. */
044 public static final transient String HEADER_LOCAL_ADDRESS = "CamelMinaLocalAddress";
045 /** The socket address of the remote machine that send the message. */
046 public static final transient String HEADER_REMOTE_ADDRESS = "CamelMinaRemoteAddress";
047
048 private SocketAddress address;
049 private IoAcceptor acceptor;
050 private IoConnector connector;
051 private IoAcceptorConfig acceptorConfig;
052 private IoConnectorConfig connectorConfig;
053 private MinaConfiguration configuration;
054
055 public MinaEndpoint() {
056 }
057
058 public MinaEndpoint(String endpointUri, MinaComponent component) {
059 super(endpointUri, component);
060 }
061
062 public Producer createProducer() throws Exception {
063 ObjectHelper.notNull(configuration, "configuration");
064 ObjectHelper.notNull(address, "address");
065 ObjectHelper.notNull(connector, "connector");
066 // wm protocol does not have config
067 if (!configuration.getProtocol().equalsIgnoreCase("vm")) {
068 ObjectHelper.notNull(connectorConfig, "connectorConfig");
069 }
070 return new MinaProducer(this);
071 }
072
073 public Consumer createConsumer(Processor processor) throws Exception {
074 ObjectHelper.notNull(configuration, "configuration");
075 ObjectHelper.notNull(address, "address");
076 ObjectHelper.notNull(acceptor, "acceptor");
077 // wm protocol does not have config
078 if (!configuration.getProtocol().equalsIgnoreCase("vm")) {
079 ObjectHelper.notNull(acceptorConfig, "acceptorConfig");
080 }
081 return new MinaConsumer(this, processor);
082 }
083
084 public Exchange createExchange(IoSession session, Object payload) {
085 Exchange exchange = createExchange();
086 exchange.getIn().setHeader(HEADER_MINA_IOSESSION, session);
087 exchange.getIn().setHeader(HEADER_LOCAL_ADDRESS, session.getLocalAddress());
088 exchange.getIn().setHeader(HEADER_REMOTE_ADDRESS , session.getRemoteAddress());
089 MinaPayloadHelper.setIn(exchange, payload);
090 return exchange;
091 }
092
093 public boolean isSingleton() {
094 return true;
095 }
096
097 public boolean isMultipleConsumersSupported() {
098 // only datagram should allow multiple consumers
099 return configuration.isDatagramProtocol();
100 }
101
102 // Properties
103 // -------------------------------------------------------------------------
104
105 public MinaConfiguration getConfiguration() {
106 return configuration;
107 }
108
109 public void setConfiguration(MinaConfiguration configuration) {
110 this.configuration = configuration;
111 }
112
113 public SocketAddress getAddress() {
114 return address;
115 }
116
117 public void setAddress(SocketAddress address) {
118 this.address = address;
119 }
120
121 public IoAcceptor getAcceptor() {
122 return acceptor;
123 }
124
125 public void setAcceptor(IoAcceptor acceptor) {
126 this.acceptor = acceptor;
127 }
128
129 public IoConnector getConnector() {
130 return connector;
131 }
132
133 public void setConnector(IoConnector connector) {
134 this.connector = connector;
135 }
136
137 public IoAcceptorConfig getAcceptorConfig() {
138 return acceptorConfig;
139 }
140
141 public void setAcceptorConfig(IoAcceptorConfig acceptorConfig) {
142 this.acceptorConfig = acceptorConfig;
143 }
144
145 public IoConnectorConfig getConnectorConfig() {
146 return connectorConfig;
147 }
148
149 public void setConnectorConfig(IoConnectorConfig connectorConfig) {
150 this.connectorConfig = connectorConfig;
151 }
152
153 }