1 8 package org.codehaus.spice.jndikit; 9 10 import java.io.IOException ; 11 import java.io.Serializable ; 12 import java.rmi.MarshalledObject ; 13 import java.util.Hashtable ; 14 import java.util.Iterator ; 15 import javax.naming.Binding ; 16 import javax.naming.CommunicationException ; 17 import javax.naming.ConfigurationException ; 18 import javax.naming.Context ; 19 import javax.naming.InvalidNameException ; 20 import javax.naming.Name ; 21 import javax.naming.NameClassPair ; 22 import javax.naming.NameParser ; 23 import javax.naming.NamingEnumeration ; 24 import javax.naming.NamingException ; 25 import javax.naming.Reference ; 26 import javax.naming.Referenceable ; 27 28 34 public class RemoteContext 35 extends AbstractContext 36 implements Serializable 37 { 38 public static final String NAMESPACE_NAME = "org.codehaus.spice.jndikit.Namespace/NAME"; 39 public static final String NAMESPACE = "org.codehaus.spice.jndikit.Namespace"; 40 public static final String NAMING_PROVIDER = "org.codehaus.spice.jndikit.NamingProvider"; 41 42 private transient NamingProvider m_provider; 43 private transient NameParser m_nameParser; 44 private transient Namespace m_namespace; 45 46 private Name m_baseName; 47 48 public RemoteContext() 50 { 51 } 52 53 public RemoteContext( final Hashtable environment, final Name baseName ) 54 throws NamingException 55 { 56 super( environment ); 57 m_baseName = baseName; 58 } 59 60 63 protected void bind( final Name name, Object object, final boolean rebind ) 64 throws NamingException 65 { 66 if( isSelf( name ) ) 67 { 68 throw new InvalidNameException ( "Failed to bind self" ); 69 } 70 71 String className = null; 72 73 object = getNamespace().getStateToBind( object, name, this, getRawEnvironment() ); 74 75 if( object instanceof Reference ) 76 { 77 className = ( (Reference )object ).getClassName(); 78 } 79 else if( object instanceof Referenceable ) 80 { 81 object = ( (Referenceable )object ).getReference(); 82 className = ( (Reference )object ).getClassName(); 83 } 84 else 85 { 86 className = object.getClass().getName(); 87 88 try 89 { 90 object = new MarshalledObject ( object ); 91 } 92 catch( final IOException ioe ) 93 { 94 throw new NamingException ( "Only Reference, Referenceables and " + 95 "Serializable objects can be bound " + 96 "to context" ); 97 } 98 } 99 100 try 101 { 102 if( rebind ) 103 { 104 getProvider().rebind( getAbsoluteName( name ), className, object ); 105 } 106 else 107 { 108 getProvider().bind( getAbsoluteName( name ), className, object ); 109 } 110 } 111 catch( final Exception e ) 112 { 113 throw handleException( e ); 114 } 115 } 116 117 120 public void close() 121 { 122 super.close(); 123 m_namespace = null; 124 m_provider = null; 125 } 126 127 135 public Context createSubcontext( final Name name ) 136 throws NamingException 137 { 138 if( isSelf( name ) ) 139 { 140 throw new InvalidNameException ( "Failed to create null subcontext" ); 141 } 142 143 Context result = null; 144 try 145 { 146 result = getProvider().createSubcontext( getAbsoluteName( name ) ); 147 } 148 catch( final Exception e ) 149 { 150 throw handleException( e ); 151 } 152 153 fillInContext( result ); 154 155 return result; 156 } 157 158 public void destroySubcontext( final Name name ) 159 throws NamingException 160 { 161 if( isSelf( name ) ) 162 { 163 throw new InvalidNameException ( "Failed to destroy self" ); 164 } 165 166 try 167 { 168 getProvider().destroySubcontext( getAbsoluteName( name ) ); 169 } 170 catch( final Exception e ) 171 { 172 throw handleException( e ); 173 } 174 } 175 176 public String getNameInNamespace() 177 throws NamingException 178 { 179 return getAbsoluteName( getNameParser().parse( "" ) ).toString(); 180 } 181 182 189 public NamingEnumeration list( final Name name ) 190 throws NamingException 191 { 192 try 193 { 194 final NameClassPair [] result = getProvider().list( getAbsoluteName( name ) ); 195 return new ArrayNamingEnumeration( this, getNamespace(), result ); 196 } 197 catch( final Exception e ) 198 { 199 throw handleException( e ); 200 } 201 } 202 203 210 public NamingEnumeration listBindings( final Name name ) 211 throws NamingException 212 { 213 try 214 { 215 final Binding [] result = getProvider().listBindings( getAbsoluteName( name ) ); 216 217 for( int i = 0; i < result.length; i++ ) 218 { 219 final Object object = result[ i ].getObject(); 220 if( object instanceof Context ) 221 { 222 fillInContext( (Context )object ); 223 } else if (object instanceof MarshalledObject ) { 224 result[i].setObject(( (MarshalledObject )object ).get()); 225 } 226 } 227 228 return new ArrayNamingEnumeration( this, getNamespace(), result ); 229 } 230 catch( final Exception e ) 231 { 232 throw handleException( e ); 233 } 234 } 235 236 244 public Object lookup( final Name name ) 245 throws NamingException 246 { 247 if( isSelf( name ) ) 248 { 249 return new RemoteContext( getRawEnvironment(), m_baseName ); 250 } 251 252 Object object = null; 254 try 255 { 256 object = getProvider().lookup( getAbsoluteName( name ) ); 257 258 if( object instanceof MarshalledObject ) 259 { 260 object = ( (MarshalledObject )object ).get(); 261 } 262 263 object = getNamespace().getObjectInstance( object, name, this, getRawEnvironment() ); 264 265 if( object instanceof Context ) 266 { 267 fillInContext( (Context )object ); 268 } 269 } 270 catch( final Exception e ) 271 { 272 throw handleException( e ); 273 } 274 275 return object; 276 } 277 278 284 public void unbind( final Name name ) 285 throws NamingException 286 { 287 if( isSelf( name ) ) 288 { 289 throw new InvalidNameException ( "Failed to unbind self" ); 290 } 291 292 try 293 { 294 getProvider().unbind( getAbsoluteName( name ) ); 295 } 296 catch( final Exception e ) 297 { 298 throw handleException( e ); 299 } 300 } 301 302 protected void fillInContext( final Context object ) 303 throws NamingException 304 { 305 final Hashtable environment = getRawEnvironment(); 306 final Iterator keys = environment.keySet().iterator(); 307 308 while( keys.hasNext() ) 309 { 310 final String key = (String )keys.next(); 311 final Object value = environment.get( key ); 312 object.addToEnvironment( key, value ); 313 } 314 } 315 316 protected Namespace getNamespace() 317 throws NamingException 318 { 319 if( null == m_namespace ) 320 { 321 final Object object = getRawEnvironment().get( RemoteContext.NAMESPACE ); 322 323 if( !( object instanceof Namespace ) || null == object ) 324 { 325 throw new ConfigurationException ( "Context does not contain Namespace" ); 326 } 327 else 328 { 329 m_namespace = (Namespace)object; 330 } 331 } 332 333 return m_namespace; 334 } 335 336 protected NamingProvider getProvider() 337 throws NamingException 338 { 339 if( null == m_provider ) 340 { 341 final Object object = getRawEnvironment().get( RemoteContext.NAMING_PROVIDER ); 342 343 if( !( object instanceof NamingProvider ) || null == object ) 344 { 345 throw new ConfigurationException ( "Context does not contain provider" ); 346 } 347 else 348 { 349 m_provider = (NamingProvider)object; 350 } 351 } 352 353 return m_provider; 354 } 355 356 protected NameParser getNameParser() 357 throws NamingException 358 { 359 if( null == m_nameParser ) 360 { 361 try 363 { 364 m_nameParser = getProvider().getNameParser(); 365 } 366 catch( final Exception e ) 367 { 368 throw handleException( e ); 369 } 370 371 } 372 return m_nameParser; 373 } 374 375 protected Name getAbsoluteName( final Name name ) 376 throws NamingException 377 { 378 return composeName( name, m_baseName ); 379 } 380 381 protected NamingException handleException( final Exception e ) 382 { 383 if( e instanceof NamingException ) 384 { 385 return (NamingException )e; 386 } 387 else 388 { 389 return new CommunicationException ( e.toString() ); 390 } 391 } 392 } 393 | Popular Tags |