1 package org.jacorb.ir; 2 3 22 23 import java.util.StringTokenizer ; 24 import org.omg.CORBA.portable.BoxedValueHelper ; 25 import org.omg.CORBA.INTF_REPOS ; 26 27 32 public class RepositoryID 33 { 34 38 public static String className (String repId, 39 ClassLoader loader) 40 { 41 return className (repId, null, loader); 42 } 43 44 50 public static String className (String repId, 51 String suffix, 52 ClassLoader loader) 53 { 54 if (repId.startsWith ("RMI:")) 55 { 56 return repId.substring (4, repId.indexOf (':', 4)) 57 + ( suffix != null ? suffix : "" ); 58 } 59 else if (repId.startsWith ("IDL:")) 60 { 61 String id = repId.substring (4, repId.lastIndexOf(':')) 62 + ( suffix != null ? suffix : "" ); 63 if (id.equals ("omg.org/CORBA/WStringValue")) 64 return "java.lang.String"; 65 else 66 { 67 int firstSlash = id.indexOf ("/"); 68 String prefix = id.substring (0, firstSlash); 69 70 if (prefix.equals ("omg.org")) 71 return ir2scopes ("org.omg", 72 id.substring (firstSlash + 1), 73 loader); 74 else if (prefix.indexOf ('.') != -1) 75 return ir2scopes (reversePrefix (prefix), 76 id.substring (firstSlash + 1), 77 loader); 78 else 79 return ir2scopes ("", id, loader); 80 } 81 } 82 else 83 { 84 throw new INTF_REPOS ("Unrecognized RepositoryID: " + repId); 85 } 86 } 87 88 private static final String reversePrefix (String prefix) 89 { 90 StringTokenizer tok = new StringTokenizer (prefix, "."); 91 String result = tok.nextToken(); 92 93 while (tok.hasMoreTokens()) 94 { 95 result = tok.nextToken() + '.' + result; 96 } 97 return result; 98 } 99 100 104 private static String ir2scopes (String prefix, 105 String s, 106 ClassLoader loader) 107 { 108 if( s.indexOf("/") < 0) 109 return s; 110 java.util.StringTokenizer strtok = 111 new java.util.StringTokenizer ( s, "/" ); 112 113 int count = strtok.countTokens(); 114 StringBuffer sb = new StringBuffer (); 115 sb.append(prefix); 116 117 for( int i = 0; strtok.hasMoreTokens(); i++ ) 118 { 119 String sc = strtok.nextToken(); 120 Class c = null; 121 if( sb.toString().length() > 0 ) 122 c = loadClass (sb.toString() + "." + sc, loader); 123 else 124 c = loadClass (sc, loader); 125 if (c == null) 126 if( sb.toString().length() > 0 ) 127 sb.append( "." + sc ); 128 else 129 sb.append( sc ); 130 else 131 if( i < count-1) 132 sb.append( "." + sc + "Package"); 133 else 134 sb.append( "." + sc ); 135 } 136 137 return sb.toString(); 138 } 139 140 public static String repId (Class c) 141 { 142 if (org.omg.CORBA.portable.IDLEntity .class.isAssignableFrom (c)) 143 { 144 String className = c.getName(); 145 String head = ""; 146 String body = ""; 147 148 151 if( className.startsWith("org.omg") || 152 className.startsWith("org/omg") ) 153 { 154 if( className.length() > 7 ) 155 body = className.substring(7); 156 return "IDL:omg.org/" + scopesToIR(body) + ":1.0"; 157 } 158 else 159 return "IDL:" + scopesToIR(className) + ":1.0" ; 160 } 161 else 162 return org.jacorb.util.ValueHandler.getRMIRepositoryID (c); 163 } 164 165 166 private static String scopesToIR( String s ) 167 { 168 if( s.indexOf(".") < 0) 169 return s; 170 java.util.StringTokenizer strtok = 171 new java.util.StringTokenizer ( s, "." ); 172 173 String scopes[] = new String [strtok.countTokens()]; 174 175 for( int i = 0; strtok.hasMoreTokens(); i++ ) 176 { 177 String sc = strtok.nextToken(); 178 if( sc.endsWith("Package")) 179 scopes[i] = sc.substring(0,sc.indexOf("Package")); 180 else 181 scopes[i] = sc; 182 } 183 184 StringBuffer sb = new StringBuffer (); 185 if( scopes.length > 1 ) 186 { 187 for( int i = 0; i < scopes.length-1; i++) 188 sb.append( scopes[i] + "/" ); 189 } 190 191 sb.append( scopes[scopes.length-1] ); 192 return sb.toString(); 193 } 194 195 202 public static String toRepositoryID ( String className, 203 boolean resolveClass, 204 ClassLoader loader ) 205 { 206 if( className.equals("") || 207 className.startsWith("IDL:") || 208 className.startsWith ("RMI:")) 209 return className; 210 else 211 { 212 if( resolveClass ) 213 { 214 215 Class c = loadClass(className, loader); 216 if (c == null) 217 throw new IllegalArgumentException ("cannot find class: " + className); 218 else 219 return repId (c); 220 } 221 return "IDL:" + className + ":1.0"; 222 } 223 } 224 225 public static String toRepositoryID( String className, ClassLoader loader ) 226 { 227 return toRepositoryID( className, true, loader ); 228 } 229 230 235 private static Class loadClass (String name, ClassLoader loader) 236 { 237 try 238 { 239 if (loader != null) 240 return loader.loadClass (name); 241 else 242 return org.jacorb.util.ObjectUtil.classForName(name); 243 } 244 catch (ClassNotFoundException e) 245 { 246 return null; 247 } 248 } 249 250 257 public static BoxedValueHelper createBoxedValueHelper(String repId, 258 ClassLoader loader) 259 { 260 String className = className(repId, "Helper", loader); 261 Class c = loadClass(className, loader); 262 if (c != null) 263 try 264 { 265 return (BoxedValueHelper )c.newInstance(); 266 } 267 catch (Exception e) 268 { 269 throw new RuntimeException (e.toString()); 270 } 271 else 272 return null; 273 } 274 } 275 | Popular Tags |