1 50 package org.apache.avalon.excalibur.naming; 51 52 import java.io.IOException ; 53 import java.io.Serializable ; 54 import java.rmi.MarshalledObject ; 55 import java.util.Hashtable ; 56 import java.util.Iterator ; 57 58 import javax.naming.Binding ; 59 import javax.naming.CommunicationException ; 60 import javax.naming.ConfigurationException ; 61 import javax.naming.Context ; 62 import javax.naming.InvalidNameException ; 63 import javax.naming.Name ; 64 import javax.naming.NameClassPair ; 65 import javax.naming.NameParser ; 66 import javax.naming.NamingEnumeration ; 67 import javax.naming.NamingException ; 68 import javax.naming.Reference ; 69 import javax.naming.Referenceable ; 70 71 78 public class RemoteContext 79 extends AbstractContext 80 implements Serializable 81 { 82 public static final String NAMESPACE_NAME = "org.apache.avalon.excalibur.naming.Namespace/NAME"; 83 public static final String NAMESPACE = "org.apache.avalon.excalibur.naming.Namespace"; 84 public static final String NAMING_PROVIDER = "org.apache.avalon.excalibur.naming.NamingProvider"; 85 86 private transient NamingProvider m_provider; 87 private transient NameParser m_nameParser; 88 private transient Namespace m_namespace; 89 90 private Name m_baseName; 91 92 public RemoteContext() 94 { 95 } 96 97 public RemoteContext( final Hashtable environment, final Name baseName ) 98 throws NamingException 99 { 100 super( environment ); 101 m_baseName = baseName; 102 } 103 104 107 protected void bind( final Name name, Object object, final boolean rebind ) 108 throws NamingException 109 { 110 if( isSelf( name ) ) 111 { 112 throw new InvalidNameException ( "Failed to bind self" ); 113 } 114 115 String className = null; 116 117 object = getNamespace().getStateToBind( object, name, this, getRawEnvironment() ); 118 119 if( object instanceof Reference ) 120 { 121 className = ( (Reference )object ).getClassName(); 122 } 123 else if( object instanceof Referenceable ) 124 { 125 object = ( (Referenceable )object ).getReference(); 126 className = ( (Reference )object ).getClassName(); 127 } 128 else 129 { 130 className = object.getClass().getName(); 131 132 try 133 { 134 object = new MarshalledObject ( object ); 135 } 136 catch( final IOException ioe ) 137 { 138 throw new NamingException ( "Only Reference, Referenceables and " + 139 "Serializable objects can be bound " + 140 "to context" ); 141 } 142 } 143 144 try 145 { 146 if( rebind ) 147 { 148 getProvider().rebind( getAbsoluteName( name ), className, object ); 149 } 150 else 151 { 152 getProvider().bind( getAbsoluteName( name ), className, object ); 153 } 154 } 155 catch( final Exception e ) 156 { 157 throw handleException( e ); 158 } 159 } 160 161 164 public void close() 165 { 166 super.close(); 167 m_namespace = null; 168 m_provider = null; 169 } 170 171 178 public Context createSubcontext( final Name name ) 179 throws NamingException 180 { 181 if( isSelf( name ) ) 182 { 183 throw new InvalidNameException ( "Failed to create null subcontext" ); 184 } 185 186 Context result = null; 187 try 188 { 189 result = getProvider().createSubcontext( getAbsoluteName( name ) ); 190 } 191 catch( final Exception e ) 192 { 193 throw handleException( e ); 194 } 195 196 fillInContext( result ); 197 198 return result; 199 } 200 201 public void destroySubcontext( final Name name ) 202 throws NamingException 203 { 204 if( isSelf( name ) ) 205 { 206 throw new InvalidNameException ( "Failed to destroy self" ); 207 } 208 209 try 210 { 211 getProvider().destroySubcontext( getAbsoluteName( name ) ); 212 } 213 catch( final Exception e ) 214 { 215 throw handleException( e ); 216 } 217 } 218 219 public String getNameInNamespace() 220 throws NamingException 221 { 222 return getAbsoluteName( getNameParser().parse( "" ) ).toString(); 223 } 224 225 232 public NamingEnumeration list( final Name name ) 233 throws NamingException 234 { 235 try 236 { 237 final NameClassPair [] result = getProvider().list( getAbsoluteName( name ) ); 238 return new ArrayNamingEnumeration( this, m_namespace, result ); 239 } 240 catch( final Exception e ) 241 { 242 throw handleException( e ); 243 } 244 } 245 246 253 public NamingEnumeration listBindings( final Name name ) 254 throws NamingException 255 { 256 try 257 { 258 final Binding [] result = getProvider().listBindings( getAbsoluteName( name ) ); 259 260 for( int i = 0; i < result.length; i++ ) 261 { 262 final Object object = result[ i ].getObject(); 263 if( object instanceof Context ) 264 { 265 fillInContext( (Context )object ); 266 } 267 } 268 269 return new ArrayNamingEnumeration( this, m_namespace, result ); 270 } 271 catch( final Exception e ) 272 { 273 throw handleException( e ); 274 } 275 } 276 277 284 public Object lookup( final Name name ) 285 throws NamingException 286 { 287 if( isSelf( name ) ) 288 { 289 return new RemoteContext( getRawEnvironment(), m_baseName ); 290 } 291 292 Object object = null; 294 try 295 { 296 object = getProvider().lookup( getAbsoluteName( name ) ); 297 298 if( object instanceof MarshalledObject ) 299 { 300 object = ( (MarshalledObject )object ).get(); 301 } 302 303 object = getNamespace().getObjectInstance( object, name, this, getRawEnvironment() ); 304 305 if( object instanceof Context ) 306 { 307 fillInContext( (Context )object ); 308 } 309 } 310 catch( final Exception e ) 311 { 312 throw handleException( e ); 313 } 314 315 return object; 316 } 317 318 324 public void unbind( final Name name ) 325 throws NamingException 326 { 327 if( isSelf( name ) ) 328 { 329 throw new InvalidNameException ( "Failed to unbind self" ); 330 } 331 332 try 333 { 334 getProvider().unbind( getAbsoluteName( name ) ); 335 } 336 catch( final Exception e ) 337 { 338 throw handleException( e ); 339 } 340 } 341 342 protected void fillInContext( final Context object ) 343 throws NamingException 344 { 345 final Hashtable environment = getRawEnvironment(); 346 final Iterator keys = environment.keySet().iterator(); 347 348 while( keys.hasNext() ) 349 { 350 final String key = (String )keys.next(); 351 final Object value = environment.get( key ); 352 object.addToEnvironment( key, value ); 353 } 354 } 355 356 protected Namespace getNamespace() 357 throws NamingException 358 { 359 if( null == m_namespace ) 360 { 361 final Object object = getRawEnvironment().get( RemoteContext.NAMESPACE ); 362 363 if( !( object instanceof Namespace ) || null == object ) 364 { 365 throw new ConfigurationException ( "Context does not contain Namespace" ); 366 } 367 else 368 { 369 m_namespace = (Namespace)object; 370 } 371 } 372 373 return m_namespace; 374 } 375 376 protected NamingProvider getProvider() 377 throws NamingException 378 { 379 if( null == m_provider ) 380 { 381 final Object object = getRawEnvironment().get( RemoteContext.NAMING_PROVIDER ); 382 383 if( !( object instanceof NamingProvider ) || null == object ) 384 { 385 throw new ConfigurationException ( "Context does not contain provider" ); 386 } 387 else 388 { 389 m_provider = (NamingProvider)object; 390 } 391 } 392 393 return m_provider; 394 } 395 396 protected NameParser getNameParser() 397 throws NamingException 398 { 399 if( null == m_nameParser ) 400 { 401 try 403 { 404 m_nameParser = getProvider().getNameParser(); 405 } 406 catch( final Exception e ) 407 { 408 throw handleException( e ); 409 } 410 411 } 412 return m_nameParser; 413 } 414 415 protected Name getAbsoluteName( final Name name ) 416 throws NamingException 417 { 418 return composeName( name, m_baseName ); 419 } 420 421 protected NamingException handleException( final Exception e ) 422 { 423 if( e instanceof NamingException ) 424 { 425 return (NamingException )e; 426 } 427 else 428 { 429 return new CommunicationException ( e.toString() ); 430 } 431 } 432 } 433 | Popular Tags |