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.server.schema;
021
022
023 import java.util.Comparator;
024
025 import javax.naming.Name;
026 import javax.naming.NamingException;
027
028 import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
029 import org.apache.directory.server.schema.registries.Registries;
030 import org.apache.directory.shared.ldap.name.LdapDN;
031
032
033 /**
034 *
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev$
038 */
039 public class DnComparator implements Comparator
040 {
041 // @TODO you'll need this to fix the way normalization is done
042 private AttributeTypeRegistry attrRegistry;
043
044
045 public DnComparator( AttributeTypeRegistry attrRegistry )
046 {
047 this.attrRegistry = attrRegistry;
048 }
049
050
051 public DnComparator()
052 {
053 }
054
055
056 public void setRegistries( Registries registries )
057 {
058 attrRegistry = registries.getAttributeTypeRegistry();
059 }
060
061
062 public int compare( Object obj0, Object obj1 )
063 {
064 LdapDN dn0 = null;
065 LdapDN dn1 = null;
066
067 try
068 {
069 dn0 = getDn( obj0 );
070 dn1 = getDn( obj1 );
071 }
072 catch ( NamingException e )
073 {
074 // -- what do we do here ?
075 return -1;
076 }
077
078 return dn0.compareTo( dn1 );
079 }
080
081
082 public LdapDN getDn( Object obj ) throws NamingException
083 {
084 LdapDN dn = null;
085
086 if ( obj instanceof LdapDN )
087 {
088 dn = (LdapDN)obj;
089
090 dn = ( dn.isNormalized() ? dn : LdapDN.normalize( dn, attrRegistry.getNormalizerMapping() ) );
091 }
092 else if ( obj instanceof Name )
093 {
094 dn = new LdapDN( ( Name ) obj );
095 dn.normalize( attrRegistry.getNormalizerMapping() );
096 }
097 else if ( obj instanceof String )
098 {
099 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 }