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 */
020package org.apache.directory.shared.ldap.model.schema.registries;
021
022
023import java.util.HashMap;
024import java.util.Iterator;
025import java.util.Map;
026
027import org.apache.directory.shared.i18n.I18n;
028import org.apache.directory.shared.ldap.model.exception.LdapException;
029import org.apache.directory.shared.ldap.model.schema.DITStructureRule;
030import org.apache.directory.shared.ldap.model.schema.SchemaObject;
031import org.apache.directory.shared.ldap.model.schema.SchemaObjectType;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035
036/**
037 * A DITStructureRule registry's service default implementation.
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class DefaultDITStructureRuleRegistry extends DefaultSchemaObjectRegistry<DITStructureRule>
042    implements DITStructureRuleRegistry
043{
044    /** static class logger */
045    private static final Logger LOG = LoggerFactory.getLogger( DefaultDITStructureRuleRegistry.class );
046
047    /** A speedup for debug */
048    private static final boolean DEBUG = LOG.isDebugEnabled();
049
050    /** a map of DITStructureRule looked up by RuleId */
051    protected Map<Integer, DITStructureRule> byRuleId;
052
053
054    /**
055     * Creates a new default NormalizerRegistry instance.
056     */
057    public DefaultDITStructureRuleRegistry()
058    {
059        super( SchemaObjectType.DIT_STRUCTURE_RULE, new OidRegistry<DITStructureRule>() );
060        byRuleId = new HashMap<Integer, DITStructureRule>();
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    public boolean contains( int ruleId )
068    {
069        return byRuleId.containsKey( ruleId );
070    }
071
072
073    /**
074     * {@inheritDoc}
075     */
076    public Iterator<DITStructureRule> iterator()
077    {
078        return byRuleId.values().iterator();
079    }
080
081
082    /**
083     * {@inheritDoc}
084     */
085    public Iterator<Integer> ruleIdIterator()
086    {
087        return byRuleId.keySet().iterator();
088    }
089
090
091    /**
092     * {@inheritDoc}
093     */
094    public String getSchemaName( int ruleId ) throws LdapException
095    {
096        DITStructureRule ditStructureRule = byRuleId.get( ruleId );
097
098        if ( ditStructureRule != null )
099        {
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}