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.ServerDirObjectFactory;
21  import org.apache.ldap.server.schema.ObjectFactoryRegistry;
22  import org.apache.ldap.server.schema.OidRegistry;
23  
24  import javax.naming.NamingException;
25  import javax.naming.directory.Attribute;
26  import javax.naming.ldap.LdapContext;
27  import java.util.HashMap;
28  
29  
30  /***
31   * A boostrap service implementation for an ObjectFactoryRegistry.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev$
35   */
36  public class BootstrapObjectFactoryRegistry implements ObjectFactoryRegistry
37  {
38      /*** Used to lookup a state factory by objectClass id */
39      private final HashMap byOid = new HashMap();
40  
41      /*** The oid registry used to get numeric ids */
42      private final OidRegistry oidRegistry;
43  
44  
45      // ------------------------------------------------------------------------
46      // C O N S T R U C T O R S
47      // ------------------------------------------------------------------------
48  
49  
50      /***
51       * Creates an ObjectFactoryRegistry that looks up an object factory to use.
52       *
53       * @param oidRegistry an object identifier registry
54       */
55      public BootstrapObjectFactoryRegistry( OidRegistry oidRegistry )
56      {
57          this.oidRegistry = oidRegistry;
58      }
59  
60  
61      public ServerDirObjectFactory getObjectFactories( LdapContext ctx ) throws NamingException
62      {
63          Attribute objectClass = ctx.getAttributes( "" ).get( "objectClass" );
64  
65          if ( objectClass == null )
66          {
67              return null;
68          }
69  
70          if ( ctx.getEnvironment().containsKey( "factory.hint" ) )
71          {
72              String oid = ( String ) ctx.getEnvironment().get( "factory.hint" );
73  
74              String noid = oidRegistry.getOid( oid );
75  
76              if ( byOid.containsKey( noid ) )
77              {
78                  return ( ServerDirObjectFactory ) byOid.get( noid );
79              }
80          }
81  
82          // hint did not work or was not provided so we return what we find first
83  
84          for ( int ii = 0; ii < objectClass.size(); ii++ )
85          {
86              String noid = oidRegistry.getOid( ( String ) objectClass.get( ii ) );
87              if ( byOid.containsKey( noid ) )
88              {
89                  return ( ServerDirObjectFactory ) byOid.get( noid );
90              }
91          }
92  
93          return null;
94      }
95  
96  
97      public void register( ServerDirObjectFactory factory ) throws NamingException
98      {
99          byOid.put( oidRegistry.getOid( factory.getObjectClassId() ), factory );
100     }
101 }