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