1 50 package org.apache.avalon.excalibur.naming; 51 52 import java.util.Hashtable ; 53 54 import javax.naming.Context ; 55 import javax.naming.InvalidNameException ; 56 import javax.naming.Name ; 57 import javax.naming.NameParser ; 58 import javax.naming.NamingEnumeration ; 59 import javax.naming.NamingException ; 60 61 69 public abstract class AbstractContext 70 implements Context 71 { 72 private Hashtable m_environment; 73 74 public AbstractContext() 75 { 76 this( new Hashtable () ); 77 } 78 79 public AbstractContext( final Hashtable environment ) 80 { 81 m_environment = environment; 82 } 83 84 protected abstract NameParser getNameParser() 85 throws NamingException ; 86 87 94 public Object addToEnvironment( final String key, final Object value ) 95 throws NamingException 96 { 97 if( null == m_environment ) m_environment = new Hashtable ( 5, 0.75f ); 98 return m_environment.put( key, value ); 99 } 100 101 105 public void close() 106 { 107 m_environment = null; 108 } 109 110 protected boolean isSelf( final Name name ) 111 { 112 return ( name.isEmpty() || name.get( 0 ).equals( "" ) ); 113 } 114 115 122 public void bind( final String name, final Object object ) 123 throws NamingException 124 { 125 bind( getNameParser().parse( name ), object ); 126 } 127 128 135 public void bind( final Name name, final Object object ) 136 throws NamingException 137 { 138 bind( name, object, false ); 139 } 140 141 144 protected abstract void bind( Name name, Object object, boolean rebind ) 145 throws NamingException ; 146 147 155 public String composeName( final String name, final String prefix ) 156 throws NamingException 157 { 158 final NameParser nameParser = getNameParser(); 159 final Name result = 160 composeName( nameParser.parse( name ), nameParser.parse( prefix ) ); 161 return result.toString(); 162 } 163 164 172 public Name composeName( final Name name, final Name prefix ) 173 throws NamingException 174 { 175 final Name result = (Name )( prefix.clone() ); 176 result.addAll( name ); 177 return result; 178 } 179 180 187 public Context createSubcontext( final String name ) 188 throws NamingException 189 { 190 return createSubcontext( getNameParser().parse( name ) ); 191 } 192 193 200 public void destroySubcontext( final String name ) 201 throws NamingException 202 { 203 destroySubcontext( getNameParser().parse( name ) ); 204 } 205 206 211 public Hashtable getEnvironment() 212 throws NamingException 213 { 214 if( null == m_environment ) 215 return new Hashtable ( 3, 0.75f ); 216 else 217 return (Hashtable )m_environment.clone(); 218 } 219 220 227 public NameParser getNameParser( final String name ) 228 throws NamingException 229 { 230 return getNameParser( getNameParser().parse( name ) ); 231 } 232 233 240 public NameParser getNameParser( final Name name ) 241 throws NamingException 242 { 243 if( name.isEmpty() ) 244 { 245 return getNameParser(); 246 } 247 248 Object object = lookup( name ); 249 if( !( object instanceof Context ) ) 250 { 251 object = lookup( getPathName( name ) ); 252 } 253 254 final Context context = (Context )object; 255 final NameParser parser = context.getNameParser( "" ); 256 context.close(); 257 return parser; 258 } 259 260 267 public NamingEnumeration list( final String name ) 268 throws NamingException 269 { 270 return list( getNameParser().parse( name ) ); 271 } 272 273 280 public NamingEnumeration listBindings( final String name ) 281 throws NamingException 282 { 283 return listBindings( getNameParser().parse( name ) ); 284 } 285 286 293 public Object lookup( final String name ) 294 throws NamingException 295 { 296 return lookup( getNameParser().parse( name ) ); 297 } 298 299 306 public Object lookupLink( final String name ) 307 throws NamingException 308 { 309 return lookupLink( getNameParser().parse( name ) ); 310 } 311 312 319 public Object lookupLink( final Name name ) 320 throws NamingException 321 { 322 return lookup( name ); 323 } 324 325 332 public void rebind( final String name, final Object object ) 333 throws NamingException 334 { 335 rebind( getNameParser().parse( name ), object ); 336 } 337 338 345 public void rebind( final Name name, final Object object ) 346 throws NamingException 347 { 348 bind( name, object, true ); 349 } 350 351 357 public Object removeFromEnvironment( final String key ) 358 throws NamingException 359 { 360 if( null == m_environment ) return null; 361 return m_environment.remove( key ); 362 } 363 364 371 public void rename( final String oldName, final String newName ) 372 throws NamingException 373 { 374 rename( getNameParser().parse( oldName ), getNameParser().parse( newName ) ); 375 } 376 377 public void rename( final Name oldName, final Name newName ) 378 throws NamingException 379 { 380 if( isSelf( oldName ) || isSelf( newName ) ) 381 { 382 throw new InvalidNameException ( "Failed to rebind self" ); 383 } 384 else if( oldName.equals( newName ) ) 385 { 386 throw new InvalidNameException ( "Failed to rebind identical names" ); 387 } 388 389 bind( newName, lookup( oldName ) ); 390 unbind( oldName ); 391 } 392 393 399 public void unbind( final String name ) 400 throws NamingException 401 { 402 unbind( getNameParser().parse( name ) ); 403 } 404 405 411 protected final Hashtable getRawEnvironment() 412 { 413 return m_environment; 414 } 415 416 423 protected Name getPathName( final Name name ) 424 throws NamingException 425 { 426 return name.getPrefix( name.size() - 1 ); 427 } 428 429 436 protected Name getLeafName( final Name name ) 437 throws NamingException 438 { 439 return name.getSuffix( name.size() - 1 ); 440 } 441 } 442 | Popular Tags |