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 org.apache.ldap.server.jndi.ServerDirStateFactory;
21  import org.apache.ldap.server.schema.StateFactoryRegistry;
22  
23  import javax.naming.NamingException;
24  import java.util.HashMap;
25  
26  
27  /***
28   * A bootstrap service implementation for a state factory registry.
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev$
32   */
33  public class BootstrapStateFactoryRegistry implements StateFactoryRegistry
34  {
35      /*** Used to lookup a state factory by class */
36      private final HashMap byClass = new HashMap();
37  
38  
39      public ServerDirStateFactory getStateFactories( Object obj ) throws NamingException
40      {
41          Class c = obj.getClass();
42  
43          // if the class is mapped to a factory then this is most specific
44  
45          if ( byClass.containsKey( c ) )
46          {
47              return ( ServerDirStateFactory ) byClass.get( c );
48          }
49  
50          while ( ( c = c.getSuperclass() ) != null )
51          {
52              if ( byClass.containsKey( c ) )
53              {
54                  return ( ServerDirStateFactory ) byClass.get( c );
55              }
56          }
57  
58          // if we get this far start searching interfaces for a factory
59  
60          Class[] interfaces = c.getInterfaces();
61  
62          for ( int ii = 0; ii < interfaces.length; ii++ )
63          {
64              if ( byClass.containsKey( interfaces[ii] ) )
65              {
66                  return ( ServerDirStateFactory ) byClass.get( interfaces[ii] );
67              }
68          }
69  
70          return null;
71      }
72  
73  
74      public void register( ServerDirStateFactory factory )
75      {
76          byClass.put( factory.getAssociatedClass(), factory );
77      }
78  }