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   *    https://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.i18n.I18n;
26  import org.apache.directory.api.ldap.model.exception.LdapException;
27  import org.apache.directory.api.ldap.model.schema.Normalizer;
28  import org.apache.directory.api.ldap.model.schema.SchemaObject;
29  import org.apache.directory.api.ldap.model.schema.SchemaObjectType;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A Normalizer registry's service default implementation.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class DefaultNormalizerRegistry extends DefaultSchemaObjectRegistry<Normalizer>
40      implements NormalizerRegistry
41  {
42      /** static class logger */
43      private static final Logger LOG = LoggerFactory.getLogger( DefaultNormalizerRegistry.class );
44  
45  
46      /**
47       * Creates a new default NormalizerRegistry instance.
48       */
49      public DefaultNormalizerRegistry()
50      {
51          super( SchemaObjectType.NORMALIZER, new OidRegistry<Normalizer>() );
52      }
53  
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public void unregisterSchemaElements( String schemaName ) throws LdapException
60      {
61          if ( schemaName == null )
62          {
63              return;
64          }
65  
66          // Loop on all the SchemaObjects stored and remove those associated
67          // with the give schemaName
68          for ( Normalizer normalizer : this )
69          {
70              if ( schemaName.equalsIgnoreCase( normalizer.getSchemaName() ) )
71              {
72                  String oid = normalizer.getOid();
73                  SchemaObject removed = unregister( oid );
74  
75                  if ( LOG.isDebugEnabled() )
76                  {
77                      LOG.debug( I18n.msg( I18n.MSG_13702_REMOVED_FROM_REGISTRY, removed, oid ) );
78                  }
79              }
80          }
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public DefaultNormalizerRegistry copy()
89      {
90          DefaultNormalizerRegistry copy = new DefaultNormalizerRegistry();
91  
92          // Copy the base data
93          copy.copy( this );
94  
95          return copy;
96      }
97  
98  
99      /**
100      * @see Object#toString()
101      */
102     @Override
103     public String toString()
104     {
105         StringBuilder sb = new StringBuilder();
106 
107         sb.append( schemaObjectType ).append( ": " );
108         boolean isFirst = true;
109 
110         for ( Map.Entry<String, Normalizer> entry : byName.entrySet() )
111         {
112             if ( isFirst )
113             {
114                 isFirst = false;
115             }
116             else
117             {
118                 sb.append( ", " );
119             }
120 
121             Normalizer normalizer = entry.getValue();
122 
123             String fqcn = normalizer.getFqcn();
124             int lastDotPos = fqcn.lastIndexOf( '.' );
125 
126             sb.append( '<' ).append( normalizer.getOid() ).append( ", " );
127 
128             if ( lastDotPos > 0 )
129             {
130                 sb.append( fqcn.substring( lastDotPos + 1 ) );
131             }
132             else
133             {
134                 sb.append( fqcn );
135             }
136 
137             sb.append( '>' );
138         }
139 
140         return sb.toString();
141     }
142 }