1 8 package org.codehaus.spice.jndikit; 9 10 import java.util.Hashtable ; 11 import javax.naming.Context ; 12 import javax.naming.InvalidNameException ; 13 import javax.naming.Name ; 14 import javax.naming.NameParser ; 15 import javax.naming.NamingEnumeration ; 16 import javax.naming.NamingException ; 17 18 25 public abstract class AbstractContext 26 implements Context 27 { 28 private Hashtable m_environment; 29 30 public AbstractContext() 31 { 32 this( new Hashtable () ); 33 } 34 35 public AbstractContext( final Hashtable environment ) 36 { 37 m_environment = environment; 38 } 39 40 protected abstract NameParser getNameParser() 41 throws NamingException ; 42 43 50 public Object addToEnvironment( final String key, final Object value ) 51 throws NamingException 52 { 53 if( null == m_environment ) 54 { 55 m_environment = new Hashtable ( 5, 0.75f ); 56 } 57 return m_environment.put( key, value ); 58 } 59 60 64 public void close() 65 { 66 m_environment = null; 67 } 68 69 protected boolean isSelf( final Name name ) 70 { 71 return ( name.isEmpty() || name.get( 0 ).equals( "" ) ); 72 } 73 74 82 public void bind( final String name, final Object object ) 83 throws NamingException 84 { 85 bind( getNameParser().parse( name ), object ); 86 } 87 88 96 public void bind( final Name name, final Object object ) 97 throws NamingException 98 { 99 bind( name, object, false ); 100 } 101 102 105 protected abstract void bind( Name name, Object object, boolean rebind ) 106 throws NamingException ; 107 108 116 public String composeName( final String name, final String prefix ) 117 throws NamingException 118 { 119 final NameParser nameParser = getNameParser(); 120 final Name result = 121 composeName( nameParser.parse( name ), nameParser.parse( prefix ) ); 122 return result.toString(); 123 } 124 125 133 public Name composeName( final Name name, final Name prefix ) 134 throws NamingException 135 { 136 final Name result = (Name )( prefix.clone() ); 137 result.addAll( name ); 138 return result; 139 } 140 141 149 public Context createSubcontext( final String name ) 150 throws NamingException 151 { 152 return createSubcontext( getNameParser().parse( name ) ); 153 } 154 155 162 public void destroySubcontext( final String name ) 163 throws NamingException 164 { 165 destroySubcontext( getNameParser().parse( name ) ); 166 } 167 168 173 public Hashtable getEnvironment() 174 throws NamingException 175 { 176 if( null == m_environment ) 177 { 178 return new Hashtable ( 3, 0.75f ); 179 } 180 else 181 { 182 return (Hashtable )m_environment.clone(); 183 } 184 } 185 186 193 public NameParser getNameParser( final String name ) 194 throws NamingException 195 { 196 return getNameParser( getNameParser().parse( name ) ); 197 } 198 199 206 public NameParser getNameParser( final Name name ) 207 throws NamingException 208 { 209 if( name.isEmpty() ) 210 { 211 return getNameParser(); 212 } 213 214 Object object = lookup( name ); 215 if( !( object instanceof Context ) ) 216 { 217 object = lookup( getPathName( name ) ); 218 } 219 220 final Context context = (Context )object; 221 final NameParser parser = context.getNameParser( "" ); 222 context.close(); 223 return parser; 224 } 225 226 233 public NamingEnumeration list( final String name ) 234 throws NamingException 235 { 236 return list( getNameParser().parse( name ) ); 237 } 238 239 246 public NamingEnumeration listBindings( final String name ) 247 throws NamingException 248 { 249 return listBindings( getNameParser().parse( name ) ); 250 } 251 252 260 public Object lookup( final String name ) 261 throws NamingException 262 { 263 return lookup( getNameParser().parse( name ) ); 264 } 265 266 274 public Object lookupLink( final String name ) 275 throws NamingException 276 { 277 return lookupLink( getNameParser().parse( name ) ); 278 } 279 280 288 public Object lookupLink( final Name name ) 289 throws NamingException 290 { 291 return lookup( name ); 292 } 293 294 301 public void rebind( final String name, final Object object ) 302 throws NamingException 303 { 304 rebind( getNameParser().parse( name ), object ); 305 } 306 307 314 public void rebind( final Name name, final Object object ) 315 throws NamingException 316 { 317 bind( name, object, true ); 318 } 319 320 326 public Object removeFromEnvironment( final String key ) 327 throws NamingException 328 { 329 if( null == m_environment ) 330 { 331 return null; 332 } 333 return m_environment.remove( key ); 334 } 335 336 343 public void rename( final String oldName, final String newName ) 344 throws NamingException 345 { 346 rename( getNameParser().parse( oldName ), getNameParser().parse( newName ) ); 347 } 348 349 public void rename( final Name oldName, final Name newName ) 350 throws NamingException 351 { 352 if( isSelf( oldName ) || isSelf( newName ) ) 353 { 354 final String message = "Failed to rebind self"; 355 throw new InvalidNameException ( message ); 356 } 357 else if( oldName.equals( newName ) ) 358 { 359 final String message = "Failed to rebind identical names"; 360 throw new InvalidNameException ( message ); 361 } 362 363 bind( newName, lookup( oldName ) ); 364 unbind( oldName ); 365 } 366 367 373 public void unbind( final String name ) 374 throws NamingException 375 { 376 unbind( getNameParser().parse( name ) ); 377 } 378 379 385 protected final Hashtable getRawEnvironment() 386 { 387 return m_environment; 388 } 389 390 397 protected Name getPathName( final Name name ) 398 throws NamingException 399 { 400 return name.getPrefix( name.size() - 1 ); 401 } 402 403 410 protected Name getLeafName( final Name name ) 411 throws NamingException 412 { 413 return name.getSuffix( name.size() - 1 ); 414 } 415 } 416 | Popular Tags |