KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > common > ExpiringSessionRecycler


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 import java.net.SocketAddress JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
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 Directory Project (mina-dev@directory.apache.org)
35  * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13 7월 2007) $
36  */

37 public class ExpiringSessionRecycler implements IoSessionRecycler {
38     private ExpiringMap<Object JavaDoc, IoSession> sessionMap;
39
40     private ExpiringMap.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 JavaDoc, 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 JavaDoc key = generateKey(session);
61
62         if (!sessionMap.containsKey(key)) {
63             sessionMap.put(key, session);
64         }
65     }
66
67     public IoSession recycle(SocketAddress JavaDoc localAddress,
68             SocketAddress JavaDoc 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 JavaDoc generateKey(IoSession session) {
97         return generateKey(session.getLocalAddress(), session
98                 .getRemoteAddress());
99     }
100
101     private Object JavaDoc generateKey(SocketAddress JavaDoc localAddress,
102             SocketAddress JavaDoc remoteAddress) {
103         List JavaDoc<SocketAddress JavaDoc> key = new ArrayList JavaDoc<SocketAddress JavaDoc>(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();
113         }
114     }
115 }
116
Popular Tags