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