1 17 package org.apache.ldap.server.schema; 18 19 20 import org.apache.ldap.server.SystemPartition; 21 import org.apache.ldap.server.schema.bootstrap.BootstrapOidRegistry; 22 23 import javax.naming.NamingException ; 24 import java.util.*; 25 26 27 34 public class GlobalOidRegistry implements OidRegistry 35 { 36 37 private Hashtable byOid = new Hashtable(); 38 39 40 private Hashtable byName = new Hashtable(); 41 42 43 private OidRegistryMonitor monitor = new OidRegistryMonitorAdapter(); 44 45 46 private BootstrapOidRegistry bootstrap; 47 48 49 private SystemPartition systemPartition; 50 51 52 56 57 60 public GlobalOidRegistry( SystemPartition systemPartition, 61 BootstrapOidRegistry bootstrap ) 62 { 63 this.bootstrap = bootstrap; 64 65 if ( this.bootstrap == null ) 66 { 67 throw new NullPointerException ( "the bootstrap registry cannot be null" ) ; 68 } 69 70 this.systemPartition = systemPartition; 71 72 if ( this.systemPartition == null ) 73 { 74 throw new NullPointerException ( "the system partition cannot be null" ) ; 75 } 76 } 77 78 79 80 81 86 OidRegistryMonitor getMonitor() 87 { 88 return monitor; 89 } 90 91 92 97 void setMonitor( OidRegistryMonitor monitor ) 98 { 99 this.monitor = monitor; 100 } 101 102 103 107 108 111 public String getOid( String name ) throws NamingException 112 { 113 if ( name == null ) 114 { 115 throw new NamingException ( "name should not be null" ); 116 } 117 118 122 if ( Character.isDigit( name.charAt( 0 ) ) ) 123 { 124 monitor.getOidWithOid( name ); 125 126 return name; 127 } 128 129 131 if ( byName.containsKey( name ) ) 132 { 133 String oid = ( String ) byName.get( name ); 134 135 monitor.oidResolved( name, oid ); 136 137 return oid; 138 } 139 140 if ( bootstrap.hasOid( name ) ) 141 { 142 String oid = bootstrap.getOid( name ); 143 144 monitor.oidResolved( name, oid ); 145 146 return oid; 147 } 148 149 156 String lowerCase = name.trim().toLowerCase(); 157 158 if ( ! name.equals( lowerCase ) ) 159 { 160 if ( byName.containsKey( lowerCase ) ) 161 { 162 String oid = ( String ) byName.get( lowerCase ); 163 164 monitor.oidResolved( name, lowerCase, oid ); 165 166 168 byName.put( name, oid ); 169 170 return oid; 171 } 172 173 178 179 if ( bootstrap.hasOid( lowerCase) ) 180 { 181 String oid = bootstrap.getOid( name ); 182 183 monitor.oidResolved( name, oid ); 184 185 187 byName.put( name, oid ); 188 189 return oid; 190 } 191 } 192 193 String msg = "OID for name '" + name + "' was not " + "found within the OID registry"; 194 195 NamingException fault = new NamingException ( msg ); 196 197 monitor.oidResolutionFailed( name, fault ); 198 199 throw fault; 200 } 201 202 203 206 public boolean hasOid( String name ) 207 { 208 return this.byName.containsKey( name ) || this.byOid.containsKey( name ) || 209 this.bootstrap.hasOid( name ); 210 } 211 212 213 216 public String getPrimaryName( String oid ) throws NamingException 217 { 218 Object value = byOid.get( oid ); 219 220 if ( null == value ) 221 { 222 String msg = "OID '" + oid + "' was not found within the OID registry"; 223 224 NamingException fault = new NamingException ( msg ); 225 226 monitor.oidDoesNotExist( oid, fault ); 227 228 throw fault; 229 } 230 231 if ( value instanceof String ) 232 { 233 monitor.nameResolved( oid, ( String ) value ); 234 235 return ( String ) value; 236 } 237 238 String name = ( String ) ( ( List ) value ).get( 0 ); 239 240 monitor.nameResolved( oid, name ); 241 242 return name; 243 } 244 245 246 249 public List getNameSet( String oid ) throws NamingException 250 { 251 Object value = byOid.get( oid ); 252 253 if ( null == value ) 254 { 255 String msg = "OID '" + oid + "' was not found within the OID registry"; 256 257 NamingException fault = new NamingException ( msg ); 258 259 monitor.oidDoesNotExist( oid, fault ); 260 261 throw fault; 262 } 263 264 if ( value instanceof String ) 265 { 266 List list = Collections.singletonList( value ); 267 268 monitor.namesResolved( oid, list ); 269 270 return list; 271 } 272 273 monitor.namesResolved( oid, ( List ) value ); 274 275 return ( List ) value; 276 } 277 278 279 282 public Iterator list() 283 { 284 return Collections.unmodifiableSet( byOid.keySet() ).iterator(); 285 } 286 287 288 291 public void register( String name, String oid ) 292 { 293 if ( ! Character.isDigit( oid.charAt( 0 ) ) ) 294 { 295 throw new RuntimeException ( "Swap the parameter order: the oid " + 296 "does not start with a digit!" ); 297 } 298 299 303 String lowerCase = name.toLowerCase(); 304 305 if ( ! lowerCase.equals( name ) ) 306 { 307 byName.put( lowerCase, oid ); 308 } 309 310 byName.put( name, oid ); 312 313 byName.put( oid, oid ); 314 315 325 Object value = null; 326 327 if ( ! byOid.containsKey( oid ) ) 328 { 329 value = name; 330 } 331 else 332 { 333 ArrayList list = null; 334 335 value = byOid.get( oid ); 336 337 if ( value instanceof String ) 338 { 339 String existingName = ( String ) value; 340 341 if ( existingName.equalsIgnoreCase( name ) ) 343 { 344 return; 345 } 346 347 list = new ArrayList(); 348 349 list.add( value ); 350 351 value = list; 352 } 353 else if ( value instanceof ArrayList ) 354 { 355 list = ( ArrayList ) value; 356 357 for ( int ii = 0; ii < list.size(); ii++ ) 358 { 359 if ( ! name.equalsIgnoreCase( ( String ) list.get( ii ) ) ) 361 { 362 return; 363 } 364 } 365 366 list.add( name ); 367 } 368 } 369 370 byOid.put( oid, value ); 371 372 monitor.registered( name, oid ); 373 } 374 } 375 376 | Popular Tags |