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   * TODO Document me.
    31   *
    32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    33   * @org.apache.xbean.XBean
    34   */
    35  public class ExpiringSessionRecycler implements IoSessionRecycler {
    36      private ExpiringMap<SocketAddress, IoSession> sessionMap;
    37
    38      private ExpiringMap<SocketAddress, IoSession>.Expirer mapExpirer;
    39
    40      public ExpiringSessionRecycler() {
    41          this(ExpiringMap.DEFAULT_TIME_TO_LIVE);
    42      }
    43
    44      public ExpiringSessionRecycler(int timeToLive) {
    45          this(timeToLive, ExpiringMap.DEFAULT_EXPIRATION_INTERVAL);
    46      }
    47
    48      public ExpiringSessionRecycler(int timeToLive, int expirationInterval) {
    49          sessionMap = new ExpiringMap<SocketAddress, IoSession>(timeToLive, expirationInterval);
    50          mapExpirer = sessionMap.getExpirer();
    51          sessionMap.addExpirationListener(new DefaultExpirationListener());
    52      }
    53
    54      public void put(IoSession session) {
    55          mapExpirer.startExpiringIfNotStarted();
    56
    57          SocketAddress key = session.getRemoteAddress();
    58
    59          if (!sessionMap.containsKey(key)) {
    60              sessionMap.put(key, session);
    61          }
    62      }
    63
    64      public IoSession recycle(SocketAddress remoteAddress) {
    65          return sessionMap.get(remoteAddress);
    66      }
    67
    68      public void remove(IoSession session) {
    69          sessionMap.remove(session.getRemoteAddress());
    70      }
    71
    72      public void stopExpiring() {
    73          mapExpirer.stopExpiring();
    74      }
    75
    76      public int getExpirationInterval() {
    77          return sessionMap.getExpirationInterval();
    78      }
    79
    80      public int getTimeToLive() {
    81          return sessionMap.getTimeToLive();
    82      }
    83
    84      public void setExpirationInterval(int expirationInterval) {
    85          sessionMap.setExpirationInterval(expirationInterval);
    86      }
    87
    88      public void setTimeToLive(int timeToLive) {
    89          sessionMap.setTimeToLive(timeToLive);
    90      }
    91
    92      private class DefaultExpirationListener implements ExpirationListener<IoSession> {
    93          public void expired(IoSession expiredSession) {
    94              expiredSession.close(true);
    95          }
    96      }
    97  }