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.directory.server.ldap; 021 022 023import java.util.Map; 024import java.util.concurrent.ConcurrentHashMap; 025 026import org.apache.mina.core.session.IoSession; 027 028 029/** 030 * Manages sessions in a thread safe manner for the LdapServer. This class is 031 * used primarily by the {@link LdapProtocolHandler} to manage sessions and is 032 * created by the LdapServer which makes it available to the handler. It's job 033 * is simple and this class was mainly created to be able to expose the session 034 * manager safely to things like the LdapProtocolHandler. 035 * 036 * Basically, Ldap sessions are stored in a Map, added or removed when a new connection 037 * is created or deleted. Most of the time, a new operation is processed and the associated 038 * Ldap session is pulled from the map. 039 * 040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 041 */ 042public class LdapSessionManager 043{ 044 /** Concurrent hashMap backing for IoSession to LdapSession mapping */ 045 private Map<IoSession, LdapSession> ldapSessions = new ConcurrentHashMap<>( 100 ); 046 047 048 /** 049 * Gets the active sessions managed by the LdapServer. 050 * 051 * @return The active sessions 052 */ 053 public LdapSession[] getSessions() 054 { 055 return ldapSessions.values().toArray( new LdapSession[0] ); 056 } 057 058 059 /** 060 * Adds a new LdapSession to the LdapServer. 061 * 062 * @param ldapSession the newly created {@link LdapSession} 063 */ 064 public void addLdapSession( LdapSession ldapSession ) 065 { 066 ldapSessions.put( ldapSession.getIoSession(), ldapSession ); 067 } 068 069 070 /** 071 * Removes an LdapSession managed by the {@link LdapServer}. This method 072 * has no side effects: meaning it does not perform cleanup tasks after 073 * removing the session. This task is handled by the callers. 074 * 075 * @param session the MINA session of the LdapSession to be removed 076 * @return the LdapSession to remove 077 */ 078 public LdapSession removeLdapSession( IoSession session ) 079 { 080 return ldapSessions.remove( session ); 081 } 082 083 084 /** 085 * Gets the LdapSession associated with the MINA session. 086 * 087 * @param session the MINA session of the LdapSession to retrieve 088 * @return the LdapSession associated with the MINA {@link IoSession} 089 */ 090 public LdapSession getLdapSession( IoSession session ) 091 { 092 return ldapSessions.get( session ); 093 } 094}