KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_rar > raconfig > RAConfig


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ____________________________________.
22  * Contributor(s): Eric Hardesty
23  *
24  *
25  * --------------------------------------------------------------------------
26  * $Id: RAConfig.java,v 1.10 2004/11/02 00:24:44 ehardesty Exp $
27  * --------------------------------------------------------------------------
28  */

29
30 package org.objectweb.jonas_rar.raconfig;
31
32 import java.io.BufferedReader JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37 import java.io.FileReader JavaDoc;
38 import java.io.FileWriter JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.PrintStream JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Properties JavaDoc;
45 import java.util.zip.ZipEntry JavaDoc;
46 import java.util.zip.ZipFile JavaDoc;
47 import java.util.zip.ZipOutputStream JavaDoc;
48
49 import org.objectweb.jonas.security.realm.lib.Base64;
50 import org.objectweb.jonas_rar.deployment.xml.Connector;
51 import org.objectweb.jonas_rar.deployment.api.ConfigPropertyDesc;
52 import org.objectweb.jonas_rar.deployment.xml.ConfigProperty;
53 import org.objectweb.jonas_rar.deployment.api.RarDeploymentDesc;
54 import org.objectweb.jonas_rar.deployment.lib.RarDeploymentDescManager;
55 import org.objectweb.jonas_rar.deployment.xml.Activationspec;
56 import org.objectweb.jonas_rar.deployment.xml.Adminobject;
57 import org.objectweb.jonas_rar.deployment.xml.ConnectionDefinition;
58 import org.objectweb.jonas_rar.deployment.xml.InboundResourceadapter;
59 import org.objectweb.jonas_rar.deployment.xml.JdbcConnParams;
60 import org.objectweb.jonas_rar.deployment.xml.JonasActivationspec;
61 import org.objectweb.jonas_rar.deployment.xml.JonasAdminobject;
62 import org.objectweb.jonas_rar.deployment.xml.JonasConfigProperty;
63 import org.objectweb.jonas_rar.deployment.xml.JonasConnectionDefinition;
64 import org.objectweb.jonas_rar.deployment.xml.JonasConnector;
65 import org.objectweb.jonas_rar.deployment.xml.JonasSecurityMapping;
66 import org.objectweb.jonas_rar.deployment.xml.Messageadapter;
67 import org.objectweb.jonas_rar.deployment.xml.Messagelistener;
68 import org.objectweb.jonas_rar.deployment.xml.OutboundResourceadapter;
69 import org.objectweb.jonas_rar.deployment.xml.PoolParams;
70 import org.objectweb.jonas_rar.deployment.xml.RequiredConfigProperty;
71 import org.objectweb.jonas_rar.deployment.xml.Resourceadapter;
72 import org.objectweb.jonas_rar.deployment.xml.SecurityEntry;
73
74 /**
75  * This class may be used to parse a resource adapter deployment descriptor and
76  * generate the corresponding properties files for the jca service java
77  * org.objectweb.jonas.resource.RAConfig -j jndiname -r rarlink -verbose
78  * <input_rar>java org.objectweb.jonas.resource.RAConfig -u jonas-ra.xml
79  * <input_rar>java org.objectweb.jonas.resource.RAConfig -dm -p db.properties
80  * <input_rar><output_rar>
81  */

