1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.schema.bootstrap;
18
19
20 import java.util.HashMap;
21
22 import javax.naming.NamingException;
23 import javax.naming.directory.Attribute;
24 import javax.naming.ldap.LdapContext;
25
26 import org.apache.ldap.server.jndi.ServerDirObjectFactory;
27 import org.apache.ldap.server.schema.ObjectFactoryRegistry;
28 import org.apache.ldap.server.schema.OidRegistry;
29
30
31 /***
32 * A boostrap service implementation for an ObjectFactoryRegistry.
33 *
34 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35 * @version $Rev: 264732 $
36 */
37 public class BootstrapObjectFactoryRegistry implements ObjectFactoryRegistry
38 {
39 /*** Used to lookup a state factory by objectClass id */
40 private final HashMap byOid = new HashMap();
41
42 /*** The oid registry used to get numeric ids */
43 private final OidRegistry oidRegistry;
44
45
46
47
48
49
50
51 /***
52 * Creates an ObjectFactoryRegistry that looks up an object factory to use.
53 *
54 * @param oidRegistry an object identifier registry
55 */
56 public BootstrapObjectFactoryRegistry( OidRegistry oidRegistry )
57 {
58 this.oidRegistry = oidRegistry;
59 }
60
61
62 public ServerDirObjectFactory getObjectFactories( LdapContext ctx ) throws NamingException
63 {
64 Attribute objectClass = ctx.getAttributes( "" ).get( "objectClass" );
65
66 if ( objectClass == null )
67 {
68 return null;
69 }
70
71 if ( ctx.getEnvironment().containsKey( "factory.hint" ) )
72 {
73 String oid = ( String ) ctx.getEnvironment().get( "factory.hint" );
74
75 String noid = oidRegistry.getOid( oid );
76
77 if ( byOid.containsKey( noid ) )
78 {
79 return ( ServerDirObjectFactory ) byOid.get( noid );
80 }
81 }
82
83
84
85 for ( int ii = 0; ii < objectClass.size(); ii++ )
86 {
87 String noid = oidRegistry.getOid( ( String ) objectClass.get( ii ) );
88 if ( byOid.containsKey( noid ) )
89 {
90 return ( ServerDirObjectFactory ) byOid.get( noid );
91 }
92 }
93
94 return null;
95 }
96
97
98 public void register( ServerDirObjectFactory factory ) throws NamingException
99 {
100 byOid.put( oidRegistry.getOid( factory.getObjectClassId() ), factory );
101 }
102 }