1 8 package org.codehaus.spice.jndikit.rmi; 9 10 import java.io.BufferedInputStream ; 11 import java.io.IOException ; 12 import java.io.ObjectInputStream ; 13 import java.net.Socket ; 14 import java.rmi.MarshalledObject ; 15 import java.util.Hashtable ; 16 import javax.naming.ConfigurationException ; 17 import javax.naming.Context ; 18 import javax.naming.NamingException ; 19 import javax.naming.ServiceUnavailableException ; 20 import javax.naming.Name ; 21 import javax.naming.spi.InitialContextFactory ; 22 import org.codehaus.spice.jndikit.DefaultNamespace; 23 import org.codehaus.spice.jndikit.Namespace; 24 import org.codehaus.spice.jndikit.NamingProvider; 25 import org.codehaus.spice.jndikit.RemoteContext; 26 27 33 public class RMIInitialContextFactory 34 implements InitialContextFactory 35 { 36 public Context getInitialContext( final Hashtable environment ) 37 throws NamingException 38 { 39 final NamingProvider provider = newNamingProvider( environment ); 40 environment.put( RemoteContext.NAMING_PROVIDER, provider ); 41 42 final Namespace namespace = newNamespace( environment ); 43 environment.put( RemoteContext.NAMESPACE, namespace ); 44 45 final Name baseName = namespace.getNameParser().parse( "" ); 46 return new RemoteContext( environment, baseName ); 47 } 48 49 protected NamingProvider newNamingProvider( final Hashtable environment ) 50 throws NamingException 51 { 52 final String url = (String )environment.get( Context.PROVIDER_URL ); 53 if( null == url ) 54 { 55 return newNamingProvider( "localhost", 1977 ); 56 } 57 else 58 { 59 if( !url.startsWith( "rmi://" ) ) 60 { 61 throw new ConfigurationException ( "Malformed url - " + url ); 62 } 63 64 final int index = url.indexOf( ':', 6 ); 65 int end = index; 66 67 int port = 1977; 68 69 if( -1 == index ) 70 { 71 end = url.length(); 72 } 73 else 74 { 75 port = Integer.parseInt( url.substring( index + 1 ) ); 76 } 77 78 final String host = url.substring( 6, end ); 79 80 return newNamingProvider( host, port ); 81 } 82 } 83 84 protected NamingProvider newNamingProvider( final String host, 85 final int port ) 86 throws NamingException 87 { 88 Socket socket = null; 89 90 try 91 { 92 socket = new Socket ( host, port ); 93 94 final BufferedInputStream buffered = 95 new BufferedInputStream ( socket.getInputStream() ); 96 final ObjectInputStream input = new ObjectInputStream ( buffered ); 97 98 final MarshalledObject object = 99 (MarshalledObject )input.readObject(); 100 final NamingProvider provider =(NamingProvider)object.get(); 101 102 socket.close(); 103 socket = null; 104 105 return provider; 106 } 107 catch( final Exception e ) 108 { 109 final ServiceUnavailableException sue = 110 new ServiceUnavailableException ( e.getMessage() ); 111 sue.setRootCause( e ); 112 throw sue; 113 } 114 finally 115 { 116 if( null != socket ) 117 { 118 try 119 { 120 socket.close(); 121 } 122 catch( final IOException ioe ) 123 { 124 } 126 } 127 } 128 } 129 130 protected Namespace newNamespace( final Hashtable environment ) 131 throws NamingException 132 { 133 try 134 { 135 final NamingProvider provider = 136 (NamingProvider)environment.get( RemoteContext.NAMING_PROVIDER ); 137 138 return new DefaultNamespace( provider.getNameParser() ); 139 } 140 catch( final Exception e ) 141 { 142 if( e instanceof NamingException ) 143 { 144 throw (NamingException )e; 145 } 146 else 147 { 148 final ServiceUnavailableException sue = 149 new ServiceUnavailableException ( e.getMessage() ); 150 sue.setRootCause( e ); 151 throw sue; 152 } 153 } 154 } 155 } 156 157 | Popular Tags |