82 public class RAConfig {
83
84     /**
85      * Buffer Size
86      */

87     private static final int BUFFER_SIZE = 500;
88
89     /** JCA 1.5 */
90     private static final double JCA_1_5 = 1.5;
91     /** JCA 1.0 */
92     private static final double JCA_1_0 = 1.0;
93
94     RarDeploymentDesc rarDD = null;
95
96     /**
97      * Validate the XML
98      */

99     static boolean validation = true;
100
101     static String JavaDoc outputFile = "META-INF/jonas-ra.xml";
102
103     private static final String JavaDoc xmlVers = "<?xml version = \"1.0\" encoding = \"UTF-8\"?>\n";
104
105     // Constructor
106
public RAConfig(String JavaDoc fileName) throws RAConfigException {
107         ClassLoader JavaDoc curLoader = Thread.currentThread().getContextClassLoader();
108
109         try {
110             RarDeploymentDescManager.setParsingWithValidation(validation);
111             rarDD = RarDeploymentDescManager.getInstance(fileName, curLoader);
112         } catch (Exception JavaDoc e) {
113             throw new RAConfigException(e);
114         }
115
116     }
117
118     public RarDeploymentDesc getRarDeploymentDesc() {
119         return rarDD;
120     }
121
122     /**
123      * Process the ra.xml file in the input rar file and build the corresponding
124      * jonas-ra.xml. If the -p argument is specified, then retrieve the
125      * appropriate configuration values to pre-configure the corrsponding JDBC
126      * RA config properties.
127      * @param fileInputName String file name
128      * @param jndiname String jndi name
129      * @param rarlink String rar link
130      * @param dbProps Properties
131      * @param encrypt boolean
132      * @param securityfile String security file
133      * @param path String path
134      * @param isVerbose String
135      * @throws RAConfigException if an Exception occurs
136      */

137     private static void buildXML(String JavaDoc fileInputName, String JavaDoc jndiname, String JavaDoc rarlink, Properties JavaDoc dbProps,
138             boolean encrypt, String JavaDoc securityfile, String JavaDoc path,
139             boolean isVerbose) throws RAConfigException {
140
141         RAConfig raxml = new RAConfig(fileInputName);
142         Connector conn = raxml.getRarDeploymentDesc().getConnector();
143         double specVersion = 0.0;
144         ConfigPropertyDesc[] raProps = raxml.getRarDeploymentDesc().getRaConfigPropTags();
145         if (raProps == null) {
146             throw new RAConfigException("RAR file doesn't contain ra.xml");
147         }
148         if (conn.getSpecVersion().equals("1.0")) {
149             specVersion = JCA_1_0;
150         } else if (conn.getSpecVersion().equals("1.5")) {
151             specVersion = JCA_1_5;
152         } else {
153             throw new RAConfigException("ra.xml doesn't correct spec version");
154         }
155
156         JonasConnector jConn = new JonasConnector();
157         PoolParams jPool = new PoolParams();
158         JdbcConnParams jConnParams = new JdbcConnParams();
159         JonasConfigProperty jCfg = null;
160
161         String JavaDoc val = "";
162         String JavaDoc str = "";
163         jPool.setPoolInit("0");
164
165         val = "0";
166         if (dbProps.getProperty("jdbc.minconpool") != null && dbProps.getProperty("jdbc.minconpool").length() > 0) {
167             val = dbProps.getProperty("jdbc.minconpool").trim();
168         }
169         jPool.setPoolMin(val);
170
171         val = "-1";
172         if (dbProps.getProperty("jdbc.maxconpool") != null && dbProps.getProperty("jdbc.maxconpool").length() > 0) {
173             val = dbProps.getProperty("jdbc.maxconpool").trim();
174         }
175         jPool.setPoolMax(val);
176
177         val = "0";
178         if (dbProps.getProperty("jdbc.connmaxage") != null && dbProps.getProperty("jdbc.connmaxage").length() > 0) {
179             val = dbProps.getProperty("jdbc.connmaxage").trim();
180         }
181         jPool.setPoolMaxAgeMinutes(val);
182
183         jPool.setPstmtMax("10");
184
185         val = "0";
186         if (dbProps.getProperty("jdbc.maxopentime") != null && dbProps.getProperty("jdbc.maxopentime").length() > 0) {
187             val = dbProps.getProperty("jdbc.maxopentime").trim();
188         }
189         jPool.setPoolMaxOpentime(val);
190
191         val = "0";
192         if (dbProps.getProperty("jdbc.maxwaiters") != null && dbProps.getProperty("jdbc.maxwaiters").length() > 0) {
193             val = dbProps.getProperty("jdbc.maxwaiters").trim();
194         }
195         jPool.setPoolMaxWaiters(val);
196
197         val = "0";
198         if (dbProps.getProperty("jdbc.maxwaittime") != null && dbProps.getProperty("jdbc.maxwaittime").length() > 0) {
199             val = dbProps.getProperty("jdbc.maxwaittime").trim();
200         }
201         jPool.setPoolMaxWaittime(val);
202
203         val = "30";
204         if (dbProps.getProperty("jdbc.samplingperiod") != null && dbProps.getProperty("jdbc.samplingperiod").length() > 0) {
205             val = dbProps.getProperty("jdbc.samplingperiod").trim();
206         }
207         jPool.setPoolSamplingPeriod(val);
208
209
210         jConn.setLogEnabled("false");
211         jConn.setLogTopic("");
212         jConn.setNativeLib("");
213         if (jndiname.length() == 0) {
214             jndiname = dbProps.getProperty("datasource.name", "").trim();
215         }
216
217         val = "0";
218         if (dbProps.getProperty("jdbc.connchecklevel") != null
219                 && dbProps.getProperty("jdbc.connchecklevel").length() > 0) {
220             val = dbProps.getProperty("jdbc.connchecklevel").trim();
221         }
222         jConnParams.setJdbcCheckLevel(val);
223
224         val = "";
225         if (dbProps.getProperty("jdbc.connteststmt") != null && dbProps.getProperty("jdbc.connteststmt").length() > 0) {
226             val = dbProps.getProperty("jdbc.connteststmt").trim();
227             jConnParams.setJdbcTestStatement(val);
228         }
229
230         if (specVersion == 1.0) {
231             jConn.setJndiName(jndiname);
232         }
233         jConn.setRarlink(rarlink);
234         jConn.setPoolParams(jPool);
235         jConn.setJdbcConnParams(jConnParams);
236
237         if (specVersion == JCA_1_0) {
238             val = "";
239             str = "";
240             for (int i = 0; i < raProps.length; i++) {
241                 jCfg = new JonasConfigProperty();
242                 str = raProps[i].getConfigPropertyName();
243                 jCfg.setJonasConfigPropertyName(str);
244                 if (dbProps.size() > 0) {
245                     val = getProp(dbProps, str);
246                 } else {
247                     val = "";
248                 }
249                 jCfg.setJonasConfigPropertyValue(val);
250                 jConn.addJonasConfigProperty(jCfg);
251             }
252         } else if (specVersion == JCA_1_5) {
253             Resourceadapter rAdapter = conn.getResourceadapter();
254             ConfigProperty cProperty = null;
255             // TODO rProperty never used !!!
256
RequiredConfigProperty rProperty = null;
257             // Process config properties if they exist
258
for (Iterator JavaDoc i = rAdapter.getConfigPropertyList().iterator(); i.hasNext();) {
259                 cProperty = (ConfigProperty) i.next();
260                 jCfg = new JonasConfigProperty();
261                 str = cProperty.getConfigPropertyName();
262                 jCfg.setJonasConfigPropertyName(str);
263                 if (dbProps.size() > 0) {
264                     val = getProp(dbProps, str);
265                 } else {
266                     val = "";
267                 }
268                 jCfg.setJonasConfigPropertyValue(val);
269                 jConn.addJonasConfigProperty(jCfg);
270             }
271             // Now process the outbound-resourceadapter
272
OutboundResourceadapter oRes = rAdapter.getOutboundResourceadapter();
273             if (oRes != null) {
274                 ConnectionDefinition cDef = null;
275                 JonasConnectionDefinition jcDef = null;
276                 for (Iterator JavaDoc it = oRes.getConnectionDefinitionList().iterator(); it.hasNext();) {
277                     cDef = (ConnectionDefinition) it.next();
278                     jcDef = new JonasConnectionDefinition();
279                     jcDef.setId(cDef.getId());
280                     jcDef.addDescription(cDef.getManagedconnectionfactoryClass());
281                     jcDef.setJndiName("");
282                     for (Iterator JavaDoc i = cDef.getConfigPropertyList().iterator(); i.hasNext();) {
283                         cProperty = (ConfigProperty) i.next();
284                         jCfg = new JonasConfigProperty();
285                         str = cProperty.getConfigPropertyName();
286                         jCfg.setJonasConfigPropertyName(str);
287                         jCfg.setJonasConfigPropertyValue("");
288                         jcDef.addJonasConfigProperty(jCfg);
289                     }
290                     jConn.addJonasConnectionDefinition(jcDef);
291                 }
292             }
293             // Now process the inbound-resourceadapter
294
InboundResourceadapter iRes = rAdapter.getInboundResourceadapter();
295             if (iRes != null) {
296                 Messageadapter mAdapter = iRes.getMessageadapter();
297                 if (mAdapter != null) {
298                     Messagelistener mList = null;
299                     JonasActivationspec jaSpec = null;
300                     Activationspec aSpec = null;
301                     for (Iterator JavaDoc it = mAdapter.getMessagelistenerList().iterator(); it.hasNext();) {
302                         mList = (Messagelistener) it.next();
303                         aSpec = mList.getActivationspec();
304                         if (aSpec != null) {
305                             jaSpec = new JonasActivationspec();
306                             jaSpec.setId(mList.getId());
307                             jaSpec.addDescription(mList.getMessagelistenerType());
308                             jaSpec.setJndiName("");
309                             jConn.addJonasActivationspec(jaSpec);
310                         }
311                     }
312                 }
313             }
314             // Now process the adminobject
315
for (Iterator JavaDoc it = rAdapter.getAdminobjectList().iterator(); it.hasNext();) {
316                 Adminobject aObj = (Adminobject) it.next();
317                 JonasAdminobject jaObj = null;
318                 if (aObj != null) {
319                     jaObj = new JonasAdminobject();
320                     jaObj.setId(aObj.getId());
321                     jaObj.addDescription(aObj.getAdminobjectClass());
322                     jaObj.setJndiName("");
323                     // Process config properties if they exist
324
for (Iterator JavaDoc i = aObj.getConfigPropertyList().iterator(); i.hasNext();) {
325                         cProperty = (ConfigProperty) i.next();
326                         jCfg = new JonasConfigProperty();
327                         str = cProperty.getConfigPropertyName();
328                         jCfg.setJonasConfigPropertyName(str);
329                         jCfg.setJonasConfigPropertyValue("");
330                         jaObj.addJonasConfigProperty(jCfg);
331                     }
332                     jConn.addJonasAdminobject(jaObj);
333                 }
334             }
335         }
336
337         if (securityfile != null) {
338             Properties JavaDoc secProps = new Properties JavaDoc();
339             File JavaDoc f = null;
340             try {
341                 f = new File JavaDoc(securityfile);
342                 FileInputStream JavaDoc is = new FileInputStream JavaDoc(f);
343                 secProps.load(is);
344             } catch (FileNotFoundException JavaDoc e) {
345                 System.err.println("Cannot find security properties file: " + securityfile);
346                 System.err.println("Continuing with creation of jonas-ra.xml");
347             } catch (IOException JavaDoc e) {
348                 System.err.println(e);
349             }
350
351             JonasSecurityMapping jsMap = new JonasSecurityMapping();
352             String JavaDoc pName = null;
353             String JavaDoc user = null;
354             String JavaDoc pass = null;
355             String JavaDoc tmp = null;
356             int offset = 0;
357             SecurityEntry sEnt = null;
358             for (Enumeration JavaDoc e = secProps.propertyNames(); e.hasMoreElements();) {
359                 pName = (String JavaDoc) e.nextElement();
360                 tmp = secProps.getProperty(pName);
361                 offset = tmp.indexOf("::");
362                 if (offset < 0) {
363                     System.err.println("Error in format of file: principalName = user::password");
364                     continue;
365                 }
366                 user = tmp.substring(0, offset);
367                 pass = tmp.substring(offset + 2);
368
369                 sEnt = new SecurityEntry();
370                 sEnt.setPrincipalName(pName);
371                 sEnt.setUser(user);
372                 if (encrypt) {
373                     try {
374                         pass = new String JavaDoc(Base64.encode(pass.getBytes()));
375                         sEnt.setEncrypted("" + encrypt);
376                     } catch (Exception JavaDoc ex) {
377                         System.err.println("Unable to encrypt the password.");
378                     }
379                 }
380                 sEnt.setPassword(pass);
381                 jsMap.addSecurityEntry(sEnt);
382             }
383             jConn.setJonasSecurityMapping(jsMap);
384         }
385
386         try {
387             String JavaDoc tmpDir = (path.length() > 0 ? path : System.getProperty("java.io.tmpdir"));
388             String JavaDoc fileName = tmpDir + File.separator + "jonas-ra.xml";
389             FileWriter JavaDoc fw = new FileWriter JavaDoc(fileName);
390             fw.write(xmlVers);
391             fw.write(jConn.toXML());
392             fw.close();
393             if (isVerbose) {
394                 System.out.println("Build Jonas Specific DD File '" + fileName + "' OK");
395             }
396
397         } catch (Exception JavaDoc e) {
398             throw new RAConfigException("Error writing output", e);
399         }
400     }
401
402     /**
403      * Mapping routine for the JDBC RA config values and the existing
404      * <DB>.properties values.
405      * @param prop Properties
406      * @param str String
407      * @return String
408      */

409     private static String JavaDoc getProp(Properties JavaDoc prop, String JavaDoc str) {
410         String JavaDoc[][] mapping = {{"connCheckLevel", "jdbc.connchecklevel"}, {"connMaxAge", "jdbc.connmaxage"},
411                 {"connTestStmt", "jdbc.connteststmt"}, {"URL", "datasource.url"}, {"dsClass", "datasource.classname"},
412                 {"user", "datasource.username"}, {"password", "datasource.password"},
413                 {"isolationLevel", "datasource.isolationlevel"}, {"mapperName", "datasource.mapper"}, };
414
415         String JavaDoc ret = "";
416         for (int i = 0; i < mapping.length; i++) {
417             if (mapping[i][0].equalsIgnoreCase(str)) {
418                 ret = (prop.getProperty(mapping[i][1]) == null ? "" : prop.getProperty(mapping[i][1]).trim());
419             }
420         }
421
422         return ret;
423     }
424
425     /**
426      * Update the RAR file
427      * @param infile String input RAR file
428      * @param outfile String output RAR file
429      * @param updatename String update file
430      * @param rarlink String RAR link
431      * @throws RAConfigException to throw if an Exception occurs
432      */

433     private static void updateRAR(String JavaDoc infile, String JavaDoc outfile, String JavaDoc updatename, String JavaDoc rarlink)
434             throws RAConfigException {
435
436         ZipOutputStream JavaDoc zipOutput = null;
437         ZipFile JavaDoc zipFile = null;
438         Enumeration JavaDoc zippedFiles = null;
439         ZipEntry JavaDoc currEntry = null;
440         ZipEntry JavaDoc entry = null;
441         byte[] buffer = new byte[BUFFER_SIZE];
442         int num = 0;
443
444         String JavaDoc filename = infile;
445         String JavaDoc outfilename = infile;
446         boolean outNew = false;
447         if (outfile != null && outfile.length() != 0) {
448             outfilename = outfile;
449             outNew = true;
450         }
451
452         try {
453             File JavaDoc file = new File JavaDoc(filename);
454             if (file.exists()) {
455                 zipFile = new ZipFile JavaDoc(file.getAbsolutePath());
456                 //get an enumeration of all existing entries
457
zippedFiles = zipFile.entries();
458                 //create your output zip file
459
if (!outNew) {
460                     zipOutput = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(new File JavaDoc(file.getAbsolutePath() + "NEW")));
461                 } else {
462                     File JavaDoc ofile = new File JavaDoc(outfilename);
463                     if (ofile.exists()) {
464                         ofile.delete();
465                     }
466
467                     zipOutput = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(new File JavaDoc(outfilename)));
468                 }
469                 //Get all the data out of the previously zipped files and
470
// write
471
// it to a new ZipEntry to go into a new file archive
472
if ((outfile == null || outfile.length() == 0) && rarlink.length() == 0) {
473                     while (zippedFiles.hasMoreElements()) {
474                         //Retrieve entry of existing files
475
currEntry = (ZipEntry JavaDoc) zippedFiles.nextElement();
476                         if (currEntry.getName().equalsIgnoreCase(outputFile)) {
477                             continue;
478                         }
479                         //Read data from existing file
480
zipOutput.putNextEntry(new ZipEntry JavaDoc(currEntry.getName()));
481                         InputStream JavaDoc reader = zipFile.getInputStream(currEntry);
482                         while ((num = reader.read(buffer)) != -1) {
483                             zipOutput.write(buffer, 0, num);
484                         }
485                         //Commit the data
486
zipOutput.flush();
487                         zipOutput.closeEntry();
488                     }
489                 }
490                 //Close the old zip file
491
zipFile.close();
492
493                 //Write the 'new' file to the archive, commit, and close
494
entry = new ZipEntry JavaDoc(outputFile);
495                 zipOutput.putNextEntry(entry);
496
497                 //Read data from existing file
498
try {
499                     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(updatename));
500                     int ch = 0;
501                     while ((ch = reader.read()) != -1) {
502                         zipOutput.write(ch);
503                     }
504                 } catch (Exception JavaDoc ex) {
505                     System.out.println("Error reading input file: " + updatename + " " + ex);
506                     //Commit the data
507
zipOutput.flush();
508                     zipOutput.closeEntry();
509                     zipOutput.finish();
510                     zipOutput.close();
511                 }
512                 //Commit the data
513
zipOutput.flush();
514                 zipOutput.closeEntry();
515                 zipOutput.finish();
516                 zipOutput.close();
517
518                 if (!outNew) {
519                     //delete the old file and rename the new one
520
File JavaDoc toBeDeleted = new File JavaDoc(file.getAbsolutePath());
521                     toBeDeleted.delete();
522                     File JavaDoc toBeRenamed = new File JavaDoc(file.getAbsolutePath() + "NEW");
523                     toBeRenamed.renameTo(file);
524                 }
525             } else {
526                 //create your output zip file
527
zipOutput = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(new File JavaDoc(outfilename)));
528
529                 //Write the file to the archive, commit, and close
530
entry = new ZipEntry JavaDoc(outputFile);
531                 zipOutput.putNextEntry(entry);
532                 //Read data from existing file
533
BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(updatename));
534                 int ch = 0;
535                 while ((ch = reader.read()) != -1) {
536                     zipOutput.write(ch);
537                 }
538
539                 //Commit the data
540
zipOutput.flush();
541                 zipOutput.closeEntry();
542                 zipOutput.finish();
543                 zipOutput.close();
544             }
545             // finally, we delete generated jonas-ra.xml
546
new File JavaDoc(updatename).delete();
547         } catch (Exception JavaDoc ex) {
548             throw new RAConfigException(ex);
549         }
550
551     }
552
553     /**
554      * Indent output
555      * @param count int of number of indentions
556      * @return String indention
557      */

