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 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
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
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 }