1 17 package org.apache.ldap.server.schema.bootstrap; 18 19 20 import org.apache.ldap.server.schema.OidRegistry; 21 import org.apache.ldap.server.schema.OidRegistryMonitor; 22 import org.apache.ldap.server.schema.OidRegistryMonitorAdapter; 23 24 import javax.naming.NamingException ; 25 import java.util.*; 26 27 28 35 public class BootstrapOidRegistry implements OidRegistry 36 { 37 38 private Hashtable byOid = new Hashtable(); 39 40 private Hashtable byName = new Hashtable(); 41 42 private OidRegistryMonitor monitor = new OidRegistryMonitorAdapter(); 43 44 45 48 public String getOid( String name ) throws NamingException 49 { 50 if ( name == null ) 51 { 52 throw new NamingException ( "name should not be null" ); 53 } 54 58 if ( Character.isDigit( name.charAt( 0 ) ) ) 59 { 60 monitor.getOidWithOid( name ); 61 return name; 62 } 63 64 if ( byName.containsKey( name ) ) 66 { 67 String oid = ( String ) byName.get( name ); 68 monitor.oidResolved( name, oid ); 69 return oid; 70 } 71 72 79 String lowerCase = name.trim().toLowerCase(); 80 if ( ! name.equals( lowerCase ) 81 && byName.containsKey( lowerCase ) ) 82 { 83 String oid = ( String ) byName.get( lowerCase ); 84 monitor.oidResolved( name, lowerCase, oid ); 85 86 byName.put( name, oid ); 88 return oid; 89 } 90 91 NamingException fault = new NamingException ( "OID for name '" 92 + name + "' was not " + "found within the OID registry" ); 93 monitor.oidResolutionFailed( name, fault ); 94 throw fault; 95 } 96 97 98 101 public boolean hasOid( String name ) 102 { 103 return this.byName.containsKey( name ) || this.byOid.containsKey( name ); 104 } 105 106 107 110 public String getPrimaryName( String oid ) throws NamingException 111 { 112 Object value = byOid.get( oid ); 113 114 if ( null == value ) 115 { 116 NamingException fault = new NamingException ( "OID '" + oid 117 + "' was not found within the OID registry" ); 118 monitor.oidDoesNotExist( oid, fault ); 119 throw fault; 120 } 121 122 if ( value instanceof String ) 123 { 124 monitor.nameResolved( oid, ( String ) value ); 125 return ( String ) value; 126 } 127 128 String name = ( String ) ( ( List ) value ).get( 0 ); 129 monitor.nameResolved( oid, name ); 130 return name; 131 } 132 133 134 137 public List getNameSet( String oid ) throws NamingException 138 { 139 Object value = byOid.get( oid ); 140 141 if ( null == value ) 142 { 143 NamingException fault = new NamingException ( "OID '" + oid 144 + "' was not found within the OID registry" ); 145 monitor.oidDoesNotExist( oid, fault ); 146 throw fault; 147 } 148 149 if ( value instanceof String ) 150 { 151 List list = Collections.singletonList( value ); 152 monitor.namesResolved( oid, list ); 153 return list; 154 } 155 156 monitor.namesResolved( oid, ( List ) value ); 157 return ( List ) value; 158 } 159 160 161 164 public Iterator list() 165 { 166 return Collections.unmodifiableSet( byOid.keySet() ).iterator(); 167 } 168 169 170 173 public void register( String name, String oid ) 174 { 175 if ( ! Character.isDigit( oid.charAt( 0 ) ) ) 176 { 177 throw new RuntimeException ( "Swap the parameter order: the oid " + 178 "does not start with a digit!" ); 179 } 180 181 185 String lowerCase = name.toLowerCase(); 186 if ( ! lowerCase.equals( name ) ) 187 { 188 byName.put( lowerCase, oid ); 189 } 190 191 byName.put( name, oid ); 193 byName.put( oid, oid ); 194 195 205 Object value = null; 206 if ( ! byOid.containsKey( oid ) ) 207 { 208 value = name; 209 } 210 else 211 { 212 ArrayList list = null; 213 value = byOid.get( oid ); 214 215 if ( value instanceof String ) 216 { 217 String existingName = ( String ) value; 218 219 if ( existingName.equalsIgnoreCase( name ) ) 221 { 222 return; 223 } 224 225 list = new ArrayList(); 226 list.add( value ); 227 value = list; 228 } 229 else if ( value instanceof ArrayList ) 230 { 231 list = ( ArrayList ) value; 232 233 for ( int ii = 0; ii < list.size(); ii++ ) 234 { 235 if ( ! name.equalsIgnoreCase( ( String ) list.get( ii ) ) ) 237 { 238 return; 239 } 240 } 241 242 list.add( name ); 243 } 244 } 245 246 byOid.put( oid, value ); 247 monitor.registered( name, oid ); 248 } 249 250 251 256 OidRegistryMonitor getMonitor() 257 { 258 return monitor; 259 } 260 261 262 267 void setMonitor( OidRegistryMonitor monitor ) 268 { 269 this.monitor = monitor; 270 } 271 } 272 273 | Popular Tags |