KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jndi > url > corbaname > corbanameURLContextFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jndi.url.corbaname;
25
26 import javax.naming.*;
27 import javax.naming.spi.*;
28
29 import java.util.Hashtable JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import com.sun.jndi.cosnaming.CNCtx;
32 import org.omg.CORBA.ORB JavaDoc;
33
34 //START OF IASRI 4660742
35
import java.util.logging.*;
36 import com.sun.logging.*;
37 //END OF IASRI 4660742
38

39 /**
40  * A corbaname URL context factory. (INS 12-03-99)
41  *
42  * <corbaname> = "corbaname:" <corbaloc_obj> ["#" <string_name>]
43  * <string_name> = stringified Name | empty_string
44  * <corbaloc_obj> = <obj_addr_list> ["/" <key_string>]
45  * <obj_addr_list> = [<obj_addr> ","]* <obj_addr>
46  * <obj_addr> = <prot_addr> | <future_prot_addr>
47  * <prot_addr> = <rir_prot_addr> | <iiop_prot_addr>
48  * <rir_prot_addr> = "rir:"
49  * <iiop_prot_addr> = <iiop_id> <iiop_addr>
50  * <iiop_id> = ":" | "iiop:"
51  * <iiop_addr> = <version> <host> [":" <port>]
52  * <host> = DNS-style host name | IP address
53  * <version> = <major> "." <minor> "@" | empty_string
54  * <port> = number
55  * <major> = number
56  * <minor> = number
57  * <key_string> = <string> | empty_string
58  *
59  * NOTES:
60  * 1. Does NOT support having a <key_string> in its <corbaloc_obj>.
61  * 2. Does NOT support multiple "rir" in single URL
62  *
63  * @author Rosanna Lee
64  */

65
66 public class corbanameURLContextFactory implements ObjectFactory {
67     // START OF IASRI 4660742
68
static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER);
69     // END OF IASRI 4660742
70

71     private static final String JavaDoc DEFAULT_HOST = "localhost";
72     private static final String JavaDoc DEFAULT_PORT = "2089";
73
74     public Object JavaDoc getObjectInstance(Object JavaDoc urlInfo, Name name, Context nameCtx,
75                     Hashtable JavaDoc env) throws Exception JavaDoc {
76
77     if (urlInfo == null) {
78         return new corbanameURLContext(env);
79     }
80     if (urlInfo instanceof String JavaDoc) {
81         return getUsingURL((String JavaDoc)urlInfo, env);
82     } else if (urlInfo instanceof String JavaDoc[]) {
83         return getUsingURLs((String JavaDoc[])urlInfo, env);
84     } else {
85         throw (new IllegalArgumentException JavaDoc(
86             "iiopURLContextFactory.getObjectInstance: " +
87             "argument must be an iiop URL String or array of iiop URLs"));
88     }
89     }
90
91     /**
92       * Resolves 'name' into a target context with remaining name.
93       * It only resolves the hostname/port number. The remaining name
94       * contains the rest of the name found in the URL.
95       *
96       * For example, with a corbaname URL:
97       * corbaname:iiop://localhost:900#rest/of/name
98       * this method resolves
99       * corbaname:iiop://localhost:900/ to the "NameService"
100       * context on for the ORB at 'localhost' on port 900,
101       * and returns as the remaining name "rest/of/name".
102       */

