| 1 23 package org.objectweb.joram.client.connector.utils; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.FileReader ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.LineNumberReader ; 32 import java.io.ByteArrayInputStream ; 33 import java.io.BufferedReader ; 34 import java.util.Enumeration ; 35 import java.util.Hashtable ; 36 import java.util.Map ; 37 import java.util.StringTokenizer ; 38 import java.util.zip.ZipEntry ; 39 import java.util.zip.ZipFile ; 40 import java.util.zip.ZipOutputStream ; 41 import java.util.zip.ZipInputStream ; 42 import java.util.jar.JarFile ; 43 44 import org.objectweb.joram.client.connector.AdapterTracing; 45 import fr.dyade.aaa.agent.conf.A3CML; 46 import fr.dyade.aaa.agent.conf.A3CMLConfig; 47 import fr.dyade.aaa.agent.conf.A3CMLServer; 48 import fr.dyade.aaa.agent.conf.A3CMLService; 49 50 53 public class RAConfig { 54 55 private static final String RA_XML = "META-INF/ra.xml"; 56 private static final String JORAM_CONFIG_JAR = "joram-config.jar"; 57 private static final String A3SERVERS_XML = "a3servers.xml"; 58 private static final String A3DEBUG_CFG = "a3debug.cfg"; 59 private static final String RA_PROPERTIES = "ra.properties"; 60 private static final String JORAMADMIN_CFG = "joram-admin.cfg"; 61 private static final String JORAMADMIN_XML = "joramAdmin.xml"; 62 private static final int BUFFER_SIZE = 2048; 63 private static boolean debug = false; 64 private static boolean verbose = false; 65 private static String confDir = null; 66 private static String tmpDir = null; 67 68 69 private RAConfig() { 70 } 71 72 public static void main(String [] args) throws Exception { 73 RAConfig raconfig = new RAConfig(); 74 75 debug = new Boolean (System.getProperty("debug","false")).booleanValue(); 76 77 String rarName = null; 78 String jarName = null; 79 String raProperties = null; 80 String extractFile = null; 81 String hostName = null; 82 String port = null; 83 String serverId = null; 84 String path = null; 85 String newFileName = null; 86 String oldFileName = null; 87 88 int command = -1; 89 90 try { 91 int i = 0; 92 while (i < args.length) { 93 if (args[i].equals("-rar")) { 94 rarName = args[i+1]; 95 i = i + 2; 96 } else if (args[i].equals("-jar")) { 97 jarName = args[i+1]; 98 i = i + 2; 99 } else if (args[i].equals("-c")) { 100 command = 1; 101 i++; 102 } else if (args[i].equals("-u")) { 103 command = 2; 104 raProperties = args[i+1]; 105 i = i + 2; 106 } else if (args[i].equals("-x")) { 107 command = 3; 108 extractFile = args[i+1]; 109 i = i + 2; 110 } else if (args[i].equals("-uhp")) { 111 command = 4; 112 hostName = args[i+1]; 113 port = args[i+2]; 114 serverId = args[i+3]; 115 i = i + 3; 116 } else if (args[i].equals("-ua3")) { 117 command = 5; 118 hostName = args[i+1]; 119 port = args[i+2]; 120 serverId = args[i+3]; 121 i = i + 3; 122 } else if (args[i].equals("-uz")) { 123 command = 6; 124 path = args[i+1]; 125 newFileName = args[i+2]; 126 oldFileName = args[i+3]; 127 i = i + 3; 128 } else if (args[i].equals("-conf")) { 129 confDir = args[i+1]; 130 i = i + 2; 131 } else if (args[i].equals("-v")) { 132 verbose = true; 133 i = i + 1; 134 } else 135 i++; 136 } 137 } catch (Exception e) { 138 usage(); 139 e.printStackTrace(); 140 System.exit(1); 141 } 142 143 tmpDir = System.getProperty("java.io.tmpdir") + "/"; 145 146 switch (command) { 147 case 1: if (rarName == null) 149 usage(); 150 raconfig.createRaProperties(rarName); 151 break; 152 case 2: if (raProperties == null) 154 usage(); 155 raconfig.updateRAR(raProperties); 156 break; 157 case 3: if (extractFile != null) { 159 if (rarName != null) { 160 raconfig.extractFromRAR(rarName,extractFile); 161 } else if (jarName != null) { 162 raconfig.extractFromJAR(jarName,extractFile); 163 } else 164 usage(); 165 } else 166 usage(); 167 break; 168 case 4: if (rarName == null 170 || hostName == null 171 || port == null 172 || serverId == null) 173 usage(); 174 raconfig.updateHostPort(rarName,hostName,port,new Short (serverId).shortValue()); 175 break; 176 case 5: if (rarName == null 178 || hostName == null 179 || port == null 180 || serverId == null) 181 usage(); 182 raconfig.updateA3Servers(rarName,hostName,port,new Short (serverId).shortValue()); 183 break; 184 case 6: if ( path != null 186 && newFileName != null) { 187 if (rarName != null) 188 raconfig.updateZIP(rarName,path,newFileName,oldFileName); 189 if (jarName != null) 190 raconfig.updateZIP(jarName,path,newFileName,oldFileName); 191 } else 192 usage(); 193 break; 194 default: 195 usage(); 196 break; 197 } 198 } 199 200 203 public static void usage() { 204 StringBuffer buff = new StringBuffer (); 205 206 buff.append("\n\n"); 207 buff.append("\nConfigure your Joram RAR:"); 208 buff.append("\n -resource adapter deployment descriptor (ra.xml)"); 209 buff.append("\n -joram server configuration file (a3servers.xml)"); 210 buff.append("\n\n"); 211 buff.append("\nSimple: create ra.properties, modify ra.properties and update rar."); 212 buff.append("\n create ra.properties : java RAconfig -rar rarName -c"); 213 buff.append("\n update RAR : java RAconfig -u ra.properties"); 214 buff.append("\n"); 215 buff.append("\nChange host and port value in ra.xml and a3servers.xml."); 216 buff.append("\n update RAR (host/port) : java RAconfig -rar rarName -uhp host port serverId"); 217 buff.append("\nChange host and port value only in a3servers.xml"); 218 buff.append("\n update A3servers (host/port) : java RAconfig -rar rarName -ua3 host port serverId"); 219 buff.append("\n\n"); 220 buff.append("\nExpert: extract ra.xml and a3servers.xml (joram-config.jar), modify and update jar/rar."); 221 buff.append("\n extract file from RAR : java RAconfig -rar rarName -x fileName"); 222 buff.append("\n update RAR : java RAconfig -rar rarName -uz path newFileName oldFileName"); 223 buff.append("\n"); 224 buff.append("\n extract file from JAR : java RAconfig -jar jarName -x fileName"); 225 buff.append("\n update JAR : java RAconfig -jar jarName -uz path newFileName oldFileName"); 226 buff.append("\n\n"); 227 buff.append("\nVerbose : -v"); 228 buff.append("\n\n"); 229 buff.append("\nexample :"); 230 buff.append("\n java RAconfig -u ra.properties"); 231 buff.append("\n java RAconfig -rar joram.rar -uhp localhost 16010 0"); 232 buff.append("\n java RAconfig -rar joram.rar -uz META-INF/ra.xml ra.xml META-INF/ra.xml"); 233 buff.append("\n\n\n"); 234 235 System.out.println(buff.toString()); 236 } 237 238 239 245 private void createRaProperties(String rarName) 246 throws Exception { 247 248 if (debug) 249 System.out.println("RAConfig.createRaProperties(" + rarName + ")"); 250 else if (verbose) 251 System.out.println("create ra.properties " + rarName); 252 253 File file = new File (rarName); 254 if (file.exists()) { 255 ZipFile zipFile = new ZipFile (file.getAbsolutePath()); 256 for (Enumeration zippedFiles = zipFile.entries(); zippedFiles.hasMoreElements(); ) { 257 ZipEntry currEntry = (ZipEntry ) zippedFiles.nextElement(); 259 if (debug) 260 System.out.println("RAConfig.createRaProperties : currEntry = " + currEntry); 261 if (currEntry.getName().equalsIgnoreCase(RA_XML)) { 262 InputStream reader = zipFile.getInputStream(currEntry); 263 StringBuffer buff = new StringBuffer (); 264 buff.append("RAR_NAME "); 265 buff.append(file.getAbsolutePath()); 266 buff.append("\n"); 267 buff.append(parse(reader)); 269 createFile(RA_PROPERTIES,buff.toString()); 271 break; 272 } 273 } 274 zipFile.close(); 275 } 276 } 277 278 279 285 private void extractFromRAR(String rarName, String fileName) 286 throws Exception { 287 288 if (debug) 289 System.out.println("RAConfig.extractFromRAR(" + rarName + "," + fileName + ")"); 290 else if (verbose) 291 System.out.println("extract \"" + fileName + "\" from \"" + rarName + "\""); 292 293 InputStream res = null; 294 File file = new File (rarName); 295 if (file.exists()) { 296 ZipFile zipFile = new ZipFile (file.getAbsolutePath()); 297 for (Enumeration zippedFiles = zipFile.entries(); zippedFiles.hasMoreElements(); ) { 298 ZipEntry currEntry = (ZipEntry ) zippedFiles.nextElement(); 300 if (debug) 301 System.out.println("RAConfig.extractFromRAR : currEntry = " + currEntry); 302 if (currEntry.getName().equalsIgnoreCase(fileName) 303 || currEntry.getName().equalsIgnoreCase("META-INF/"+fileName)) { 304 res = zipFile.getInputStream(currEntry); 306 break; 307 } else if (currEntry.getName().endsWith(".jar")) { 308 InputStream reader = zipFile.getInputStream(currEntry); 310 res = extractFromJAR(fileName,reader); 311 if (res == null) 312 continue; 313 else { 314 reader.close(); 316 break; 317 } 318 } 319 } 320 if (res != null) 323 createFile(fileName,res); 324 zipFile.close(); 325 } 326 } 327 328 334 private InputStream extractFromJAR(String fileName, InputStream reader) 335 throws Exception { 336 337 if (debug) 338 System.out.println("RAConfig.extractFromJAR(" + fileName + "," + reader + ")"); 339 340 ZipInputStream stream = new ZipInputStream (reader); 341 ZipEntry currEntry = (ZipEntry ) stream.getNextEntry(); 342 while (stream.available() > 0) { 343 if (currEntry == null) break; 344 if (currEntry.getName().equalsIgnoreCase(fileName)) { 345 if (debug) 347 System.out.println("RAConfig.extractFromJAR : currEntry = " + currEntry); 348 else if (verbose) 349 System.out.println("extract \"" + fileName + "\" from JAR."); 350 351 return stream; 352 } 353 currEntry = (ZipEntry ) stream.getNextEntry(); 354 } 355 stream.close(); 357 return null; 358 } 359 360 366 private void extractFromJAR(String jarName, String fileName) 367 throws Exception { 368 369 if (debug) 370 System.out.println("RAConfig.extractFromJAR(" + jarName + "," + fileName + ")"); 371 else if (verbose) 372 System.out.println("extract \"" + fileName + "\" from \"" + jarName + "\""); 373 374 JarFile jar = new JarFile (jarName); 375 ZipEntry entry = jar.getEntry(fileName); 376 if (debug) 377 System.out.println("RAConfig.extractFromJAR : entry = " + entry); 378 if (entry != null) 380 createFile(fileName,jar.getInputStream(entry)); 381 jar.close(); 382 } 383 384 385 391 private void dump(InputStream is, OutputStream os) throws Exception { 392 int n = 0; 393 byte[] buffer = new byte[BUFFER_SIZE]; 394 n = is.read(buffer); 395 while (n > 0) { 396 os.write(buffer, 0, n); 397 n = is.read(buffer); 398 } 399 } 400 401 private String getFileName(String path) throws Exception { 402 int i = path.lastIndexOf("/"); 403 if (i > 0) 404 return path.substring(i+1,path.length()); 405 i = path.lastIndexOf("\\"); 406 if (i > 0) 407 return path.substring(i+1,path.length()); 408 return path; 409 } 410 411 418 private void createFile(String path, InputStream is) throws Exception { 419 if (debug) 420 System.out.println("RAConfig.createFile(" + path + "," + is + ")"); 421 422 String fileName = tmpDir + getFileName(path); 423 if (debug) 424 System.out.println("RAConfig.createFile : fileName = " + fileName); 425 else if (verbose) 426 System.out.println("create file \"" + fileName + "\""); 427 428 new File (fileName).delete(); 429 FileOutputStream fos = new FileOutputStream (fileName); 430 try { 431 dump(is,fos); 432 } finally { 433 fos.close(); 434 } 435 } 436 437 444 private void createFile(String path, String input) throws Exception { 445 if (debug) 446 System.out.println("RAConfig.createFile(" + path + "," + input + ")"); 447 String fileName = tmpDir + getFileName(path); 448 if (debug) 449 System.out.println("RAConfig.createFile : fileName = " + fileName); 450 else if (verbose) 451 System.out.println("create file \"" + fileName + "\""); 452 453 new File (fileName).delete(); 454 ByteArrayInputStream bis = new ByteArrayInputStream (input.getBytes()); 455 FileOutputStream fos = new FileOutputStream (fileName); 456 try { 457 dump(bis,fos); 458 } finally { 459 fos.close(); 460 bis.close(); 461 } 462 } 463 464 469 private String parse(String fileName) throws Exception { 470 if (debug) 471 System.out.println("RAConfig.parse(" + fileName + ")"); 472 else if (verbose) 473 System.out.println("parse \"" + fileName + "\""); 474 475 Wrapper wrapper = new Wrapper(debug); 476 FileInputStream fis = new FileInputStream (fileName); 477 return wrapper.parse(fis); 478 } 479 480 485 private String parse(InputStream is) throws Exception { 486 if (debug) 487 System.out.println("RAConfig.parse(" + is + ")"); 488 489 Wrapper wrapper = new Wrapper(debug); 490 return wrapper.parse(is); 491 } 492 493 499 private String update(InputStream is, Map map) throws Exception { 500 if (debug) 501 System.out.println("RAConfig.update(" + is + "," + map + ")"); 502 503 Wrapper wrapper = new Wrapper(debug); 504 return wrapper.update(is,map); 505 } 506 507 514 private void updateHostPort(String rarName, 515 String hostName, 516 String port, 517 short serverId) throws Exception { 518 if (debug) 519 System.out.println("RAConfig.updateHostPort(" + rarName + 520 "," + hostName + 521 "," + port + 522 "," + serverId + ")"); 523 else if (verbose) 524 System.out.println("update (ra.xml and a3server.xml) in \"" + rarName + 525 "\" with host=" + hostName + 526 " port=" + port + 527 " serverId=" + serverId); 528 529 File file = new File (rarName); 531 StringBuffer buff = new StringBuffer (); 532 buff.append("RAR_NAME "); 533 buff.append(file.getAbsolutePath()); 534 buff.append("\n[org.objectweb.joram.client.connector.JoramAdapter]"); 535 buff.append("\nHostName "); 536 buff.append(hostName); 537 buff.append("\nServerPort "); 538 buff.append(port); 539 buff.append("\nServerId "); 540 buff.append(serverId); 541 String tempFile = "ra.properties_tmp"; 543 createFile(tempFile,buff.toString()); 544 updateRAR(tmpDir + tempFile); 545 new File (tmpDir + tempFile).delete(); 546 } 547 548 555 private void updateA3Servers(String rarName, 556 String hostName, 557 String port, 558 short serverId) 559 throws Exception { 560 if (debug) 561 System.out.println("RAConfig.updateA3Servers(" + rarName + 562 "," + hostName + 563 "," + port + 564 "," + serverId + ")"); 565 else if (verbose) 566 System.out.println("update (a3server.xml) in \"" + rarName + 567 "\" host=" + hostName + 568 " port=" + port + 569 " serverId=" + serverId); 570 571 extractFromRAR(rarName,JORAM_CONFIG_JAR); 573 extractFromJAR(tmpDir + JORAM_CONFIG_JAR, A3SERVERS_XML); 575 576 if (confDir != null) { 579 File f = new File (confDir, A3SERVERS_XML); 580 if (f.exists()) { 581 copy(f.getPath(), tmpDir + A3SERVERS_XML); 582 } 583 } 584 585 A3CMLConfig conf = A3CML.getXMLConfig(tmpDir + A3SERVERS_XML); 587 A3CMLServer server = conf.getServer(serverId); 588 server.hostname = hostName; 589 A3CMLService service = 590 server.getService("org.objectweb.joram.mom.proxies.tcp.TcpProxyService"); 591 service.args = port; 592 A3CML.toXML(conf,null, tmpDir + A3SERVERS_XML); 594 595 if (debug) 596 System.out.println("RAConfig.updateA3Servers : confDir=" + confDir); 597 if (confDir != null) { 599 File f = new File (confDir, A3SERVERS_XML); 600 if (f.exists()) 601 copy(tmpDir + A3SERVERS_XML, f.getPath()); 602 if (new File (confDir, JORAMADMIN_CFG).exists()) 603 updateJoramAdminCfg(hostName, port); 604 if (new File (confDir, JORAMADMIN_XML).exists()) 605 updateJoramAdminXml(hostName, port); 606 } 607 608 updateZIP(tmpDir + JORAM_CONFIG_JAR,A3SERVERS_XML,tmpDir + A3SERVERS_XML,A3SERVERS_XML); 610 updateZIP(rarName,JORAM_CONFIG_JAR,tmpDir +JORAM_CONFIG_JAR,JORAM_CONFIG_JAR); 612 613 new File (tmpDir + JORAM_CONFIG_JAR).delete(); 615 new File (tmpDir + A3SERVERS_XML).delete(); 616 } 617 618 private boolean copy(String file1, String file2) 619 throws Exception { 620 if (! new File (file1).exists()) 621 return false; 622 623 new File (file2).delete(); 624 625 FileInputStream fis = new FileInputStream (file1); 626 FileOutputStream fos = new FileOutputStream (file2); 627 try { 628 dump(fis,fos); 629 return true; 630 } finally { 631 fos.close(); 632 fis.close(); 633 } 634 } 635 636 private void updateJoramAdminCfg(String hostName, 637 String port) 638 throws Exception { 639 File file = new File (confDir, JORAMADMIN_CFG); 640 FileReader fileReader = new FileReader (file); 641 BufferedReader reader = new BufferedReader (fileReader); 642 boolean end = false; 643 String line; 644 StringTokenizer tokenizer; 645 String firstToken; 646 StringBuffer buff = new StringBuffer (); 647 648 while (! end) { 649 line = reader.readLine(); 650 if (line == null) 651 end = true; 652 else { 653 tokenizer = new StringTokenizer (line); 654 if (tokenizer.hasMoreTokens()) { 655 firstToken = tokenizer.nextToken(); 656 if (firstToken.equalsIgnoreCase("Host")) { 657 buff.append("Host " + hostName + "\n"); 658 continue; 659 } 660 else if (firstToken.equalsIgnoreCase("Port")) { 661 buff.append("Port " + port + "\n"); 662 continue; 663 } 664 } 665 buff.append(line + "\n"); 666 } 667 } 668 file.delete(); 669 ByteArrayInputStream bis = new ByteArrayInputStream (buff.toString().getBytes()); 670 FileOutputStream fos = new FileOutputStream (file); 671 try { 672 dump(bis,fos); 673 } finally { 674 fos.close(); 675 bis.close(); 676 } 677 } 678 679 private void updateJoramAdminXml(String hostName, 680 String port) 681 throws Exception { 682 File file = new File (confDir, JORAMADMIN_XML); 683 FileReader fileReader = new FileReader (file); 684 BufferedReader reader = new BufferedReader (fileReader); 685 boolean end = false; 686 String line; 687 StringTokenizer tokenizer; 688 String firstToken; 689 StringBuffer buff = new StringBuffer (); 690 int i = -1; 691 692 while (! end) { 693 line = reader.readLine(); 694 if (line == null) 695 end = true; 696 else { 697 if (line.trim().startsWith("<connect")) { 698 while (true) { 699 i = line.indexOf("host"); 700 if (i > 0) { 701 buff.append(line.substring(0,i+10)); 702 buff.append(hostName); 703 int j = line.indexOf("\"",i+11); 704 buff.append(line.substring(j,line.length()) + "\n"); 705 if (line.trim().endsWith("/>")) { 706 line = reader.readLine(); 707 break; 708 } 709 line = reader.readLine(); 710 continue; 711 } 712 i = line.indexOf("port"); 713 if (i > 0) { 714 buff.append(line.substring(0,i+6)); 715 buff.append(port); 716 int j = line.indexOf("\"",i+7); 717 buff.append(line.substring(j,line.length()) + "\n"); 718 if (line.trim().endsWith("/>")) { 719 line = reader.readLine(); 720 break; 721 } 722 line = reader.readLine(); 723 continue; 724 } 725 buff.append(line + "\n"); 726 if (line.trim().endsWith("/>")) { 727 line = reader.readLine(); 728 break; 729 } 730 line = reader.readLine(); 731 } 732 } else if (line.trim().startsWith("<tcp")) { 733 while (true) { 734 i = line.indexOf("host"); 735 if (i > 0) { 736 buff.append(line.substring(0,i+6)); 737 buff.append(hostName); 738 int j = line.indexOf("\"",i+7); 739 buff.append(line.substring(j,line.length()) + "\n"); 740 if (line.trim().endsWith("/>")) { 741 &nb
|