camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r750338 - in /camel/trunk/components/camel-mina/src: main/java/org/apache/camel/component/mina/MinaComponent.java main/java/org/apache/camel/component/mina/MinaConfiguration.java test/java/org/apache/camel/component/mina/MinaFiltersTest.java
Date Thu, 05 Mar 2009 06:44:27 GMT
Author: davsclaus
Date: Thu Mar  5 06:44:27 2009
New Revision: 750338

URL: http://svn.apache.org/viewvc?rev=750338&view=rev
Log:
CAMEL-1418: Added option to set Mina IoFilters. Applied patch with thanks to Nicolas Bouillon.

Added:
    camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaFiltersTest.java
  (with props)
Modified:
    camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaComponent.java
    camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConfiguration.java

Modified: camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaComponent.java?rev=750338&r1=750337&r2=750338&view=diff
==============================================================================
--- camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaComponent.java
(original)
+++ camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaComponent.java
Thu Mar  5 06:44:27 2009
@@ -22,6 +22,7 @@
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.CamelContext;
@@ -29,12 +30,13 @@
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.impl.DefaultComponent;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.DefaultIoFilterChainBuilder;
 import org.apache.mina.common.IoAcceptor;
 import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.IoFilter;
 import org.apache.mina.common.IoServiceConfig;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.filter.LoggingFilter;
@@ -122,6 +124,7 @@
     protected MinaEndpoint createVmEndpoint(String uri, MinaConfiguration configuration)
{
         boolean minaLogger = configuration.isMinaLogger();
         boolean sync = configuration.isSync();
+        List<IoFilter> filters = configuration.getFilters();
 
         IoAcceptor acceptor = new VmPipeAcceptor();
         SocketAddress address = new VmPipeAddress(configuration.getPort());
@@ -132,12 +135,14 @@
         if (minaLogger) {
             connector.getFilterChain().addLast("logger", new LoggingFilter());
         }
+        appendIoFiltersToChain(filters, connector.getFilterChain());
 
         // acceptor connectorConfig
         configureCodecFactory("MinaConsumer", acceptor.getDefaultConfig(), configuration);
         if (minaLogger) {
             acceptor.getFilterChain().addLast("logger", new LoggingFilter());
         }
+        appendIoFiltersToChain(filters, acceptor.getFilterChain());
 
         MinaEndpoint endpoint = new MinaEndpoint(uri, this);
         endpoint.setAddress(address);
@@ -159,6 +164,7 @@
         boolean minaLogger = configuration.isMinaLogger();
         long timeout = configuration.getTimeout();
         boolean sync = configuration.isSync();
+        List<IoFilter> filters = configuration.getFilters();
 
         IoAcceptor acceptor = new SocketAcceptor();
         SocketAddress address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
@@ -170,6 +176,8 @@
         if (minaLogger) {
             connectorConfig.getFilterChain().addLast("logger", new LoggingFilter());
         }
+        appendIoFiltersToChain(filters, connectorConfig.getFilterChain());
+
         // set connect timeout to mina in seconds
         connectorConfig.setConnectTimeout((int) (timeout / 1000));
 
@@ -181,6 +189,7 @@
         if (minaLogger) {
             acceptorConfig.getFilterChain().addLast("logger", new LoggingFilter());
         }
+        appendIoFiltersToChain(filters, acceptorConfig.getFilterChain());
 
         MinaEndpoint endpoint = new MinaEndpoint(uri, this);
         endpoint.setAddress(address);
@@ -229,6 +238,7 @@
         long timeout = configuration.getTimeout();
         boolean transferExchange = configuration.isTransferExchange();
         boolean sync = configuration.isSync();
+        List<IoFilter> filters = configuration.getFilters();
 
         IoAcceptor acceptor = new DatagramAcceptor();
         SocketAddress address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
@@ -243,6 +253,7 @@
         if (minaLogger) {
             connectorConfig.getFilterChain().addLast("logger", new LoggingFilter());
         }
+        appendIoFiltersToChain(filters, connectorConfig.getFilterChain());
         // set connect timeout to mina in seconds
         connectorConfig.setConnectTimeout((int) (timeout / 1000));
 
@@ -253,6 +264,7 @@
         if (minaLogger) {
             acceptorConfig.getFilterChain().addLast("logger", new LoggingFilter());
         }
+        appendIoFiltersToChain(filters, acceptorConfig.getFilterChain());
 
         MinaEndpoint endpoint = new MinaEndpoint(uri, this);
         endpoint.setAddress(address);
@@ -395,6 +407,14 @@
         return Charset.forName(encoding);
     }
 
