1 8 package org.codehaus.spice.jndikit; 9 10 import java.util.Hashtable ; 11 import javax.naming.Context ; 12 import javax.naming.ContextNotEmptyException ; 13 import javax.naming.InvalidNameException ; 14 import javax.naming.Name ; 15 import javax.naming.NameAlreadyBoundException ; 16 import javax.naming.NameParser ; 17 import javax.naming.NamingEnumeration ; 18 import javax.naming.NamingException ; 19 import javax.naming.NotContextException ; 20 import javax.naming.OperationNotSupportedException ; 21 import javax.naming.Referenceable ; 22 23 31 public abstract class AbstractLocalContext 32 extends AbstractContext 33 { 34 private Context m_parent; 35 private Namespace m_namespace; 36 37 public AbstractLocalContext( final Namespace namespace, 38 final Hashtable environment, 39 final Context parent ) 40 { 41 super( environment ); 42 m_namespace = namespace; 43 m_parent = parent; 44 } 45 46 51 protected final Context getParent() 52 { 53 return m_parent; 54 } 55 56 61 protected final Namespace getNamespace() 62 { 63 return m_namespace; 64 } 65 66 protected boolean isDestroyableContext( final Object object ) 67 throws NamingException 68 { 69 return getClass().isInstance( object ); 70 } 71 72 protected abstract Context newContext() 73 throws NamingException ; 74 75 protected abstract Context cloneContext() 76 throws NamingException ; 77 78 81 protected void bind( final Name name, Object object, final boolean rebind ) 82 throws NamingException 83 { 84 if( isSelf( name ) ) 85 { 86 throw new InvalidNameException ( "Failed to bind self" ); 87 } 88 89 if( 1 == name.size() ) 90 { 91 boolean alreadyBound; 92 try 93 { 94 localLookup( name ); 95 alreadyBound = true; 96 } 97 catch( final NamingException ne ) 98 { 99 alreadyBound = false; 100 } 101 102 if( !rebind && alreadyBound ) 103 { 104 throw new NameAlreadyBoundException ( name.get( 0 ) ); 105 } 106 else 107 { 108 if( object instanceof Referenceable ) 109 { 110 object = ( (Referenceable )object ).getReference(); 111 } 112 113 final Name atom = name.getPrefix( 1 ); 115 object = m_namespace.getStateToBind( object, atom, this, getRawEnvironment() ); 116 117 doLocalBind( name, object ); 118 } 119 } 120 else 121 { 122 final Context context = lookupSubContext( getPathName( name ) ); 123 if( rebind ) 124 { 125 context.rebind( getLeafName( name ), object ); 126 } 127 else 128 { 129 context.bind( getLeafName( name ), object ); 130 } 131 } 132 } 133 134 protected abstract void doLocalBind( Name name, Object object ) 135 throws NamingException ; 136 137 public void close() 138 { 139 m_parent = null; 140 m_namespace = null; 141 } 142 143 151 public Context createSubcontext( final Name name ) 152 throws NamingException 153 { 154 final Context context = newContext(); 155 bind( name, context ); 156 return context; 157 } 158 159 public void destroySubcontext( final Name name ) 160 throws NamingException 161 { 162 if( isSelf( name ) ) 163 { 164 throw new InvalidNameException ( "Failed to destroy self" ); 165 } 166 167 if( 1 == name.size() ) 168 { 169 Object object = null; 170 try 171 { 172 object = localLookup( name ); 173 } 174 catch( final NamingException ne ) 175 { 176 return; 177 } 178 179 checkUnbindContext( name, object ); 180 181 doLocalUnbind( name ); 182 } 183 else 184 { 185 final Context context = lookupSubContext( getPathName( name ) ); 186 187 Object object = null; 188 189 final Name atom = getLeafName( name ); 190 try 191 { 192 object = context.lookup( atom ); 193 } 194 catch( final NamingException ne ) 195 { 196 return; 197 } 198 199 checkUnbindContext( atom, object ); 200 201 context.destroySubcontext( atom ); 202 } 203 } 204 205 protected void checkUnbindContext( final Name name, final Object entry ) 206 throws NamingException 207 { 208 if( !isDestroyableContext( entry ) ) 209 { 210 throw new NotContextException ( name.toString() ); 211 } 212 213 final Context context = (Context )entry; 214 if( context.list( "" ).hasMoreElements() ) 215 { 216 throw new ContextNotEmptyException ( name.toString() ); 217 } 218 } 219 220 public String getNameInNamespace() 221 throws NamingException 222 { 223 throw new OperationNotSupportedException ( "Namespace has no notion of a 'full name'" ); 224 } 225 226 protected NameParser getNameParser() 227 throws NamingException 228 { 229 return m_namespace.getNameParser(); 230 } 231 232 239 public NamingEnumeration list( final Name name ) 240 throws NamingException 241 { 242 if( isSelf( name ) ) 243 { 244 return doLocalList(); 245 } 246 else 247 { 248 final Context context = lookupSubContext( name ); 250 return context.list( "" ); 251 } 252 } 253 254 protected abstract NamingEnumeration doLocalList() 255 throws NamingException ; 256 257 protected abstract NamingEnumeration doLocalListBindings() 258 throws NamingException ; 259 260 267 public NamingEnumeration listBindings( final Name name ) 268 throws NamingException 269 { 270 if( isSelf( name ) ) 271 { 272 return doLocalListBindings(); 273 } 274 else 275 { 276 final Context context = lookupSubContext( name ); 278 return context.listBindings( "" ); 279 } 280 } 281 282 290 public Object lookup( final Name name ) 291 throws NamingException 292 { 293 if( isSelf( name ) ) 295 { 296 return cloneContext(); 297 } 298 299 if( 1 == name.size() ) 300 { 301 Object obj = localLookup( name ); 302 if (obj instanceof AbstractLocalContext) 303 { 304 return ((AbstractLocalContext) obj).cloneContext(); 305 } 306 307 return obj; 308 } 309 else 310 { 311 final Context context = lookupSubContext( getPathName( name ) ); 312 313 return context.lookup( getLeafName( name ) ); 314 } 315 } 316 317 324 protected Object localLookup( final Name name ) 325 throws NamingException 326 { 327 final Object value = doLocalLookup( name ); 328 329 try 331 { 332 final Name atom = name.getPrefix( 1 ); 333 return m_namespace.getObjectInstance( value, atom, this, getRawEnvironment() ); 334 } 335 catch( final Exception e ) 336 { 337 final NamingException ne = new NamingException ( "getObjectInstance failed" ); 338 ne.setRootCause( e ); 339 throw ne; 340 } 341 } 342 343 351 protected abstract Object doLocalLookup( Name name ) 352 throws NamingException ; 353 354 362 protected Context lookupSubContext( final Name name ) 363 throws NamingException 364 { 365 final Name atom = name.getPrefix( 1 ); 366 Object object = localLookup( atom ); 367 368 if( 1 != name.size() ) 369 { 370 if( !( object instanceof Context ) ) 371 { 372 throw new NotContextException ( atom.toString() ); 373 } 374 375 object = ( (Context )object ).lookup( name.getSuffix( 1 ) ); 376 } 377 378 if( !( object instanceof Context ) ) 379 { 380 throw new NotContextException ( name.toString() ); 381 } 382 383 return (Context )object; 385 } 386 387 393 public void unbind( final Name name ) 394 throws NamingException 395 { 396 if( isSelf( name ) ) 397 { 398 throw new InvalidNameException ( "Cannot unbind self" ); 399 } 400 else if( 1 == name.size() ) 401 { 402 doLocalUnbind( name ); 403 } 404 else 405 { 406 final Context context = lookupSubContext( getPathName( name ) ); 407 context.unbind( getLeafName( name ) ); 408 } 409 } 410 411 417 protected abstract void doLocalUnbind( Name name ) 418 throws NamingException ; 419 } 420 | Popular Tags |