/****************************************************************
 * 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 javax.net.ssl.SSLContext;

import org.apache.james.protocols.api.Protocol;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Generic NettyServer
 */
public class NettyServer extends AbstractAsyncServer {

    private Protocol protocol;
    
    private Logger logger = LoggerFactory.getLogger(NettyServer.class);

    protected SSLContext context;

    private ExecutionHandler eHandler;
    
    private ChannelUpstreamHandler coreHandler;

    public NettyServer(Protocol protocol) {
        this(protocol, null);
    }
    
    
    public NettyServer(Protocol protocol, SSLContext context) {
        super();
        this.protocol = protocol;
        this.context = context;
    }
    
    protected ExecutionHandler createExecutionHandler(int size) {
        return new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(size, 0, 0));
    }
    
    
    public void setUseExecutionHandler(boolean useHandler, int size) {
        if (isBound()) throw new IllegalStateException("Server running already");
        if (useHandler) {
            eHandler =createExecutionHandler(size);
        } else {
            if (eHandler != null) {
                eHandler.releaseExternalResources();
            }
            eHandler = null;
        }
    }
    
  
    protected ChannelUpstreamHandler createCoreHandler() {
        return coreHandler;
    }
    
    @Override
    public synchronized void bind() throws Exception {
        coreHandler = new BasicChannelUpstreamHandler(protocol, logger, context, null);
        super.bind();
    }


    @Override
    protected ChannelPipelineFactory createPipelineFactory(ChannelGroup group) {
        return new AbstractSSLAwareChannelPipelineFactory(getTimeout(), 0, getBacklog(), group, eHandler) {

            @Override
            protected ChannelUpstreamHandler createHandler() {
                return coreHandler;
            }

            @Override
            protected boolean isSSLSocket() {
                return context != null && !protocol.isStartTLSSupported();
            }

            @Override
            protected SSLContext getSSLContext() {
                return context;
            }
        };

    }

}

@@ -1757,21 +1757,23 @@
 %0A%0A    pr
-ivate
+otected
  Protoco
@@ -1795,21 +1795,23 @@
  %0A    pr
-ivate
+otected
  Logger 
@@ -2614,16 +2614,17 @@
 andler =
+ 
 createEx
@@ -2888,35 +2888,88 @@
      return 
-coreHandler
+new BasicChannelUpstreamHandler(protocol, logger, context, null)
 ;%0A    %7D%0A    
@@ -3064,71 +3064,26 @@
 r = 
-new BasicChannelUpstreamHandler(protocol, logger, context, null
+createCoreHandler(
 );%0A 

use createCoreHandler method the right way