View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.schema.bootstrap;
18  
19  
20  import java.util.HashMap;
21  
22  import javax.naming.NamingException;
23  
24  import org.apache.ldap.server.jndi.ServerDirStateFactory;
25  import org.apache.ldap.server.schema.StateFactoryRegistry;
26  
27  
28  /***
29   * A bootstrap service implementation for a state factory registry.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 264732 $
33   */
34  public class BootstrapStateFactoryRegistry implements StateFactoryRegistry
35  {
36      /*** Used to lookup a state factory by class */
37      private final HashMap byClass = new HashMap();
38  
39  
40      public ServerDirStateFactory getStateFactories( Object obj ) throws NamingException
41      {
42          Class c = obj.getClass();
43  
44          // if the class is mapped to a factory then this is most specific
45  
46          if ( byClass.containsKey( c ) )
47          {
48              return ( ServerDirStateFactory ) byClass.get( c );
49          }
50  
51          while ( ( c = c.getSuperclass() ) != null )
52          {
53              if ( byClass.containsKey( c ) )
54              {
55                  return ( ServerDirStateFactory ) byClass.get( c );
56              }
57          }
58  
59          // if we get this far start searching interfaces for a factory
60  
61          Class[] interfaces = c.getInterfaces();
62  
63          for ( int ii = 0; ii < interfaces.length; ii++ )
64          {
65              if ( byClass.containsKey( interfaces[ii] ) )
66              {
67                  return ( ServerDirStateFactory ) byClass.get( interfaces[ii] );
68              }
69          }
70  
71          return null;
72      }
73  
74  
75      public void register( ServerDirStateFactory factory )
76      {
77          byClass.put( factory.getAssociatedClass(), factory );
78      }
79  }