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.ldap.model.exception.LdapException;
026import org.apache.directory.api.ldap.model.schema.LdapSyntax;
027import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
028import org.apache.directory.api.ldap.model.schema.registries.Registries;
029import org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker;
030
031
032/**
033 * An helper class used to store all the methods associated with an LdapSyntax
034 * in relation with the Registries and SchemaManager.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class LdapSyntaxHelper
039{
040    /**
041     * Inject the LdapSyntax into the registries, updating the references to
042     * other SchemaObject
043     *
044     * @param ldapSyntax The LdapSyntax to add to the Registries
045     * @param errors The errors we got while adding the LdapSyntax to the Registries
046     * @param registries The Registries
047     * @exception If the addition failed
048     */
049    public static void addToRegistries( LdapSyntax ldapSyntax, List<Throwable> errors, Registries registries )
050        throws LdapException
051    {
052        if ( registries != null )
053        {
054            try
055            {
056                ldapSyntax.unlock();
057
058                SyntaxChecker syntaxChecker = null;
059
060                try
061                {
062                    // Gets the associated SyntaxChecker
063                    syntaxChecker = registries.getSyntaxCheckerRegistry().lookup( ldapSyntax.getOid() );
064                }
065                catch ( LdapException ne )
066                {
067                    // No SyntaxChecker ? Associate the Syntax to a catch all SyntaxChecker
068                    syntaxChecker = new OctetStringSyntaxChecker( ldapSyntax.getOid() );
069                }
070
071                // Add the references for S :
072                // S -> SC
073                if ( syntaxChecker != null )
074                {
075                    registries.addReference( ldapSyntax, syntaxChecker );
076                    ldapSyntax.setSyntaxChecker( syntaxChecker );
077                }
078            }
079            finally
080            {
081                ldapSyntax.lock();
082            }
083        }
084    }
085
086
087    /**
088     * Remove the LdapSyntax from the Registries, updating the references to
089     * other SchemaObject.
090     * 
091     * If one of the referenced SchemaObject does not exist,
092     * an exception is thrown.
093     *
094     * @param ldapSyntax The LdapSyntax to remove from the Registries
095     * @param errors The errors we got while removing the LdapSyntax from the Registries
096     * @param registries The Registries
097     * @exception If the LdapSyntax is not valid
098     */
099    public static void removeFromRegistries( LdapSyntax ldapSyntax, List<Throwable> errors, Registries registries )
100        throws LdapException
101    {
102        if ( ( registries != null ) && ( ldapSyntax.getSyntaxChecker() != null ) )
103        {
104            /**
105             * Remove the Syntax references (using and usedBy) :
106             * S -> SC
107             */
108            registries.delReference( ldapSyntax, ldapSyntax.getSyntaxChecker() );
109        }
110    }
111}