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.core.session;
21  
22  import java.net.SocketAddress;
23  
24  import org.apache.mina.util.ExpirationListener;
25  import org.apache.mina.util.ExpiringMap;
26  
27  /**
28   * An {@link IoSessionRecycler} with sessions that time out on inactivity.
29   *
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   * @org.apache.xbean.XBean
32   */
33  public class ExpiringSessionRecycler implements IoSessionRecycler {
34      /** A map used to store the session */
35      private ExpiringMap<SocketAddress, IoSession> sessionMap;
36  
37      private ExpiringMap<SocketAddress, IoSession>.Expirer mapExpirer;
38  
39      public ExpiringSessionRecycler() {
40          this(ExpiringMap.DEFAULT_TIME_TO_LIVE);
41      }
42  
43      public ExpiringSessionRecycler(int timeToLive) {
44          this(timeToLive, ExpiringMap.DEFAULT_EXPIRATION_INTERVAL);
45      }
46  
47      public ExpiringSessionRecycler(int timeToLive, int expirationInterval) {
48          sessionMap = new ExpiringMap<SocketAddress, IoSession>(timeToLive, expirationInterval);
49          mapExpirer = sessionMap.getExpirer();
50          sessionMap.addExpirationListener(new DefaultExpirationListener());
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public void put(IoSession session) {
57          mapExpirer.startExpiringIfNotStarted();
58  
59          SocketAddress key = session.getRemoteAddress();
60  
61          if (!sessionMap.containsKey(key)) {
62              sessionMap.put(key, session);
63          }
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public IoSession recycle(SocketAddress remoteAddress) {
70          return sessionMap.get(remoteAddress);
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      public void remove(IoSession session) {
77          sessionMap.remove(session.getRemoteAddress());
78      }
79  
80      public void stopExpiring() {
81          mapExpirer.stopExpiring();
82      }
83  
84      public int getExpirationInterval() {
85          return sessionMap.getExpirationInterval();
86      }
87  
88      public int getTimeToLive() {
89          return sessionMap.getTimeToLive();
90      }
91  
92      public void setExpirationInterval(int expirationInterval) {
93          sessionMap.setExpirationInterval(expirationInterval);
94      }
95  
96      public void setTimeToLive(int timeToLive) {
97          sessionMap.setTimeToLive(timeToLive);
98      }
99  
100     private class DefaultExpirationListener implements ExpirationListener<IoSession> {
101         public void expired(IoSession expiredSession) {
102             expiredSession.close(true);
103         }
104     }
105 }