1 50 package org.apache.avalon.excalibur.naming; 51 52 import java.util.Hashtable ; 53 54 import javax.naming.Context ; 55 import javax.naming.ContextNotEmptyException ; 56 import javax.naming.InvalidNameException ; 57 import javax.naming.Name ; 58 import javax.naming.NameAlreadyBoundException ; 59 import javax.naming.NameParser ; 60 import javax.naming.NamingEnumeration ; 61 import javax.naming.NamingException ; 62 import javax.naming.NotContextException ; 63 import javax.naming.OperationNotSupportedException ; 64 import javax.naming.Reference ; 65 import javax.naming.Referenceable ; 66 67 76 public abstract class AbstractLocalContext 77 extends AbstractContext 78 { 79 private Context m_parent; 80 private Namespace m_namespace; 81 82 public AbstractLocalContext( final Namespace namespace, 83 final Hashtable environment, 84 final Context parent ) 85 { 86 super( environment ); 87 m_namespace = namespace; 88 m_parent = parent; 89 } 90 91 96 protected final Context getParent() 97 { 98 return m_parent; 99 } 100 101 106 protected final Namespace getNamespace() 107 { 108 return m_namespace; 109 } 110 111 protected boolean isDestroyableContext( final Object object ) 112 throws NamingException 113 { 114 return getClass().isInstance( object ); 115 } 116 117 protected abstract Context newContext() 118 throws NamingException ; 119 120 protected abstract Context cloneContext() 121 throws NamingException ; 122 123 126 protected void bind( final Name name, Object object, final boolean rebind ) 127 throws NamingException 128 { 129 if( isSelf( name ) ) 130 { 131 throw new InvalidNameException ( "Failed to bind self" ); 132 } 133 134 if( 1 == name.size() ) 135 { 136 boolean alreadyBound = false; 137 try 138 { 139 localLookup( name ); 140 alreadyBound = true; 141 } 142 catch( final NamingException ne ) 143 { 144 } 145 146 if( !rebind && alreadyBound ) 147 { 148 throw new NameAlreadyBoundException ( name.get( 0 ) ); 149 } 150 else 151 { 152 if( object instanceof Referenceable ) 154 { 155 object = ( (Referenceable )object ).getReference(); 156 } 157 158 final Name atom = name.getPrefix( 1 ); 160 object = m_namespace.getStateToBind( object, atom, this, getRawEnvironment() ); 161 162 doLocalBind( name, object ); 163 } 164 } 165 else 166 { 167 final Context context = lookupSubContext( getPathName( name ) ); 168 if( rebind ) 169 { 170 context.rebind( getLeafName( name ), object ); 171 } 172 else 173 { 174 context.bind( getLeafName( name ), object ); 175 } 176 } 177 } 178 179 protected abstract void doLocalBind( Name name, Object object ) 180 throws NamingException ; 181 182 public void close() 183 { 184 m_parent = null; 185 m_namespace = null; 186 } 187 188 195 public Context createSubcontext( final Name name ) 196 throws NamingException 197 { 198 final Context context = newContext(); 199 bind( name, context ); 200 return context; 201 } 202 203 public void destroySubcontext( final Name name ) 204 throws NamingException 205 { 206 if( isSelf( name ) ) 207 { 208 throw new InvalidNameException ( "Failed to destroy self" ); 209 } 210 211 if( 1 == name.size() ) 212 { 213 Object object = null; 214 try 215 { 216 object = localLookup( name ); 217 } 218 catch( final NamingException ne ) 219 { 220 return; 221 } 222 223 checkUnbindContext( name, object ); 224 225 doLocalUnbind( name ); 226 } 227 else 228 { 229 final Context context = lookupSubContext( getPathName( name ) ); 230 231 Object object = null; 232 233 final Name atom = getLeafName( name ); 234 try 235 { 236 object = context.lookup( atom ); 237 } 238 catch( final NamingException ne ) 239 { 240 return; 241 } 242 243 checkUnbindContext( atom, object ); 244 245 context.destroySubcontext( atom ); 246 } 247 } 248 249 protected void checkUnbindContext( final Name name, final Object entry ) 250 throws NamingException 251 { 252 if( !isDestroyableContext( entry ) ) 253 { 254 throw new NotContextException ( name.toString() ); 255 } 256 257 final Context context = (Context )entry; 258 if( context.list( "" ).hasMoreElements() ) 259 { 260 throw new ContextNotEmptyException ( name.toString() ); 261 } 262 } 263 264 public String getNameInNamespace() 265 throws NamingException 266 { 267 throw new OperationNotSupportedException ( "Namespace has no notion of a 'full name'" ); 268 } 269 270 protected NameParser getNameParser() 271 throws NamingException 272 { 273 return m_namespace.getNameParser(); 274 } 275 276 283 public NamingEnumeration list( final Name name ) 284 throws NamingException 285 { 286 if( isSelf( name ) ) 287 { 288 return doLocalList(); 289 } 290 else 291 { 292 final Context context = lookupSubContext( name ); 294 return context.list( "" ); 295 } 296 } 297 298 protected abstract NamingEnumeration doLocalList() 299 throws NamingException ; 300 301 protected abstract NamingEnumeration doLocalListBindings() 302 throws NamingException ; 303 304 311 public NamingEnumeration listBindings( final Name name ) 312 throws NamingException 313 { 314 if( isSelf( name ) ) 315 { 316 return doLocalListBindings(); 317 } 318 else 319 { 320 final Context context = lookupSubContext( name ); 322 return context.listBindings( "" ); 323 } 324 } 325 326 333 public Object lookup( final Name name ) 334 throws NamingException 335 { 336 if( isSelf( name ) ) 338 { 339 return cloneContext(); 340 } 341 342 if( 1 == name.size() ) 343 { 344 Object obj = localLookup( name ); 345 if( obj instanceof Reference ) 346 { 347 try 348 { 349 obj = m_namespace.getObjectInstance( obj, name, this, this.getEnvironment() ); 350 } 351 catch( Exception e ) 352 { 353 throw new NamingException ( "Could not resolve reference" ); 354 } 355 } 356 357 return obj; 358 } 359 else 360 { 361 final Context context = lookupSubContext( getPathName( name ) ); 362 return context.lookup( getLeafName( name ) ); 363 } 364 } 365 366 373 protected Object localLookup( final Name name ) 374 throws NamingException 375 { 376 final Object value = doLocalLookup( name ); 377 378 try 380 { 381 final Name atom = name.getPrefix( 1 ); 382 return m_namespace.getObjectInstance( value, atom, this, getRawEnvironment() ); 383 } 384 catch( final Exception e ) 385 { 386 final NamingException ne = new NamingException ( "getObjectInstance failed" ); 387 ne.setRootCause( e ); 388 throw ne; 389 } 390 } 391 392 400 protected abstract Object doLocalLookup( Name name ) 401 throws NamingException ; 402 403 411 protected Context lookupSubContext( final Name name ) 412 throws NamingException 413 { 414 final Name atom = name.getPrefix( 1 ); 415 Object object = localLookup( atom ); 416 417 if( 1 != name.size() ) 418 { 419 if( !( object instanceof Context ) ) 420 { 421 throw new NotContextException ( atom.toString() ); 422 } 423 424 object = ( (Context )object ).lookup( name.getSuffix( 1 ) ); 425 } 426 427 if( !( object instanceof Context ) ) 428 { 429 throw new NotContextException ( name.toString() ); 430 } 431 432 return (Context )object; 434 } 435 436 442 public void unbind( final Name name ) 443 throws NamingException 444 { 445 if( isSelf( name ) ) 446 { 447 throw new InvalidNameException ( "Cannot unbind self" ); 448 } 449 else if( 1 == name.size() ) 450 { 451 doLocalUnbind( name ); 452 } 453 else 454 { 455 final Context context = lookupSubContext( getPathName( name ) ); 456 context.unbind( getLeafName( name ) ); 457 } 458 } 459 460 466 protected abstract void doLocalUnbind( Name name ) 467 throws NamingException ; 468 } 469 | Popular Tags |