KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > jndi > InitialContextFactory


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.4 2005/06/07 14:20:37 tanderson Exp $
44  */

45 package org.exolab.jms.jndi;
46
47 import java.io.IOException JavaDoc;
48 import java.rmi.NotBoundException JavaDoc;
49 import java.rmi.RemoteException JavaDoc;
50 import java.util.Hashtable JavaDoc;
51 import javax.naming.CommunicationException JavaDoc;
52 import javax.naming.ConfigurationException JavaDoc;
53 import javax.naming.Context JavaDoc;
54 import javax.naming.NameParser JavaDoc;
55 import javax.naming.NamingException JavaDoc;
56 import javax.naming.ServiceUnavailableException JavaDoc;
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
68
69 /**
70  * A factory that creates an initial context to an embedded OpenJMS JNDI
71  * provider.
72  *
73  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
74  * @version $Revision: 1.4 $ $Date: 2005/06/07 14:20:37 $
75  */

76 public class InitialContextFactory
77         implements javax.naming.spi.InitialContextFactory JavaDoc {
78
79     /**
80      * HTTP connector URI scheme.
81      */

82     private static final String JavaDoc HTTP_SCHEME = "http";
83
84     /**
85      * HTTPS connector URI scheme.
86      */

87     private static final String JavaDoc HTTPS_SCHEME = "https";
88
89     /**
90      * Default tunnel servlet path.
91      */

92     private static final String JavaDoc SERVLET = "/openjms-tunnel/tunnel";
93
94     /**
95      * Old style VM connector URI scheme.
96      */

97     private static final String JavaDoc EMBEDDED_SCHEME = "embedded";
98
99     /**
100      * VM connector URI scheme.
101      */

102     private static final String JavaDoc VM_SCHEME = "vm";
103
104     /**
105      * Default VM connector path.
106      */

107     private static final String JavaDoc VM_PATH = "openjms";
108
109
110     /**
111      * Creates an initial context for beginning name resolution, based on the
112      * {@link Context#PROVIDER_URL} attribute.
113      *
114      * @param environment the environment specifying information to be used in
115      * the creation of the initial context.
116      * @return an initial context
117      * @throws NamingException if the initial context cannot be created
118      */

119     public Context JavaDoc getInitialContext(Hashtable JavaDoc environment)
120             throws NamingException JavaDoc {
121         if (environment == null) {
122             throw new ConfigurationException JavaDoc(
123                     "Cannot connect to JNDI provider - environment not set");
124         }
125         String JavaDoc url = (String JavaDoc) environment.get(Context.PROVIDER_URL);
126         if (url == null) {
127             throw new ConfigurationException JavaDoc("Cannot connect to JNDI provider - "
128                                              + Context.PROVIDER_URL
129                                              + " not set ");
130         }
131
132         // map JNDI properties to the ORB equivalents, if unset
133
Hashtable JavaDoc properties = new Hashtable JavaDoc(environment);
134         properties.put(Context.PROVIDER_URL, getProviderURI(url));
135         map(properties, Context.PROVIDER_URL, ORB.PROVIDER_URI);
136         map(properties, Context.SECURITY_PRINCIPAL, ORB.SECURITY_PRINCIPAL);
137         map(properties, Context.SECURITY_CREDENTIALS, ORB.SECURITY_CREDENTIALS);
138
139         ORB orb;
140         Registry registry;
141         try {
142             orb = SharedORB.getInstance();
143             registry = orb.getRegistry(properties);
144         } catch (RemoteException JavaDoc exception) {
145             NamingException JavaDoc error = new CommunicationException JavaDoc(
146                     "Failed to get registry service for URL: " + url);
147             error.setRootCause(exception);
148             throw error;
149         }
150
151         NamingProvider provider;
152         try {
153             provider = (NamingProvider) registry.lookup("jndi");
154         } catch (NotBoundException JavaDoc exception) {
155             throw new ServiceUnavailableException JavaDoc(
156                     "JNDI service is not bound in the registry for URL: "
157                     + url);
158         } catch (RemoteException JavaDoc exception) {
159             NamingException JavaDoc error = new CommunicationException JavaDoc(
160                     "Failed to lookup JNDI provider for URL: " + url);
161             error.setRootCause(exception);
162             throw error;
163         }
164
165         NameParser JavaDoc parser;
166         try {
167             parser = provider.getNameParser();
168         } catch (NamingException JavaDoc exception) {
169             throw exception;
170         } catch (Exception JavaDoc exception) {
171             NamingException JavaDoc error = new ServiceUnavailableException JavaDoc(
172                     exception.getMessage());
173             error.setRootCause(exception);
174             throw error;
175         }
176         Namespace namespace = new StandardNamespace(parser);
177         properties.put(RemoteContext.NAMING_PROVIDER, provider);
178         properties.put(RemoteContext.NAMESPACE, namespace);
179         return new RemoteContext(properties, parser.parse(""));
180     }
181
182     /**
183      * Modifies the supplied provider URI with default values if required
184      * details haven't been specified.
185      *
186      * @param uri the provider URI
187      * @return the modified provider URI
188      * @throws ConfigurationException if <code>uri</code> is invalid
189      */

190     private String JavaDoc getProviderURI(String JavaDoc uri)
191             throws ConfigurationException JavaDoc {
192         URI parsed;
193         try {
194             parsed = new URI(uri);
195             String JavaDoc scheme = parsed.getScheme();
196             if (scheme.equals(HTTP_SCHEME) || scheme.equals(HTTPS_SCHEME)) {
197                 String JavaDoc path = parsed.getPath();
198                 if (path == null || path.length() == 0) {
199                     parsed.setPath(SERVLET);
200                 }
201             } else if (scheme.equals(EMBEDDED_SCHEME)) {
202                 parsed.setScheme(VM_SCHEME);
203                 parsed.setPath(VM_PATH);
204             }
205         } catch (IOException JavaDoc exception) {
206             throw new ConfigurationException JavaDoc(exception.getMessage());
207         }
208         return parsed.toString();
209     }
210
211     /**
212      * Helper to copy a property from one to name to another, if it exists.
213      *
214      * @param properties the properties to examine
215      * @param from the property name to map from
216      * @param to the property name to map to
217      */

218     private void map(Hashtable JavaDoc properties, String JavaDoc from, String JavaDoc to) {
219         String JavaDoc value = (String JavaDoc) properties.get(from);
220         if (value != null) {
221             properties.put(to, value);
222         }
223     }
224
225 }
226
Popular Tags