1 22 package org.jboss.naming; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.net.HttpURLConnection ; 28 import java.net.URL ; 29 import java.util.Hashtable ; 30 import java.lang.reflect.InvocationTargetException ; 31 import javax.naming.Context ; 32 import javax.naming.Name ; 33 import javax.naming.NamingException ; 34 import javax.naming.Reference ; 35 import javax.naming.RefAddr ; 36 import javax.naming.spi.InitialContextFactory ; 37 import javax.naming.spi.ObjectFactory ; 38 39 import org.jboss.invocation.InvocationException; 40 import org.jboss.invocation.MarshalledValue; 41 import org.jboss.invocation.http.interfaces.Util; 42 import org.jboss.logging.Logger; 43 import org.jnp.interfaces.Naming; 44 import org.jnp.interfaces.NamingContext; 45 46 54 public class HttpNamingContextFactory 55 implements InitialContextFactory , ObjectFactory 56 { 57 private static Logger log = Logger.getLogger(HttpNamingContextFactory.class); 58 59 public Context getInitialContext(Hashtable env) 61 throws NamingException 62 { 63 String provider = (String ) env.get(Context.PROVIDER_URL); 65 if( provider.startsWith("jnp:") == true ) 66 provider = "http:" + provider.substring(4); 67 else if( provider.startsWith("jnps:") == true ) 68 provider = "https:" + provider.substring(5); 69 else if( provider.startsWith("jnp-http:") == true ) 70 provider = "http:" + provider.substring(9); 71 else if( provider.startsWith("jnp-https:") == true ) 72 provider = "https:" + provider.substring(10); 73 74 URL providerURL = null; 75 Naming namingServer = null; 76 try 77 { 78 providerURL = new URL (provider); 79 namingServer = getNamingServer(providerURL); 81 } 82 catch(Exception e) 83 { 84 NamingException ex = new NamingException ("Failed to retrieve Naming interface"); 85 ex.setRootCause(e); 86 throw ex; 87 } 88 89 env = (Hashtable ) env.clone(); 91 return new NamingContext(env, null, namingServer); 92 } 93 94 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 96 Hashtable env) 97 throws Exception 98 { 99 Context ctx = getInitialContext(env); 100 Reference ref = (Reference ) obj; 101 RefAddr addr = ref.get("URL"); 102 String path = (String ) addr.getContent(); 103 return ctx.lookup(path); 104 } 105 106 116 private Naming getNamingServer(URL providerURL) 117 throws ClassNotFoundException , IOException , InvocationTargetException , 118 IllegalAccessException 119 { 120 Util.init(); 122 if( log.isTraceEnabled() ) 123 log.trace("Retrieving content from : "+providerURL); 124 125 HttpURLConnection conn = (HttpURLConnection ) providerURL.openConnection(); 126 Util.configureHttpsHostVerifier(conn); 127 Util.configureSSLSocketFactory(conn); 128 int length = conn.getContentLength(); 129 String type = conn.getContentType(); 130 if( log.isTraceEnabled() ) 131 log.trace("ContentLength: "+length+"\nContentType: "+type); 132 133 InputStream is = conn.getInputStream(); 134 ObjectInputStream ois = new ObjectInputStream (is); 135 MarshalledValue mv = (MarshalledValue) ois.readObject(); 136 ois.close(); 137 138 Object obj = mv.get(); 139 if( (obj instanceof Naming) == false ) 140 { 141 String msg = "Invalid reply content seen: "+obj.getClass(); 142 Throwable t = null; 143 if( obj instanceof Throwable ) 144 { 145 t = (Throwable ) obj; 146 if( t instanceof InvocationException ) 147 t = ((InvocationException)t).getTargetException(); 148 } 149 if( t != null ) 150 log.warn(msg, t); 151 else 152 log.warn(msg); 153 IOException e = new IOException (msg); 154 throw e; 155 } 156 Naming namingServer = (Naming) obj; 157 return namingServer; 158 } 159 } 160 | Popular Tags |