View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.registries;
21  
22  
23  import java.util.Map;
24  
25  import org.apache.directory.api.ldap.model.exception.LdapException;
26  import org.apache.directory.api.ldap.model.schema.Normalizer;
27  import org.apache.directory.api.ldap.model.schema.SchemaObject;
28  import org.apache.directory.api.ldap.model.schema.SchemaObjectType;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * A Normalizer registry's service default implementation.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class DefaultNormalizerRegistry extends DefaultSchemaObjectRegistry<Normalizer>
39      implements NormalizerRegistry
40  {
41      /** static class logger */
42      private static final Logger LOG = LoggerFactory.getLogger( DefaultNormalizerRegistry.class );
43  
44      /** A speedup for debug */
45      private static final boolean DEBUG = LOG.isDebugEnabled();
46  
47  
48      /**
49       * Creates a new default NormalizerRegistry instance.
50       */
51      public DefaultNormalizerRegistry()
52      {
53          super( SchemaObjectType.NORMALIZER, new OidRegistry<Normalizer>() );
54      }
55  
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public void unregisterSchemaElements( String schemaName ) throws LdapException
62      {
63          if ( schemaName == null )
64          {
65              return;
66          }
67  
68          // Loop on all the SchemaObjects stored and remove those associated
69          // with the give schemaName
70          for ( Normalizer normalizer : this )
71          {
72              if ( schemaName.equalsIgnoreCase( normalizer.getSchemaName() ) )
73              {
74                  String oid = normalizer.getOid();
75                  SchemaObject removed = unregister( oid );
76  
77                  if ( DEBUG )
78                  {
79                      LOG.debug( "Removed {} with oid {} from the registry", removed, oid );
80                  }
81              }
82          }
83      }
84  
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public DefaultNormalizerRegistry copy()
91      {
92          DefaultNormalizerRegistry copy = new DefaultNormalizerRegistry();
93  
94          // Copy the base data
95          copy.copy( this );
96  
97          return copy;
98      }
99  
100 
101     /**
102      * @see Object#toString()
103      */
104     @Override
105     public String toString()
106     {
107         StringBuilder sb = new StringBuilder();
108 
109         sb.append( schemaObjectType ).append( ": " );
110         boolean isFirst = true;
111 
112         for ( Map.Entry<String, Normalizer> entry : byName.entrySet() )
113         {
114             if ( isFirst )
115             {
116                 isFirst = false;
117             }
118             else
119             {
120                 sb.append( ", " );
121             }
122 
123             Normalizer normalizer = entry.getValue();
124 
125             String fqcn = normalizer.getFqcn();
126             int lastDotPos = fqcn.lastIndexOf( '.' );
127 
128             sb.append( '<' ).append( normalizer.getOid() ).append( ", " );
129 
130             if ( lastDotPos > 0 )
131             {
132                 sb.append( fqcn.substring( lastDotPos + 1 ) );
133             }
134             else
135             {
136                 sb.append( fqcn );
137             }
138 
139             sb.append( '>' );
140         }
141 
142         return sb.toString();
143     }
144 }