+    private void appendIoFiltersToChain(List<IoFilter> filters, DefaultIoFilterChainBuilder
filterChain) {
+        if (filters != null && filters.size() > 0) {
+            for (IoFilter ioFilter : filters) {
+                filterChain.addLast(ioFilter.getClass().getCanonicalName(), ioFilter);
+            }
+        }
+    }
+
     // Properties
     //-------------------------------------------------------------------------
 

Modified: camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConfiguration.java?rev=750338&r1=750337&r2=750338&view=diff
==============================================================================
--- camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConfiguration.java
(original)
+++ camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConfiguration.java
Thu Mar  5 06:44:27 2009
@@ -17,8 +17,10 @@
 package org.apache.camel.component.mina;
 
 import java.nio.charset.Charset;
+import java.util.List;
 
 import org.apache.camel.RuntimeCamelException;
+import org.apache.mina.common.IoFilter;
 
 /**
  * Mina configuration
@@ -36,6 +38,7 @@
     private boolean lazySessionCreation;
     private boolean transferExchange;
     private boolean minaLogger;
+    private List<IoFilter> filters;
 
     /**
      * Returns a copy of this configuration
@@ -155,4 +158,13 @@
     public void setMinaLogger(boolean minaLogger) {
         this.minaLogger = minaLogger;
     }
+
+    public List<IoFilter> getFilters() {
+        return filters;
+    }
+
+    public void setFilters(List<IoFilter> filters) {
+        this.filters = filters;
+    }
+    
 }

Added: camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaFiltersTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaFiltersTest.java?rev=750338&view=auto
==============================================================================
--- camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaFiltersTest.java
(added)
+++ camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaFiltersTest.java
Thu Mar  5 06:44:27 2009
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mina;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import javax.naming.Context;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.jndi.JndiContext;
+import org.apache.mina.common.IoFilter;
+import org.apache.mina.common.IoFilterAdapter;
+import org.apache.mina.common.IoSession;
+
+/**
+ * For unit testing the <tt>logger</tt> option.
+ */
+public class MinaFiltersTest extends ContextTestSupport {
+
+    public void testFilter() throws Exception {
+        final String uri = "mina:tcp://localhost:6321?textline=true&minaLogger=true&sync=false&filters=#myFilters";
+        context.addRoutes(new RouteBuilder() {
+
+            public void configure() throws Exception {
+                from(uri).to("mock:result");
+            }
+        });
+
+        MockEndpoint mock = this.getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        Endpoint endpoint = context.getEndpoint(uri);
+        Exchange exchange = endpoint.createExchange();
+        Producer producer = endpoint.createProducer();
+        producer.start();
+
+        // set input and execute it
+        exchange.getIn().setBody("Hello World");
+        producer.process(exchange);
+
+        Field field = producer.getClass().getDeclaredField("session");
+        field.setAccessible(true);
+        IoSession session = (IoSession) field.get(producer);
+        assertTrue("There should be a test filter", session.getFilterChain().contains(TestFilter.class.getCanonicalName()));
+
+        assertTrue("The filter should have been called twice (producer and consumer)", TestFilter.called
== 2);
+
+        producer.stop();
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected Context createJndiContext() throws Exception {
+        JndiContext answer = new JndiContext();
+        List<IoFilter> myFilters = new ArrayList<IoFilter>();
+        myFilters.add(new TestFilter());
+
+        answer.bind("myFilters", myFilters);
+        return answer;
+    }
+}
+
+class TestFilter extends IoFilterAdapter {
+
+    public static int called;
+
+    @Override
+    public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception
{
+        incCalled();
+        nextFilter.sessionCreated(session);
+    }
+
+    public static synchronized void incCalled() {
+        called++;
+    }
+}

Propchange: camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaFiltersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaFiltersTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message