KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.schema.DITStructureRule;
21 import org.apache.ldap.server.schema.DITStructureRuleRegistry;
22 import org.apache.ldap.server.schema.DITStructureRuleRegistryMonitor;
23 import org.apache.ldap.server.schema.DITStructureRuleRegistryMonitorAdapter;
24 import org.apache.ldap.server.schema.OidRegistry;
25
26 import javax.naming.NamingException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31
32 /**
33  * A plain old java object implementation of an DITStructureRuleRegistry.
34  *
35  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
36  * @version $Rev: 169198 $
37  */

38 public class BootstrapDitStructureRuleRegistry implements DITStructureRuleRegistry
39 {
40     /** maps an OID to an DITStructureRule */
41     private final Map JavaDoc byOid;
42     /** maps an OID to a schema name*/
43     private final Map JavaDoc oidToSchema;
44     /** the registry used to resolve names to OIDs */
45     private final OidRegistry oidRegistry;
46     /** monitor notified via callback events */
47     private DITStructureRuleRegistryMonitor monitor;
48
49
50     // ------------------------------------------------------------------------
51
// C O N S T R U C T O R S
52
// ------------------------------------------------------------------------
53

54
55     /**
56      * Creates an empty BootstrapDitStructureRuleRegistry.
57      */

58     public BootstrapDitStructureRuleRegistry( OidRegistry oidRegistry )
59     {
60         this.byOid = new HashMap JavaDoc();
61         this.oidToSchema = new HashMap JavaDoc();
62         this.monitor = new DITStructureRuleRegistryMonitorAdapter();
63         this.oidRegistry = oidRegistry;
64     }
65
66
67     /**
68      * Sets the monitor that is to be notified via callback events.
69      *
70      * @param monitor the new monitor to notify of notable events
71      */

72     public void setMonitor( DITStructureRuleRegistryMonitor monitor )
73     {
74         this.monitor = monitor;
75     }
76
77
78     // ------------------------------------------------------------------------
79
// Service Methods
80
// ------------------------------------------------------------------------
81

82
83     public void register( String JavaDoc schema, DITStructureRule dITStructureRule ) throws NamingException JavaDoc
84     {
85         if ( byOid.containsKey( dITStructureRule.getOid() ) )
86         {
87             NamingException JavaDoc e = new NamingException JavaDoc( "dITStructureRule w/ OID " +
88                 dITStructureRule.getOid() + " has already been registered!" );
89             monitor.registerFailed( dITStructureRule, e );
90             throw e;
91         }
92
93         oidToSchema.put( dITStructureRule.getOid(), schema );
94         oidRegistry.register( dITStructureRule.getName(), dITStructureRule.getOid() );
95         byOid.put( dITStructureRule.getOid(), dITStructureRule );
96         monitor.registered( dITStructureRule );
97     }
98
99
100     public DITStructureRule lookup( String JavaDoc id ) throws NamingException JavaDoc
101     {
102         id = oidRegistry.getOid( id );
103
104         if ( ! byOid.containsKey( id ) )
105         {
106             NamingException JavaDoc e = new NamingException JavaDoc( "dITStructureRule w/ OID "
107                 + id + " not registered!" );
108             monitor.lookupFailed( id, e );
109             throw e;
110         }
111
112         DITStructureRule dITStructureRule = ( DITStructureRule ) byOid.get( id );
113         monitor.lookedUp( dITStructureRule );
114         return dITStructureRule;
115     }
116
117
118     public boolean hasDITStructureRule( String JavaDoc id )
119     {
120         if ( oidRegistry.hasOid( id ) )
121         {
122             try
123             {
124                 return byOid.containsKey( oidRegistry.getOid( id ) );
125             }
126             catch ( NamingException JavaDoc e )
127             {
128                 return false;
129             }
130         }
131
132         return false;
133     }
134
135
136     public String JavaDoc getSchemaName( String JavaDoc id ) throws NamingException JavaDoc
137     {
138         id = oidRegistry.getOid( id );
139         if ( oidToSchema.containsKey( id ) )
140         {
141             return ( String JavaDoc ) oidToSchema.get( id );
142         }
143
144         throw new NamingException JavaDoc( "OID " + id + " not found in oid to " +
145             "schema name map!" );
146     }
147
148
149
150
151     public Iterator JavaDoc list()
152     {
153         return byOid.values().iterator();
154     }
155 }
156
Popular Tags