1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.registries;
21
22
23 import java.util.ArrayList;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.naming.NamingException;
31
32 import org.apache.directory.shared.ldap.schema.syntax.ComparatorDescription;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43 public class DefaultComparatorRegistry implements ComparatorRegistry
44 {
45
46 private static final Logger LOG = LoggerFactory.getLogger( DefaultComparatorRegistry.class );
47
48
49 private final Map<String,Comparator> byOid;
50
51
52 private final Map<String,ComparatorDescription> oidToDescription;
53
54
55 private static final boolean DEBUG = LOG.isDebugEnabled();
56
57
58
59
60
61
62
63
64
65
66 public DefaultComparatorRegistry()
67 {
68 this.byOid = new HashMap<String, Comparator>();
69 this.oidToDescription = new HashMap<String,ComparatorDescription>();
70 }
71
72
73
74
75
76
77
78 public void register( ComparatorDescription description, Comparator comparator ) throws NamingException
79 {
80 if ( byOid.containsKey( description.getNumericOid() ) )
81 {
82 throw new NamingException( "Comparator with OID " + description.getNumericOid()
83 + " already registered!" );
84 }
85
86 oidToDescription.put( description.getNumericOid(), description );
87 byOid.put( description.getNumericOid(), comparator );
88
89 if ( DEBUG )
90 {
91 LOG.debug( "registed comparator with OID: " + description.getNumericOid() );
92 }
93 }
94
95
96 private static String getSchema( ComparatorDescription desc )
97 {
98 List values = desc.getExtensions().get( "X-SCHEMA" );
99
100 if ( values == null || values.size() == 0 )
101 {
102 return "other";
103 }
104
105 return desc.getExtensions().get( "X-SCHEMA" ).get( 0 );
106 }
107
108
109 public Comparator lookup( String oid ) throws NamingException
110 {
111 if ( byOid.containsKey( oid ) )
112 {
113 Comparator c = byOid.get( oid );
114
115 if ( DEBUG )
116 {
117 LOG.debug( "looked up comparator with OID: " + oid );
118 }
119
120 return c;
121 }
122
123 throw new NamingException( "Comparator not found for OID: " + oid );
124 }
125
126
127 public boolean hasComparator( String oid )
128 {
129 return byOid.containsKey( oid );
130 }
131
132
133 public String getSchemaName( String oid ) throws NamingException
134 {
135 if ( ! Character.isDigit( oid.charAt( 0 ) ) )
136 {
137 throw new NamingException( "OID " + oid + " is not a numeric OID" );
138 }
139
140 if ( oidToDescription.containsKey( oid ) )
141 {
142 return getSchema( oidToDescription.get( oid ) );
143 }
144
145 throw new NamingException( "OID " + oid + " not found in oid to " + "description map!" );
146 }
147
148
149 public Iterator<String> oidIterator()
150 {
151 return byOid.keySet().iterator();
152 }
153
154
155 public void unregister( String oid ) throws NamingException
156 {
157 if ( ! Character.isDigit( oid.charAt( 0 ) ) )
158 {
159 throw new NamingException( "OID " + oid + " is not a numeric OID" );
160 }
161
162 this.byOid.remove( oid );
163 this.oidToDescription.remove( oid );
164 }
165
166
167 public void unregisterSchemaElements( String schemaName )
168 {
169 List<String> oids = new ArrayList<String>( byOid.keySet() );
170 for ( String oid : oids )
171 {
172 ComparatorDescription description = oidToDescription.get( oid );
173 String schemaNameForOid = getSchema( description );
174 if ( schemaNameForOid.equalsIgnoreCase( schemaName ) )
175 {
176 byOid.remove( oid );
177 oidToDescription.remove( oid );
178 }
179 }
180 }
181
182
183 public void renameSchema( String originalSchemaName, String newSchemaName )
184 {
185 List<String> oids = new ArrayList<String>( byOid.keySet() );
186 for ( String oid : oids )
187 {
188 ComparatorDescription description = oidToDescription.get( oid );
189 String schemaNameForOid = getSchema( description );
190 if ( schemaNameForOid.equalsIgnoreCase( originalSchemaName ) )
191 {
192 List<String> schemaExt = description.getExtensions().get( "X-SCHEMA" );
193 schemaExt.clear();
194 schemaExt.add( newSchemaName );
195 }
196 }
197 }
198
199
200 public Iterator<ComparatorDescription> comparatorDescriptionIterator()
201 {
202 return oidToDescription.values().iterator();
203 }
204 }