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.api.ldap.model.schema.registries;
021
022
023import java.util.HashMap;
024import java.util.Iterator;
025import java.util.Map;
026
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.model.exception.LdapException;
029import org.apache.directory.api.ldap.model.schema.DitStructureRule;
030import org.apache.directory.api.ldap.model.schema.SchemaObject;
031import org.apache.directory.api.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<>();
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public boolean contains( int ruleId )
069    {
070        return byRuleId.containsKey( ruleId );
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public Iterator<DitStructureRule> iterator()
079    {
080        return byRuleId.values().iterator();
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public Iterator<Integer> ruleIdIterator()
089    {
090        return byRuleId.keySet().iterator();
091    }
092
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public String getSchemaName( int ruleId ) throws LdapException
099    {
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}