001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.schema.registries;
021    
022    
023    import java.util.HashMap;
024    import java.util.Iterator;
025    import java.util.Map;
026    
027    import javax.naming.NamingException;
028    
029    import org.apache.directory.shared.i18n.I18n;
030    import org.apache.directory.shared.ldap.schema.DITStructureRule;
031    import org.apache.directory.shared.ldap.schema.SchemaObject;
032    import org.apache.directory.shared.ldap.schema.SchemaObjectType;
033    import org.slf4j.Logger;
034    import org.slf4j.LoggerFactory;
035    
036    
037    /**
038     * A DITStructureRule registry's service default implementation.
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev: 828111 $
042     */
043    public class DefaultDITStructureRuleRegistry extends DefaultSchemaObjectRegistry<DITStructureRule>
044        implements DITStructureRuleRegistry
045    {
046        /** static class logger */
047        private static final Logger LOG = LoggerFactory.getLogger( DefaultDITStructureRuleRegistry.class );
048    
049        /** A speedup for debug */
050        private static final boolean DEBUG = LOG.isDebugEnabled();
051        
052        /** a map of DITStructureRule looked up by RuleId */
053        protected Map<Integer, DITStructureRule> byRuleId;
054        
055        /**
056         * Creates a new default NormalizerRegistry instance.
057         */
058        public DefaultDITStructureRuleRegistry()
059        {
060            super( SchemaObjectType.DIT_STRUCTURE_RULE, new OidRegistry() );
061            byRuleId = new HashMap<Integer, DITStructureRule>();
062        }
063    
064    
065        /**
066         * {@inheritDoc}
067         */
068        public boolean contains( int ruleId )
069        {
070            return byRuleId.containsKey( ruleId );
071        }
072    
073        
074        /**
075         * {@inheritDoc}
076         */
077        public Iterator<DITStructureRule> iterator()
078        {
079            return byRuleId.values().iterator();
080        }
081        
082        
083        /**
084         * {@inheritDoc}
085         */
086        public Iterator<Integer> ruleIdIterator()
087        {
088            return byRuleId.keySet().iterator();
089        }
090        
091        
092        /**
093         * {@inheritDoc}
094         */
095        public String getSchemaName( int ruleId ) throws NamingException
096        {
097            DITStructureRule ditStructureRule = byRuleId.get( ruleId );
098    
099            if ( ditStructureRule != null )
100            {
101                return ditStructureRule.getSchemaName();
102            }
103            
104            String msg = I18n.err( I18n.ERR_04263, ruleId );
105            LOG.warn( msg );
106            throw new NamingException( msg );
107        }
108    
109        
110        /**
111         * {@inheritDoc}
112         */
113        public void register( DITStructureRule ditStructureRule ) throws NamingException
114        {
115            int ruleId = ditStructureRule.getRuleId();
116            
117            if ( byRuleId.containsKey( ruleId ) )
118            {
119                String msg = I18n.err( I18n.ERR_04264, ruleId );
120                LOG.warn( msg );
121                throw new NamingException( msg );
122            }
123    
124            byRuleId.put( ruleId, ditStructureRule );
125            
126            if ( LOG.isDebugEnabled() )
127            {
128                LOG.debug( "registered {} for OID {}", ditStructureRule, ruleId );
129            }
130        }
131    
132        
133        /**
134         * {@inheritDoc}
135         */
136        public DITStructureRule lookup( int ruleId ) throws NamingException
137        {
138            DITStructureRule ditStructureRule = byRuleId.get( ruleId );
139    
140            if ( ditStructureRule == null )
141            {
142                String msg = I18n.err( I18n.ERR_04265, ruleId );
143                LOG.debug( msg );
144                throw new NamingException( msg );
145            }
146    
147            if ( DEBUG )
148            {
149                LOG.debug( "Found {} with ruleId: {}", ditStructureRule, ruleId );
150            }
151            
152            return ditStructureRule;
153        }
154    
155    
156        /**
157         * {@inheritDoc}
158         */
159        public void unregister( int ruleId ) throws NamingException
160        {
161            DITStructureRule ditStructureRule = byRuleId.remove( ruleId );
162            
163            if ( DEBUG )
164            {
165                LOG.debug( "Removed {} with ruleId {} from the registry", ditStructureRule, ruleId );
166            }
167        }
168        
169        
170        /**
171         * {@inheritDoc}
172         */
173        public void unregisterSchemaElements( String schemaName )
174        {
175            if ( schemaName == null )
176            {
177                return;
178            }
179            
180            // Loop on all the SchemaObjects stored and remove those associated
181            // with the give schemaName
182            for ( DITStructureRule ditStructureRule : this )
183            {
184                if ( schemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
185                {
186                    int ruleId = ditStructureRule.getRuleId();
187                    SchemaObject removed = byRuleId.remove( ruleId );
188                    
189                    if ( DEBUG )
190                    {
191                        LOG.debug( "Removed {} with ruleId {} from the registry", removed, ruleId );
192                    }
193                }
194            }
195        }
196    
197        
198        /**
199         * {@inheritDoc}
200         */
201        public void renameSchema( String originalSchemaName, String newSchemaName )
202        {
203            // Loop on all the SchemaObjects stored and remove those associated
204            // with the give schemaName
205            for ( DITStructureRule ditStructureRule : this )
206            {
207                if ( originalSchemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
208                {
209                    ditStructureRule.setSchemaName( newSchemaName );
210    
211                    if ( DEBUG )
212                    {
213                        LOG.debug( "Renamed {} schemaName to {}", ditStructureRule, newSchemaName );
214                    }
215                }
216            }
217        }
218        
219        
220        /**
221         * {@inheritDoc}
222         */
223        public DefaultDITStructureRuleRegistry copy()
224        {
225            DefaultDITStructureRuleRegistry copy = new DefaultDITStructureRuleRegistry();
226            
227            // Copy the base data
228            copy.copy( this );
229            
230            return copy;
231        }
232    }