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.MatchingRule;
30  
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * A MatchingRuleRegistry service used to lookup matching rules by OID.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev: 664295 $
40   */
41  public class DefaultMatchingRuleRegistry implements MatchingRuleRegistry
42  {
43      /** static class logger */
44      private static final Logger LOG = LoggerFactory.getLogger( DefaultMatchingRuleRegistry.class );
45      /** a map using an OID for the key and a MatchingRule for the value */
46      private final Map<String,MatchingRule> byOid;
47      /** the registry used to resolve names to OIDs */
48      private final OidRegistry oidRegistry;
49  
50  
51      // ------------------------------------------------------------------------
52      // C O N S T R U C T O R S
53      // ------------------------------------------------------------------------
54  
55      
56      /**
57       * Creates a DefaultMatchingRuleRegistry using existing MatchingRulees
58       * for lookups.
59       *
60       * @param oidRegistry used by this registry for OID to name resolution of
61       * dependencies and to automatically register and unregister it's aliases and OIDs
62       */
63      public DefaultMatchingRuleRegistry( OidRegistry oidRegistry )
64      {
65          this.oidRegistry = oidRegistry;
66          this.byOid = new HashMap<String,MatchingRule>();
67      }
68  
69  
70      // ------------------------------------------------------------------------
71      // MatchingRuleRegistry interface methods
72      // ------------------------------------------------------------------------
73  
74      /**
75       * @see org.apache.directory.server.schema.registries.MatchingRuleRegistry#lookup(String)
76       */
77      public MatchingRule lookup( String id ) throws NamingException
78      {
79          id = oidRegistry.getOid( id );
80  
81          if ( byOid.containsKey( id ) )
82          {
83              MatchingRule matchingRule = byOid.get( id );
84              if ( LOG.isDebugEnabled() )
85              {
86                  LOG.debug( "lookup with id '"+id+"' of matchingRule: " + matchingRule );
87              }
88              return matchingRule;
89          }
90  
91          throw new NamingException( "Unknown MatchingRule OID " + id );
92      }
93  
94  
95      /**
96       * @see MatchingRuleRegistry#register(MatchingRule)
97       */
98      public void register( MatchingRule matchingRule ) throws NamingException
99      {
100         if ( byOid.containsKey( matchingRule.getOid() ) )
101         {
102             throw new NamingException( "matchingRule w/ OID " + matchingRule.getOid()
103                 + " has already been registered!" );
104         }
105 
106         String[] names = matchingRule.getNamesRef();
107         
108         for ( String name : names )
109         {
110             oidRegistry.register( name, matchingRule.getOid() );
111         }
112         oidRegistry.register( matchingRule.getOid(), matchingRule.getOid() );
113 
114         byOid.put( matchingRule.getOid(), matchingRule );
115         if ( LOG.isDebugEnabled() )
116         {
117             LOG.debug( "registed matchingRule: " + matchingRule);
118         }
119     }
120 
121 
122     /**
123      * @see org.apache.directory.server.schema.registries.MatchingRuleRegistry#hasMatchingRule(String)
124      */
125     public boolean hasMatchingRule( String id )
126     {
127         if ( oidRegistry.hasOid( id ) )
128         {
129             try
130             {
131                 return byOid.containsKey( oidRegistry.getOid( id ) );
132             }
133             catch ( NamingException e )
134             {
135                 return false;
136             }
137         }
138 
139         return false;
140     }
141 
142 
143     public String getSchemaName( String id ) throws NamingException
144     {
145         id = oidRegistry.getOid( id );
146         MatchingRule mr = byOid.get( id );
147         if ( mr != null )
148         {
149             return mr.getSchema();
150         }
151 
152         throw new NamingException( "OID " + id + " not found in oid to " + "MatchingRule name map!" );
153     }
154 
155 
156     public Iterator<MatchingRule> iterator()
157     {
158         return byOid.values().iterator();
159     }
160     
161     
162     public void unregister( String numericOid ) throws NamingException
163     {
164         if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
165         {
166             throw new NamingException( "Looks like the arg is not a numeric OID" );
167         }
168 
169         byOid.remove( numericOid );
170     }
171 }