KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > naming > HttpNamingContextFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.naming;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.net.HttpURLConnection JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.RefAddr JavaDoc;
36 import javax.naming.spi.InitialContextFactory JavaDoc;
37 import javax.naming.spi.ObjectFactory JavaDoc;
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 /** A naming provider InitialContextFactory implementation that obtains a
47  Naming proxy from an HTTP URL.
48
49  @see javax.naming.spi.InitialContextFactory
50
51  @author Scott.Stark@jboss.org
52  @version $Revision: 37459 $
53  */

54 public class HttpNamingContextFactory
55    implements InitialContextFactory JavaDoc, ObjectFactory JavaDoc
56 {
57    private static Logger log = Logger.getLogger(HttpNamingContextFactory.class);
58
59    // InitialContextFactory implementation --------------------------
60
public Context JavaDoc getInitialContext(Hashtable JavaDoc env)
61       throws NamingException JavaDoc
62    {
63       // Parse the Context.PROVIDER_URL
64
String JavaDoc provider = (String JavaDoc) 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 JavaDoc providerURL = null;
75       Naming namingServer = null;
76       try
77       {
78          providerURL = new URL JavaDoc(provider);
79          // Retrieve the Naming interface
80
namingServer = getNamingServer(providerURL);
81       }
82       catch(Exception JavaDoc e)
83       {
84          NamingException JavaDoc ex = new NamingException JavaDoc("Failed to retrieve Naming interface");
85          ex.setRootCause(e);
86          throw ex;
87       }
88
89       // Copy the context env
90
env = (Hashtable JavaDoc) env.clone();
91       return new NamingContext(env, null, namingServer);
92    }
93
94    // ObjectFactory implementation ----------------------------------
95
public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
96       Hashtable JavaDoc env)
97       throws Exception JavaDoc
98    {
99       Context JavaDoc ctx = getInitialContext(env);
100       Reference JavaDoc ref = (Reference JavaDoc) obj;
101       RefAddr JavaDoc addr = ref.get("URL");
102       String JavaDoc path = (String JavaDoc) addr.getContent();
103       return ctx.lookup(path);
104    }
105
106    /** Obtain the JNDI Naming stub by reading its marshalled object from the
107     * servlet specified by the providerURL
108     *
109     * @param providerURL the naming factory servlet URL
110     * @return
111     * @throws ClassNotFoundException throw during unmarshalling
112     * @throws IOException thrown on any trasport failure
113     * @throws InvocationTargetException throw on failure to install a JSSE host verifier
114     * @throws IllegalAccessException throw on failure to install a JSSE host verifier
115     */

116    private Naming getNamingServer(URL JavaDoc providerURL)
117       throws ClassNotFoundException JavaDoc, IOException JavaDoc, InvocationTargetException JavaDoc,
118          IllegalAccessException JavaDoc
119    {
120       // Initialize the proxy Util class to integrate JAAS authentication
121
Util.init();
122       if( log.isTraceEnabled() )
123          log.trace("Retrieving content from : "+providerURL);
124
125       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) providerURL.openConnection();
126       Util.configureHttpsHostVerifier(conn);
127       Util.configureSSLSocketFactory(conn);
128       int length = conn.getContentLength();
129       String JavaDoc type = conn.getContentType();
130       if( log.isTraceEnabled() )
131          log.trace("ContentLength: "+length+"\nContentType: "+type);
132
133       InputStream JavaDoc is = conn.getInputStream();
134       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
135       MarshalledValue mv = (MarshalledValue) ois.readObject();
136       ois.close();
137
138       Object JavaDoc obj = mv.get();
139       if( (obj instanceof Naming) == false )
140       {
141          String JavaDoc msg = "Invalid reply content seen: "+obj.getClass();
142          Throwable JavaDoc t = null;
143          if( obj instanceof Throwable JavaDoc )
144          {
145             t = (Throwable JavaDoc) 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 JavaDoc e = new IOException JavaDoc(msg);
154          throw e;
155       }
156       Naming namingServer = (Naming) obj;
157       return namingServer;
158    }
159 }
160
Popular Tags