1 22 23 24 package com.mchange.v2.naming; 25 26 import java.io.InvalidObjectException ; 27 import java.io.IOException ; 28 import java.util.Hashtable ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.naming.Name ; 32 import javax.naming.NamingException ; 33 import javax.naming.Reference ; 34 import javax.naming.Referenceable ; 35 import com.mchange.v2.log.MLevel; 36 import com.mchange.v2.log.MLog; 37 import com.mchange.v2.log.MLogger; 38 import com.mchange.v2.ser.Indirector; 39 import com.mchange.v2.ser.IndirectlySerialized; 40 41 public class ReferenceIndirector implements Indirector 42 { 43 final static MLogger logger = MLog.getLogger( ReferenceIndirector.class ); 44 45 Name name; 46 Name contextName; 47 Hashtable environmentProperties; 48 49 public Name getName() 50 { return name; } 51 52 public void setName( Name name ) 53 { this.name = name; } 54 55 public Name getNameContextName() 56 { return contextName; } 57 58 public void setNameContextName( Name contextName ) 59 { this.contextName = contextName; } 60 61 public Hashtable getEnvironmentProperties() 62 { return environmentProperties; } 63 64 public void setEnvironmentProperties( Hashtable environmentProperties ) 65 { this.environmentProperties = environmentProperties; } 66 67 public IndirectlySerialized indirectForm( Object orig ) throws Exception 68 { 69 Reference ref = ((Referenceable ) orig).getReference(); 70 return new ReferenceSerialized( ref, name, contextName, environmentProperties ); 71 } 72 73 private static class ReferenceSerialized implements IndirectlySerialized 74 { 75 Reference reference; 76 Name name; 77 Name contextName; 78 Hashtable env; 79 80 ReferenceSerialized( Reference reference, 81 Name name, 82 Name contextName, 83 Hashtable env ) 84 { 85 this.reference = reference; 86 this.name = name; 87 this.contextName = contextName; 88 this.env = env; 89 } 90 91 92 public Object getObject() throws ClassNotFoundException , IOException 93 { 94 try 95 { 96 Context initialContext; 97 if ( env == null ) 98 initialContext = new InitialContext (); 99 else 100 initialContext = new InitialContext ( env ); 101 102 Context nameContext = null; 103 if ( contextName != null ) 104 nameContext = (Context ) initialContext.lookup( contextName ); 105 106 return ReferenceableUtils.referenceToObject( reference, name, nameContext, env ); 107 } 108 catch (NamingException e) 109 { 110 if ( logger.isLoggable( MLevel.WARNING ) ) 112 logger.log( MLevel.WARNING, "Failed to acquire the Context necessary to lookup an Object.", e ); 113 throw new InvalidObjectException ( "Failed to acquire the Context necessary to lookup an Object: " + e.toString() ); 114 } 115 } 116 } 117 } 118 | Popular Tags |