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 */ 020 package org.apache.directory.server.ldap; 021 022 023 import java.util.Map; 024 import java.util.concurrent.ConcurrentHashMap; 025 026 import 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 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 * @version $Rev$, $Date$ 038 */ 039 public class LdapSessionManager 040 { 041 /** Concurrent hashMap backing for IoSession to LdapSession mapping */ 042 private Map<IoSession, LdapSession> ldapSessions = new ConcurrentHashMap<IoSession, LdapSession>( 100 ); 043 044 045 /** 046 * Gets the active sessions managed by the LdapServer. 047 */ 048 public LdapSession[] getSessions() 049 { 050 return ldapSessions.values().toArray( new LdapSession[0] ); 051 } 052 053 054 /** 055 * Adds a new LdapSession to the LdapServer. 056 * 057 * @param ldapSession the newly created {@link LdapSession} 058 */ 059 public void addLdapSession( LdapSession ldapSession ) 060 { 061 synchronized ( ldapSessions ) 062 { 063 ldapSessions.put( ldapSession.getIoSession(), ldapSession ); 064 } 065 } 066 067 068 /** 069 * Removes an LdapSession managed by the {@link LdapServer}. This method 070 * has no side effects: meaning it does not perform cleanup tasks after 071 * removing the session. This task is handled by the callers. 072 * 073 * @param session the MINA session of the LdapSession to be removed 074 * @return the LdapSession to remove 075 */ 076 public LdapSession removeLdapSession( IoSession session ) 077 { 078 synchronized ( ldapSessions ) 079 { 080 return ldapSessions.remove( session ); 081 } 082 } 083 084 085 /** 086 * Gets the LdapSession associated with the MINA session. 087 * 088 * @param session the MINA session of the LdapSession to retrieve 089 * @return the LdapSession associated with the MINA {@link IoSession} 090 */ 091 public LdapSession getLdapSession( IoSession session ) 092 { 093 synchronized ( ldapSessions ) 094 { 095 return ldapSessions.get( session ); 096 } 097 } 098 }