001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.transport.nhttp;
021:
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.commons.logging.Log;
024: import org.apache.http.params.HttpConnectionParams;
025:
026: import java.util.Properties;
027: import java.io.IOException;
028: import java.net.URL;
029:
030: /**
031: * Store and manage properties that tune the nhttp transport
032: */
033: public class NHttpConfiguration {
034:
035: // defaults
036: private static final int WORKERS_CORE_THREADS = 20;
037: private static final int WORKERS_MAX_THREADS = 100;
038: private static final int WORKER_KEEP_ALIVE = 5;
039: private static final int BLOCKING_QUEUE_LENGTH = -1;
040: private static final int IO_WORKER_COUNT = 2;
041: private static final int BUFFER_SIZE = 2048;
042:
043: // server listener
044: private static final String S_T_CORE = "snd_t_core";
045: private static final String S_T_MAX = "snd_t_max";
046: private static final String S_T_ALIVE = "snd_alive_sec";
047: private static final String S_T_QLEN = "snd_qlen";
048: private static final String S_IO_WORKERS = "snd_io_threads";
049:
050: // client sender
051: private static final String C_T_CORE = "lst_t_core";
052: private static final String C_T_MAX = "lst_t_max";
053: private static final String C_T_ALIVE = "lst_alive_sec";
054: private static final String C_T_QLEN = "lst_qlen";
055: private static final String C_IO_WORKERS = "lst_io_threads";
056:
057: // general
058: private static final String G_BUFFER_SIZE = "nhttp_buffer_size";
059:
060: private static final Log log = LogFactory
061: .getLog(NHttpConfiguration.class);
062: private static NHttpConfiguration _instance = new NHttpConfiguration();
063: private Properties props = new Properties();
064:
065: private NHttpConfiguration() {
066: try {
067: props.load(getClass().getClassLoader().getResourceAsStream(
068: "nhttp.properties"));
069: } catch (Exception ignore) {
070: }
071: }
072:
073: public static NHttpConfiguration getInstance() {
074: return _instance;
075: }
076:
077: public int getServerCoreThreads() {
078: return getProperty(S_T_CORE, WORKERS_CORE_THREADS);
079: }
080:
081: public int getServerMaxThreads() {
082: return getProperty(S_T_MAX, WORKERS_MAX_THREADS);
083: }
084:
085: public int getServerKeepalive() {
086: return getProperty(S_T_ALIVE, WORKER_KEEP_ALIVE);
087: }
088:
089: public int getServerQueueLen() {
090: return getProperty(S_T_QLEN, BLOCKING_QUEUE_LENGTH);
091: }
092:
093: public int getServerIOWorkers() {
094: return getProperty(S_IO_WORKERS, IO_WORKER_COUNT);
095: }
096:
097: public int getClientCoreThreads() {
098: return getProperty(C_T_CORE, WORKERS_CORE_THREADS);
099: }
100:
101: public int getClientMaxThreads() {
102: return getProperty(C_T_MAX, WORKERS_MAX_THREADS);
103: }
104:
105: public int getClientKeepalive() {
106: return getProperty(C_T_ALIVE, WORKER_KEEP_ALIVE);
107: }
108:
109: public int getClientQueueLen() {
110: return getProperty(C_T_QLEN, BLOCKING_QUEUE_LENGTH);
111: }
112:
113: public int getClientIOWorkers() {
114: return getProperty(C_IO_WORKERS, IO_WORKER_COUNT);
115: }
116:
117: public int getBufferZise() {
118: return getProperty(G_BUFFER_SIZE, BUFFER_SIZE);
119: }
120:
121: /**
122: * Get properties that tune nhttp transport. Preference to system properties
123: * @param name name of the system/config property
124: * @param def default value to return if the property is not set
125: * @return the value of the property to be used
126: */
127: public int getProperty(String name, int def) {
128: String val = System.getProperty(name);
129: if (val == null) {
130: val = props.getProperty(name);
131: }
132:
133: if (val != null && Integer.valueOf(val).intValue() > 0) {
134: if (log.isDebugEnabled()) {
135: log.debug("Using nhttp tuning parameter : " + name
136: + " = " + val);
137: }
138: return Integer.valueOf(val).intValue();
139: }
140: return def;
141: }
142:
143: }
|