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.bootstrap;
21
22
23 import java.io.Serializable;
24 import java.util.Comparator;
25
26 import javax.naming.NamingException;
27
28 import org.apache.directory.server.schema.registries.OidRegistry;
29 import org.apache.directory.server.schema.registries.Registries;
30 import org.apache.directory.shared.ldap.util.StringTools;
31
32
33 /**
34 * A comparator that sorts OIDs based on their numeric id value. Needs a
35 * OidRegistry to properly do it's job. Public method to set the oid
36 * registry will be used by the server after instantiation in deserialization.
37 *
38 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39 * @version $Rev$
40 */
41 public class NameOrNumericIdComparator implements Comparator, Serializable
42 {
43 private static final long serialVersionUID = 1L;
44 private transient OidRegistry registry;
45
46
47 public NameOrNumericIdComparator( OidRegistry registry )
48 {
49 this.registry = registry;
50 }
51
52
53 public NameOrNumericIdComparator()
54 {
55 }
56
57
58 /* (non-Javadoc)
59 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
60 */
61 public int compare( Object o1, Object o2 )
62 {
63 String s1 = getNumericIdString( o1 );
64 String s2 = getNumericIdString( o2 );
65
66 if ( s1 == null && s2 == null )
67 {
68 return 0;
69 }
70
71 if ( s1 == null )
72 {
73 return -1;
74 }
75
76 if ( s2 == null )
77 {
78 return 1;
79 }
80
81 return s1.compareTo( s2 );
82 }
83
84
85 public void setRegistries( Registries registries )
86 {
87 registry = registries.getOidRegistry();
88 }
89
90
91 String getNumericIdString( Object obj )
92 {
93 String strValue;
94
95 if ( obj == null )
96 {
97 return null;
98 }
99
100 if ( obj instanceof String )
101 {
102 strValue = ( String ) obj;
103 }
104 else if ( obj instanceof byte[] )
105 {
106 strValue = StringTools.utf8ToString( ( byte[] ) obj );
107 }
108 else
109 {
110 strValue = obj.toString();
111 }
112
113 if ( strValue.length() == 0 )
114 {
115 return "";
116 }
117
118 if ( registry.hasOid( strValue ) )
119 {
120 try
121 {
122 return registry.getOid( strValue );
123 }
124 catch ( NamingException e )
125 {
126 throw new RuntimeException( "Failed to lookup OID for " + strValue, e );
127 }
128 }
129
130 return strValue;
131 }
132 }