558     private static String JavaDoc indent(int count) {
559         String JavaDoc front = "";
560         String JavaDoc indentChars = " ";
561         for (int i = 0; i < count; i++) {
562             front += indentChars;
563         }
564         return front;
565     }
566
567     /**
568      * Usage of RAConfig
569      *
570      */

571     public static void usage() {
572         System.out.println("");
573         System.out.println("Usage: java org.objectweb.jonas.resource.RAConfig <Options> <Input_File> [Output_File]");
574         System.out.println(" to generate the jonas-ra.xml file for a Resource Adapter.");
575         System.out.println("");
576         System.out.println(" With Options:");
577         System.out.println(" -? or -help print this help message");
578         System.out.println(" -DM,-DS,-PC,-XA DriverManager, Datasource, PooledConnection,");
579         System.out.println(" XAConnection only used with -p");
580         System.out.println(" -Jndiname <jndiname> specifies the jndiname RA");
581         System.out.println(" -Property <property file> specifies the database property file");
582         System.out.println(" to process");
583         System.out.println(" -Rarlink <rarlink> specifies the rar file to link to");
584         System.out.println(" -Update <input name> update the input rar with the specified file");
585         System.out.println(" unless an output_file named is specified");
586         System.out.println(" -ENcrypt used with -sf to encrypt the passwords ");
587         System.out.println(" -SecurityFile security xml file to add to jonas-ra.xml ");
588         System.out.println(" -Verbose output the contents of the deployment descriptor ");
589         System.out.println(" -NoValidation turn off xml dtd/schema validation ");
590         System.out.println(" -PATH <path> path where jonas-ra.xml should be written to ");
591         System.out.println(" default is System.property('java.io.tmpdir')");
592         System.out.println("");
593         System.out.println(" Input_File Resource Adapter RAR file");
594         System.out.println(" Output_File Resource Adapter RAR file only used with ");
595         System.out.println(" -p(required) or -u(optional)");
596         System.out.println("");
597     }
598
599     // Main Method
600
/**
601      * Main routine
602      * @param args String [] input arguments
603      * @throws RAConfigException if an Exception occurs
604      */

