Author: norman
Date: Mon Aug  9 18:05:35 2010
New Revision: 983754

URL: http://svn.apache.org/viewvc?rev=983754&view=rev
Log:
Fix Race-Condition when using TimeoutHandler. TimeoutHandler is not thread-safe 
so it need to be a new instance per Pipeline (PROTOCOLS-5)

Modified:
    
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractChannelPipelineFactory.java
    
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/ChannelGroupHandler.java
    
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/TimeoutHandler.java

Modified: 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractChannelPipelineFactory.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractChannelPipelineFactory.java?rev=983754&r1=983753&r2=983754&view=diff
==============================================================================
--- 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractChannelPipelineFactory.java
 (original)
+++ 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractChannelPipelineFactory.java
 Mon Aug  9 18:05:35 2010
@@ -1,103 +1,105 @@
-/****************************************************************
- * 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.james.protocols.impl;
-
-import static org.jboss.netty.channel.Channels.pipeline;
-
-import org.apache.james.protocols.api.Response;
-import org.jboss.netty.channel.ChannelPipeline;
-import org.jboss.netty.channel.ChannelPipelineFactory;
-import org.jboss.netty.channel.ChannelUpstreamHandler;
-import org.jboss.netty.channel.group.ChannelGroup;
-import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
-import org.jboss.netty.handler.codec.frame.Delimiters;
-import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
-import org.jboss.netty.handler.connection.ConnectionLimitUpstreamHandler;
-import org.jboss.netty.handler.connection.ConnectionPerIpLimitUpstreamHandler;
-import org.jboss.netty.handler.stream.ChunkedWriteHandler;
-import org.jboss.netty.util.HashedWheelTimer;
-
-/**
- * Abstract base class for {...@link ChannelPipelineFactory} implementations
- * 
- *
- */
-public abstract class AbstractChannelPipelineFactory implements 
ChannelPipelineFactory{
-
-    public final static int MAX_LINE_LENGTH = 8192;
-    private final ConnectionLimitUpstreamHandler connectionLimitHandler;
-    private final ConnectionPerIpLimitUpstreamHandler 
connectionPerIpLimitHandler;
-    private TimeoutHandler timeoutHandler;
-    private ChannelGroupHandler groupHandler;
-    public AbstractChannelPipelineFactory(int timeout, int maxConnections, int 
maxConnectsPerIp, ChannelGroup channels) {
-        timeoutHandler = new TimeoutHandler(new HashedWheelTimer(), timeout);
-        connectionLimitHandler = new 
ConnectionLimitUpstreamHandler(maxConnections);
-        connectionPerIpLimitHandler = new 
ConnectionPerIpLimitUpstreamHandler(maxConnectsPerIp);
-        groupHandler = new ChannelGroupHandler(channels);
-    }
-    
-    
-    /*
-     * (non-Javadoc)
-     * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
-     */
-    public ChannelPipeline getPipeline() throws Exception {
-        // Create a default pipeline implementation.
-        ChannelPipeline pipeline = pipeline();
-        pipeline.addLast("groupHandler", groupHandler);
-
-        pipeline.addLast("connectionLimit", connectionLimitHandler);
-
-        pipeline.addLast("connectionPerIpLimit", connectionPerIpLimitHandler);
-
-        
-        // Add the text line decoder which limit the max line length, don't 
strip the delimiter and use CRLF as delimiter
-        pipeline.addLast("framer", new 
DelimiterBasedFrameDecoder(MAX_LINE_LENGTH, false, Delimiters.lineDelimiter()));
-       
-        // encoder
-        pipeline.addLast("encoderResponse", createEncoder());
-
-        pipeline.addLast("streamer", new ChunkedWriteHandler());
-        pipeline.addLast("timeoutHandler", timeoutHandler);
-        pipeline.addLast("coreHandler", createHandler());
-
-
-        return pipeline;
-    }
-
-
-
-    
-    /**
-     * Create the core {...@link ChannelUpstreamHandler} to use
-     * 
-     * @return coreHandeler
-     */
-    protected abstract ChannelUpstreamHandler createHandler();
-    
-    /**
-     * Create the {...@link Response} Encoder
-     * 
-     * @return encoder
-     */
-    protected abstract OneToOneEncoder createEncoder();
-    
-
-
-}
+/****************************************************************
+ * 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.james.protocols.impl;
+
+import static org.jboss.netty.channel.Channels.pipeline;
+
+import org.apache.james.protocols.api.Response;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.channel.ChannelPipelineFactory;
+import org.jboss.netty.channel.ChannelUpstreamHandler;
+import org.jboss.netty.channel.group.ChannelGroup;
+import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
+import org.jboss.netty.handler.codec.frame.Delimiters;
+import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
+import org.jboss.netty.handler.connection.ConnectionLimitUpstreamHandler;
+import org.jboss.netty.handler.connection.ConnectionPerIpLimitUpstreamHandler;
+import org.jboss.netty.handler.stream.ChunkedWriteHandler;
+import org.jboss.netty.util.HashedWheelTimer;
+
+/**
+ * Abstract base class for {...@link ChannelPipelineFactory} implementations
+ * 
+ *
+ */
+public abstract class AbstractChannelPipelineFactory implements 
ChannelPipelineFactory{
+
+    public final static int MAX_LINE_LENGTH = 8192;
+    private final ConnectionLimitUpstreamHandler connectionLimitHandler;
+    private final ConnectionPerIpLimitUpstreamHandler 
connectionPerIpLimitHandler;
+    private final HashedWheelTimer timer = new HashedWheelTimer();
+    private ChannelGroupHandler groupHandler;
+       private int timeout;
+    public AbstractChannelPipelineFactory(int timeout, int maxConnections, int 
maxConnectsPerIp, ChannelGroup channels) {
+        connectionLimitHandler = new 
ConnectionLimitUpstreamHandler(maxConnections);
+        connectionPerIpLimitHandler = new 
ConnectionPerIpLimitUpstreamHandler(maxConnectsPerIp);
+        groupHandler = new ChannelGroupHandler(channels);
+        this.timeout = timeout;
+    }
+    
+    
+    /*
+     * (non-Javadoc)
+     * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
+     */
+    public ChannelPipeline getPipeline() throws Exception {
+        // Create a default pipeline implementation.
+        ChannelPipeline pipeline = pipeline();
+        pipeline.addLast("groupHandler", groupHandler);
+
+        pipeline.addLast("connectionLimit", connectionLimitHandler);
+
+        pipeline.addLast("connectionPerIpLimit", connectionPerIpLimitHandler);
+
+        
+        // Add the text line decoder which limit the max line length, don't 
strip the delimiter and use CRLF as delimiter
+        pipeline.addLast("framer", new 
DelimiterBasedFrameDecoder(MAX_LINE_LENGTH, false, Delimiters.lineDelimiter()));
+       
+        // encoder
+        pipeline.addLast("encoderResponse", createEncoder());
+
+        pipeline.addLast("streamer", new ChunkedWriteHandler());
+        pipeline.addLast("timeoutHandler", new TimeoutHandler(timer, timeout));
+
+        pipeline.addLast("coreHandler", createHandler());
+
+
+        return pipeline;
+    }
+
+
+
+    
+    /**
+     * Create the core {...@link ChannelUpstreamHandler} to use
+     * 
+     * @return coreHandeler
+     */
+    protected abstract ChannelUpstreamHandler createHandler();
+    
+    /**
+     * Create the {...@link Response} Encoder
+     * 
+     * @return encoder
+     */
+    protected abstract OneToOneEncoder createEncoder();
+    
+
+
+}

