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.LdapComparator;
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 Comparator registry service default implementation.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class DefaultComparatorRegistry extends DefaultSchemaObjectRegistry<LdapComparator<?>>
40      implements ComparatorRegistry
41  {
42      /** static class logger */
43      private static final Logger LOG = LoggerFactory.getLogger( DefaultComparatorRegistry.class );
44  
45      /**
46       * Creates a new default ComparatorRegistry instance.
47       */
48      public DefaultComparatorRegistry()
49      {
50          super( SchemaObjectType.COMPARATOR, new OidRegistry<LdapComparator<?>>() );
51      }
52  
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public void unregisterSchemaElements( String schemaName ) throws LdapException
59      {
60          if ( schemaName == null )
61          {
62              return;
63          }
64  
65          // Loop on all the SchemaObjects stored and remove those associated
66          // with the give schemaName
67          for ( LdapComparator<?> comparator : this )
68          {
69              if ( schemaName.equalsIgnoreCase( comparator.getSchemaName() ) )
70              {
71                  String oid = comparator.getOid();
72                  SchemaObject removed = unregister( oid );
73  
74                  if ( LOG.isDebugEnabled() )
75                  {
76                      LOG.debug( I18n.msg( I18n.MSG_13702_REMOVED_FROM_REGISTRY, removed, oid ) );
77                  }
78              }
79          }
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public DefaultComparatorRegistry copy()
88      {
89          DefaultComparatorRegistry copy = new DefaultComparatorRegistry();
90  
91          // Copy the base data
92          copy.copy( this );
93  
94          return copy;
95      }
96  
97  
98      /**
99       * @see Object#toString()
100      */
101     @Override
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, LdapComparator<?>> entry : byName.entrySet() )
110         {
111             if ( isFirst )
112             {
113                 isFirst = false;
114             }
115             else
116             {
117                 sb.append( ", " );
118             }
119 
120             LdapComparator<?> comparator = entry.getValue();
121 
122             String fqcn = comparator.getFqcn();
123             int lastDotPos = fqcn.lastIndexOf( '.' );
124 
125             sb.append( '<' ).append( comparator.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 }