View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.schema.registries;
21  
22  
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import javax.naming.NamingException;
28  
29  import org.apache.directory.shared.ldap.schema.MatchingRuleUse;
30  
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * A plain old java object implementation of an MatchingRuleUseRegistry.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev: 601982 $
40   */
41  public class DefaultMatchingRuleUseRegistry implements MatchingRuleUseRegistry
42  {
43      /** static class logger */
44      private static final Logger LOG = LoggerFactory.getLogger( DefaultMatchingRuleUseRegistry.class );
45      /** maps a name to an MatchingRuleUse */
46      private final Map<String,MatchingRuleUse> byName;
47  
48  
49      // ------------------------------------------------------------------------
50      // C O N S T R U C T O R S
51      // ------------------------------------------------------------------------
52  
53      
54      /**
55       * Creates an empty DefaultMatchingRuleUseRegistry.
56       */
57      public DefaultMatchingRuleUseRegistry()
58      {
59          this.byName = new HashMap<String,MatchingRuleUse>();
60      }
61  
62  
63      // ------------------------------------------------------------------------
64      // Service Methods
65      // ------------------------------------------------------------------------
66  
67      
68      public void register( MatchingRuleUse matchingRuleUse ) throws NamingException
69      {
70          if ( byName.containsKey( matchingRuleUse.getName() ) )
71          {
72              throw new NamingException( "matchingRuleUse w/ name " + matchingRuleUse.getName()
73                  + " has already been registered!" );
74          }
75  
76          byName.put( matchingRuleUse.getName(), matchingRuleUse );
77          if ( LOG.isDebugEnabled() )
78          {
79              LOG.debug( "registed matchingRuleUse: " + matchingRuleUse );
80          }
81      }
82  
83  
84      public MatchingRuleUse lookup( String name ) throws NamingException
85      {
86          if ( !byName.containsKey( name ) )
87          {
88              throw new NamingException( "matchingRuleUse w/ name " + name + " not registered!" );
89          }
90  
91          MatchingRuleUse matchingRuleUse = byName.get( name );
92          if ( LOG.isDebugEnabled() )
93          {
94              LOG.debug( "lookup with name '"+ name + "' of matchingRuleUse: " + matchingRuleUse );
95          }
96          return matchingRuleUse;
97      }
98  
99  
100     public boolean hasMatchingRuleUse( String name )
101     {
102         return byName.containsKey( name );
103     }
104 
105 
106     public String getSchemaName( String id ) throws NamingException
107     {
108         MatchingRuleUse mru = byName.get( id );
109         if ( mru != null )
110         {
111             return mru.getSchema();
112         }
113 
114         throw new NamingException( "Name " + id + " not found in name to " + "MatchingRuleUse map!" );
115     }
116 
117 
118     public Iterator<MatchingRuleUse> iterator()
119     {
120         return byName.values().iterator();
121     }
122     
123     
124     public void unregister( String name ) throws NamingException
125     {
126         byName.remove( name );
127     }
128 }