/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed 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.activemq.transport.ember;

import pyrasun.eio.EIOEvent;
import pyrasun.eio.EIOEventDescriptor;
import pyrasun.eio.EIOGlobalContext;
import pyrasun.eio.EIOPoolingStrategy;
import pyrasun.eio.services.EmberServiceController;
import pyrasun.eio.services.EmberServiceControllerImpl;

import javax.jms.JMSException;
import java.io.IOException;

/**
 * An abstract base class useful for implementation inheritence
 * 
 * @version $Revision: 1.1 $
 */
public class EmberSupport {

    private EIOGlobalContext context;
    private String key = "emberIo";
    private int maxEm = 1;
    private EmberServiceControllerImpl controller;
    private EIOPoolingStrategy ioPoolingStrategy;

    /**
     * TODO when EmberIO can support flushing when in NIO mode
     */
    //private String poolingStrategyName = "USE_RWP_POOLS";

    private String poolingStrategyName = "SELECTOR_READER";

    public EmberSupport() {
    }

    public EmberSupport(EIOGlobalContext context, EIOPoolingStrategy ioPoolingStrategy) {
        this.context = context;
        this.ioPoolingStrategy = ioPoolingStrategy;
    }

    public EIOGlobalContext getContext() throws IOException {
        if (context == null) {
            context = new EIOGlobalContext(maxEm, key);
        }
        return context;
    }

    public String getKey() {
        return key;
    }

    protected EmberServiceController getController() throws IOException {
        if (controller == null) {
            controller = new EmberServiceControllerImpl(getContext());
        }
        return controller;
    }

    protected EIOPoolingStrategy getIoPoolingStrategy() {
        if (ioPoolingStrategy == null) {
            ioPoolingStrategy = getPoolingStrategyByName(poolingStrategyName);
        }
        return ioPoolingStrategy;
    }

    protected EIOPoolingStrategy getPoolingStrategyByName(String name) {
        EIOPoolingStrategy strategy = EIOPoolingStrategy.getStrategyByName(name);

        // lets default some pool sizes
        EIOEventDescriptor evRead = strategy.getEventDescriptor(EIOEvent.READ);
        evRead.setPoolSize(1);

        EIOEventDescriptor evWrite = strategy.getEventDescriptor(EIOEvent.WRITE);
        evWrite.setPoolSize(1);

        EIOEventDescriptor evProcess = strategy.getEventDescriptor(EIOEvent.PROCESS);
        evProcess.setPoolSize(1);
        return strategy;
    }

    /**
     * Factory method to create a JMSException which is linked to the base exception
     */
    protected JMSException createJMSException(String message, Exception ex) {
        JMSException jmsEx = new JMSException(message + ex.getMessage());
        jmsEx.setLinkedException(ex);
        return jmsEx;
    }

}