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.server.schema;
21  
22  
23  import java.util.Comparator;
24  
25  import javax.naming.Name;
26  import javax.naming.NamingException;
27  
28  import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
29  import org.apache.directory.server.schema.registries.Registries;
30  import org.apache.directory.shared.ldap.name.LdapDN;
31  
32  
33  /**
34   * 
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev$
38   */
39  public class DnComparator implements Comparator
40  {
41      // @TODO you'll need this to fix the way normalization is done
42      private AttributeTypeRegistry attrRegistry;
43      
44      
45      public DnComparator( AttributeTypeRegistry attrRegistry )
46      {
47          this.attrRegistry = attrRegistry;
48      }
49      
50      
51      public DnComparator()
52      {
53      }
54  
55      
56      public void setRegistries( Registries registries )
57      {
58          attrRegistry = registries.getAttributeTypeRegistry();
59      }
60      
61      
62      public int compare( Object obj0, Object obj1 ) 
63      {
64          LdapDN dn0 = null;
65          LdapDN dn1 = null;
66          
67          try 
68          {
69              dn0 = getDn( obj0 );
70              dn1 = getDn( obj1 );
71          }
72          catch ( NamingException e )
73          {
74              // -- what do we do here ?
75              return -1;
76          }
77          
78          return dn0.compareTo( dn1 );
79      }
80  
81  
82      public LdapDN getDn( Object obj ) throws NamingException
83      {
84          LdapDN dn = null;
85          
86          if ( obj instanceof LdapDN )
87          {
88              dn = (LdapDN)obj;
89              
90              dn = ( dn.isNormalized() ? dn : LdapDN.normalize( dn, attrRegistry.getNormalizerMapping() ) );
91          }
92          else if ( obj instanceof Name )
93          {
94              dn = new LdapDN( ( Name ) obj );
95              dn.normalize( attrRegistry.getNormalizerMapping() );
96          }
97          else if ( obj instanceof String )
98          {
99              dn = new LdapDN( ( String ) obj );
100             dn.normalize( attrRegistry.getNormalizerMapping() );
101         }
102         else
103         {
104             throw new IllegalStateException( "I do not know how to handle dn comparisons with objects of class: " 
105                 + (obj == null ? null : obj.getClass() ) );
106         }
107         
108         return dn;
109     }
110 }