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.helper;
021
022
023import java.util.List;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.exception.LdapSchemaException;
028import org.apache.directory.api.ldap.model.exception.LdapSchemaExceptionCodes;
029import org.apache.directory.api.ldap.model.schema.LdapComparator;
030import org.apache.directory.api.ldap.model.schema.LdapSyntax;
031import org.apache.directory.api.ldap.model.schema.MatchingRule;
032import org.apache.directory.api.ldap.model.schema.MutableMatchingRule;
033import org.apache.directory.api.ldap.model.schema.Normalizer;
034import org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator;
035import org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer;
036import org.apache.directory.api.ldap.model.schema.registries.Registries;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040
041/**
042 * An helper class used to store all the methods associated with an MatchingRule
043 * in relation with the Registries and SchemaManager.
044 * 
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public final class MatchingRuleHelper
048{
049    /** A logger for this class */
050    private static final Logger LOG = LoggerFactory.getLogger( MatchingRuleHelper.class );
051
052
053    private MatchingRuleHelper()
054    {
055    }
056
057
058    /**
059     * Inject the MatchingRule into the Registries, updating the references to
060     * other SchemaObject
061     *
062     * @param matchingRule The MatchingRule to add to the Registries
063     * @param errors The errors we got while adding the MatchingRule to the registries
064     * @param registries The Registries
065     * @throws LdapException If the addition failed
066     */
067    @SuppressWarnings("rawtypes")
068    public static void addToRegistries( MutableMatchingRule matchingRule, List<Throwable> errors, Registries registries )
069        throws LdapException
070    {
071        if ( registries != null )
072        {
073            try
074            {
075                matchingRule.unlock();
076
077                LdapComparator<?> ldapComparator = null;
078                Normalizer normalizer = null;
079                LdapSyntax ldapSyntax = null;
080
081                try
082                {
083                    // Gets the associated Comparator
084                    ldapComparator = registries.getComparatorRegistry().lookup( matchingRule.getOid() );
085                }
086                catch ( LdapException ne )
087                {
088                    // Default to a catch all comparator
089                    ldapComparator = new ComparableComparator( matchingRule.getOid() );
090                }
091
092                try
093                {
094                    // Gets the associated Normalizer
095                    normalizer = registries.getNormalizerRegistry().lookup( matchingRule.getOid() );
096                }
097                catch ( LdapException ne )
098                {
099                    // Default to the NoOp normalizer
100                    normalizer = new NoOpNormalizer( matchingRule.getOid() );
101                }
102
103                try
104                {
105                    // Get the associated LdapSyntax
106                    ldapSyntax = registries.getLdapSyntaxRegistry().lookup( matchingRule.getSyntaxOid() );
107                }
108                catch ( LdapException ne )
109                {
110                    // The Syntax is a mandatory element, it must exist.
111                    String msg = I18n.err( I18n.ERR_04317 );
112
113                    LdapSchemaException ldapSchemaException = new LdapSchemaException(
114                        LdapSchemaExceptionCodes.MR_NONEXISTENT_SYNTAX, msg, ne );
115                    ldapSchemaException.setSourceObject( matchingRule );
116                    ldapSchemaException.setRelatedId( matchingRule.getSyntaxOid() );
117                    errors.add( ldapSchemaException );
118                    LOG.info( msg );
119                }
120
121                /**
122                 * Add the MR references (using and usedBy) :
123                 * MR -> C
124                 * MR -> N
125                 * MR -> S
126                 */
127                if ( ldapComparator != null )
128                {
129                    registries.addReference( matchingRule, ldapComparator );
130                    matchingRule.setLdapComparator( ldapComparator );
131                }
132
133                if ( normalizer != null )
134                {
135                    registries.addReference( matchingRule, normalizer );
136                    matchingRule.setNormalizer( normalizer );
137                }
138
139                if ( ldapSyntax != null )
140                {
141                    registries.addReference( matchingRule, ldapSyntax );
142                    matchingRule.setSyntax( ldapSyntax );
143                }
144            }
145            finally
146            {
147                matchingRule.lock();
148            }
149        }
150    }
151
152
153    /**
154     * Remove the MatchingRule from the Registries, updating the references to
155     * other SchemaObject.
156     * 
157     * If one of the referenced SchemaObject does not exist,
158     * an exception is thrown.
159     *
160     * @param matchingRule The MatchingRule to remove from the Registries
161     * @param errors The errors we got while removing the MatchingRule from the registries
162     * @param registries The Registries
163     * @throws LdapException If the MatchingRule is not valid
164     */
165    public static void removeFromRegistries( MatchingRule matchingRule, List<Throwable> errors, Registries registries )
166        throws LdapException
167    {
168        if ( registries != null )
169        {
170            /**
171             * Remove the MR references (using and usedBy) :
172             * MR -> C
173             * MR -> N
174             * MR -> S
175             */
176            if ( matchingRule.getLdapComparator() != null )
177            {
178                registries.delReference( matchingRule, matchingRule.getLdapComparator() );
179            }
180
181            if ( matchingRule.getSyntax() != null )
182            {
183                registries.delReference( matchingRule, matchingRule.getSyntax() );
184            }
185
186            if ( matchingRule.getNormalizer() != null )
187            {
188                registries.delReference( matchingRule, matchingRule.getNormalizer() );
189            }
190        }
191    }
192}