View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.common;
21  
22  
23  /**
24   * A base implementation of {@link IoSessionConfig}.
25   *
26   * @author The Apache MINA Project (dev@mina.apache.org)
27   * @version $Rev: 597940 $, $Date: 2007-11-24 18:00:09 -0700 (Sat, 24 Nov 2007) $
28   */
29  public abstract class AbstractIoSessionConfig implements IoSessionConfig {
30  
31      private int minReadBufferSize = 64;
32      private int readBufferSize = 2048;
33      private int maxReadBufferSize = 65536;
34      private int idleTimeForRead;
35      private int idleTimeForWrite;
36      private int idleTimeForBoth;
37      private int writeTimeout = 60;
38      private boolean useReadOperation;
39      private int throughputCalculationInterval = 3;
40  
41      protected AbstractIoSessionConfig() {
42      }
43  
44      public final void setAll(IoSessionConfig config) {
45          if (config == null) {
46              throw new NullPointerException("config");
47          }
48  
49          setReadBufferSize(config.getReadBufferSize());
50          setMinReadBufferSize(config.getMinReadBufferSize());
51          setMaxReadBufferSize(config.getMaxReadBufferSize());
52          setIdleTime(IdleStatus.BOTH_IDLE, config.getIdleTime(IdleStatus.BOTH_IDLE));
53          setIdleTime(IdleStatus.READER_IDLE, config.getIdleTime(IdleStatus.READER_IDLE));
54          setIdleTime(IdleStatus.WRITER_IDLE, config.getIdleTime(IdleStatus.WRITER_IDLE));
55          setWriteTimeout(config.getWriteTimeout());
56          setUseReadOperation(config.isUseReadOperation());
57          setThroughputCalculationInterval(config.getThroughputCalculationInterval());
58  
59          doSetAll(config);
60      }
61  
62      /**
63       * Implement this method to set all transport-specific configuration
64       * properties retrieved from the specified <tt>config</tt>.
65       */
66      protected abstract void doSetAll(IoSessionConfig config);
67  
68      public int getReadBufferSize() {
69          return readBufferSize;
70      }
71  
72      public void setReadBufferSize(int readBufferSize) {
73          if (readBufferSize <= 0) {
74              throw new IllegalArgumentException("readBufferSize: " + readBufferSize + " (expected: 1+)");
75          }
76          this.readBufferSize = readBufferSize;
77      }
78  
79      public int getMinReadBufferSize() {
80          return minReadBufferSize;
81      }
82  
83      public void setMinReadBufferSize(int minReadBufferSize) {
84          if (minReadBufferSize <= 0) {
85              throw new IllegalArgumentException("minReadBufferSize: " + minReadBufferSize + " (expected: 1+)");
86          }
87          if (minReadBufferSize > maxReadBufferSize ) {
88              throw new IllegalArgumentException("minReadBufferSize: " + minReadBufferSize + " (expected: smaller than " + maxReadBufferSize + ')');
89  
90          }
91          this.minReadBufferSize = minReadBufferSize;
92      }
93  
94      public int getMaxReadBufferSize() {
95          return maxReadBufferSize;
96      }
97  
98      public void setMaxReadBufferSize(int maxReadBufferSize) {
99          if (maxReadBufferSize <= 0) {
100             throw new IllegalArgumentException("maxReadBufferSize: " + maxReadBufferSize + " (expected: 1+)");
101         }
102 
103         if (maxReadBufferSize < minReadBufferSize) {
104             throw new IllegalArgumentException("maxReadBufferSize: " + maxReadBufferSize + " (expected: greater than " + minReadBufferSize + ')');
105 
106         }
107         this.maxReadBufferSize = maxReadBufferSize;
108     }
109 
110     public int getIdleTime(IdleStatus status) {
111         if (status == IdleStatus.BOTH_IDLE) {
112             return idleTimeForBoth;
113         }
114 
115         if (status == IdleStatus.READER_IDLE) {
116             return idleTimeForRead;
117         }
118 
119         if (status == IdleStatus.WRITER_IDLE) {
120             return idleTimeForWrite;
121         }
122 
123         throw new IllegalArgumentException("Unknown idle status: " + status);
124     }
125 
126     public long getIdleTimeInMillis(IdleStatus status) {
127         return getIdleTime(status) * 1000L;
128     }
129 
130     public void setIdleTime(IdleStatus status, int idleTime) {
131         if (idleTime < 0) {
132             throw new IllegalArgumentException("Illegal idle time: " + idleTime);
133         }
134 
135         if (status == IdleStatus.BOTH_IDLE) {
136             idleTimeForBoth = idleTime;
137         } else if (status == IdleStatus.READER_IDLE) {
138             idleTimeForRead = idleTime;
139         } else if (status == IdleStatus.WRITER_IDLE) {
140             idleTimeForWrite = idleTime;
141         } else {
142             throw new IllegalArgumentException("Unknown idle status: " + status);
143         }
144     }
145     
146     public final int getBothIdleTime() {
147         return getIdleTime(IdleStatus.BOTH_IDLE);
148     }
149 
150     public final long getBothIdleTimeInMillis() {
151         return getIdleTimeInMillis(IdleStatus.BOTH_IDLE);
152     }
153 
154     public final int getReaderIdleTime() {
155         return getIdleTime(IdleStatus.READER_IDLE);
156     }
157 
158     public final long getReaderIdleTimeInMillis() {
159         return getIdleTimeInMillis(IdleStatus.READER_IDLE);
160     }
161 
162     public final int getWriterIdleTime() {
163         return getIdleTime(IdleStatus.WRITER_IDLE);
164     }
165 
166     public final long getWriterIdleTimeInMillis() {
167         return getIdleTimeInMillis(IdleStatus.WRITER_IDLE);
168     }
169     
170     public void setBothIdleTime(int idleTime) {
171         setIdleTime(IdleStatus.BOTH_IDLE, idleTime);
172     }
173 
174     public void setReaderIdleTime(int idleTime) {
175         setIdleTime(IdleStatus.READER_IDLE, idleTime);
176     }
177 
178     public void setWriterIdleTime(int idleTime) {
179         setIdleTime(IdleStatus.WRITER_IDLE, idleTime);
180     }
181 
182     public int getWriteTimeout() {
183         return writeTimeout;
184     }
185 
186     public long getWriteTimeoutInMillis() {
187         return writeTimeout * 1000L;
188     }
189 
190     public void setWriteTimeout(int writeTimeout) {
191         if (writeTimeout < 0) {
192             throw new IllegalArgumentException("Illegal write timeout: "
193                     + writeTimeout);
194         }
195         this.writeTimeout = writeTimeout;
196     }
197 
198     public boolean isUseReadOperation() {
199         return useReadOperation;
200     }
201 
202     public void setUseReadOperation(boolean useReadOperation) {
203         this.useReadOperation = useReadOperation;
204     }
205 
206     public int getThroughputCalculationInterval() {
207         return throughputCalculationInterval;
208     }
209 
210     public void setThroughputCalculationInterval(int throughputCalculationInterval) {
211         if (throughputCalculationInterval < 0) {
212             throw new IllegalArgumentException(
213                     "throughputCalculationInterval: " + throughputCalculationInterval);
214         }
215 
216         this.throughputCalculationInterval = throughputCalculationInterval;
217     }
218     
219     public long getThroughputCalculationIntervalInMillis() {
220         return throughputCalculationInterval * 1000L;
221     }
222 }