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 map of DitStructureRule looked up by RuleId */
048    protected Map<Integer, DitStructureRule> byRuleId;
049
050
051    /**
052     * Creates a new default NormalizerRegistry instance.
053     */
054    public DefaultDitStructureRuleRegistry()
055    {
056        super( SchemaObjectType.DIT_STRUCTURE_RULE, new OidRegistry<DitStructureRule>() );
057        byRuleId = new HashMap<>();
058    }
059
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public boolean contains( int ruleId )
066    {
067        return byRuleId.containsKey( ruleId );
068    }
069
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public Iterator<DitStructureRule> iterator()
076    {
077        return byRuleId.values().iterator();
078    }
079
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public Iterator<Integer> ruleIdIterator()
086    {
087        return byRuleId.keySet().iterator();
088    }
089
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public String getSchemaName( int ruleId ) throws LdapException
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_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}