103     static ResolveResult getUsingURLIgnoreRest(String JavaDoc url, Hashtable JavaDoc env)
104         throws NamingException {
105
106     ORB JavaDoc inOrb = (env != null)
107         ? (ORB JavaDoc) env.get("java.naming.corba.orb")
108         : null;
109         
110     if (inOrb != null) {
111         try {
112         CorbanameUrl parsedUrl = new CorbanameUrl(url);
113
114         // See if ORB can handle corbaname URL directly
115
org.omg.CORBA.Object JavaDoc ncRef = null;
116
117         try {
118             // Get object ref for NameService specified in corbaname URL
119
ncRef = inOrb.string_to_object(parsedUrl.getLocation());
120         } catch (Exception JavaDoc e) {
121         }
122
123         if (ncRef != null) {
124             // Convert to JNDI Context
125
Context ctx = CNCtxHelper.getInstance(inOrb, ncRef, env);
126
127             return new ResolveResult(ctx, parsedUrl.getCosName());
128         }
129         } catch (MalformedURLException JavaDoc e) {
130         throw new ConfigurationException(e.getMessage());
131         }
132     }
133
134     // Rewrite to iiop URL for handling by JNDI/COS provider
135
url = rewriteUrl(url);
136         
137     return CNCtx.createUsingURL(url, env);
138     }
139
140     private static Object JavaDoc getUsingURL(String JavaDoc url, Hashtable JavaDoc env)
141         throws NamingException {
142     ResolveResult res = getUsingURLIgnoreRest(url, env);
143
144     Context ctx = (Context)res.getResolvedObj();
145     try {
146         return ctx.lookup(res.getRemainingName());
147     } finally {
148         ctx.close();
149     }
150     }
151
152     private static Object JavaDoc getUsingURLs(String JavaDoc[] urls, Hashtable JavaDoc env) {
153     for (int i = 0; i < urls.length; i++) {
154         String JavaDoc url = urls[i];
155         try {
156         Object JavaDoc obj = getUsingURL(url, env);
157         if (obj != null) {
158             return obj;
159         }
160         } catch (NamingException e) {
161         }
162     }
163     return null; // %%% exception??
164
}
165
166
167     /**
168      * corbaname:rir: -> iiopname://localhost:2089/
169      * corbaname:rir:/NameService -> iiopname://localhost:2089/
170      * corbaname:rir:/dev/NameService
171      * -> InvalidNameException (key_string not supported)
172      *
173      * corbaname:: -> iiopname://localhost:2089/
174      * corbaname::orbhost:999#this/is/a/name
175      * -> iiopname://orbhost:999/this/is/a/name
176      * corbaname::orbhost:999,:webhost#this/is/a/name
177      * -> iiopname://orbhost:999,webhost:2089/this/is/a/name
178      * corbaname::orbhost:999,:webhost/key/String#this/is/a/name
179      * -> InvalidNameException (key_string not supported)
180     
181      * corbaname:iiop: -> iiopname://localhost:2089/
182      * corbaname:iiop:orbhost:999#this/is/a/name
183      * -> iiopname://orbhost:999/this/is/a/name
184      * corbaname:iiop:orbhost:999,iiop:webhost#this/is/a/name
185      * -> iiopname://orbhost:999,webhost:2089/this/is/a/name
186      * corbaname:iiop:orbhost:999,iiop:webhost/key/String#this/is/a/name
187      * -> InvalidNameException (key_string not supported)
188      *
189      * corbaname:iiop:orbhost:999,:webhost#this/is/a/name
190      * -> iiopname://orbhost:999,webhost:2089/this/is/a/name
191      * corbaname::orbhost:999,iiop:webhost#this/is/a/name
192      * -> iiopname://orbhost:999,webhost:2089/this/is/a/name
193      */

