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 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;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
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: 159259 $
36   */
37  public class BootstrapMatchingRuleUseRegistry implements MatchingRuleUseRegistry
38  {
39      /*** maps a name to an MatchingRuleUse */
40      private final Map byName;
41      /*** maps a MatchingRuleUse name to a schema name*/
42      private final Map 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();
58          this.nameToSchema = new HashMap();
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 schema, MatchingRuleUse matchingRuleUse )
80          throws NamingException
81      {
82          if ( byName.containsKey( matchingRuleUse.getName() ) )
83          {
84              NamingException e = new NamingException( "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 name ) throws NamingException
97      {
98          if ( ! byName.containsKey( name ) )
99          {
100             NamingException e = new NamingException( "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 name )
113     {
114         return byName.containsKey( name );
115     }
116 
117 
118     public String getSchemaName( String id ) throws NamingException
119     {
120         if ( nameToSchema.containsKey( id ) )
121         {
122             return ( String ) nameToSchema.get( id );
123         }
124 
125         throw new NamingException( "Name " + id + " not found in name to " +
126             "schema name map!" );
127     }
128 
129 
130     public Iterator list()
131     {
132         return byName.values().iterator();
133     }
134 }