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   * @org.apache.xbean.XBean
36   */
37  public class ExpiringSessionRecycler implements IoSessionRecycler {
38      private ExpiringMap<Object, IoSession> sessionMap;
39  
40      private ExpiringMap<Object, IoSession>.Expirer mapExpirer;
41  
42      public ExpiringSessionRecycler() {
43          this(ExpiringMap.DEFAULT_TIME_TO_LIVE);
44      }
45  
46      public ExpiringSessionRecycler(int timeToLive) {
47          this(timeToLive, ExpiringMap.DEFAULT_EXPIRATION_INTERVAL);
48      }
49  
50      public ExpiringSessionRecycler(int timeToLive, int expirationInterval) {
51          sessionMap = new ExpiringMap<Object, IoSession>(timeToLive,
52                  expirationInterval);
53          mapExpirer = sessionMap.getExpirer();
54          sessionMap.addExpirationListener(new DefaultExpirationListener());
55      }
56  
57      public void put(IoSession session) {
58          mapExpirer.startExpiringIfNotStarted();
59  
60          Object key = generateKey(session);
61  
62          if (!sessionMap.containsKey(key)) {
63              sessionMap.put(key, session);
64          }
65      }
66  
67      public IoSession recycle(SocketAddress localAddress,
68              SocketAddress remoteAddress) {
69          return sessionMap.get(generateKey(localAddress, remoteAddress));
70      }
71  
72      public void remove(IoSession session) {
73          sessionMap.remove(generateKey(session));
74      }
75  
76      public void stopExpiring() {
77          mapExpirer.stopExpiring();
78      }
79  
80      public int getExpirationInterval() {
81          return sessionMap.getExpirationInterval();
82      }
83  
84      public int getTimeToLive() {
85          return sessionMap.getTimeToLive();
86      }
87  
88      public void setExpirationInterval(int expirationInterval) {
89          sessionMap.setExpirationInterval(expirationInterval);
90      }
91  
92      public void setTimeToLive(int timeToLive) {
93          sessionMap.setTimeToLive(timeToLive);
94      }
95  
96      private Object generateKey(IoSession session) {
97          return generateKey(session.getLocalAddress(), session
98                  .getRemoteAddress());
99      }
100 
101     private Object generateKey(SocketAddress localAddress,
102             SocketAddress remoteAddress) {
103         List<SocketAddress> key = new ArrayList<SocketAddress>(2);
104         key.add(remoteAddress);
105         key.add(localAddress);
106         return key;
107     }
108 
109     private class DefaultExpirationListener implements
110             ExpirationListener<IoSession> {
111         public void expired(IoSession expiredSession) {
112             expiredSession.close(true);
113         }
114     }
115 }