194     static String JavaDoc rewriteUrl(String JavaDoc url) throws NamingException {
195
196     // Find string_name
197
String JavaDoc stringName = null, corbaloc;
198     int hash = url.indexOf('#');
199     if (hash >= 0) {
200         stringName = url.substring(hash+1);
201
202         // get rid of 'corbaname:' and string_name
203
corbaloc = url.substring(10, hash);
204     } else {
205         corbaloc = url.substring(10); // get rid of 'corbaname:'
206
}
207
208     // Make sure key_string is one that we can support
209
String JavaDoc objAddrList;
210     int firstSlash = corbaloc.indexOf('/');
211     if (firstSlash >= 0) {
212         String JavaDoc keyString = corbaloc.substring(firstSlash+1); // skip slash
213

214         // An empty key_string is interpreted as "NameService"
215
if ("".equals(keyString)) {
216         keyString = "NameService";
217         } else if (!"NameService".equals(keyString)) {
218         throw new InvalidNameException(
219             "Support is available only for the NameService key string");
220         }
221
222         objAddrList = corbaloc.substring(0, firstSlash);
223     } else {
224         objAddrList = corbaloc;
225     }
226         
227     // Rewrite objAddrList into iiopname format
228

229     int len = objAddrList.length();
230     int colon, start = 0, comma;
231     String JavaDoc prot, addr;
232     StringBuffer JavaDoc newUrl = new StringBuffer JavaDoc("iiopname://");
233     while (start < len) {
234         colon = objAddrList.indexOf(':', start);
235         prot = objAddrList.substring(start, colon);
236         if (prot.equals("") || prot.equals("iiop")) {
237         // Find end of this address
238
comma = objAddrList.indexOf(',', colon+1);
239         if (comma < 0) {
240             // last address in list
241
addr = objAddrList.substring(colon+1, len);
242             start = len;
243         } else {
244             addr = objAddrList.substring(colon+1, comma);
245             start = comma + 1;
246         }
247
248         newUrl.append(addr);
249
250         // Add default port if none has been specified
251
if (addr.indexOf(':') < 0) {
252             newUrl.append(':');
253             newUrl.append(DEFAULT_PORT);
254         }
255
256         if (comma >= 0) {
257             // add comma
258
newUrl.append(',');
259         }
260         } else if (prot.equals("rir")) {
261         newUrl.append(DEFAULT_HOST);
262         newUrl.append(':');
263         newUrl.append(DEFAULT_PORT);
264
265         start = colon + 1; // skip colon
266

267         if (start != len) {
268             throw new InvalidNameException("Only one rir is supported");
269         }
270         } else {
271         throw new InvalidNameException("Unknown subscheme: " + url);
272         }
273     }
274
275     if (stringName != null) {
276         newUrl.append('/');
277         newUrl.append(stringName);
278     }
279
280     return newUrl.toString();
281     }
282
283     public static final String JavaDoc[] tests = {
284     "corbaname:rir:",
285     "corbaname:rir:/NameService",
286     "corbaname:rir:/dev/NameService",
287     "corbaname:rir:,rir:/NameService",
288     "corbaname::",
289     "corbaname::orbhost:999#this/is/a/name",
290     "corbaname::orbhost:999,:webhost#this/is/a/name",
291     "corbaname::orbhost:999,:webhost/key/String#this/is/a/name",
292     "corbaname:iiop:",
293     "corbaname:iiop:orbhost:999#this/is/a/name",
294     "corbaname:iiop:orbhost:999,iiop:webhost#this/is/a/name",
295     "corbaname:iiop:orbhost:999,iiop:webhost/key/String#this/is/a/name",
296     "corbaname:iiop:orbhost:999,:webhost#this/is/a/name",
297     "corbaname::orbhost:999,iiop:webhost#this/is/a/name",
298     "corbaname:foo:bar#this/is/a/name"
299     };
300
301     public static void main(String JavaDoc[] args) {
302
303     for (int i = 0; i < tests.length; i++) {
304       /** IASRI 4660742
305         System.out.println(tests[i]);
306       **/

307       //START OF IASRI 4660742
308
if (_logger.isLoggable(Level.FINE))
309           _logger.log(Level.FINE,tests[i]);
310       //END OF IASRI 4660742
311

312         try {
313     /** IASRI 4660742
314         System.out.println(" " + rewriteUrl(tests[i]));
315     **/

316     //START OF IASRI 4660742
317
if (_logger.isLoggable(Level.FINE))
318      _logger.log(Level.FINE," " + rewriteUrl(tests[i]));
319     //END OF IASRI 4660742
320
} catch (NamingException e) {
321     /** IASRI 4660742
322         System.out.println(" " + e);
323     **/

324     //START OF IASRI 4660742
325
_logger.log(Level.SEVERE,"java_jndi.excep_in_main", e);
326     //END OF IASRI 4660742
327
}
328     }
329     }
330 }
331
Popular Tags