1 2 package SOFA.SOFAnode.Made.Tools; 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 7 import javax.xml.parsers.DocumentBuilder ; 8 import javax.xml.parsers.DocumentBuilderFactory ; 9 import javax.xml.parsers.ParserConfigurationException ; 10 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.Node ; 14 import org.w3c.dom.NodeList ; 15 import org.xml.sax.SAXException ; 16 17 31 public class InstallToTR { 32 33 private static Element dd; 34 private static String version; 35 private static String arch; 36 private static java.util.Hashtable ent; 37 private static java.util.Hashtable fullNamesWithoutVersion; 38 private static java.util.Properties trIndex; 39 private static java.util.Properties implIndex; 40 private static String lastIndex; 41 42 public static void main(String [] argv) { 43 if (argv.length < 2) { 44 System.out.println("too few arguments"); 45 System.exit(1); 46 } 47 48 49 String trDir = System.getProperty("sofa.tr.dir", null); 50 if (trDir == null) { 51 System.out.println("Specify TR directrory (java property \"sofa.tr.dir\")"); 52 System.exit(1); 53 } 54 trIndex = new java.util.Properties (); 55 implIndex = new java.util.Properties (); 56 try { 57 FileInputStream fis = new FileInputStream (trDir+File.separator+"iface"+File.separator+"index"); 58 trIndex.load(fis); 59 fis.close(); 60 61 fis = new FileInputStream (trDir+File.separator+"impl"+File.separator+"info"); 62 implIndex.load(fis); 63 fis.close(); 64 65 fis = new FileInputStream (trDir+File.separator+"iface"+File.separator+"last"); 66 StringBuffer sb = new StringBuffer (); 67 int a = fis.read(); 68 while (a != -1) { 69 sb.append((char) a); 70 a = fis.read(); 71 } 72 fis.close(); 73 lastIndex = sb.toString(); 74 } catch (java.io.IOException e) { 75 System.out.println("IOException: "+e.getMessage()); 76 System.exit(1); 77 } 78 79 String fileRoot = System.getProperty("file.root", "."); 80 fileRoot += File.separator; 81 82 try { 83 dd = getDDElem(fileRoot+argv[0]); 84 } catch (java.io.IOException e) { 85 System.out.println("IOException: "+e.getMessage()); 86 System.exit(1); 87 } catch (javax.xml.parsers.ParserConfigurationException e) { 88 System.out.println("ParserConfigurationException: "+e.getMessage() ); 89 System.exit(1); 90 } catch (org.xml.sax.SAXException e) { 91 System.out.println("SAXException: "+e.getMessage() ); 92 System.exit(1); 93 } 94 95 version = getImplVersion(dd); 96 ent = getCDLEntities(dd); 97 arch = getArchitectureFullName(dd); 98 fullNamesWithoutVersion = getFullNamesWithoutVersion(trIndex); 99 100 java.util.Hashtable trTable = new java.util.Hashtable (); 102 for (java.util.Enumeration e = ent.keys(); e.hasMoreElements(); ) { 104 String javaName = (String ) e.nextElement(); 105 String fullName = (String ) ent.get(javaName); 106 String specVersion = fullName.substring(fullName.indexOf('?')+1); 107 trTable.put(toVMName(javaName), toVMName(javaName+specVersionToFileName(specVersion))); 108 } 109 for (int i=1; i<argv.length; i++) { 111 try { 112 SOFA.Tools.Resolver.ClassInfo info = new SOFA.Tools.Resolver.ClassInfo(argv[i]); 113 trTable.put(info.className(), info.className()+implVersionToFileName(version)); 114 } catch (java.io.IOException ex) { 115 System.out.println("IOException: "+ex.getMessage()); 116 System.exit(1); 117 } 118 } 119 SOFA.Tools.Resolver.Resolver resolver = new SOFA.Tools.Resolver.Resolver(trTable); 121 122 System.out.print("Installing to TR...."); 123 124 for (java.util.Enumeration e = ent.keys(); e.hasMoreElements(); ) { 126 String javaName = (String ) e.nextElement(); 127 String fullName = (String ) ent.get(javaName); 128 String withoutVersion = fullName.substring(0, fullName.indexOf('?')); 129 String specVersion = fullName.substring(fullName.indexOf('?')+1); 130 if (fullNamesWithoutVersion.containsKey(withoutVersion)) { 131 if (trIndex.containsKey(fullName)) { 132 String ind = (String ) trIndex.get(fullName); 134 File fsource = new File (fileRoot+ classNameToFileName(javaName)); 135 if (!fsource.exists()) { 136 System.out.println("Error: file "+fsource.toString()+" does not exist."); 137 System.exit(1); 138 } 139 File fdest = new File (trDir+File.separator+"iface"+File.separator+ind+File.separator+classNameToFileName(javaName)); 140 File dir = fdest.getParentFile(); 141 if (dir != null) 142 if (! dir.exists()) 143 dir.mkdirs(); 144 try { 145 FileInputStream fis = new FileInputStream (fsource); 146 FileOutputStream fos = new FileOutputStream (fdest); 147 int a; 148 while ( (a = fis.read()) != -1) 149 fos.write(a); 150 fis.close(); 151 fos.close(); 152 } catch (java.io.IOException ex) { 153 System.out.println("IOException: "+ex.getMessage()); 154 System.exit(1); 155 } 156 fdest = new File (trDir+File.separator+"classes"+File.separator+classNameToFileName(javaName+specVersionToFileName(specVersion))); 158 dir = fdest.getParentFile(); 159 if (dir != null) 160 if (! dir.exists()) 161 dir.mkdirs(); 162 try { 163 FileInputStream fis = new FileInputStream (fsource); 164 byte [] b = resolver.process(fis); 165 FileOutputStream fos = new FileOutputStream (fdest); 166 fos.write(b); 167 fis.close(); 168 fos.close(); 169 } catch (java.io.IOException ex) { 170 System.out.println("IOException: "+ex.getMessage()); 171 System.exit(1); 172 } 173 } else { lastIndex = incrementLastIndex(lastIndex); 175 File fsource = new File (fileRoot+ classNameToFileName(javaName)); 176 if (!fsource.exists()) { 177 System.out.println("Error: file "+fsource.toString()+" does not exist."); 178 System.exit(1); 179 } 180 File fdest = new File (trDir+File.separator+"iface"+File.separator+lastIndex+File.separator+classNameToFileName(javaName)); 181 File dir = fdest.getParentFile(); 182 if (dir != null) 183 if (! dir.exists()) 184 dir.mkdirs(); 185 try { 186 FileInputStream fis = new FileInputStream (fsource); 187 FileOutputStream fos = new FileOutputStream (fdest); 188 int a; 189 while ( (a = fis.read()) != -1) 190 fos.write(a); 191 fis.close(); 192 fos.close(); 193 } catch (java.io.IOException ex) { 194 System.out.println("IOException: "+ex.getMessage()); 195 System.exit(1); 196 } 197 fdest = new File (trDir+File.separator+"classes"+File.separator+classNameToFileName(javaName+specVersionToFileName(specVersion))); 199 dir = fdest.getParentFile(); 200 if (dir != null) 201 if (! dir.exists()) 202 dir.mkdirs(); 203 try { 204 FileInputStream fis = new FileInputStream (fsource); 205 byte [] b = resolver.process(fis); 206 FileOutputStream fos = new FileOutputStream (fdest); 207 fos.write(b); 208 fis.close(); 209 fos.close(); 210 } catch (java.io.IOException ex) { 211 System.out.println("IOException: "+ex.getMessage()); 212 System.exit(1); 213 } 214 trIndex.put(fullName, lastIndex); 215 } 216 } else { 217 File fsource = new File (fileRoot+ classNameToFileName(javaName)); 218 if (!fsource.exists()) { 219 System.out.println("Error: file "+fsource.toString()+" does not exist."); 220 System.exit(1); 221 } 222 File fdest = new File (trDir+File.separator+"iface"+File.separator+"a"+File.separator+classNameToFileName(javaName)); 223 File dir = fdest.getParentFile(); 224 if (dir != null) 225 if (! dir.exists()) 226 dir.mkdirs(); 227 try { 228 FileInputStream fis = new FileInputStream (fsource); 229 FileOutputStream fos = new FileOutputStream (fdest); 230 int a; 231 while ( (a = fis.read()) != -1) 232 fos.write(a); 233 fis.close(); 234 fos.close(); 235 } catch (java.io.IOException ex) { 236 System.out.println("IOException: "+ex.getMessage()); 237 System.exit(1); 238 } 239 fdest = new File (trDir+File.separator+"classes"+File.separator+classNameToFileName(javaName+specVersionToFileName(specVersion))); 241 dir = fdest.getParentFile(); 242 if (dir != null) 243 if (! dir.exists()) 244 dir.mkdirs(); 245 try { 246 FileInputStream fis = new FileInputStream (fsource); 247 byte [] b = resolver.process(fis); 248 FileOutputStream fos = new FileOutputStream (fdest); 249 fos.write(b); 250 fis.close(); 251 fos.close(); 252 } catch (java.io.IOException ex) { 253 System.out.println("IOException: "+ex.getMessage()); 254 System.exit(1); 255 } 256 trIndex.put(fullName, "a"); 257 } 258 } 259 260 String sdir = fullNameToFileName(arch)+File.separator+version+File.separator; 262 for (int i=1; i<argv.length; i++) { 263 File fsource = new File (fileRoot+ File.separator+argv[i]); 264 if (!fsource.exists()) { 265 System.out.println("Error: file "+fsource.toString()+" does not exist."); 266 System.exit(1); 267 } 268 File fdest = new File (trDir+File.separator+"impl"+File.separator+sdir+argv[i]); 269 File dir = fdest.getParentFile(); 270 if (dir != null) 271 if (! dir.exists()) 272 dir.mkdirs(); 273 try { 274 FileInputStream fis = new FileInputStream (fsource); 275 FileOutputStream fos = new FileOutputStream (fdest); 276 int a; 277 while ( (a = fis.read()) != -1) 278 fos.write(a); 279 fis.close(); 280 fos.close(); 281 } catch (java.io.IOException ex) { 282 System.out.println("IOException: "+ex.getMessage()); 283 System.exit(1); 284 } 285 fdest = new File (trDir+File.separator+"classes"+File.separator+argv[i].substring(0, argv[i].length()-6)+implVersionToFileName(version)+".class"); 287 dir = fdest.getParentFile(); 288 if (dir != null) 289 if (! dir.exists()) 290 dir.mkdirs(); 291 try { 292 FileInputStream fis = new FileInputStream (fsource); 293 byte [] b = resolver.process(fis); 294 FileOutputStream fos = new FileOutputStream (fdest); 295 fos.write(b); 296 fis.close(); 297 fos.close(); 298 } catch (java.io.IOException ex) { 299 System.out.println("IOException: "+ex.getMessage()); 300 System.exit(1); 301 } 302 } 303 File fsource = new File (fileRoot+ File.separator+argv[0]); 305 if (!fsource.exists()) { 306 System.out.println("Error: file "+fsource.toString()+" does not exist."); 307 System.exit(1); 308 } 309 File fdest = new File (trDir+File.separator+"impl"+File.separator+sdir+"index.dc"); 310 try { 311 FileInputStream fis = new FileInputStream (fsource); 312 FileOutputStream fos = new FileOutputStream (fdest); 313 int a; 314 while ( (a = fis.read()) != -1) 315 fos.write(a); 316 fis.close(); 317 fos.close(); 318 } catch (java.io.IOException ex) { 319 System.out.println("IOException: "+ex.getMessage()); 320 System.exit(1); 321 } 322 implIndex.put(arch+"["+version+"]", "1"); 323 324 try { 326 FileOutputStream fos = new FileOutputStream (trDir+File.separator+"iface"+File.separator+"last"); 327 for (int i=0; i<lastIndex.length(); i++) { 328 fos.write((byte)lastIndex.charAt(i)); 329 } 330 fos.close(); 331 fos = new FileOutputStream (trDir+File.separator+"iface"+File.separator+"index"); 332 trIndex.store(fos, null); 333 fos.close(); 334 fos = new FileOutputStream (trDir+File.separator+"impl"+File.separator+"info"); 335 implIndex.store(fos, null); 336 fos.close(); 337 } catch (java.io.IOException ex) { 338 System.out.println("IOException: "+ex.getMessage()); 339 System.exit(1); 340 } 341 342 System.out.println("OK"); 343 } 344 345 350 static Element getDDElem(String fname) throws java.io.IOException , ParserConfigurationException , SAXException { 351 java.io.File file = new java.io.File (fname); 352 if (!file.exists()) { 353 throw new java.io.IOException ("File \""+fname+"\" doesn't exist."); 354 } 355 356 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 357 DocumentBuilder builder = factory.newDocumentBuilder(); 358 Document document = builder.parse(file); 359 Element element = document.getDocumentElement(); 360 element.normalize(); 361 return element; 362 } 363 364 static String getImplVersion(Element element) { 365 NodeList nl = element.getChildNodes(); 366 for (int i=0; i<nl.getLength(); i++) { 367 Node n = nl.item(i); 368 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("version")==0)) { 369 return n.getFirstChild().getNodeValue(); 370 } 371 } 372 return null; 373 } 374 375 static String getArchitectureFullName(Element element) { 376 NodeList nl = element.getChildNodes(); 377 for (int i=0; i<nl.getLength(); i++) { 378 Node n = nl.item(i); 379 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) { 380 String ar = n.getFirstChild().getNodeValue(); 381 ar = ar.substring(0,ar.indexOf('?')); 382 return ar; 383 } 384 } 385 return null; 386 } 387 388 static java.util.Hashtable getCDLEntities(Element element) { 389 NodeList nl = element.getChildNodes(); 390 for (int i=0; i<nl.getLength(); i++) { 391 Node n = nl.item(i); 392 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("cdl_entities")==0)) { 393 NodeList nl1 = n.getChildNodes(); 394 java.util.Hashtable ret = new java.util.Hashtable (); 395 for (int j=0; j<nl1.getLength(); j++) { 396 Node n1 = nl1.item(j); 397 if ((n1.getNodeType() == Node.ELEMENT_NODE) && (n1.getNodeName().compareTo("entity")==0)) { 398 ret.put(((Element )n1).getAttribute("javaname"), ((Element )n1).getAttribute("cdlname")); 399 } 400 } 401 return ret; 402 } 403 } 404 return null; 405 } 406 407 private static java.util.Hashtable getFullNamesWithoutVersion(java.util.Properties prop) { 408 java.util.Hashtable ret = new java.util.Hashtable (); 409 for (java.util.Enumeration e = prop.keys() ; e.hasMoreElements() ;) { 410 String k = (String ) e.nextElement(); 411 k = k.substring(0, k.indexOf('?')); 412 ret.put(k, ""); 413 } 414 return ret; 415 } 416 417 private static boolean compareFullNameWithoutVersion(String n1, String n2) { 418 n1 = n1.substring(0, n1.indexOf('?')); 419 n2 = n2.substring(0, n2.indexOf('?')); 420 if (n1.compareTo(n2) == 0) 421 return true; 422 return false; 423 } 424 425 427 static String classNameToFileName(String clname) { 428 return clname.replace('.', File.separatorChar) + ".class"; 429 } 430 431 433 static String fullNameToFileName(String clname) { 434 StringBuffer sb = new StringBuffer (); 435 boolean s = true; 436 for (int i=0; i<clname.length(); i++) { 437 if (clname.charAt(i)==':') { 438 if (s) { 439 sb.append(":"); 440 s = false; 441 } 442 } else { 443 s = true; 444 sb.append(clname.charAt(i)); 445 } 446 } 447 return sb.toString().replace(':', File.separatorChar); 450 } 451 452 private static String incrementLastIndex(String last) { 453 StringBuffer sb = new StringBuffer (last); 454 int now = last.length() - 1; 455 while (true) { 456 if (last.charAt(now) != 'z') { 457 sb.setCharAt(now, (char) ((char)last.charAt(now)+(char)1)); 458 return sb.toString(); 459 } 460 sb.setCharAt(now, 'a'); 461 now--; 462 if (now < 0) { 463 sb.append('a'); 464 return sb.toString(); 465 } 466 } 467 } 468 469 static String specVersionToFileName(String specVersion) { 470 StringBuffer ret = new StringBuffer ("_Q_"); 471 for (int i=0; i<specVersion.length(); i++) { 472 switch (specVersion.charAt(i)) { 473 case '.': 474 ret.append('_'); 475 break; 476 case '!': 477 ret.append("_E_"); 478 break; 479 default: 480 ret.append(specVersion.charAt(i)); 481 } 482 } 483 return ret.toString(); 484 } 485 486 static String toVMName(String name) { 487 StringBuffer ret = new StringBuffer (); 488 for (int i=0; i<name.length(); i++) { 489 if (name.charAt(i) == '.') 490 ret.append('/'); 491 else 492 ret.append(name.charAt(i)); 493 } 494 return ret.toString(); 495 } 496 497 static String implVersionToFileName(String implVersion) { 498 StringBuffer ret = new StringBuffer ("_V_"); 499 for (int i=0; i<implVersion.length(); i++) { 500 switch (implVersion.charAt(i)) { 501 case '.': 502 ret.append('_'); 503 break; 504 case '!': 505 ret.append("_E_"); 506 break; 507 default: 508 ret.append(implVersion.charAt(i)); 509 } 510 } 511 return ret.toString(); 512 } 513 } 514 | Popular Tags |