KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > connector > utils > RAConfig


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2005 - Bull SA
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package org.objectweb.joram.client.connector.utils;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.LineNumberReader JavaDoc;
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.BufferedReader JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.zip.ZipEntry JavaDoc;
39 import java.util.zip.ZipFile JavaDoc;
40 import java.util.zip.ZipOutputStream JavaDoc;
41 import java.util.zip.ZipInputStream JavaDoc;
42 import java.util.jar.JarFile JavaDoc;
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 /**
51  *
52  */

53 public class RAConfig {
54
55   private static final String JavaDoc RA_XML = "META-INF/ra.xml";
56   private static final String JavaDoc JORAM_CONFIG_JAR = "joram-config.jar";
57   private static final String JavaDoc A3SERVERS_XML = "a3servers.xml";
58   private static final String JavaDoc A3DEBUG_CFG = "a3debug.cfg";
59   private static final String JavaDoc RA_PROPERTIES = "ra.properties";
60   private static final String JavaDoc JORAMADMIN_CFG = "joram-admin.cfg";
61   private static final String JavaDoc 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 JavaDoc confDir = null;
66   private static String JavaDoc tmpDir = null;
67
68
69   private RAConfig() {
70   }
71
72   public static void main(String JavaDoc [] args) throws Exception JavaDoc {
73     RAConfig raconfig = new RAConfig();
74
75     debug = new Boolean JavaDoc(System.getProperty("debug","false")).booleanValue();
76
77     String JavaDoc rarName = null;
78     String JavaDoc jarName = null;
79     String JavaDoc raProperties = null;
80     String JavaDoc extractFile = null;
81     String JavaDoc hostName = null;
82     String JavaDoc port = null;
83     String JavaDoc serverId = null;
84     String JavaDoc path = null;
85     String JavaDoc newFileName = null;
86     String JavaDoc 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 JavaDoc e) {
138       usage();
139       e.printStackTrace();
140       System.exit(1);
141     }
142
143     // Get the temp directory
144
tmpDir = System.getProperty("java.io.tmpdir") + "/";
145
146     switch (command) {
147     case 1: // createRaProperties
148
if (rarName == null)
149         usage();
150       raconfig.createRaProperties(rarName);
151       break;
152     case 2: // updateRAR
153
if (raProperties == null)
154         usage();
155       raconfig.updateRAR(raProperties);
156       break;
157     case 3: // extractFile
158
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: // updateHostPort
169
if (rarName == null
170           || hostName == null
171           || port == null
172           || serverId == null)
173         usage();
174       raconfig.updateHostPort(rarName,hostName,port,new Short JavaDoc(serverId).shortValue());
175       break;
176     case 5: // updateA3Servers
177
if (rarName == null
178           || hostName == null
179           || port == null
180           || serverId == null)
181         usage();
182       raconfig.updateA3Servers(rarName,hostName,port,new Short JavaDoc(serverId).shortValue());
183       break;
184     case 6: // updateZIP
185
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   /**
201    * Usage of RAConfig.
202    */

203   public static void usage() {
204     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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   /**
240    * create ra.properties
241    * build from the ra.xml file from RAR.
242    * @param rarName String input RAR file name
243    * @throws Exception to throw if an Exception occurs
244    */

245   private void createRaProperties(String JavaDoc rarName)
246     throws Exception JavaDoc {
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 JavaDoc file = new File JavaDoc(rarName);
254     if (file.exists()) {
255       ZipFile JavaDoc zipFile = new ZipFile JavaDoc(file.getAbsolutePath());
256       for (Enumeration JavaDoc zippedFiles = zipFile.entries(); zippedFiles.hasMoreElements(); ) {
257         //Retrieve entry of existing files
258
ZipEntry JavaDoc currEntry = (ZipEntry JavaDoc) zippedFiles.nextElement();
259         if (debug)
260           System.out.println("RAConfig.createRaProperties : currEntry = " + currEntry);
261         if (currEntry.getName().equalsIgnoreCase(RA_XML)) {
262           InputStream JavaDoc reader = zipFile.getInputStream(currEntry);
263           StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
264           buff.append("RAR_NAME ");
265           buff.append(file.getAbsolutePath());
266           buff.append("\n");
267           //parse ra.xml file
268
buff.append(parse(reader));
269           // create ra.properties file
270
createFile(RA_PROPERTIES,buff.toString());
271           break;
272         }
273       }
274       zipFile.close();
275     }
276   }
277
278
279   /**
280    * Extract the fileName from the RAR file.
281    * @param rarName RAR file name
282    * @param fileName file name
283    * @throws Exception to throw if an Exception occurs
284    */

285   private void extractFromRAR(String JavaDoc rarName, String JavaDoc fileName)
286     throws Exception JavaDoc {
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 JavaDoc res = null;
294     File JavaDoc file = new File JavaDoc(rarName);
295     if (file.exists()) {
296       ZipFile JavaDoc zipFile = new ZipFile JavaDoc(file.getAbsolutePath());
297       for (Enumeration JavaDoc zippedFiles = zipFile.entries(); zippedFiles.hasMoreElements(); ) {
298         //Retrieve entry of existing files
299
ZipEntry JavaDoc currEntry = (ZipEntry JavaDoc) 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           // the fileName is found.
305
res = zipFile.getInputStream(currEntry);
306           break;
307         } else if (currEntry.getName().endsWith(".jar")) {
308           // search fileName in jar file.
309
InputStream JavaDoc reader = zipFile.getInputStream(currEntry);
310           res = extractFromJAR(fileName,reader);
311           if (res == null)
312             continue;
313           else {
314             // the fileName found in jar file.
315
reader.close();
316             break;
317           }
318         }
319       }
320       // extract the fileName from InputStream
321
// in the tmp directory.
322
if (res != null)
323         createFile(fileName,res);
324       zipFile.close();
325     }
326   }
327
328   /**
329    * Extract fileName from the JAR file.
330    * @param fileName file name
331    * @param reader Input stream
332    * @throws Exception to throw if an Exception occurs
333    */

334   private InputStream JavaDoc extractFromJAR(String JavaDoc fileName, InputStream JavaDoc reader)
335     throws Exception JavaDoc {
336
337     if (debug)
338       System.out.println("RAConfig.extractFromJAR(" + fileName + "," + reader + ")");
339
340     ZipInputStream JavaDoc stream = new ZipInputStream JavaDoc(reader);
341     ZipEntry JavaDoc currEntry = (ZipEntry JavaDoc) stream.getNextEntry();
342     while (stream.available() > 0) {
343       if (currEntry == null) break;
344       if (currEntry.getName().equalsIgnoreCase(fileName)) {
345         // the fileName is found, return the InputStream.
346
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 JavaDoc) stream.getNextEntry();
354     }
355     // close the stream and return null if file not found.
356
stream.close();
357     return null;
358   }
359
360   /**
361    * Extract config files from the JAR file.
362    * @param jarName JAR file name
363    * @param fileName file to be extract
364    * @throws Exception to throw if an Exception occurs
365    */

366   private void extractFromJAR(String JavaDoc jarName, String JavaDoc fileName)
367     throws Exception JavaDoc {
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 JavaDoc jar = new JarFile JavaDoc(jarName);
375     ZipEntry JavaDoc entry = jar.getEntry(fileName);
376     if (debug)
377       System.out.println("RAConfig.extractFromJAR : entry = " + entry);
378     // extract the fileName from jar in the tmp directory.
379
if (entry != null)
380       createFile(fileName,jar.getInputStream(entry));
381     jar.close();
382   }
383
384
385   /**
386    * write the inputstream in outputstream.
387    * @param is input stream
388    * @param os output stream
389    * @throws Exception to throw if an Exception occurs
390    */

391   private void dump(InputStream JavaDoc is, OutputStream JavaDoc os) throws Exception JavaDoc {
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 JavaDoc getFileName(String JavaDoc path) throws Exception JavaDoc {
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   /**
412    * create the filename in the tmp directory
413    * by writing the input stream in the file name.
414    * @param path new file
415    * @param is input stream
416    * @throws Exception to throw if an Exception occurs
417    */

418   private void createFile(String JavaDoc path, InputStream JavaDoc is) throws Exception JavaDoc {
419     if (debug)
420       System.out.println("RAConfig.createFile(" + path + "," + is + ")");
421
422     String JavaDoc 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 JavaDoc(fileName).delete();
429     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fileName);
430     try {
431       dump(is,fos);
432     } finally {
433       fos.close();
434     }
435   }
436
437   /**
438    * create the filename in the tmp directory,
439    * by writing the input in the file name.
440    * @param path new file
441    * @param input string to write
442    * @throws Exception to throw if an Exception occurs
443    */

444   private void createFile(String JavaDoc path, String JavaDoc input) throws Exception JavaDoc {
445     if (debug)
446       System.out.println("RAConfig.createFile(" + path + "," + input + ")");
447     String JavaDoc 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 JavaDoc(fileName).delete();
454     ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(input.getBytes());
455     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fileName);
456     try {
457       dump(bis,fos);
458     } finally {
459       fos.close();
460       bis.close();
461     }
462   }
463
464   /**
465    * parse file name.
466    * @param fileName file
467    * @throws Exception to throw if an Exception occurs
468    */

469   private String JavaDoc parse(String JavaDoc fileName) throws Exception JavaDoc {
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 JavaDoc fis = new FileInputStream JavaDoc(fileName);
477     return wrapper.parse(fis);
478   }
479
480   /**
481    * parse input stream.
482    * @param is input stream
483    * @throws Exception to throw if an Exception occurs
484    */

485   private String JavaDoc parse(InputStream JavaDoc is) throws Exception JavaDoc {
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   /**
494    * update input stream with map value.
495    * @param is input stream
496    * @param map map value (see ra.properties)
497    * @throws Exception to throw if an Exception occurs
498    */

499   private String JavaDoc update(InputStream JavaDoc is, Map JavaDoc map) throws Exception JavaDoc {
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   /**
508    * update host/port in ra.xml and a3server.xml in RAR.
509    * @param rarName rar file name
510    * @param hostName new host name
511    * @param port new port
512    * @param serverId server Id
513    */

514   private void updateHostPort(String JavaDoc rarName,
515                               String JavaDoc hostName,
516                               String JavaDoc port,
517                               short serverId) throws Exception JavaDoc {
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     // update ra.xml file
530
File JavaDoc file = new File JavaDoc(rarName);
531     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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     // create temporary raProperty file
542
String JavaDoc tempFile = "ra.properties_tmp";
543     createFile(tempFile,buff.toString());
544     updateRAR(tmpDir + tempFile);
545     new File JavaDoc(tmpDir + tempFile).delete();
546   }
547
548   /**
549    * update A3SERVERS_XML file
550    * @param rarName rar file name
551    * @param hostName new host name
552    * @param port new port
553    * @param serverId server Id
554    */

555   private void updateA3Servers(String JavaDoc rarName,
556                                String JavaDoc hostName,
557                                String JavaDoc port,
558                                short serverId)
559     throws Exception JavaDoc {
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     // extract the joram-config.jar file from RAR in the temp dir
572
extractFromRAR(rarName,JORAM_CONFIG_JAR);
573     // extract the a3servers.xml file from joram-config.jar in the tmp dir
574
extractFromJAR(tmpDir + JORAM_CONFIG_JAR, A3SERVERS_XML);
575
576     // if present the a3server.xml source is the confdir one
577
// otherwise, we take the RAR one
578
if (confDir != null) {
579         File JavaDoc f = new File JavaDoc(confDir, A3SERVERS_XML);
580         if (f.exists()) {
581             copy(f.getPath(), tmpDir + A3SERVERS_XML);
582         }
583     }
584
585     // update A3SERVERS_XML
586
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     // write changes to A3SERVERS_XML file
593
A3CML.toXML(conf,null, tmpDir + A3SERVERS_XML);
594
595     if (debug)
596       System.out.println("RAConfig.updateA3Servers : confDir=" + confDir);
597     // update file in conf dir.
598
if (confDir != null) {
599       File JavaDoc f = new File JavaDoc(confDir, A3SERVERS_XML);
600       if (f.exists())
601         copy(tmpDir + A3SERVERS_XML, f.getPath());
602       if (new File JavaDoc(confDir, JORAMADMIN_CFG).exists())
603         updateJoramAdminCfg(hostName, port);
604       if (new File JavaDoc(confDir, JORAMADMIN_XML).exists())
605         updateJoramAdminXml(hostName, port);
606     }
607
608     // update jar
609
updateZIP(tmpDir + JORAM_CONFIG_JAR,A3SERVERS_XML,tmpDir + A3SERVERS_XML,A3SERVERS_XML);
610     // update rar
611
updateZIP(rarName,JORAM_CONFIG_JAR,tmpDir +JORAM_CONFIG_JAR,JORAM_CONFIG_JAR);
612
613     // remove temporary file
614
new File JavaDoc(tmpDir + JORAM_CONFIG_JAR).delete();
615     new File JavaDoc(tmpDir + A3SERVERS_XML).delete();
616   }
617
618   private boolean copy(String JavaDoc file1, String JavaDoc file2)
619     throws Exception JavaDoc {
620     if (! new File JavaDoc(file1).exists())
621       return false;
622
623     new File JavaDoc(file2).delete();
624
625     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file1);
626     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(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 JavaDoc hostName,
637                                    String JavaDoc port)
638     throws Exception JavaDoc {
639     File JavaDoc file = new File JavaDoc(confDir, JORAMADMIN_CFG);
640     FileReader JavaDoc fileReader = new FileReader JavaDoc(file);
641     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(fileReader);
642     boolean end = false;
643     String JavaDoc line;
644     StringTokenizer JavaDoc tokenizer;
645     String JavaDoc firstToken;
646     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
647
648     while (! end) {
649       line = reader.readLine();
650       if (line == null)
651         end = true;
652       else {
653         tokenizer = new StringTokenizer JavaDoc(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 JavaDoc bis = new ByteArrayInputStream JavaDoc(buff.toString().getBytes());
670     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
671     try {
672       dump(bis,fos);
673     } finally {
674       fos.close();
675       bis.close();
676     }
677   }
678
679   private void updateJoramAdminXml(String JavaDoc hostName,
680                                    String JavaDoc port)
681     throws Exception JavaDoc {
682     File JavaDoc file = new File JavaDoc(confDir, JORAMADMIN_XML);
683     FileReader JavaDoc fileReader = new FileReader JavaDoc(file);
684     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(fileReader);
685     boolean end = false;
686     String JavaDoc line;
687     StringTokenizer JavaDoc tokenizer;
688     String JavaDoc firstToken;
689     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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                 line = reader.readLine();
742                 break;
743               }
744               line = reader.readLine();
745               continue;
746             }
747             i = line.indexOf("port");
748             if (i > 0) {
749               buff.append(line.substring(0,i+6));
750               buff.append(port);
751               int j = line.indexOf("\"",i+7);
752               buff.append(line.substring(j,line.length()) + "\n");
753               if (line.trim().endsWith("/>")) {
754                 line = reader.readLine();
755                 break;
756               }
757               line = reader.readLine();
758               continue;
759             }
760             buff.append(line + "\n");
761             if (line.trim().endsWith("/>"))
762               break;
763             line = reader.readLine();
764           }
765         }
766         buff.append(line + "\n");
767       }
768     }
769     file.delete();
770     ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(buff.toString().getBytes());
771     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
772     try {
773       dump(bis,fos);
774     } finally {
775       fos.close();
776       bis.close();
777     }
778   }
779
780   /**
781    * update RA_XML file
782    * @param raProperties ra.properties file
783    */

784   private void updateRAR(String JavaDoc raProperties) throws Exception JavaDoc {
785     if (debug)
786       System.out.println("RAConfig.updateRAR(" + raProperties + ")");
787
788     FileReader JavaDoc file = new FileReader JavaDoc(raProperties);
789     LineNumberReader JavaDoc lnr = new LineNumberReader JavaDoc(file);
790
791     // get RAR name
792
String JavaDoc line = lnr.readLine();
793     while (line != null) {
794       if (line.startsWith("RAR_NAME"))
795         break;
796       line = lnr.readLine();
797     }
798     int i = line.indexOf("RAR_NAME");
799     String JavaDoc rarName = line.substring(i+"RAR_NAME".length()).trim();
800     if (debug)
801       System.out.println("RAConfig.updateRAR : rarName = " + rarName);
802     else if (verbose)
803       System.out.println("update rar \"" + rarName +
804                          "\" with \"" + raProperties + "\"");
805
806     // create Map of properties
807
Map JavaDoc map = new Hashtable JavaDoc();
808     Hashtable JavaDoc prop = null;
809     String JavaDoc nodeName = null;
810     line = lnr.readLine();
811     while (line != null) {
812       if (line.startsWith("[")) {
813         prop = new Hashtable JavaDoc();
814         nodeName = line.substring(line.indexOf("[")+1,line.indexOf("]"));
815         map.put(nodeName.trim(),prop);
816       } else {
817         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line);
818         if (st.countTokens() == 2 && prop != null) {
819           prop.put(st.nextToken(),st.nextToken());
820         }
821       }
822       line = lnr.readLine();
823     }
824     file.close();
825
826     if (debug)
827       System.out.println("RAConfig.updateRAR : map = " + map);
828
829     // create ra.xml file with new values
830
File JavaDoc rarFile = new File JavaDoc(rarName);
831     if (rarFile.exists()) {
832       ZipFile JavaDoc zipFile = new ZipFile JavaDoc(rarFile.getAbsolutePath());
833       for (Enumeration JavaDoc zippedFiles = zipFile.entries(); zippedFiles.hasMoreElements(); ) {
834         // Retrieve entry of existing files
835
ZipEntry JavaDoc currEntry = (ZipEntry JavaDoc) zippedFiles.nextElement();
836         if (debug)
837           System.out.println("RAConfig.updateRAR : currEntry = " + currEntry);
838         if (currEntry.getName().equalsIgnoreCase(RA_XML)) {
839           InputStream JavaDoc reader = zipFile.getInputStream(currEntry);
840           createFile("ra.xml",update(reader,map));
841           reader.close();
842           break;
843         }
844       }
845       zipFile.close();
846     }
847
848     // update rar
849
updateZIP(rarName,RA_XML, tmpDir + "ra.xml",RA_XML);
850
851     // update a3servers.xml (host and port).
852
prop = (Hashtable JavaDoc) map.get("org.objectweb.joram.client.connector.JoramAdapter");
853     if (prop != null) {
854       if (debug)
855         System.out.println("RAConfig.updateRAR : prop = " + prop);
856       String JavaDoc host = (String JavaDoc) prop.get("HostName");
857       short serverId = -1;
858       String JavaDoc sid = (String JavaDoc) prop.get("ServerId");
859       if (sid != null)
860         serverId = new Short JavaDoc(sid).shortValue();
861       else {
862         if (debug)
863           System.out.println("RAConfig.updateRAR : ServerId not found in ra.properties");
864       }
865       String JavaDoc port = (String JavaDoc) prop.get("ServerPort");
866       if (host != null && host.length() > 0
867           && port != null && port.length() > 0
868           && serverId >= 0) {
869         updateA3Servers(rarName,host,port,serverId);
870       }
871     }
872     new File JavaDoc(tmpDir + "ra.xml").delete();
873   }
874
875   /**
876    * Update the RAR file
877    * @param zipName String JAR or RAR file
878    * @param path path in JAR or RAR file
879    * @param newFileName add new filename
880    * @param oldFileName remove old file from JAR or RAR
881    */

