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<Integer, DitStructureRule>();
61      }
62  
63  
64      /**
65       * {@inheritDoc}
66       */
67      public boolean contains( int ruleId )
68      {
69          return byRuleId.containsKey( ruleId );
70      }
71  
72  
73      /**
74       * {@inheritDoc}
75       */
76      public Iterator<DitStructureRule> iterator()
77      {
78          return byRuleId.values().iterator();
79      }
80  
81  
82      /**
83       * {@inheritDoc}
84       */
85      public Iterator<Integer> ruleIdIterator()
86      {
87          return byRuleId.keySet().iterator();
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      public String getSchemaName( int ruleId ) throws LdapException
95      {
96          DitStructureRule ditStructureRule = byRuleId.get( ruleId );
97  
98          if ( ditStructureRule != null )
99          {
100             return ditStructureRule.getSchemaName();
101         }
102 
103         String msg = I18n.err( I18n.ERR_04263, ruleId );
104         LOG.warn( msg );
105         throw new LdapException( msg );
106     }
107 
108 
109     /**
110      * {@inheritDoc}
111      */
112     public void register( DitStructureRule ditStructureRule ) throws LdapException
113     {
114         int ruleId = ditStructureRule.getRuleId();
115 
116         if ( byRuleId.containsKey( ruleId ) )
117         {
118             String msg = I18n.err( I18n.ERR_04264, ruleId );
119             LOG.warn( msg );
120             throw new LdapException( msg );
121         }
122 
123         byRuleId.put( ruleId, ditStructureRule );
124 
125         if ( LOG.isDebugEnabled() )
126         {
127             LOG.debug( "registered {} for OID {}", ditStructureRule, ruleId );
128         }
129     }
130 
131 
132     /**
133      * {@inheritDoc}
134      */
135     public DitStructureRule lookup( int ruleId ) throws LdapException
136     {
137         DitStructureRule ditStructureRule = byRuleId.get( ruleId );
138 
139         if ( ditStructureRule == null )
140         {
141             String msg = I18n.err( I18n.ERR_04265, ruleId );
142             LOG.debug( msg );
143             throw new LdapException( msg );
144         }
145 
146         if ( DEBUG )
147         {
148             LOG.debug( "Found {} with ruleId: {}", ditStructureRule, ruleId );
149         }
150 
151         return ditStructureRule;
152     }
153 
154 
155     /**
156      * {@inheritDoc}
157      */
158     public void unregister( int ruleId ) throws LdapException
159     {
160         DitStructureRule ditStructureRule = byRuleId.remove( ruleId );
161 
162         if ( DEBUG )
163         {
164             LOG.debug( "Removed {} with ruleId {} from the registry", ditStructureRule, ruleId );
165         }
166     }
167 
168 
169     /**
170      * {@inheritDoc}
171      */
172     public void unregisterSchemaElements( String schemaName )
173     {
174         if ( schemaName == null )
175         {
176             return;
177         }
178 
179         // Loop on all the SchemaObjects stored and remove those associated
180         // with the give schemaName
181         for ( DitStructureRule ditStructureRule : this )
182         {
183             if ( schemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
184             {
185                 int ruleId = ditStructureRule.getRuleId();
186                 SchemaObject removed = byRuleId.remove( ruleId );
187 
188                 if ( DEBUG )
189                 {
190                     LOG.debug( "Removed {} with ruleId {} from the registry", removed, ruleId );
191                 }
192             }
193         }
194     }
195 
196 
197     /**
198      * {@inheritDoc}
199      */
200     public void renameSchema( String originalSchemaName, String newSchemaName )
201     {
202         // Loop on all the SchemaObjects stored and remove those associated
203         // with the give schemaName
204         for ( DitStructureRule ditStructureRule : this )
205         {
206             if ( originalSchemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
207             {
208                 ditStructureRule.setSchemaName( newSchemaName );
209 
210                 if ( DEBUG )
211                 {
212                     LOG.debug( "Renamed {} schemaName to {}", ditStructureRule, newSchemaName );
213                 }
214             }
215         }
216     }
217 
218 
219     /**
220      * {@inheritDoc}
221      */
222     public DefaultDitStructureRuleRegistry copy()
223     {
224         DefaultDitStructureRuleRegistry copy = new DefaultDitStructureRuleRegistry();
225 
226         // Copy the base data
227         copy.copy( this );
228 
229         return copy;
230     }
231 }