KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > schema > bootstrap > BootstrapObjectFactoryRegistry


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 JavaDoc;
25 import javax.naming.directory.Attribute JavaDoc;
26 import javax.naming.ldap.LdapContext JavaDoc;
27 import java.util.HashMap JavaDoc;
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 JavaDoc byOid = new HashMap JavaDoc();
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 JavaDoc ctx ) throws NamingException JavaDoc
62     {
63         Attribute JavaDoc 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 JavaDoc oid = ( String JavaDoc ) ctx.getEnvironment().get( "factory.hint" );
73
74             String JavaDoc 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 JavaDoc noid = oidRegistry.getOid( ( String JavaDoc ) 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 JavaDoc
98     {
99         byOid.put( oidRegistry.getOid( factory.getObjectClassId() ), factory );
100     }
101 }
102
Popular Tags