1 20 21 package org.jacorb.orb.util; 22 23 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 keyString; 39 private byte[] key; 40 private String bodyString; 41 private boolean is_rir; 42 43 public Profile[] profileList; 44 45 public CorbaLoc(ORB o, String 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 toString() 58 { 59 return "corbaloc:" + body(); 60 } 61 62 public String body() 63 { 64 StringBuffer sb = new StringBuffer (); 65 66 sb.append(bodyString); 67 68 if (keyString != null) 69 sb.append("/" + keyString); 70 71 return sb.toString(); 72 } 73 74 public String getKeyString() 75 { 76 return keyString; 77 } 78 79 public byte[] getKey() 80 { 81 return key; 82 } 83 84 public void defaultKeyString(String s) 85 { 86 if( keyString == null ) 87 keyString = s; 88 else 89 throw new RuntimeException ("KeyString not empty, cannot default to " + s ); 90 } 91 92 public String toCorbaName(String 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 e) 104 { 105 return null; 106 } 107 } 108 else 109 return "corbaname:" + body(); 110 } 111 112 115 private void parse(String addr) 116 { 117 if( addr == null || !addr.startsWith("corbaloc:")) 118 throw new IllegalArgumentException ("URL must start with \'corbaloc:\'"); 119 120 String 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 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 addr) 170 { 171 int colon = addr.indexOf(':'); 172 if (colon == -1) 173 throw new IllegalArgumentException ( 174 "Illegal object address format: " + addr); 175 if (addr.equals("rir:")) 176 { 177 is_rir = true; 178 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 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 ( 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 ("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 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 ("Illegal escape in URL character"); 259 } 260 else 261 throw new IllegalArgumentException ("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 parseKey(byte[] key) 282 { 283 StringBuffer sb = new StringBuffer (); 284 285 for( int i = 0; i < key.length; i++ ) 286 { 287 if( !legalChar((char)key[i]) ) 288 { 289 sb.append( '%' ); 290 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 [] args) 304 { 305 String [] noarg = new String []{}; 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 |