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.directory.server.ldap;
21
22
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.apache.mina.common.IoSession;
27
28
29 /**
30 * Manages sessions in a thread safe manner for the LdapService. This class is
31 * used primarily by the {@link LdapProtocolHandler} to manage sessions and is
32 * created by the LdapService which makes it available to the handler. It's job
33 * is simple and this class was mainly created to be able to expose the session
34 * manager safely to things like the LdapProtocolHandler.
35 *
36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37 * @version $Rev$, $Date$
38 */
39 public class LdapSessionManager
40 {
41 /** Concurrent hashMap backing for IoSession to LdapSession mapping */
42 private Map<IoSession, LdapSession> ldapSessions = new ConcurrentHashMap<IoSession, LdapSession>( 100 );
43
44
45 /**
46 * Gets the active sessions managed by the LdapService.
47 */
48 public LdapSession[] getSessions()
49 {
50 return ldapSessions.values().toArray( new LdapSession[0] );
51 }
52
53
54 /**
55 * Adds a new LdapSession to the LdapService.
56 *
57 * @param ldapSession the newly created {@link LdapSession}
58 */
59 public void addLdapSession( LdapSession ldapSession )
60 {
61 synchronized ( ldapSessions )
62 {
63 ldapSessions.put( ldapSession.getIoSession(), ldapSession );
64 }
65 }
66
67
68 /**
69 * Removes an LdapSession managed by the {@link LdapService}. This method
70 * has no side effects: meaning it does not perform cleanup tasks after
71 * removing the session. This task is handled by the callers.
72 *
73 * @param session the MINA session of the LdapSession to be removed
74 * @return the LdapSession to remove
75 */
76 public LdapSession removeLdapSession( IoSession session )
77 {
78 synchronized ( ldapSessions )
79 {
80 return ldapSessions.remove( session );
81 }
82 }
83
84
85 /**
86 * Gets the LdapSession associated with the MINA session.
87 *
88 * @param session the MINA session of the LdapSession to retrieve
89 * @return the LdapSession associated with the MINA {@link IoSession}
90 */
91 public LdapSession getLdapSession( IoSession session )
92 {
93 synchronized ( ldapSessions )
94 {
95 return ldapSessions.get( session );
96 }
97 }
98 }