View Javadoc

1   /*
2    *   @(#) $Id: BaseSessionConfig.java 210062 2005-07-11 03:52:38Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.common;
20  
21  import org.apache.mina.common.IdleStatus;
22  import org.apache.mina.common.SessionConfig;
23  
24  /***
25   * Base implementation of {@link SessionConfig}s.
26   * 
27   * @author Trustin Lee (trustin@apache.org)
28   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
29   */
30  public abstract class BaseSessionConfig implements SessionConfig
31  {
32      private int idleTimeForRead;
33  
34      private int idleTimeForWrite;
35  
36      private int idleTimeForBoth;
37  
38      private int writeTimeout;
39  
40      protected BaseSessionConfig()
41      {
42      }
43  
44      public int getIdleTime( IdleStatus status )
45      {
46          if( status == IdleStatus.BOTH_IDLE )
47              return idleTimeForBoth;
48  
49          if( status == IdleStatus.READER_IDLE )
50              return idleTimeForRead;
51  
52          if( status == IdleStatus.WRITER_IDLE )
53              return idleTimeForWrite;
54  
55          throw new IllegalArgumentException( "Unknown idle status: " + status );
56      }
57  
58      public long getIdleTimeInMillis( IdleStatus status )
59      {
60          return getIdleTime( status ) * 1000L;
61      }
62  
63      public void setIdleTime( IdleStatus status, int idleTime )
64      {
65          if( idleTime < 0 )
66              throw new IllegalArgumentException( "Illegal idle time: "
67                                                  + idleTime );
68  
69          if( status == IdleStatus.BOTH_IDLE )
70              idleTimeForBoth = idleTime;
71          else if( status == IdleStatus.READER_IDLE )
72              idleTimeForRead = idleTime;
73          else if( status == IdleStatus.WRITER_IDLE )
74              idleTimeForWrite = idleTime;
75          else
76              throw new IllegalArgumentException( "Unknown idle status: "
77                                                  + status );
78      }
79  
80      public int getWriteTimeout()
81      {
82          return writeTimeout;
83      }
84  
85      public long getWriteTimeoutInMillis()
86      {
87          return writeTimeout * 1000L;
88      }
89  
90      public void setWriteTimeout( int writeTimeout )
91      {
92          if( writeTimeout < 0 )
93              throw new IllegalArgumentException( "Illegal write timeout: "
94                                                  + writeTimeout );
95          this.writeTimeout = writeTimeout;
96      }
97  }