View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2003-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: InitialContextFactory.java,v 1.6 2005/12/20 21:42:06 tanderson Exp $
44   */
45  package org.exolab.jms.jndi;
46  
47  import java.io.IOException;
48  import java.rmi.NotBoundException;
49  import java.rmi.RemoteException;
50  import java.util.Hashtable;
51  import javax.naming.CommunicationException;
52  import javax.naming.ConfigurationException;
53  import javax.naming.Context;
54  import javax.naming.NameParser;
55  import javax.naming.NamingException;
56  import javax.naming.ServiceUnavailableException;
57  
58  import org.codehaus.spice.jndikit.Namespace;
59  import org.codehaus.spice.jndikit.NamingProvider;
60  import org.codehaus.spice.jndikit.RemoteContext;
61  import org.codehaus.spice.jndikit.StandardNamespace;
62  
63  import org.exolab.jms.client.net.SharedORB;
64  import org.exolab.jms.net.orb.ORB;
65  import org.exolab.jms.net.registry.Registry;
66  import org.exolab.jms.net.uri.URI;
67  import org.exolab.jms.net.proxy.Proxy;
68  
69  
70  /***
71   * A factory that creates an initial context to an embedded OpenJMS JNDI
72   * provider.
73   *
74   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
75   * @version $Revision: 1.6 $ $Date: 2005/12/20 21:42:06 $
76   */
77  public class InitialContextFactory
78          implements javax.naming.spi.InitialContextFactory {
79  
80      /***
81       * HTTP connector URI scheme.
82       */
83      private static final String HTTP_SCHEME = "http";
84  
85      /***
86       * HTTPS connector URI scheme.
87       */
88      private static final String HTTPS_SCHEME = "https";
89  
90      /***
91       * Default tunnel servlet path.
92       */
93      private static final String SERVLET = "/openjms-tunnel/tunnel";
94  
95      /***
96       * Old style VM connector URI scheme.
97       */
98      private static final String EMBEDDED_SCHEME = "embedded";
99  
100     /***
101      * VM connector URI scheme.
102      */
103     private static final String VM_SCHEME = "vm";
104 
105     /***
106      * Default VM connector path.
107      */
108     private static final String VM_PATH = "openjms";
109 
110 
111     /***
112      * Creates an initial context for beginning name resolution, based on the
113      * {@link Context#PROVIDER_URL} attribute.
114      *
115      * @param environment the environment specifying information to be used in
116      *                    the creation of the initial context.
117      * @return an initial context
118      * @throws NamingException if the initial context cannot be created
119      */
120     public Context getInitialContext(Hashtable environment)
121             throws NamingException {
122         if (environment == null) {
123             throw new ConfigurationException(
124                     "Cannot connect to JNDI provider - environment not set");
125         }
126         String url = (String) environment.get(Context.PROVIDER_URL);
127         if (url == null) {
128             throw new ConfigurationException("Cannot connect to JNDI provider - "
129                                              + Context.PROVIDER_URL
130                                              + " not set ");
131         }
132 
133         // map JNDI properties to the ORB equivalents, if unset
134         Hashtable properties = new Hashtable(environment);
135         properties.put(Context.PROVIDER_URL, getProviderURI(url));
136         map(properties, Context.PROVIDER_URL, ORB.PROVIDER_URI);
137         map(properties, Context.SECURITY_PRINCIPAL, ORB.SECURITY_PRINCIPAL);
138         map(properties, Context.SECURITY_CREDENTIALS, ORB.SECURITY_CREDENTIALS);
139 
140         ORB orb;
141         Registry registry;
142         try {
143             orb = SharedORB.getInstance();
144             registry = orb.getRegistry(properties);
145         } catch (RemoteException exception) {
146             NamingException error = new CommunicationException(
147                     "Failed to get registry service for URL: " + url);
148             error.setRootCause(exception);
149             throw error;
150         }
151 
152         NamingProvider provider;
153         try {
154             provider = (NamingProvider) registry.lookup("jndi");
155         } catch (NotBoundException exception) {
156             throw new ServiceUnavailableException(
157                     "JNDI service is not bound in the registry for URL: "
158                     + url);
159         } catch (RemoteException exception) {
160             NamingException error = new CommunicationException(
161                     "Failed to lookup JNDI provider for URL: " + url);
162             error.setRootCause(exception);
163             throw error;
164         } finally {
165             // get rid of the proxy now rather than waiting for GC
166             if (registry instanceof Proxy) {
167                 ((Proxy) registry).disposeProxy();
168             }
169         }
170 
171         NameParser parser;
172         try {
173             parser = provider.getNameParser();
174         } catch (NamingException exception) {
175             throw exception;
176         } catch (Exception exception) {
177             NamingException error = new ServiceUnavailableException(
178                     exception.getMessage());
179             error.setRootCause(exception);
180             throw error;
181         }
182         Namespace namespace = new StandardNamespace(parser);
183         properties.put(RemoteContext.NAMING_PROVIDER, provider);
184         properties.put(RemoteContext.NAMESPACE, namespace);
185         RemoteContext root = new RemoteContext(properties, parser.parse(""));
186         return new ORBRemoteContext(root);
187     }
188 
189     /***
190      * Modifies the supplied provider URI with default values if required
191      * details haven't been specified.
192      *
193      * @param uri the provider URI
194      * @return the modified provider URI
195      * @throws ConfigurationException if <code>uri</code> is invalid
196      */
197     private String getProviderURI(String uri)
198             throws ConfigurationException {
199         URI parsed;
200         try {
201             parsed = new URI(uri);
202             String scheme = parsed.getScheme();
203             if (scheme.equals(HTTP_SCHEME) || scheme.equals(HTTPS_SCHEME)) {
204                 String path = parsed.getPath();
205                 if (path == null || path.length() == 0 || path.equals("/")) {
206                     parsed.setPath(SERVLET);
207                 }
208             } else if (scheme.equals(EMBEDDED_SCHEME)) {
209                 parsed.setScheme(VM_SCHEME);
210                 parsed.setPath(VM_PATH);
211             }
212         } catch (IOException exception) {
213             throw new ConfigurationException(exception.getMessage());
214         }
215         return parsed.toString();
216     }
217 
218     /***
219      * Helper to copy a property from one to name to another, if it exists.
220      *
221      * @param properties the properties to examine
222      * @param from       the property name to map from
223      * @param to         the property name to map to
224      */
225     private void map(Hashtable properties, String from, String to) {
226         String value = (String) properties.get(from);
227         if (value != null) {
228             properties.put(to, value);
229         }
230     }
231 
232 }