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.api.ldap.model.schema.registries;
21  
22  
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.model.exception.LdapException;
29  import org.apache.directory.api.ldap.model.schema.DitStructureRule;
30  import org.apache.directory.api.ldap.model.schema.SchemaObject;
31  import org.apache.directory.api.ldap.model.schema.SchemaObjectType;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  
36  /**
37   * A DitStructureRule registry's service default implementation.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class DefaultDitStructureRuleRegistry extends DefaultSchemaObjectRegistry<DitStructureRule>
42      implements DitStructureRuleRegistry
43  {
44      /** static class logger */
45      private static final Logger LOG = LoggerFactory.getLogger( DefaultDitStructureRuleRegistry.class );
46  
47      /** A speedup for debug */
48      private static final boolean DEBUG = LOG.isDebugEnabled();
49  
50      /** a map of DitStructureRule looked up by RuleId */
51      protected Map<Integer, DitStructureRule> byRuleId;
52  
53  
54      /**
55       * Creates a new default NormalizerRegistry instance.
56       */
57      public DefaultDitStructureRuleRegistry()
58      {
59          super( SchemaObjectType.DIT_STRUCTURE_RULE, new OidRegistry<DitStructureRule>() );
60          byRuleId = new HashMap<>();
61      }
62  
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public boolean contains( int ruleId )
69      {
70          return byRuleId.containsKey( ruleId );
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public Iterator<DitStructureRule> iterator()
79      {
80          return byRuleId.values().iterator();
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public Iterator<Integer> ruleIdIterator()
89      {
90          return byRuleId.keySet().iterator();
91      }
92  
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String getSchemaName( int ruleId ) throws LdapException
99      {
100         DitStructureRule ditStructureRule = byRuleId.get( ruleId );
101 
102         if ( ditStructureRule != null )
103         {
104             return ditStructureRule.getSchemaName();
105         }
106 
107         String msg = I18n.err( I18n.ERR_04263, ruleId );
108         LOG.warn( msg );
109         throw new LdapException( msg );
110     }
111 
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public void register( DitStructureRule ditStructureRule ) throws LdapException
118     {
119         int ruleId = ditStructureRule.getRuleId();
120 
121         if ( byRuleId.containsKey( ruleId ) )
122         {
123             String msg = I18n.err( I18n.ERR_04264, ruleId );
124             LOG.warn( msg );
125             throw new LdapException( msg );
126         }
127 
128         byRuleId.put( ruleId, ditStructureRule );
129 
130         if ( LOG.isDebugEnabled() )
131         {
132             LOG.debug( "registered {} for OID {}", ditStructureRule, ruleId );
133         }
134     }
135 
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public DitStructureRule lookup( int ruleId ) throws LdapException
142     {
143         DitStructureRule ditStructureRule = byRuleId.get( ruleId );
144 
145         if ( ditStructureRule == null )
146         {
147             String msg = I18n.err( I18n.ERR_04265, ruleId );
148             LOG.debug( msg );
149             throw new LdapException( msg );
150         }
151 
152         if ( DEBUG )
153         {
154             LOG.debug( "Found {} with ruleId: {}", ditStructureRule, ruleId );
155         }
156 
157         return ditStructureRule;
158     }
159 
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public void unregister( int ruleId ) throws LdapException
166     {
167         DitStructureRule ditStructureRule = byRuleId.remove( ruleId );
168 
169         if ( DEBUG )
170         {
171             LOG.debug( "Removed {} with ruleId {} from the registry", ditStructureRule, ruleId );
172         }
173     }
174 
175 
176     /**
177      * {@inheritDoc}
178      */
179     @Override
180     public void unregisterSchemaElements( String schemaName )
181     {
182         if ( schemaName == null )
183         {
184             return;
185         }
186 
187         // Loop on all the SchemaObjects stored and remove those associated
188         // with the give schemaName
189         for ( DitStructureRule ditStructureRule : this )
190         {
191             if ( schemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
192             {
193                 int ruleId = ditStructureRule.getRuleId();
194                 SchemaObject removed = byRuleId.remove( ruleId );
195 
196                 if ( DEBUG )
197                 {
198                     LOG.debug( "Removed {} with ruleId {} from the registry", removed, ruleId );
199                 }
200             }
201         }
202     }
203 
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public void renameSchema( String originalSchemaName, String newSchemaName )
210     {
211         // Loop on all the SchemaObjects stored and remove those associated
212         // with the give schemaName
213         for ( DitStructureRule ditStructureRule : this )
214         {
215             if ( originalSchemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
216             {
217                 ditStructureRule.setSchemaName( newSchemaName );
218 
219                 if ( DEBUG )
220                 {
221                     LOG.debug( "Renamed {} schemaName to {}", ditStructureRule, newSchemaName );
222                 }
223             }
224         }
225     }
226 
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public DefaultDitStructureRuleRegistry copy()
233     {
234         DefaultDitStructureRuleRegistry copy = new DefaultDitStructureRuleRegistry();
235 
236         // Copy the base data
237         copy.copy( this );
238 
239         return copy;
240     }
241 }