882   private void updateZIP(String JavaDoc zipName,
883                          String JavaDoc path,
884                          String JavaDoc newFileName,
885                          String JavaDoc oldFileName)
886     throws Exception JavaDoc {
887
888     if (debug)
889       System.out.println("RAConfig.updateZIP(" + zipName +
890                          "," + path + "," + newFileName +
891                          "," + oldFileName + ")");
892     else if (verbose)
893       System.out.println("updateZIP \"" + zipName +
894                          "\", path \"" + path +
895                          "\", new file \"" + newFileName +
896                          "\", old file \"" + oldFileName + "\"");
897
898
899     ZipEntry JavaDoc entry = null;
900
901     File JavaDoc file = new File JavaDoc(zipName);
902     if (file.exists()) {
903       ZipFile JavaDoc zipFile = new ZipFile JavaDoc(file.getAbsolutePath());
904       Enumeration JavaDoc zipEntries = zipFile.entries();
905       // create your output zip file
906
ZipOutputStream JavaDoc newZip = new ZipOutputStream JavaDoc(
907         new FileOutputStream JavaDoc(new File JavaDoc(file.getAbsolutePath() + "_TMP")));
908
909       // Get all data (except the oldFileName) from zip file and
910
// write it to the tmp zip file
911
while (zipEntries.hasMoreElements()) {
912         entry = (ZipEntry JavaDoc) zipEntries.nextElement();
913         if (entry.getName().equalsIgnoreCase(oldFileName))
914           continue;
915         newZip.putNextEntry(new ZipEntry JavaDoc(entry.getName()));
916         InputStream JavaDoc is = zipFile.getInputStream(entry);
917         try {
918           dump(is,newZip);
919         } finally {
920           newZip.closeEntry();
921           is.close();
922         }
923       }
924       zipFile.close();
925
926       // Write the new fileName to the zip
927
entry = new ZipEntry JavaDoc(path);
928       newZip.putNextEntry(entry);
929       try {
930         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(newFileName);
931         dump(fis,newZip);
932         fis.close();
933       } catch (Exception JavaDoc exc) {
934         System.out.println("Error reading input file: " + newFileName + " " + exc);
935         newZip.close();
936         new File JavaDoc(file.getAbsolutePath() + "_TMP").delete();
937         throw exc;
938       } finally {
939         newZip.flush();
940         newZip.closeEntry();
941         newZip.finish();
942         newZip.close();
943       }
944       String JavaDoc toRename = file.getAbsolutePath() + "_TMP";
945       new File JavaDoc(file.getAbsolutePath()).delete();
946       new File JavaDoc(toRename).renameTo(file);
947     }
948   }
949 }
950
Popular Tags