KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > util > CorbaLoc


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.orb.util;
22
23 /**
24  * @author Gerald Brose
25  * @version $Id: CorbaLoc.java,v 1.14 2004/05/06 12:40:00 nicolas Exp $
26  */

27
28
29 import java.io.*;
30 import java.util.*;
31 import org.jacorb.orb.ORB;
32 import org.jacorb.orb.iiop.IIOPProfile;
33 import org.omg.ETF.Profile;
34
35 public class CorbaLoc
36 {
37     private ORB orb;
38     private String JavaDoc keyString;
39     private byte[] key;
40     private String JavaDoc bodyString;
41     private boolean is_rir;
42
43     public Profile[] profileList;
44
45     public CorbaLoc(ORB o, String JavaDoc addr)
46     {
47         orb = o;
48         is_rir = false;
49         parse(addr);
50     }
51
52     public boolean rir()
53     {
54         return is_rir;
55     }
56
57     public String JavaDoc toString()
58     {
59         return "corbaloc:" + body();
60     }
61
62     public String JavaDoc body()
63     {
64         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
65
66         sb.append(bodyString);
67
68         if (keyString != null)
69             sb.append("/" + keyString);
70
71         return sb.toString();
72     }
73
74     public String JavaDoc getKeyString()
75     {
76         return keyString;
77     }
78
79     public byte[] getKey()
80     {
81         return key;
82     }
83
84     public void defaultKeyString(String JavaDoc s)
85     {
86         if( keyString == null )
87             keyString = s;
88         else
89             throw new RuntimeException JavaDoc("KeyString not empty, cannot default to " + s );
90     }
91
92     public String JavaDoc toCorbaName(String JavaDoc str_name)
93     {
94         if (getKeyString() == null)
95             defaultKeyString("NameService");
96
97         if (str_name != null && str_name.length() > 0)
98         {
99             try
100             {
101                 return "corbaname:" + body() + "#" + str_name;
102             }
103             catch (Exception JavaDoc e)
104             {
105                 return null;
106             }
107         }
108         else
109             return "corbaname:" + body();
110     }
111
112     /**
113      * parses a string representing a corbaloc: reference
114      */

115     private void parse(String JavaDoc addr)
116     {
117         if( addr == null || !addr.startsWith("corbaloc:"))
118             throw new IllegalArgumentException JavaDoc("URL must start with \'corbaloc:\'");
119
120         String JavaDoc sb;
121         if( addr.indexOf('/') == -1 )
122         {
123             sb = addr.substring( addr.indexOf(':')+1 );
124             if (addr.startsWith("corbaloc:rir:"))
125             {
126                 is_rir = true;
127                 // default key string for rir protocol
128
keyString = "NameService";
129             }
130             else
131             {
132                 keyString = null;
133             }
134             key = new byte[0];
135         }
136         else
137         {
138             sb = addr.substring( addr.indexOf(':')+1, addr.indexOf('/') );
139             keyString = addr.substring( addr.indexOf('/')+1 );
140             key = parseKey( keyString );
141         }
142
143         if( sb.indexOf(',') > 0 )
144         {
145             StringTokenizer tokenizer = new StringTokenizer( sb, "," );
146             profileList = new Profile[tokenizer.countTokens()];
147             int pIndex = 0;
148             for( int i = 0; i < profileList.length; i++ )
149             {
150                 Profile p = parseAddress(tokenizer.nextToken());
151                 if (p == null)
152                     continue;
153                 profileList[pIndex] = p;
154                 pIndex++;
155             }
156             while (pIndex < profileList.length)
157             {
158                 profileList[pIndex] = null;
159                 pIndex++;
160             }
161
162         }
163         else
164             profileList = new Profile[]{ parseAddress(sb) };
165
166         bodyString = sb;
167     }
168
169     private Profile parseAddress(String JavaDoc addr)
170     {
171         int colon = addr.indexOf(':');
172         if (colon == -1)
173             throw new IllegalArgumentException JavaDoc(
174                 "Illegal object address format: " + addr);
175         if (addr.equals("rir:"))
176         {
177             is_rir = true;
178             /* resolve initials references protocol */
179             return null;
180         }
181
182         Profile result = null;
183         if (orb == null
184             && (colon == 0
185                 || addr.startsWith("iiop:")
186                 || addr.startsWith("ssliop:")))
187             result = new IIOPProfile(addr);
188         else if (orb != null)
189         {
190             String JavaDoc token = addr.substring(0, colon);
191             List factories = orb.getTransportManager().getFactoriesList();
192             for (Iterator i = factories.iterator();
193                  result == null && i.hasNext();)
194             {
195                 org.omg.ETF.Factories f = (org.omg.ETF.Factories)i.next();
196                 result = f.decode_corbaloc(addr);
197             }
198         }
199         if (result == null)
200             throw new IllegalArgumentException JavaDoc(
201                 "Unknown protocol in object address format: " + addr);
202         return result;
203     }
204
205     private static boolean legalChar(char c)
206     {
207         if(( c >= '0' && c <= '9') ||
208            ( c >= 'a' && c <= 'z') ||
209            ( c >= 'A' && c <= 'Z' ))
210             return true;
211         else
212             return ( c == ';' || c == '/' ||c == ':' || c == '?' ||
213                      c == '@' || c == '&' ||c == '=' || c == '+' ||
214                      c == '$' || c == ',' ||c == '_' || c == '.' ||
215                      c == '!' || c == '~' ||c == '*' || c == '\'' ||
216                      c == '-' || c == '(' || c == ')' );
217     }
218
219     private static byte hexValue(char c)
220     {
221         return (byte)((c >= 'a') ? (10 + c - 'a') :
222                       ((c >= 'A') ? (10 + c - 'A') : (c - '0'))
223                       );
224     }
225
226     private static char hexDigit(byte b)
227     {
228         if( (b & 0xf0) != 0 )
229             throw new IllegalArgumentException JavaDoc("Hex digit out of range " + b);
230
231         return (char)( b < 10 ? '0' + (char)b : 'A' + (char)b - 10 ) ;
232     }
233
234     private static boolean isHex(char c)
235     {
236         return ( ( c >= '0' && c <= '9') ||
237                  ( c >= 'a' && c <='f') ||
238                  ( c >= 'A' && c <='F'));
239     }
240
241     public static byte[] parseKey(String JavaDoc s)
242     {
243         char[] tmp = s.toCharArray();
244         int count = tmp.length;
245
246         for( int i = 0; i < tmp.length; i++ )
247         {
248             if( !legalChar(tmp[i]) )
249             {
250                 if( tmp[i] == '%' )
251                 {
252                     if( isHex(tmp[i+1]) && isHex(tmp[i+2]))
253                     {
254                         count -= 2;
255                         i+=2;
256                     }
257                     else
258                         throw new IllegalArgumentException JavaDoc("Illegal escape in URL character");
259                 }
260                 else
261                     throw new IllegalArgumentException JavaDoc("URL character out of range: " + tmp[i]);
262             }
263         }
264
265         byte[] result = new byte[count];
266         int idx = 0;
267
268         for( int i = 0; i < count; i++ )
269         {
270             if( legalChar( tmp[idx]))
271                 result[i] = (byte)tmp[idx++];
272             else
273             {
274                 result[i] = (byte)( (hexValue(tmp[idx+1]))<<4 | hexValue(tmp[idx+2]) );
275                 idx += 3;
276             }
277         }
278         return result;
279     }
280
281     public static String JavaDoc parseKey(byte[] key)
282     {
283         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
284
285         for( int i = 0; i < key.length; i++ )
286         {
287             if( !legalChar((char)key[i]) )
288             {
289                 sb.append( '%' );
290                 // Mask the bytes before shift to ensure 10001001 doesn't get
291
// shifted to 11111000 but 00001000 (linden java faq).
292
sb.append( hexDigit( (byte)((key[i] & 0xff) >> 4 )));
293                 sb.append( hexDigit( (byte)( key[i] & 0x0f )));
294             }
295             else
296             {
297                 sb.append( (char)key[i]);
298             }
299         }
300         return sb.toString();
301     }
302
303     public static void main(String JavaDoc[] args)
304     {
305         String JavaDoc [] noarg = new String JavaDoc[]{};
306         ORB orb = (org.jacorb.orb.ORB)ORB.init(noarg,null);
307         for( int i = 0; i < args.length; i++ )
308         {
309             System.out.println( new CorbaLoc(orb, args[i] ).toString());
310         }
311     }
312
313 }
314
Popular Tags