Modified: 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/ChannelGroupHandler.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/ChannelGroupHandler.java?rev=983754&r1=983753&r2=983754&view=diff
==============================================================================
--- 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/ChannelGroupHandler.java
 (original)
+++ 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/ChannelGroupHandler.java
 Mon Aug  9 18:05:35 2010
@@ -25,7 +25,9 @@ import org.jboss.netty.channel.SimpleCha
 import org.jboss.netty.channel.group.ChannelGroup;
 
 /**
- * Add channels to the channel group after the channel was opened
+ * Add channels to the channel group after the channel was opened.
+ * 
+ * This handler is thread-safe and thus can be shared across pipelines
  *
  */
 public final class ChannelGroupHandler extends SimpleChannelUpstreamHandler {

Modified: 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/TimeoutHandler.java
URL: 
http://svn.apache.org/viewvc/james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/TimeoutHandler.java?rev=983754&r1=983753&r2=983754&view=diff
==============================================================================
--- 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/TimeoutHandler.java
 (original)
+++ 
james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/TimeoutHandler.java
 Mon Aug  9 18:05:35 2010
@@ -1,46 +1,46 @@
-/****************************************************************
- * 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.james.protocols.impl;
-
-import org.jboss.netty.channel.Channel;
-import org.jboss.netty.channel.ChannelHandlerContext;
-import org.jboss.netty.handler.timeout.IdleState;
-import org.jboss.netty.handler.timeout.IdleStateHandler;
-import org.jboss.netty.util.Timer;
-
-/**
- * {...@link IdleStateHandler} implementation which disconnect the {...@link 
Channel} after a configured
- * idle timeout
- *
- */
-public class TimeoutHandler extends IdleStateHandler{
-
-    public TimeoutHandler(Timer timer, int readerIdleTimeSeconds) {
-        super(timer, readerIdleTimeSeconds, 0, 0);
-    }
-
-    @Override
-    protected void channelIdle(ChannelHandlerContext ctx, IdleState state, 
long lastActivityTimeMillis) throws Exception {
-        if (state.equals(IdleState.READER_IDLE)) {
-            ctx.getChannel().close();
-        }
-    }
-
-
-}
+/****************************************************************
+ * 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.james.protocols.impl;
+
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.handler.timeout.IdleState;
+import org.jboss.netty.handler.timeout.IdleStateHandler;
+import org.jboss.netty.util.Timer;
+
+/**
+ * {...@link IdleStateHandler} implementation which disconnect the {...@link 
Channel} after a configured
+ * idle timeout. Be aware that this handle is not thread safe so it can't be 
shared across pipelines
+ *
+ */
+public class TimeoutHandler extends IdleStateHandler{
+
+    public TimeoutHandler(Timer timer, int readerIdleTimeSeconds) {
+        super(timer, readerIdleTimeSeconds, 0, 0);
+    }
+
+    @Override
+    protected void channelIdle(ChannelHandlerContext ctx, IdleState state, 
long lastActivityTimeMillis) throws Exception {
+        if (state.equals(IdleState.READER_IDLE)) {
+            ctx.getChannel().close();
+        }
+    }
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to