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.mina.common;
021:
022: /**
023: * A base implementation of {@link IoSessionConfig}.
024: *
025: * @author The Apache MINA Project (dev@mina.apache.org)
026: * @version $Rev: 597940 $, $Date: 2007-11-24 18:00:09 -0700 (Sat, 24 Nov 2007) $
027: */
028: public abstract class AbstractIoSessionConfig implements
029: IoSessionConfig {
030:
031: private int minReadBufferSize = 64;
032: private int readBufferSize = 2048;
033: private int maxReadBufferSize = 65536;
034: private int idleTimeForRead;
035: private int idleTimeForWrite;
036: private int idleTimeForBoth;
037: private int writeTimeout = 60;
038: private boolean useReadOperation;
039: private int throughputCalculationInterval = 3;
040:
041: protected AbstractIoSessionConfig() {
042: }
043:
044: public final void setAll(IoSessionConfig config) {
045: if (config == null) {
046: throw new NullPointerException("config");
047: }
048:
049: setReadBufferSize(config.getReadBufferSize());
050: setMinReadBufferSize(config.getMinReadBufferSize());
051: setMaxReadBufferSize(config.getMaxReadBufferSize());
052: setIdleTime(IdleStatus.BOTH_IDLE, config
053: .getIdleTime(IdleStatus.BOTH_IDLE));
054: setIdleTime(IdleStatus.READER_IDLE, config
055: .getIdleTime(IdleStatus.READER_IDLE));
056: setIdleTime(IdleStatus.WRITER_IDLE, config
057: .getIdleTime(IdleStatus.WRITER_IDLE));
058: setWriteTimeout(config.getWriteTimeout());
059: setUseReadOperation(config.isUseReadOperation());
060: setThroughputCalculationInterval(config
061: .getThroughputCalculationInterval());
062:
063: doSetAll(config);
064: }
065:
066: /**
067: * Implement this method to set all transport-specific configuration
068: * properties retrieved from the specified <tt>config</tt>.
069: */
070: protected abstract void doSetAll(IoSessionConfig config);
071:
072: public int getReadBufferSize() {
073: return readBufferSize;
074: }
075:
076: public void setReadBufferSize(int readBufferSize) {
077: if (readBufferSize <= 0) {
078: throw new IllegalArgumentException("readBufferSize: "
079: + readBufferSize + " (expected: 1+)");
080: }
081: this .readBufferSize = readBufferSize;
082: }
083:
084: public int getMinReadBufferSize() {
085: return minReadBufferSize;
086: }
087:
088: public void setMinReadBufferSize(int minReadBufferSize) {
089: if (minReadBufferSize <= 0) {
090: throw new IllegalArgumentException("minReadBufferSize: "
091: + minReadBufferSize + " (expected: 1+)");
092: }
093: if (minReadBufferSize > maxReadBufferSize) {
094: throw new IllegalArgumentException("minReadBufferSize: "
095: + minReadBufferSize + " (expected: smaller than "
096: + maxReadBufferSize + ')');
097:
098: }
099: this .minReadBufferSize = minReadBufferSize;
100: }
101:
102: public int getMaxReadBufferSize() {
103: return maxReadBufferSize;
104: }
105:
106: public void setMaxReadBufferSize(int maxReadBufferSize) {
107: if (maxReadBufferSize <= 0) {
108: throw new IllegalArgumentException("maxReadBufferSize: "
109: + maxReadBufferSize + " (expected: 1+)");
110: }
111:
112: if (maxReadBufferSize < minReadBufferSize) {
113: throw new IllegalArgumentException("maxReadBufferSize: "
114: + maxReadBufferSize + " (expected: greater than "
115: + minReadBufferSize + ')');
116:
117: }
118: this .maxReadBufferSize = maxReadBufferSize;
119: }
120:
121: public int getIdleTime(IdleStatus status) {
122: if (status == IdleStatus.BOTH_IDLE) {
123: return idleTimeForBoth;
124: }
125:
126: if (status == IdleStatus.READER_IDLE) {
127: return idleTimeForRead;
128: }
129:
130: if (status == IdleStatus.WRITER_IDLE) {
131: return idleTimeForWrite;
132: }
133:
134: throw new IllegalArgumentException("Unknown idle status: "
135: + status);
136: }
137:
138: public long getIdleTimeInMillis(IdleStatus status) {
139: return getIdleTime(status) * 1000L;
140: }
141:
142: public void setIdleTime(IdleStatus status, int idleTime) {
143: if (idleTime < 0) {
144: throw new IllegalArgumentException("Illegal idle time: "
145: + idleTime);
146: }
147:
148: if (status == IdleStatus.BOTH_IDLE) {
149: idleTimeForBoth = idleTime;
150: } else if (status == IdleStatus.READER_IDLE) {
151: idleTimeForRead = idleTime;
152: } else if (status == IdleStatus.WRITER_IDLE) {
153: idleTimeForWrite = idleTime;
154: } else {
155: throw new IllegalArgumentException("Unknown idle status: "
156: + status);
157: }
158: }
159:
160: public final int getBothIdleTime() {
161: return getIdleTime(IdleStatus.BOTH_IDLE);
162: }
163:
164: public final long getBothIdleTimeInMillis() {
165: return getIdleTimeInMillis(IdleStatus.BOTH_IDLE);
166: }
167:
168: public final int getReaderIdleTime() {
169: return getIdleTime(IdleStatus.READER_IDLE);
170: }
171:
172: public final long getReaderIdleTimeInMillis() {
173: return getIdleTimeInMillis(IdleStatus.READER_IDLE);
174: }
175:
176: public final int getWriterIdleTime() {
177: return getIdleTime(IdleStatus.WRITER_IDLE);
178: }
179:
180: public final long getWriterIdleTimeInMillis() {
181: return getIdleTimeInMillis(IdleStatus.WRITER_IDLE);
182: }
183:
184: public void setBothIdleTime(int idleTime) {
185: setIdleTime(IdleStatus.BOTH_IDLE, idleTime);
186: }
187:
188: public void setReaderIdleTime(int idleTime) {
189: setIdleTime(IdleStatus.READER_IDLE, idleTime);
190: }
191:
192: public void setWriterIdleTime(int idleTime) {
193: setIdleTime(IdleStatus.WRITER_IDLE, idleTime);
194: }
195:
196: public int getWriteTimeout() {
197: return writeTimeout;
198: }
199:
200: public long getWriteTimeoutInMillis() {
201: return writeTimeout * 1000L;
202: }
203:
204: public void setWriteTimeout(int writeTimeout) {
205: if (writeTimeout < 0) {
206: throw new IllegalArgumentException(
207: "Illegal write timeout: " + writeTimeout);
208: }
209: this .writeTimeout = writeTimeout;
210: }
211:
212: public boolean isUseReadOperation() {
213: return useReadOperation;
214: }
215:
216: public void setUseReadOperation(boolean useReadOperation) {
217: this .useReadOperation = useReadOperation;
218: }
219:
220: public int getThroughputCalculationInterval() {
221: return throughputCalculationInterval;
222: }
223:
224: public void setThroughputCalculationInterval(
225: int throughputCalculationInterval) {
226: if (throughputCalculationInterval < 0) {
227: throw new IllegalArgumentException(
228: "throughputCalculationInterval: "
229: + throughputCalculationInterval);
230: }
231:
232: this .throughputCalculationInterval = throughputCalculationInterval;
233: }
234:
235: public long getThroughputCalculationIntervalInMillis() {
236: return throughputCalculationInterval * 1000L;
237: }
238: }
|