1 50 package org.apache.avalon.excalibur.naming.rmi; 51 52 import java.io.BufferedInputStream ; 53 import java.io.IOException ; 54 import java.io.ObjectInputStream ; 55 import java.net.Socket ; 56 import java.rmi.MarshalledObject ; 57 import java.util.Hashtable ; 58 59 import javax.naming.ConfigurationException ; 60 import javax.naming.Context ; 61 import javax.naming.NamingException ; 62 import javax.naming.ServiceUnavailableException ; 63 import javax.naming.spi.InitialContextFactory ; 64 65 import org.apache.avalon.excalibur.naming.DefaultNamespace; 66 import org.apache.avalon.excalibur.naming.Namespace; 67 import org.apache.avalon.excalibur.naming.NamingProvider; 68 import org.apache.avalon.excalibur.naming.RemoteContext; 69 70 77 public class RMIInitialContextFactory 78 implements InitialContextFactory 79 { 80 public Context getInitialContext( final Hashtable environment ) 81 throws NamingException 82 { 83 final NamingProvider provider = newNamingProvider( environment ); 84 environment.put( RemoteContext.NAMING_PROVIDER, provider ); 85 86 final Namespace namespace = newNamespace( environment ); 87 environment.put( RemoteContext.NAMESPACE, namespace ); 88 89 return new RemoteContext( environment, namespace.getNameParser().parse( "" ) ); 90 } 91 92 protected NamingProvider newNamingProvider( final Hashtable environment ) 93 throws NamingException 94 { 95 final String url = (String )environment.get( Context.PROVIDER_URL ); 96 if( null == url ) 97 { 98 return newNamingProvider( "localhost", 1977 ); 99 } 100 else 101 { 102 if( !url.startsWith( "rmi://" ) ) 103 { 104 throw new ConfigurationException ( "Malformed url - " + url ); 105 } 106 107 final int index = url.indexOf( ':', 6 ); 108 int end = index; 109 110 int port = 1977; 111 112 if( -1 == index ) 113 { 114 end = url.length(); 115 } 116 else 117 { 118 port = Integer.parseInt( url.substring( index + 1 ) ); 119 } 120 121 final String host = url.substring( 6, end ); 122 123 return newNamingProvider( host, port ); 124 } 125 } 126 127 protected NamingProvider newNamingProvider( final String host, final int port ) 128 throws NamingException 129 { 130 Socket socket = null; 131 132 try 133 { 134 socket = new Socket ( host, port ); 135 136 final ObjectInputStream input = 137 new ObjectInputStream ( new BufferedInputStream ( socket.getInputStream() ) ); 138 139 final NamingProvider provider = 140 ( (NamingProvider)( (MarshalledObject )input.readObject() ).get() ); 141 142 socket.close(); 143 144 return provider; 145 } 146 catch( final Exception e ) 147 { 148 final ServiceUnavailableException sue = 149 new ServiceUnavailableException ( e.getMessage() ); 150 sue.setRootCause( e ); 151 throw sue; 152 } 153 finally 154 { 155 if( null != socket ) 156 { 157 try 158 { 159 socket.close(); 160 } 161 catch( final IOException ioe ) 162 { 163 } 164 } 165 } 166 } 167 168 protected Namespace newNamespace( final Hashtable environment ) 169 throws NamingException 170 { 171 try 172 { 173 final NamingProvider provider = 174 (NamingProvider)environment.get( RemoteContext.NAMING_PROVIDER ); 175 176 return new DefaultNamespace( provider.getNameParser() ); 177 } 178 catch( final Exception e ) 179 { 180 if( e instanceof NamingException ) 181 { 182 throw (NamingException )e; 183 } 184 else 185 { 186 final ServiceUnavailableException sue = 187 new ServiceUnavailableException ( e.getMessage() ); 188 sue.setRootCause( e ); 189 throw sue; 190 } 191 } 192 } 193 } 194 195 | Popular Tags |