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