Package org.wso2.carbon.transport.relay.config

Source Code of org.wso2.carbon.transport.relay.config.BaseConfiguration

/**
*  Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  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.wso2.carbon.transport.relay.config;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.description.ParameterInclude;
import org.apache.axis2.transport.base.ParamUtils;
import org.apache.axis2.transport.base.threads.WorkerPool;
import org.apache.axis2.transport.base.threads.WorkerPoolFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.nio.params.NIOReactorPNames;
import org.apache.http.nio.util.HeapByteBufferAllocator;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.wso2.carbon.transport.relay.RelayConstants;
import org.wso2.carbon.transport.relay.jmx.RelayMetricsCollector;
import org.wso2.carbon.transport.relay.util.BufferFactory;

/**
* This class has common configurations for both sender and receiver.
*/
public abstract class BaseConfiguration {
    private Log log = LogFactory.getLog(BaseConfiguration.class);

    /** Core size of the thread pool */
    public static final String
            WORKER_PROCESSING_POOL_CORE_SIZE = "WorkerPoolCoreSize";
    /** Max number of threads in the pool */
    public static final String
            WORKER_PROCESSING_POOL_MAX_SIZE = "WorkerPoolMaxSize";
    /** Thread keep-alive time */
    public static final String
            WORKER_THREAD_KEEP_ALIVE_TIME = "WorkerThreadKeepAliveTime";
    /** Queue length */
    public static final String
            WORKER_PROCESSING_POOL_QUEUE_LENGTH = "WorkerQueueLength";

    public static final String PRESERVE_USER_AGENT_HEADER = "preserveUserAgentHeader";

    /** Defines weather synapse needs to preserve the original User-Agent header. */
    public static final String USER_AGENT_HEADER_PRESERVE = "http.user.agent.preserve";

    /** Defines weather synapse needs to preserve the original Server header. */
    public static final String SERVER_HEADER_PRESERVE = "http.server.preserve";

    /** Buffer size used */
    public static final String IO_BUFFER_SIZE = "IOBufferSize";

    /** The dispatcher thread */
    public static final String IO_THREAD_COUNT = "IOThreadCount";

    /** Number of dispatcher threads */
    private int iOThreadCount = 1;

    /** Configurations given by axis2.xml */
    protected ParameterInclude parameters = null;

    /** Size of the buffer */
    private int iOBufferSize = 8 * 1024;

    /** The thread pool for executing the messages through */
    private WorkerPool workerPool = null;

    /** The Axis2 Configuration Context */
    protected ConfigurationContext configurationContext = null;

    /** Default http parameters */
    protected HttpParams httpParameters = null;

    protected BufferFactory bufferFactory = null;

    private RelayMetricsCollector metrics = null;

    public BaseConfiguration(ConfigurationContext configurationContext,
                             ParameterInclude parameters,
                             WorkerPool workerPool) {
        this.parameters = parameters;
        this.workerPool = workerPool;
        this.configurationContext = configurationContext;
    }

    public void build() throws AxisFault {
        iOThreadCount = ParamUtils.getOptionalParamInt(parameters,
                IO_THREAD_COUNT, RelayConstants.DEFAULT_IO_THREAD_COUNT);

        iOBufferSize = ParamUtils.getOptionalParamInt(parameters, IO_BUFFER_SIZE, iOBufferSize);

        if (workerPool == null) {
            int core = ParamUtils.getOptionalParamInt(parameters,
                    BaseConfiguration.WORKER_PROCESSING_POOL_CORE_SIZE, 40);

            int max = ParamUtils.getOptionalParamInt(parameters,
                    BaseConfiguration.WORKER_PROCESSING_POOL_MAX_SIZE, 100);

            int keepAlive = ParamUtils.getOptionalParamInt(parameters,
                    BaseConfiguration.WORKER_THREAD_KEEP_ALIVE_TIME, 60);

            int queueLength = ParamUtils.getOptionalParamInt(parameters,
                    BaseConfiguration.WORKER_PROCESSING_POOL_QUEUE_LENGTH, -1);

            workerPool = WorkerPoolFactory.getWorkerPool(core, max, keepAlive,
                    queueLength, "Message Processing Thread Group", "MessageProcessor");
        }

        httpParameters = retrieveHttpParameters();

        bufferFactory = new BufferFactory(1024 * 8, new HeapByteBufferAllocator(), 512);
    }

    protected int getParameterIntValue(String param, int def) throws AxisFault {
        Parameter parameter = parameters.getParameter(param);
        if (parameter != null) {
            try {
                return Integer.parseInt(parameter.getValue().toString());
            } catch (NumberFormatException e) {
                String msg = "Invalud value specified for parameter: "
                        + param + ". Expected a number";
                log.error(msg);
                throw new AxisFault(msg);
            }
        }

        return def;
    }

    public String getParameterStringValue(String param, String def) {
        Parameter parameter = parameters.getParameter(param);
        if (parameter != null) {
            return parameter.getValue().toString();
        }

        return def;
    }

    public boolean getParameterBooleanValue(String param, boolean def) {
        Parameter parameter = parameters.getParameter(param);
        if (parameter != null) {
            return Boolean.parseBoolean(parameter.getValue().toString());
        }

        return def;
    }

    public int getIOThreadCount() {
        return iOThreadCount;
    }

    public int getIOBufferSize() {
        return iOBufferSize;
    }

    public WorkerPool getWorkerPool() {
        return workerPool;
    }

    public ConfigurationContext getConfigurationContext() {
        return configurationContext;
    }

    protected HttpParams retrieveHttpParameters() throws AxisFault {
        HttpParams params = new BasicHttpParams();
        params
            .setIntParameter(HttpConnectionParams.SO_TIMEOUT,
                getParameterIntValue(HttpConnectionParams.SO_TIMEOUT, 60000))
            .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
                getParameterIntValue(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024))
            .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK,
                getParameterIntValue(HttpConnectionParams.STALE_CONNECTION_CHECK, 0) == 1)
            .setBooleanParameter(HttpConnectionParams.TCP_NODELAY,
                getParameterIntValue(HttpConnectionParams.TCP_NODELAY, 1) == 1)
            .setParameter(HttpProtocolParams.ORIGIN_SERVER,
               getParameterStringValue(HttpProtocolParams.ORIGIN_SERVER,
                       "WSO2-Relay-NIO"));

        if (getParameterBooleanValue(NIOReactorPNames.INTEREST_OPS_QUEUEING, false)) {
            params.setBooleanParameter(NIOReactorPNames.INTEREST_OPS_QUEUEING, true);
        }

        return params;
    }

    public BufferFactory getBufferFactory() {
        return bufferFactory;
    }

    public RelayMetricsCollector getMetrics() {
        return metrics;
    }

    public void setMetrics(RelayMetricsCollector metrics) {
        this.metrics = metrics;
    }
}
TOP

Related Classes of org.wso2.carbon.transport.relay.config.BaseConfiguration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.