38 |
|
|
39 |
package gnu.CORBA.NamingService; |
package gnu.CORBA.NamingService; |
40 |
|
|
41 |
|
import gnu.CORBA.Functional_ORB; |
42 |
import gnu.CORBA.IOR; |
import gnu.CORBA.IOR; |
43 |
import gnu.CORBA.Unexpected; |
import gnu.CORBA.Unexpected; |
44 |
import gnu.CORBA.Version; |
import gnu.CORBA.Version; |
48 |
import org.omg.CORBA.ORB; |
import org.omg.CORBA.ORB; |
49 |
import org.omg.CORBA.Object; |
import org.omg.CORBA.Object; |
50 |
import org.omg.CORBA.ORBPackage.InvalidName; |
import org.omg.CORBA.ORBPackage.InvalidName; |
51 |
|
import org.omg.CORBA.portable.Delegate; |
52 |
|
import org.omg.CORBA.portable.ObjectImpl; |
53 |
|
import org.omg.CosNaming.NamingContext; |
54 |
|
import org.omg.CosNaming.NamingContextExtHelper; |
55 |
|
import org.omg.CosNaming.NamingContextHelper; |
56 |
|
import org.omg.CosNaming._NamingContextStub; |
57 |
|
|
58 |
import java.io.UnsupportedEncodingException; |
import java.io.UnsupportedEncodingException; |
59 |
import java.net.URLDecoder; |
import java.net.URLDecoder; |
74 |
* @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) |
* @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org) |
75 |
*/ |
*/ |
76 |
public class NameParser |
public class NameParser |
77 |
|
extends snConverter |
78 |
{ |
{ |
79 |
/** |
/** |
80 |
* The mandatory prefix. |
* The corbaloc prefix. |
81 |
*/ |
*/ |
82 |
public static final String CORBALOC = "corbaloc"; |
public static final String pxCORBALOC = "corbaloc"; |
83 |
|
|
84 |
|
/** |
85 |
|
* The corbaname prefix. |
86 |
|
*/ |
87 |
|
public static final String pxCORBANAME = "corbaname"; |
88 |
|
|
89 |
|
/** |
90 |
|
* The IOR prefix. |
91 |
|
*/ |
92 |
|
public static final String pxIOR = "ior"; |
93 |
|
|
94 |
/** |
/** |
95 |
* Marks iiop protocol. |
* Marks iiop protocol. |
107 |
public static final int DEFAULT_PORT = 2809; |
public static final int DEFAULT_PORT = 2809; |
108 |
|
|
109 |
/** |
/** |
110 |
|
* The default name. |
111 |
|
*/ |
112 |
|
public static final String DEFAULT_NAME = "NameService"; |
113 |
|
|
114 |
|
/** |
115 |
|
* The string to name converter, initialized on demand. |
116 |
|
*/ |
117 |
|
static snConverter converter; |
118 |
|
|
119 |
|
/** |
120 |
|
* The current position. |
121 |
|
*/ |
122 |
|
int p; |
123 |
|
|
124 |
|
/** |
125 |
|
* The address being parsed, splitted into tokens. |
126 |
|
*/ |
127 |
|
String[] t; |
128 |
|
|
129 |
|
/** |
130 |
* Parse CORBALOC. |
* Parse CORBALOC. |
131 |
* |
* |
132 |
* The expected format is: <br> |
* The expected format is: <br> |
133 |
* 1. corbaloc:[iiop][version.subversion@]:host[:port]/key <br> |
* 1. corbaloc:[iiop][version.subversion@]:host[:port]/key <br> |
134 |
* 2. corbaloc:rir:/key <br> |
* 2. corbaloc:rir:[/key] <br> |
135 |
|
* 3. corbaname:[iiop][version.subversion@]:host[:port]/key <br> |
136 |
|
* 4. corbaname:rir:[/key] <br> |
137 |
* |
* |
138 |
* protocol defaults to IOP. |
* Protocol defaults to IOP, the object key defaults to the NameService. |
139 |
* |
* |
140 |
* @param corbaloc the string to parse. |
* @param corbaloc the string to parse. |
141 |
* @param orb the ORB, needed to create IORs and resolve rir references. |
* @param orb the ORB, needed to create IORs and resolve rir references. |
142 |
* |
* |
143 |
* @return the constructed IOR. |
* @return the resolved object. |
144 |
*/ |
*/ |
145 |
public static java.lang.Object corbaloc(String corbaloc, ORB orb) |
public synchronized org.omg.CORBA.Object corbaloc(String corbaloc, |
146 |
|
Functional_ORB orb) |
147 |
throws BAD_PARAM |
throws BAD_PARAM |
148 |
{ |
{ |
149 |
|
boolean corbaname; |
150 |
|
|
151 |
// The alternative addresses, if given. |
// The alternative addresses, if given. |
152 |
ArrayList alt_addr = new ArrayList(); |
ArrayList alt_addr = new ArrayList(); |
164 |
// The object key as string. |
// The object key as string. |
165 |
String key; |
String key; |
166 |
|
|
167 |
StringTokenizer st = new StringTokenizer(corbaloc, ":@/.,", true); |
StringTokenizer st = new StringTokenizer(corbaloc, ":@/.,#", true); |
168 |
|
|
169 |
String[] t = new String[st.countTokens()]; |
t = new String[st.countTokens()]; |
170 |
|
|
171 |
for (int i = 0; i < t.length; i++) |
for (int i = 0; i < t.length; i++) |
172 |
{ |
{ |
173 |
t[i] = st.nextToken(); |
t[i] = st.nextToken(); |
174 |
} |
} |
175 |
|
|
176 |
int p = 0; |
p = 0; |
177 |
|
|
178 |
|
if (t[p].startsWith(pxCORBANAME)) |
179 |
|
corbaname = true; |
180 |
|
else if (t[p].equalsIgnoreCase(pxCORBALOC)) |
181 |
|
corbaname = false; |
182 |
|
else if (t[p].equalsIgnoreCase(pxIOR)) |
183 |
|
{ |
184 |
|
IOR ior = IOR.parse(corbaloc); |
185 |
|
return orb.ior_to_object(ior); |
186 |
|
} |
187 |
|
else |
188 |
|
throw new DATA_CONVERSION("Unsupported protocol: '" + t[p] + "'"); |
189 |
|
|
190 |
if (!t[p++].equalsIgnoreCase(CORBALOC)) |
p++; |
|
throw new BAD_PARAM("Must start with corbaloc:"); |
|
191 |
|
|
192 |
if (!t[p++].equals(":")) |
if (!t[p++].equals(":")) |
193 |
throw new BAD_PARAM("Must start with corbaloc:"); |
throw new BAD_PARAM("Syntax (':' expected after name prefix)"); |
194 |
|
|
195 |
// Check for rir: |
// Check for rir: |
196 |
if (t[p].equals(RIR)) |
if (t[p].equals(RIR)) |
199 |
if (!t[p++].equals(":")) |
if (!t[p++].equals(":")) |
200 |
throw new BAD_PARAM("':' expected after 'rir'"); |
throw new BAD_PARAM("':' expected after 'rir'"); |
201 |
|
|
202 |
key = readKey(p, t); |
key = readKey("/"); |
203 |
|
|
204 |
Object object; |
Object object; |
205 |
try |
try |
206 |
{ |
{ |
207 |
object = orb.resolve_initial_references(key); |
object = orb.resolve_initial_references(key); |
208 |
|
return corbaname ? resolve(object) : object; |
209 |
} |
} |
210 |
catch (InvalidName e) |
catch (InvalidName e) |
211 |
{ |
{ |
212 |
throw new BAD_PARAM("Unknown initial reference '"+key+"'"); |
throw new BAD_PARAM("Unknown initial reference '" + key + "'"); |
213 |
} |
} |
|
return object; |
|
214 |
} |
} |
215 |
else |
else |
216 |
// Check for iiop. |
// Check for iiop. |
217 |
if (t[p].equals(IIOP) || t[p].equals(":")) |
if (t[p].equals(IIOP) || t[p].equals(":")) |
218 |
{ |
{ |
219 |
IOR ior = new IOR(); |
IOR ior = new IOR(); |
220 |
|
|
221 |
Addresses: do |
Addresses: do |
222 |
{ // Read addresses. |
{ // Read addresses. |
223 |
if (t[p].equals(":")) |
if (t[p].equals(":")) |
256 |
p++; // '@' at this point. |
p++; // '@' at this point. |
257 |
} |
} |
258 |
} |
} |
259 |
|
|
260 |
ior.Internet.version = new Version(major, minor); |
ior.Internet.version = new Version(major, minor); |
261 |
|
|
262 |
// Then host data goes till '/' or ':'. |
// Then host data goes till '/' or ':'. |
263 |
StringBuffer bhost = new StringBuffer(corbaloc.length()); |
StringBuffer bhost = new StringBuffer(corbaloc.length()); |
264 |
while (!t[p].equals(":") && !t[p].equals("/") && !t[p].equals(",")) |
while (!t[p].equals(":") && !t[p].equals("/") && !t[p].equals(",")) |
281 |
throw new BAD_PARAM("Invalid port '" + t[p - 1] + "'"); |
throw new BAD_PARAM("Invalid port '" + t[p - 1] + "'"); |
282 |
} |
} |
283 |
} |
} |
284 |
|
|
285 |
ior.Internet.port = port; |
ior.Internet.port = port; |
286 |
|
|
287 |
// Id is not listed. |
// Id is not listed. |
288 |
ior.Id = ""; |
ior.Id = ""; |
289 |
|
|
290 |
if (t[p].equals(",")) |
if (t[p].equals(",")) |
291 |
p++; |
p++; |
292 |
else |
else |
294 |
} |
} |
295 |
while (true); |
while (true); |
296 |
|
|
297 |
key = readKey(p, t); |
key = readKey("/"); |
298 |
ior.key = key.getBytes(); |
ior.key = key.getBytes(); |
299 |
|
|
300 |
return ior; |
org.omg.CORBA.Object object = orb.ior_to_object(ior); |
301 |
|
return corbaname ? resolve(object) : object; |
302 |
} |
} |
303 |
|
|
304 |
else |
else |
305 |
throw new DATA_CONVERSION("Unsupported protocol '" + t[p] + "'"); |
throw new DATA_CONVERSION("Unsupported protocol '" + t[p] + "'"); |
306 |
|
} |
307 |
|
|
308 |
|
private org.omg.CORBA.Object resolve(org.omg.CORBA.Object object) |
309 |
|
{ |
310 |
|
NamingContext ns; |
311 |
|
String key = "?"; |
312 |
|
try |
313 |
|
{ |
314 |
|
if (object instanceof NamingContext) |
315 |
|
ns = (NamingContext) object; |
316 |
|
else |
317 |
|
{ |
318 |
|
Delegate delegate = ((ObjectImpl) object)._get_delegate(); |
319 |
|
ns = new _NamingContextStub(delegate); |
320 |
|
} |
321 |
|
} |
322 |
|
catch (Exception ex) |
323 |
|
{ |
324 |
|
BAD_PARAM bad = new BAD_PARAM("The CORBANAME target " + object |
325 |
|
+ " is not a NamingContext"); |
326 |
|
bad.minor = 10; |
327 |
|
bad.initCause(ex); |
328 |
|
throw bad; |
329 |
|
} |
330 |
|
|
331 |
|
if (converter == null) |
332 |
|
converter = new snConverter(); |
333 |
|
|
334 |
|
try |
335 |
|
{ |
336 |
|
key = readKey("#"); |
337 |
|
object = ns.resolve(converter.toName(key)); |
338 |
|
return object; |
339 |
|
} |
340 |
|
catch (Exception ex) |
341 |
|
{ |
342 |
|
BAD_PARAM bad = new BAD_PARAM("Wrong CORBANAME '" + key + "'"); |
343 |
|
bad.minor = 10; |
344 |
|
bad.initCause(ex); |
345 |
|
throw bad; |
346 |
|
} |
347 |
} |
} |
348 |
|
|
349 |
private static String readKey(int p, String[] t) |
private String readKey(String delimiter) |
350 |
throws BAD_PARAM |
throws BAD_PARAM |
351 |
{ |
{ |
352 |
if (!t[p].equals("/")) |
if (p < t.length) |
353 |
throw new BAD_PARAM("'/keyString' expected '" + t[p] + "' found"); |
if (!t[p].equals(delimiter)) |
354 |
|
{ |
355 |
|
if (t[p].equals("#")) |
356 |
|
return DEFAULT_NAME; |
357 |
|
else |
358 |
|
throw new BAD_PARAM("'" + delimiter + "String' expected '" + t[p] |
359 |
|
+ "' found"); |
360 |
|
} |
361 |
|
|
362 |
StringBuffer bKey = new StringBuffer(); |
StringBuffer bKey = new StringBuffer(); |
363 |
p++; |
p++; |
364 |
|
|
365 |
while (p < t.length) |
while (p < t.length && !t[p].equals("#")) |
366 |
bKey.append(t[p++]); |
bKey.append(t[p++]); |
367 |
|
|
368 |
|
if (bKey.length() == 0) |
369 |
|
return DEFAULT_NAME; |
370 |
|
|
371 |
try |
try |
372 |
{ |
{ |
373 |
return URLDecoder.decode(bKey.toString(), "UTF-8"); |
return URLDecoder.decode(bKey.toString(), "UTF-8"); |
377 |
throw new Unexpected("URLDecoder does not support UTF-8", e); |
throw new Unexpected("URLDecoder does not support UTF-8", e); |
378 |
} |
} |
379 |
} |
} |
380 |
|
|
381 |
static void corbalocT(String ior, ORB orb) |
static NameParser n = new NameParser(); |
382 |
|
|
383 |
|
static void corbalocT(String ior, Functional_ORB orb) |
384 |
{ |
{ |
385 |
System.out.println(ior); |
System.out.println(ior); |
386 |
System.out.println(corbaloc(ior, orb)); |
System.out.println(n.corbaloc(ior, orb)); |
387 |
System.out.println(); |
System.out.println(); |
388 |
} |
} |
389 |
|
|
391 |
{ |
{ |
392 |
try |
try |
393 |
{ |
{ |
394 |
ORB orb = ORB.init(args, null); |
Functional_ORB orb = (Functional_ORB) ORB.init(args, null); |
395 |
corbalocT("corbaloc:iiop:1.3@155axyz.com/Prod/aTradingService", orb); |
corbalocT("corbaloc:iiop:1.3@155axyz.com/Prod/aTradingService", orb); |
396 |
corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb); |
corbalocT("corbaloc:iiop:2.7@255bxyz.com/Prod/bTradingService", orb); |
397 |
corbalocT("corbaloc:iiop:355cxyz.com/Prod/cTradingService", orb); |
corbalocT("corbaloc:iiop:355cxyz.com/Prod/cTradingService", orb); |
402 |
corbalocT("corbaloc:iiop:1.2@host1:3076/0", orb); |
corbalocT("corbaloc:iiop:1.2@host1:3076/0", orb); |
403 |
|
|
404 |
corbalocT("corbaloc:rir:/NameService", orb); |
corbalocT("corbaloc:rir:/NameService", orb); |
405 |
|
corbalocT("corbaloc:rir:/", orb); |
406 |
|
corbalocT("corbaloc:rir:", orb); |
407 |
|
|
408 |
|
corbalocT("corbaloc:rir:/NameService", orb); |
409 |
|
corbalocT("corbaloc:rir:/", orb); |
410 |
|
corbalocT("corbaloc:rir:", orb); |
411 |
|
|
412 |
corbalocT("corbaloc::555xyz.com,:556xyz.com:80/Dev/NameService", orb); |
corbalocT("corbaloc::555xyz.com,:556xyz.com:80/Dev/NameService", orb); |
413 |
} |
} |