605     public static void main(String JavaDoc[] args) throws RAConfigException {
606         PrintStream JavaDoc out = null;
607         boolean isHelp = false;
608         boolean isVerbose = false;
609         boolean doProperty = false;
610         boolean doUpdate = false;
611         boolean encrypt = false;
612         String JavaDoc fileInputName = null;
613         String JavaDoc fileOutputName = null;
614         String JavaDoc securityfile = null;
615         String JavaDoc jndiname = "";
616         String JavaDoc rarlink = "";
617         String JavaDoc updatename = "";
618         String JavaDoc jdbcRarLink = "";
619         String JavaDoc jonasRarStr = "JOnASJDBC_";
620         String JavaDoc propertyFile = "";
621         String JavaDoc path = "";
622         // Get command args
623

624         Properties JavaDoc dbProps = null;
625
626         if (args.length < 1) {
627             isHelp = true;
628         }
629         for (int argn = 0; argn < args.length; argn++) {
630             String JavaDoc arg = args[argn].toLowerCase();
631             if (arg.equals("-help") || arg.equals("-?")) {
632                 isHelp = true;
633                 continue;
634             }
635             if (arg.equals("-verbose") || arg.equals("-v")) {
636                 isVerbose = true;
637                 continue;
638             }
639             if (arg.equals("-encrypt") || arg.equals("-en")) {
640                 encrypt = true;
641                 continue;
642             }
643             if (arg.equals("-securityfile") || arg.equals("-sf")) {
644                 if (argn + 1 == args.length || args[++argn].startsWith("-")) {
645                     usage();
646                     throw new RAConfigException("Error with -sf input");
647                 }
648                 securityfile = args[argn];
649                 continue;
650             }
651             if (arg.equals("-novalidation") || arg.equals("-nv")) {
652                 validation = false;
653                 continue;
654             }
655             if (arg.equals("-update") || arg.equals("-u")) {
656                 doUpdate = true;
657                 if (argn + 1 == args.length || args[++argn].startsWith("-")) {
658                     usage();
659                     throw new RAConfigException("Error with -u input");
660                 }
661                 updatename = args[argn];
662                 continue;
663             }
664             if (arg.equals("-jndiname") || arg.equals("-j")) {
665                 if (argn + 1 == args.length || args[++argn].startsWith("-")) {
666                     usage();
667                     throw new RAConfigException("Error with -j input");
668                 }
669                 jndiname = args[argn];
670                 continue;
671             }
672             if (arg.equals("-rarlink") || arg.equals("-r")) {
673                 if (argn + 1 == args.length || args[++argn].startsWith("-")) {
674                     usage();
675                     throw new RAConfigException("Error with -r input");
676                 }
677                 rarlink = args[argn];
678                 continue;
679             }
680             if (arg.equals("-property") || arg.equals("-p")) {
681                 doProperty = true;
682                 if (argn + 1 == args.length || args[++argn].startsWith("-")) {
683                     usage();
684                     throw new RAConfigException("Error with -p input");
685                 }
686                 propertyFile = args[argn];
687                 continue;
688             }
689             if (arg.equals("-dm")) {
690                 jdbcRarLink = jonasRarStr + "DM";
691                 continue;
692             }
693             if (arg.equals("-ds")) {
694                 jdbcRarLink = jonasRarStr + "DS";
695                 continue;
696             }
697             if (arg.equals("-xa")) {
698                 jdbcRarLink = jonasRarStr + "XA";
699                 continue;
700             }
701             if (arg.equals("-pc")) {
702                 jdbcRarLink = jonasRarStr + "PC";
703                 continue;
704             }
705             if (arg.equals("-path")) {
706                 if (argn + 1 == args.length || args[++argn].startsWith("-")) {
707                     usage();
708                     throw new RAConfigException("Error with -path");
709                 }
710                 path = args[argn];
711                 continue;
712             }
713             if (fileInputName == null) {
714                 fileInputName = args[argn];
715                 if (!fileInputName.endsWith(".rar")) {
716                     fileInputName += ".rar";
717                 }
718             } else if (doProperty || doUpdate) {
719                 if (fileOutputName == null) {
720                     fileOutputName = args[argn];
721                     if (!fileOutputName.endsWith(".rar")) {
722                         fileOutputName += ".rar";
723                     }
724                 }
725             } else {
726                 usage();
727                 throw new RAConfigException("Error multiple input files specified without -property or -update");
728             }
729         }
730
731         // Usage ?
732
if (isHelp) {
733             usage();
734             System.exit(0);
735         }
736         if (fileInputName == null) {
737             usage();
738             throw new RAConfigException("Error missing input file");
739         }
740         if (doProperty && fileOutputName == null) {
741             usage();
742             throw new RAConfigException("Error missing output file");
743         }
744         if (fileInputName.equals(fileOutputName)) {
745             throw new RAConfigException("Input and Output filenames cannot be the same");
746         }
747
748         dbProps = new Properties JavaDoc();
749         if (doProperty) {
750             if (jdbcRarLink.length() > 0 && rarlink.length() == 0) {
751                 rarlink = jdbcRarLink;
752             }
753             try {
754                 dbProps.load(new FileInputStream JavaDoc(propertyFile));
755             } catch (Exception JavaDoc ex) {
756                 if (propertyFile.endsWith(".properties")) {
757                     System.out.println("Error reading " + propertyFile + " " + ex);
758                 } else {
759                     try {
760                         dbProps.load(new FileInputStream JavaDoc(propertyFile + ".properties"));
761                     } catch (Exception JavaDoc ex1) {
762                         System.out.println("Error reading " + propertyFile + " " + ex);
763                     }
764                 }
765             }
766             buildXML(fileInputName, jndiname, rarlink, dbProps, encrypt, securityfile, path, isVerbose);
767             if (updatename.length() == 0) {
768                 updatename = System.getProperty("java.io.tmpdir") + File.separator + "jonas-ra.xml";
769             }
770             updateRAR(fileInputName, fileOutputName, updatename, rarlink);
771         } else if (doUpdate) {
772             updateRAR(fileInputName, fileOutputName, updatename, rarlink);
773         } else {
774             buildXML(fileInputName, jndiname, rarlink, dbProps, encrypt, securityfile, path, isVerbose);
775         }
776     }
777 }
778
Popular Tags