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