001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.core.session; 021 022import java.net.SocketAddress; 023 024import org.apache.mina.util.ExpirationListener; 025import org.apache.mina.util.ExpiringMap; 026 027/** 028 * An {@link IoSessionRecycler} with sessions that time out on inactivity. 029 * 030 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 031 * @org.apache.xbean.XBean 032 */ 033public class ExpiringSessionRecycler implements IoSessionRecycler { 034 /** A map used to store the session */ 035 private ExpiringMap<SocketAddress, IoSession> sessionMap; 036 037 private ExpiringMap<SocketAddress, IoSession>.Expirer mapExpirer; 038 039 public ExpiringSessionRecycler() { 040 this(ExpiringMap.DEFAULT_TIME_TO_LIVE); 041 } 042 043 public ExpiringSessionRecycler(int timeToLive) { 044 this(timeToLive, ExpiringMap.DEFAULT_EXPIRATION_INTERVAL); 045 } 046 047 public ExpiringSessionRecycler(int timeToLive, int expirationInterval) { 048 sessionMap = new ExpiringMap<SocketAddress, IoSession>(timeToLive, expirationInterval); 049 mapExpirer = sessionMap.getExpirer(); 050 sessionMap.addExpirationListener(new DefaultExpirationListener()); 051 } 052 053 /** 054 * {@inheritDoc} 055 */ 056 public void put(IoSession session) { 057 mapExpirer.startExpiringIfNotStarted(); 058 059 SocketAddress key = session.getRemoteAddress(); 060 061 if (!sessionMap.containsKey(key)) { 062 sessionMap.put(key, session); 063 } 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 public IoSession recycle(SocketAddress remoteAddress) { 070 return sessionMap.get(remoteAddress); 071 } 072 073 /** 074 * {@inheritDoc} 075 */ 076 public void remove(IoSession session) { 077 sessionMap.remove(session.getRemoteAddress()); 078 } 079 080 public void stopExpiring() { 081 mapExpirer.stopExpiring(); 082 } 083 084 public int getExpirationInterval() { 085 return sessionMap.getExpirationInterval(); 086 } 087 088 public int getTimeToLive() { 089 return sessionMap.getTimeToLive(); 090 } 091 092 public void setExpirationInterval(int expirationInterval) { 093 sessionMap.setExpirationInterval(expirationInterval); 094 } 095 096 public void setTimeToLive(int timeToLive) { 097 sessionMap.setTimeToLive(timeToLive); 098 } 099 100 private class DefaultExpirationListener implements ExpirationListener<IoSession> { 101 public void expired(IoSession expiredSession) { 102 expiredSession.closeNow(); 103 } 104 } 105}