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.Map;
024
025import org.apache.directory.api.ldap.model.exception.LdapException;
026import org.apache.directory.api.ldap.model.schema.SchemaObject;
027import org.apache.directory.api.ldap.model.schema.SchemaObjectType;
028import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032
033/**
034 * SyntaxChecker registry component's service interface.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class DefaultSyntaxCheckerRegistry extends DefaultSchemaObjectRegistry<SyntaxChecker>
039    implements SyntaxCheckerRegistry
040{
041    /** static class logger */
042    private static final Logger LOG = LoggerFactory.getLogger( DefaultSyntaxCheckerRegistry.class );
043
044    /** A speedup for debug */
045    private static final boolean DEBUG = LOG.isDebugEnabled();
046
047
048    /**
049     * Creates a new default SyntaxCheckerRegistry instance.
050     */
051    public DefaultSyntaxCheckerRegistry()
052    {
053        super( SchemaObjectType.SYNTAX_CHECKER, new OidRegistry<SyntaxChecker>() );
054    }
055
056
057    /**
058     * {@inheritDoc}
059     */
060    public void unregisterSchemaElements( String schemaName ) throws LdapException
061    {
062        if ( schemaName == null )
063        {
064            return;
065        }
066
067        // Loop on all the SchemaObjects stored and remove those associated
068        // with the give schemaName
069        for ( SyntaxChecker syntaxChecker : this )
070        {
071            if ( schemaName.equalsIgnoreCase( syntaxChecker.getSchemaName() ) )
072            {
073                String oid = syntaxChecker.getOid();
074                SchemaObject removed = unregister( oid );
075
076                if ( DEBUG )
077                {
078                    LOG.debug( "Removed {} with oid {} from the registry", removed, oid );
079                }
080            }
081        }
082    }
083
084
085    /**
086     * {@inheritDoc}
087     */
088    public DefaultSyntaxCheckerRegistry copy()
089    {
090        DefaultSyntaxCheckerRegistry copy = new DefaultSyntaxCheckerRegistry();
091
092        // Copy the base data
093        copy.copy( this );
094
095        return copy;
096    }
097
098
099    /**
100     * @see Object#toString()
101     */
102    public String toString()
103    {
104        StringBuilder sb = new StringBuilder();
105
106        sb.append( schemaObjectType ).append( ": " );
107        boolean isFirst = true;
108
109        for ( Map.Entry<String, SyntaxChecker> entry : byName.entrySet() )
110        {
111            if ( isFirst )
112            {
113                isFirst = false;
114            }
115            else
116            {
117                sb.append( ", " );
118            }
119
120            SyntaxChecker syntaxChecker = entry.getValue();
121
122            String fqcn = syntaxChecker.getFqcn();
123            int lastDotPos = fqcn.lastIndexOf( '.' );
124
125            sb.append( '<' ).append( syntaxChecker.getOid() ).append( ", " );
126
127            if ( lastDotPos > 0 )
128            {
129                sb.append( fqcn.substring( lastDotPos + 1 ) );
130            }
131            else
132            {
133                sb.append( fqcn );
134            }
135
136            sb.append( '>' );
137        }
138
139        return sb.toString();
140    }
141}