View Javadoc

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 java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.naming.NamingException;
25  
26  import org.apache.ldap.common.schema.MatchingRuleUse;
27  import org.apache.ldap.server.schema.MatchingRuleUseRegistry;
28  import org.apache.ldap.server.schema.MatchingRuleUseRegistryMonitor;
29  import org.apache.ldap.server.schema.MatchingRuleUseRegistryMonitorAdapter;
30  
31  
32  /***
33   * A plain old java object implementation of an MatchingRuleUseRegistry.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev: 264732 $
37   */
38  public class BootstrapMatchingRuleUseRegistry implements MatchingRuleUseRegistry
39  {
40      /*** maps a name to an MatchingRuleUse */
41      private final Map byName;
42      /*** maps a MatchingRuleUse name to a schema name*/
43      private final Map nameToSchema;
44      /*** monitor notified via callback events */
45      private MatchingRuleUseRegistryMonitor monitor;
46  
47  
48      // ------------------------------------------------------------------------
49      // C O N S T R U C T O R S
50      // ------------------------------------------------------------------------
51  
52  
53      /***
54       * Creates an empty BootstrapMatchingRuleUseRegistry.
55       */
56      public BootstrapMatchingRuleUseRegistry()
57      {
58          this.byName = new HashMap();
59          this.nameToSchema = new HashMap();
60          this.monitor = new MatchingRuleUseRegistryMonitorAdapter();
61      }
62  
63  
64      /***
65       * Sets the monitor that is to be notified via callback events.
66       *
67       * @param monitor the new monitor to notify of notable events
68       */
69      public void setMonitor( MatchingRuleUseRegistryMonitor monitor )
70      {
71          this.monitor = monitor;
72      }
73  
74  
75      // ------------------------------------------------------------------------
76      // Service Methods
77      // ------------------------------------------------------------------------
78  
79  
80      public void register( String schema, MatchingRuleUse matchingRuleUse )
81          throws NamingException
82      {
83          if ( byName.containsKey( matchingRuleUse.getName() ) )
84          {
85              NamingException e = new NamingException( "matchingRuleUse w/ name "
86                  + matchingRuleUse.getName() + " has already been registered!" );
87              monitor.registerFailed( matchingRuleUse, e );
88              throw e;
89          }
90  
91          nameToSchema.put( matchingRuleUse.getName(), schema );
92          byName.put( matchingRuleUse.getName(), matchingRuleUse );
93          monitor.registered( matchingRuleUse );
94      }
95  
96  
97      public MatchingRuleUse lookup( String name ) throws NamingException
98      {
99          if ( ! byName.containsKey( name ) )
100         {
101             NamingException e = new NamingException( "matchingRuleUse w/ name "
102                 + name + " not registered!" );
103             monitor.lookupFailed( name, e );
104             throw e;
105         }
106 
107         MatchingRuleUse matchingRuleUse = ( MatchingRuleUse ) byName.get( name );
108         monitor.lookedUp( matchingRuleUse );
109         return matchingRuleUse;
110     }
111 
112 
113     public boolean hasMatchingRuleUse( String name )
114     {
115         return byName.containsKey( name );
116     }
117 
118 
119     public String getSchemaName( String id ) throws NamingException
120     {
121         if ( nameToSchema.containsKey( id ) )
122         {
123             return ( String ) nameToSchema.get( id );
124         }
125 
126         throw new NamingException( "Name " + id + " not found in name to " +
127             "schema name map!" );
128     }
129 
130 
131     public Iterator list()
132     {
133         return byName.values().iterator();
134     }
135 }