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   *    https://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 map of DitStructureRule looked up by RuleId */
48      protected Map<Integer, DitStructureRule> byRuleId;
49  
50  
51      /**
52       * Creates a new default NormalizerRegistry instance.
53       */
54      public DefaultDitStructureRuleRegistry()
55      {
56          super( SchemaObjectType.DIT_STRUCTURE_RULE, new OidRegistry<DitStructureRule>() );
57          byRuleId = new HashMap<>();
58      }
59  
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public boolean contains( int ruleId )
66      {
67          return byRuleId.containsKey( ruleId );
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public Iterator<DitStructureRule> iterator()
76      {
77          return byRuleId.values().iterator();
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public Iterator<Integer> ruleIdIterator()
86      {
87          return byRuleId.keySet().iterator();
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String getSchemaName( int ruleId ) throws LdapException
96      {
97          DitStructureRule ditStructureRule = byRuleId.get( ruleId );
98  
99          if ( ditStructureRule != null )
100         {
101             return ditStructureRule.getSchemaName();
102         }
103 
104         String msg = I18n.err( I18n.ERR_13729_RULE_ID_NOT_FOUND, ruleId );
105 
106         if ( LOG.isWarnEnabled() )
107         {
108             LOG.warn( msg );
109         }
110         
111         throw new LdapException( msg );
112     }
113 
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public void register( DitStructureRule ditStructureRule ) throws LdapException
120     {
121         int ruleId = ditStructureRule.getRuleId();
122 
123         if ( byRuleId.containsKey( ruleId ) )
124         {
125             String msg = I18n.err( I18n.ERR_13730_DIT_STRUCTURE_RULE_ALREADY_REGISTRED, ruleId );
126             
127             if ( LOG.isWarnEnabled() )
128             {
129                 LOG.warn( msg );
130             }
131             
132             throw new LdapException( msg );
133         }
134 
135         byRuleId.put( ruleId, ditStructureRule );
136 
137         if ( LOG.isDebugEnabled() )
138         {
139             LOG.debug( I18n.msg( I18n.MSG_13731_REGISTRED_FOR_OID, ditStructureRule, ruleId ) );
140         }
141     }
142 
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public DitStructureRule lookup( int ruleId ) throws LdapException
149     {
150         DitStructureRule ditStructureRule = byRuleId.get( ruleId );
151 
152         if ( ditStructureRule == null )
153         {
154             String msg = I18n.err( I18n.ERR_13731_DIT_STRUCTURE_RULE_DOES_NOT_EXIST, ruleId );
155             
156             if ( LOG.isDebugEnabled() )
157             {
158                 LOG.debug( msg );
159             }
160             
161             throw new LdapException( msg );
162         }
163 
164         if ( LOG.isDebugEnabled() )
165         {
166             LOG.debug( I18n.msg( I18n.MSG_13724_FOUND_WITH_RULE_ID, ditStructureRule, ruleId ) );
167         }
168 
169         return ditStructureRule;
170     }
171 
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public void unregister( int ruleId ) throws LdapException
178     {
179         DitStructureRule ditStructureRule = byRuleId.remove( ruleId );
180 
181         if ( LOG.isDebugEnabled() )
182         {
183             LOG.debug( I18n.msg( I18n.MSG_13721_REMOVED_WITH_RULE_ID, ditStructureRule, ruleId ) );
184         }
185     }
186 
187 
188     /**
189      * {@inheritDoc}
190      */
191     @Override
192     public void unregisterSchemaElements( String schemaName )
193     {
194         if ( schemaName == null )
195         {
196             return;
197         }
198 
199         // Loop on all the SchemaObjects stored and remove those associated
200         // with the give schemaName
201         for ( DitStructureRule ditStructureRule : this )
202         {
203             if ( schemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
204             {
205                 int ruleId = ditStructureRule.getRuleId();
206                 SchemaObject removed = byRuleId.remove( ruleId );
207 
208                 if ( LOG.isDebugEnabled() )
209                 {
210                     LOG.debug( I18n.msg( I18n.MSG_13721_REMOVED_WITH_RULE_ID, removed, ruleId ) );
211                 }
212             }
213         }
214     }
215 
216 
217     /**
218      * {@inheritDoc}
219      */
220     @Override
221     public void renameSchema( String originalSchemaName, String newSchemaName )
222     {
223         // Loop on all the SchemaObjects stored and remove those associated
224         // with the give schemaName
225         for ( DitStructureRule ditStructureRule : this )
226         {
227             if ( originalSchemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
228             {
229                 ditStructureRule.setSchemaName( newSchemaName );
230 
231                 if ( LOG.isDebugEnabled() )
232                 {
233                     LOG.debug( I18n.msg( I18n.MSG_13722_RENAMED_SCHEMA_NAME_TO, ditStructureRule, newSchemaName ) );
234                 }
235             }
236         }
237     }
238 
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public DefaultDitStructureRuleRegistry copy()
245     {
246         DefaultDitStructureRuleRegistry copy = new DefaultDitStructureRuleRegistry();
247 
248         // Copy the base data
249         copy.copy( this );
250 
251         return copy;
252     }
253 }