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  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.mina.util.ExpirationListener;
27  import org.apache.mina.util.ExpiringMap;
28  
29  /**
30   * An {@link IoSessionRecycler} with sessions that time out on inactivity.
31   *
32   * TODO Document me.
33   *
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 713957 $, $Date: 2008-11-14 10:27:16 +0100 (Fri, 14 Nov 2008) $
36   * @org.apache.xbean.XBean
37   */
38  public class ExpiringSessionRecycler implements IoSessionRecycler {
39      private ExpiringMap<Object, IoSession> sessionMap;
40  
41      private ExpiringMap<Object, IoSession>.Expirer mapExpirer;
42  
43      public ExpiringSessionRecycler() {
44          this(ExpiringMap.DEFAULT_TIME_TO_LIVE);
45      }
46  
47      public ExpiringSessionRecycler(int timeToLive) {
48          this(timeToLive, ExpiringMap.DEFAULT_EXPIRATION_INTERVAL);
49      }
50  
51      public ExpiringSessionRecycler(int timeToLive, int expirationInterval) {
52          sessionMap = new ExpiringMap<Object, IoSession>(timeToLive,
53                  expirationInterval);
54          mapExpirer = sessionMap.getExpirer();
55          sessionMap.addExpirationListener(new DefaultExpirationListener());
56      }
57  
58      public void put(IoSession session) {
59          mapExpirer.startExpiringIfNotStarted();
60  
61          Object key = generateKey(session);
62  
63          if (!sessionMap.containsKey(key)) {
64              sessionMap.put(key, session);
65          }
66      }
67  
68      public IoSession recycle(SocketAddress localAddress,
69              SocketAddress remoteAddress) {
70          return sessionMap.get(generateKey(localAddress, remoteAddress));
71      }
72  
73      public void remove(IoSession session) {
74          sessionMap.remove(generateKey(session));
75      }
76  
77      public void stopExpiring() {
78          mapExpirer.stopExpiring();
79      }
80  
81      public int getExpirationInterval() {
82          return sessionMap.getExpirationInterval();
83      }
84  
85      public int getTimeToLive() {
86          return sessionMap.getTimeToLive();
87      }
88  
89      public void setExpirationInterval(int expirationInterval) {
90          sessionMap.setExpirationInterval(expirationInterval);
91      }
92  
93      public void setTimeToLive(int timeToLive) {
94          sessionMap.setTimeToLive(timeToLive);
95      }
96  
97      private Object generateKey(IoSession session) {
98          return generateKey(session.getLocalAddress(), session
99                  .getRemoteAddress());
100     }
101 
102     private Object generateKey(SocketAddress localAddress,
103             SocketAddress remoteAddress) {
104         List<SocketAddress> key = new ArrayList<SocketAddress>(2);
105         key.add(remoteAddress);
106         key.add(localAddress);
107         return key;
108     }
109 
110     private class DefaultExpirationListener implements
111             ExpirationListener<IoSession> {
112         public void expired(IoSession expiredSession) {
113             expiredSession.close(true);
114         }
115     }
116 }