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.exception.LdapNamingException;
30  import org.apache.directory.shared.ldap.message.ResultCodeEnum;
31  import org.apache.directory.shared.ldap.schema.DITStructureRule;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  
36  /**
37   * A plain old java object implementation of an DITStructureRuleRegistry.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 602007 $
41   */
42  public class DefaultDitStructureRuleRegistry implements DITStructureRuleRegistry
43  {
44      /** static class logger */
45      private static final Logger LOG = LoggerFactory.getLogger( DefaultDitStructureRuleRegistry.class );
46      /** maps an OID to an DITStructureRule */
47      private final Map<String,DITStructureRule> byOid;
48      /** maps an OID to an DITStructureRule */
49      private final Map<Integer,DITStructureRule> byRuleId;
50      /** the registry used to resolve names to OIDs */
51      private final OidRegistry oidRegistry;
52  
53  
54      // ------------------------------------------------------------------------
55      // C O N S T R U C T O R S
56      // ------------------------------------------------------------------------
57  
58      
59      /**
60       * Creates an empty DefaultDitStructureRuleRegistry.
61       *
62       * @param oidRegistry used by this registry for OID to name resolution of
63       * dependencies and to automatically register and unregister it's aliases and OIDs
64       */
65      public DefaultDitStructureRuleRegistry( OidRegistry oidRegistry )
66      {
67          this.byRuleId = new HashMap<Integer,DITStructureRule>();
68          this.byOid = new HashMap<String,DITStructureRule>();
69          this.oidRegistry = oidRegistry;
70      }
71  
72  
73      // ------------------------------------------------------------------------
74      // Service Methods
75      // ------------------------------------------------------------------------
76  
77      public void register( DITStructureRule dITStructureRule ) throws NamingException
78      {
79          if ( byOid.containsKey( dITStructureRule.getOid() ) )
80          {
81              throw new NamingException( "dITStructureRule w/ OID " + dITStructureRule.getOid()
82                  + " has already been registered!" );
83          }
84  
85          oidRegistry.register( dITStructureRule.getName(), dITStructureRule.getOid() );
86          byOid.put( dITStructureRule.getOid(), dITStructureRule );
87          byRuleId.put( dITStructureRule.getRuleId(), dITStructureRule );
88          if ( LOG.isDebugEnabled() )
89          {
90              LOG.debug( "registered dITStructureRule: " + dITStructureRule );
91          }
92      }
93  
94  
95      public DITStructureRule lookup( String id ) throws NamingException
96      {
97          id = oidRegistry.getOid( id );
98  
99          if ( !byOid.containsKey( id ) )
100         {
101             throw new NamingException( "dITStructureRule w/ OID " + id + " not registered!" );
102         }
103 
104         DITStructureRule dITStructureRule = byOid.get( id );
105         if ( LOG.isDebugEnabled() )
106         {
107             LOG.debug( "lookup with id '" + id + "' for dITStructureRule: " + dITStructureRule );
108         }
109         return dITStructureRule;
110     }
111 
112 
113     public boolean hasDITStructureRule( String id )
114     {
115         if ( oidRegistry.hasOid( id ) )
116         {
117             try
118             {
119                 return byOid.containsKey( oidRegistry.getOid( id ) );
120             }
121             catch ( NamingException e )
122             {
123                 return false;
124             }
125         }
126 
127         return false;
128     }
129 
130 
131     public String getSchemaName( String id ) throws NamingException
132     {
133         id = oidRegistry.getOid( id );
134         DITStructureRule dsr = byOid.get( id );
135         if ( dsr != null )
136         {
137             return dsr.getSchema();
138         }
139 
140         throw new NamingException( "OID " + id + " not found in oid to " + "DITStructureRule map!" );
141     }
142 
143 
144     public String getSchemaName( Integer ruleId ) throws NamingException
145     {
146         DITStructureRule dsr = byRuleId.get( ruleId );
147         if ( dsr != null )
148         {
149             return dsr.getSchema();
150         }
151 
152         throw new NamingException( "A DitStructureRule with ruleId " + ruleId 
153             + " not found in the DITStructureRule map!" );
154     }
155 
156 
157     public Iterator<DITStructureRule> iterator()
158     {
159         return byOid.values().iterator();
160     }
161 
162     
163     public void unregister( Integer ruleId ) throws NamingException
164     {
165         DITStructureRule dsr = byRuleId.remove( ruleId );
166         
167         if ( dsr == null )
168         {
169             if ( dsr == null )
170             {
171                 throw new LdapNamingException(
172                     "No such DITStructureRule for rule identifier: " + ruleId,
173                     ResultCodeEnum.OTHER );
174             }
175         }
176         
177         byOid.remove( dsr.getOid() );
178     }
179     
180     
181     public void unregister( String numericOid ) throws NamingException
182     {
183         if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
184         {
185             throw new NamingException( "Looks like the arg is not a numeric OID" );
186         }
187 
188         DITStructureRule dsr = byOid.remove( numericOid );
189         byRuleId.remove( dsr.getRuleId() );
190     }
191 
192 
193     public boolean hasDITStructureRule( Integer ruleId )
194     {
195         DITStructureRule dsr = byRuleId.get( ruleId );
196         return dsr != null;
197     }
198 
199 
200     public DITStructureRule lookup( Integer ruleId ) throws NamingException
201     {
202         DITStructureRule dsr = byRuleId.get( ruleId );
203         
204         if ( dsr == null )
205         {
206             throw new LdapNamingException(
207                 "No such DITStructureRule for rule identifier: " + ruleId,
208                 ResultCodeEnum.OTHER );
209         }
210 
211         return dsr;
212     }
213 }