KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class BootstrapMatchingRuleUseRegistry implements MatchingRuleUseRegistry
38 {
39     /** maps a name to an MatchingRuleUse */
40     private final Map JavaDoc byName;
41     /** maps a MatchingRuleUse name to a schema name*/
42     private final Map JavaDoc nameToSchema;
43     /** monitor notified via callback events */
44     private MatchingRuleUseRegistryMonitor monitor;
45
46
47     // ------------------------------------------------------------------------
48
// C O N S T R U C T O R S
49
// ------------------------------------------------------------------------
50

51
52     /**
53      * Creates an empty BootstrapMatchingRuleUseRegistry.
54      */

55     public BootstrapMatchingRuleUseRegistry()
56     {
57         this.byName = new HashMap JavaDoc();
58         this.nameToSchema = new HashMap JavaDoc();
59         this.monitor = new MatchingRuleUseRegistryMonitorAdapter();
60     }
61
62
63     /**
64      * Sets the monitor that is to be notified via callback events.
65      *
66      * @param monitor the new monitor to notify of notable events
67      */

68     public void setMonitor( MatchingRuleUseRegistryMonitor monitor )
69     {
70         this.monitor = monitor;
71     }
72
73
74     // ------------------------------------------------------------------------
75
// Service Methods
76
// ------------------------------------------------------------------------
77

78
79     public void register( String JavaDoc schema, MatchingRuleUse matchingRuleUse )
80         throws NamingException JavaDoc
81     {
82         if ( byName.containsKey( matchingRuleUse.getName() ) )
83         {
84             NamingException JavaDoc e = new NamingException JavaDoc( "matchingRuleUse w/ name "
85                 + matchingRuleUse.getName() + " has already been registered!" );
86             monitor.registerFailed( matchingRuleUse, e );
87             throw e;
88         }
89
90         nameToSchema.put( matchingRuleUse.getName(), schema );
91         byName.put( matchingRuleUse.getName(), matchingRuleUse );
92         monitor.registered( matchingRuleUse );
93     }
94
95
96     public MatchingRuleUse lookup( String JavaDoc name ) throws NamingException JavaDoc
97     {
98         if ( ! byName.containsKey( name ) )
99         {
100             NamingException JavaDoc e = new NamingException JavaDoc( "matchingRuleUse w/ name "
101                 + name + " not registered!" );
102             monitor.lookupFailed( name, e );
103             throw e;
104         }
105
106         MatchingRuleUse matchingRuleUse = ( MatchingRuleUse ) byName.get( name );
107         monitor.lookedUp( matchingRuleUse );
108         return matchingRuleUse;
109     }
110
111
112     public boolean hasMatchingRuleUse( String JavaDoc name )
113     {
114         return byName.containsKey( name );
115     }
116
117
118     public String JavaDoc getSchemaName( String JavaDoc id ) throws NamingException JavaDoc
119     {
120         if ( nameToSchema.containsKey( id ) )
121         {
122             return ( String JavaDoc ) nameToSchema.get( id );
123         }
124
125         throw new NamingException JavaDoc( "Name " + id + " not found in name to " +
126             "schema name map!" );
127     }
128
129
130     public Iterator JavaDoc list()
131     {
132         return byName.values().iterator();
133     }
134 }
135
Popular Tags