KickJava   Java API By Example, From Geeks To Geeks.

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


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.MatchingRule;
21 import org.apache.ldap.server.schema.MatchingRuleRegistry;
22 import org.apache.ldap.server.schema.MatchingRuleRegistryMonitor;
23 import org.apache.ldap.server.schema.MatchingRuleRegistryMonitorAdapter;
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 MatchingRuleRegistry service used to lookup matching rules by OID.
34  *
35  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
36  * @version $Rev: 169198 $
37  */

38 public class BootstrapMatchingRuleRegistry implements MatchingRuleRegistry
39 {
40     /** a map using an OID for the key and a MatchingRule for the value */
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     /** a monitor used to track noteable registry events */
47     private MatchingRuleRegistryMonitor monitor = null;
48     
49     
50     // ------------------------------------------------------------------------
51
// C O N S T R U C T O R S
52
// ------------------------------------------------------------------------
53

54     
55     /**
56      * Creates a BootstrapMatchingRuleRegistry using existing MatchingRulees
57      * for lookups.
58      *
59      */

60     public BootstrapMatchingRuleRegistry( OidRegistry oidRegistry )
61     {
62         this.oidToSchema = new HashMap JavaDoc();
63         this.oidRegistry = oidRegistry;
64         this.byOid = new HashMap JavaDoc();
65         this.monitor = new MatchingRuleRegistryMonitorAdapter();
66     }
67     
68
69     // ------------------------------------------------------------------------
70
// MatchingRuleRegistry interface methods
71
// ------------------------------------------------------------------------
72

73     
74     /**
75      * @see org.apache.ldap.server.schema.MatchingRuleRegistry#lookup(String)
76      */

77     public MatchingRule lookup( String JavaDoc id ) throws NamingException JavaDoc
78     {
79         id = oidRegistry.getOid( id );
80
81         if ( byOid.containsKey( id ) )
82         {
83             MatchingRule MatchingRule = ( MatchingRule ) byOid.get( id );
84             monitor.lookedUp( MatchingRule );
85             return MatchingRule;
86         }
87         
88         NamingException JavaDoc fault = new NamingException JavaDoc( "Unknown MatchingRule OID " + id );
89         monitor.lookupFailed( id, fault );
90         throw fault;
91     }
92     
93
94     /**
95      * @see MatchingRuleRegistry#register(String, MatchingRule)
96      */

97     public void register( String JavaDoc schema, MatchingRule matchingRule ) throws NamingException JavaDoc
98     {
99         if ( byOid.containsKey( matchingRule.getOid() ) )
100         {
101             NamingException JavaDoc e = new NamingException JavaDoc( "matchingRule w/ OID " +
102                 matchingRule.getOid() + " has already been registered!" );
103             monitor.registerFailed( matchingRule, e );
104             throw e;
105         }
106
107         oidToSchema.put( matchingRule.getOid(), schema );
108
109         String JavaDoc[] names = matchingRule.getNames();
110         for ( int ii = 0; ii < names.length; ii++ )
111         {
112             oidRegistry.register( names[ii], matchingRule.getOid() );
113         }
114
115         byOid.put( matchingRule.getOid(), matchingRule );
116         monitor.registered( matchingRule );
117     }
118
119     
120     /**
121      * @see org.apache.ldap.server.schema.MatchingRuleRegistry#hasMatchingRule(String)
122      */

123     public boolean hasMatchingRule( String JavaDoc id )
124     {
125         if ( oidRegistry.hasOid( id ) )
126         {
127             try
128             {
129                 return byOid.containsKey( oidRegistry.getOid( id ) );
130             }
131             catch ( NamingException JavaDoc e )
132             {
133                 return false;
134             }
135         }
136
137         return false;
138     }
139
140
141     public String JavaDoc getSchemaName( String JavaDoc id ) throws NamingException JavaDoc
142     {
143         id = oidRegistry.getOid( id );
144         if ( oidToSchema.containsKey( id ) )
145         {
146             return ( String JavaDoc ) oidToSchema.get( id );
147         }
148
149         throw new NamingException JavaDoc( "OID " + id + " not found in oid to " +
150             "schema name map!" );
151     }
152
153
154     // ------------------------------------------------------------------------
155
// package friendly monitor methods
156
// ------------------------------------------------------------------------
157

158     
159     /**
160      * Gets the monitor for this registry.
161      *
162      * @return the monitor
163      */

164     MatchingRuleRegistryMonitor getMonitor()
165     {
166         return monitor;
167     }
168
169     
170     /**
171      * Sets the monitor for this registry.
172      *
173      * @param monitor the monitor to set
174      */

175     void setMonitor( MatchingRuleRegistryMonitor monitor )
176     {
177         this.monitor = monitor;
178     }
179
180
181     public Iterator JavaDoc list()
182     {
183         return byOid.values().iterator();
184     }
185 }
186
Popular Tags