001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): ____________________________________.
022: * Contributor(s): Eric Hardesty
023: *
024: *
025: * --------------------------------------------------------------------------
026: * $Id: RAConfig.java 9769 2006-10-19 21:52:59Z ehardesty $
027: * --------------------------------------------------------------------------
028: */package org.objectweb.jonas_rar.raconfig;
029:
030: import java.io.BufferedReader;
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.FileNotFoundException;
034: import java.io.FileOutputStream;
035: import java.io.FileReader;
036: import java.io.FileWriter;
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.util.Enumeration;
040: import java.util.Iterator;
041: import java.util.Properties;
042: import java.util.zip.ZipEntry;
043: import java.util.zip.ZipFile;
044: import java.util.zip.ZipOutputStream;
045:
046: import org.objectweb.jonas_rar.deployment.api.ConfigPropertyDesc;
047: import org.objectweb.jonas_rar.deployment.api.RarDeploymentDesc;
048: import org.objectweb.jonas_rar.deployment.lib.RarDeploymentDescManager;
049: import org.objectweb.jonas_rar.deployment.xml.Activationspec;
050: import org.objectweb.jonas_rar.deployment.xml.Adminobject;
051: import org.objectweb.jonas_rar.deployment.xml.ConfigProperty;
052: import org.objectweb.jonas_rar.deployment.xml.ConnectionDefinition;
053: import org.objectweb.jonas_rar.deployment.xml.Connector;
054: import org.objectweb.jonas_rar.deployment.xml.InboundResourceadapter;
055: import org.objectweb.jonas_rar.deployment.xml.JdbcConnParams;
056: import org.objectweb.jonas_rar.deployment.xml.JonasActivationspec;
057: import org.objectweb.jonas_rar.deployment.xml.JonasAdminobject;
058: import org.objectweb.jonas_rar.deployment.xml.JonasConfigProperty;
059: import org.objectweb.jonas_rar.deployment.xml.JonasConnectionDefinition;
060: import org.objectweb.jonas_rar.deployment.xml.JonasConnector;
061: import org.objectweb.jonas_rar.deployment.xml.JonasSecurityMapping;
062: import org.objectweb.jonas_rar.deployment.xml.Messageadapter;
063: import org.objectweb.jonas_rar.deployment.xml.Messagelistener;
064: import org.objectweb.jonas_rar.deployment.xml.OutboundResourceadapter;
065: import org.objectweb.jonas_rar.deployment.xml.PoolParams;
066: import org.objectweb.jonas_rar.deployment.xml.RequiredConfigProperty;
067: import org.objectweb.jonas_rar.deployment.xml.Resourceadapter;
068: import org.objectweb.jonas_rar.deployment.xml.SecurityEntry;
069:
070: import org.objectweb.jonas.security.realm.lib.Base64;
071:
072: /**
073: * This class may be used to parse a resource adapter deployment descriptor and
074: * generate the corresponding properties files for the jca service java
075: * org.objectweb.jonas.resource.RAConfig -j jndiname -r rarlink -verbose
076: * <input_rar>java org.objectweb.jonas_ra.raconfig.RAConfig -u jonas-ra.xml
077: * <input_rar>java org.objectweb.jonas_ra.raconfig.RAConfig -dm -p db.properties
078: * <input_rar><output_rar>
079: */
080: public class RAConfig {
081:
082: /**
083: * Buffer Size
084: */
085: private static final int BUFFER_SIZE = 500;
086:
087: /** JCA 1.5 */
088: private static final double JCA_1_5 = 1.5;
089: /** JCA 1.0 */
090: private static final double JCA_1_0 = 1.0;
091:
092: RarDeploymentDesc rarDD = null;
093:
094: /**
095: * Validate the XML
096: */
097: private static boolean validation = true;
098:
099: /**
100: * jonas-ra.xml XML filename
101: */
102: private static final String JONAS_RA_FILENAME = "META-INF/jonas-ra.xml";
103:
104: /**
105: * Constructor
106: */
107: public RAConfig(String fileName) throws RAConfigException {
108: ClassLoader curLoader = Thread.currentThread()
109: .getContextClassLoader();
110:
111: try {
112: RarDeploymentDescManager
113: .setParsingWithValidation(validation);
114: rarDD = RarDeploymentDescManager.getInstance(fileName,
115: curLoader);
116: } catch (Exception e) {
117: throw new RAConfigException(e);
118: }
119:
120: }
121:
122: public RarDeploymentDesc getRarDeploymentDesc() {
123: return rarDD;
124: }
125:
126: /**
127: * Process the ra.xml file in the input rar file and build the corresponding
128: * jonas-ra.xml. If the -p argument is specified, then retrieve the
129: * appropriate configuration values to pre-configure the corrsponding JDBC
130: * RA config properties.
131: * @param fileInputName String file name
132: * @param jndiname String jndi name
133: * @param rarlink String rar link
134: * @param dbProps Properties
135: * @param encrypt boolean
136: * @param securityfile String security file
137: * @param path String path
138: * @param isVerbose boolean
139: * @param createNew boolean
140: * @throws RAConfigException if an Exception occurs
141: */
142: private static void buildXML(String fileInputName, String jndiname,
143: String rarlink, Properties dbProps, boolean encrypt,
144: String securityfile, String path, boolean isVerbose,
145: boolean createNew) throws RAConfigException {
146:
147: // Check if jonas-ra.xml already exists and return it
148: if (!createNew) {
149: boolean ext = extractJonasRAXml(fileInputName, path);
150: if (ext) {
151: return;
152: }
153: }
154:
155: RAConfig raxml = new RAConfig(fileInputName);
156: Connector conn = raxml.getRarDeploymentDesc().getConnector();
157: double specVersion = 0.0;
158: ConfigPropertyDesc[] raProps = raxml.getRarDeploymentDesc()
159: .getRaConfigPropTags();
160: if (raProps == null) {
161: throw new RAConfigException(
162: "RAR file doesn't contain ra.xml");
163: }
164: if (conn.getSpecVersion().equals("1.0")) {
165: specVersion = JCA_1_0;
166: } else if (conn.getSpecVersion().equals("1.5")) {
167: specVersion = JCA_1_5;
168: } else {
169: throw new RAConfigException(
170: "ra.xml doesn't correct spec version");
171: }
172:
173: JonasConnector jConn = new JonasConnector();
174: PoolParams jPool = new PoolParams();
175: JdbcConnParams jConnParams = new JdbcConnParams();
176: JonasConfigProperty jCfg = null;
177:
178: String val = "";
179: String str = "";
180: jPool.setPoolInit("0");
181:
182: val = "0";
183: if (dbProps.getProperty("jdbc.minconpool") != null
184: && dbProps.getProperty("jdbc.minconpool").length() > 0) {
185: val = dbProps.getProperty("jdbc.minconpool").trim();
186: }
187: jPool.setPoolMin(val);
188:
189: val = "-1";
190: if (dbProps.getProperty("jdbc.maxconpool") != null
191: && dbProps.getProperty("jdbc.maxconpool").length() > 0) {
192: val = dbProps.getProperty("jdbc.maxconpool").trim();
193: }
194: jPool.setPoolMax(val);
195:
196: val = "0";
197: if (dbProps.getProperty("jdbc.connmaxage") != null
198: && dbProps.getProperty("jdbc.connmaxage").length() > 0) {
199: val = dbProps.getProperty("jdbc.connmaxage").trim();
200: }
201: jPool.setPoolMaxAgeMinutes(val);
202:
203: jPool.setPstmtMax("10");
204:
205: val = "0";
206: if (dbProps.getProperty("jdbc.maxopentime") != null
207: && dbProps.getProperty("jdbc.maxopentime").length() > 0) {
208: val = dbProps.getProperty("jdbc.maxopentime").trim();
209: }
210: jPool.setPoolMaxOpentime(val);
211:
212: val = "0";
213: if (dbProps.getProperty("jdbc.maxwaiters") != null
214: && dbProps.getProperty("jdbc.maxwaiters").length() > 0) {
215: val = dbProps.getProperty("jdbc.maxwaiters").trim();
216: }
217: jPool.setPoolMaxWaiters(val);
218:
219: val = "0";
220: if (dbProps.getProperty("jdbc.maxwaittime") != null
221: && dbProps.getProperty("jdbc.maxwaittime").length() > 0) {
222: val = dbProps.getProperty("jdbc.maxwaittime").trim();
223: }
224: jPool.setPoolMaxWaittime(val);
225:
226: val = "30";
227: if (dbProps.getProperty("jdbc.samplingperiod") != null
228: && dbProps.getProperty("jdbc.samplingperiod").length() > 0) {
229: val = dbProps.getProperty("jdbc.samplingperiod").trim();
230: }
231: jPool.setPoolSamplingPeriod(val);
232:
233: val = "14";
234: if (dbProps.getProperty("jdbc.pstmtmax") != null
235: && dbProps.getProperty("jdbc.pstmtmax").length() > 0) {
236: val = dbProps.getProperty("jdbc.pstmtmax").trim();
237: jPool.setPstmtMax(val);
238: }
239:
240: jConn.setLogEnabled("false");
241: jConn.setLogTopic("");
242: jConn.setNativeLib("");
243: if (jndiname.length() == 0) {
244: jndiname = dbProps.getProperty("datasource.name", "")
245: .trim();
246: }
247:
248: val = "0";
249: if (dbProps.getProperty("jdbc.connchecklevel") != null
250: && dbProps.getProperty("jdbc.connchecklevel").length() > 0) {
251: val = dbProps.getProperty("jdbc.connchecklevel").trim();
252: }
253: jConnParams.setJdbcCheckLevel(val);
254:
255: val = "";
256: if (dbProps.getProperty("jdbc.connteststmt") != null
257: && dbProps.getProperty("jdbc.connteststmt").length() > 0) {
258: val = dbProps.getProperty("jdbc.connteststmt").trim();
259: jConnParams.setJdbcTestStatement(val);
260: }
261:
262: if (specVersion == 1.0) {
263: jConn.setJndiName(jndiname);
264: }
265: jConn.setRarlink(rarlink);
266: jConn.setPoolParams(jPool);
267: jConn.setJdbcConnParams(jConnParams);
268:
269: if (specVersion == JCA_1_0) {
270: val = "";
271: str = "";
272: for (int i = 0; i < raProps.length; i++) {
273: jCfg = new JonasConfigProperty();
274: str = raProps[i].getConfigPropertyName();
275: jCfg.setJonasConfigPropertyName(str);
276: if (dbProps.size() > 0) {
277: val = getProp(dbProps, str);
278: } else {
279: val = "";
280: }
281: jCfg.setJonasConfigPropertyValue(val);
282: jConn.addJonasConfigProperty(jCfg);
283: }
284: } else if (specVersion == JCA_1_5) {
285: Resourceadapter rAdapter = conn.getResourceadapter();
286: ConfigProperty cProperty = null;
287: // TODO rProperty never used !!!
288: RequiredConfigProperty rProperty = null;
289: // Process config properties if they exist
290: for (Iterator i = rAdapter.getConfigPropertyList()
291: .iterator(); i.hasNext();) {
292: cProperty = (ConfigProperty) i.next();
293: jCfg = new JonasConfigProperty();
294: str = cProperty.getConfigPropertyName();
295: jCfg.setJonasConfigPropertyName(str);
296: if (dbProps.size() > 0) {
297: val = getProp(dbProps, str);
298: } else {
299: val = "";
300: }
301: jCfg.setJonasConfigPropertyValue(val);
302: jConn.addJonasConfigProperty(jCfg);
303: }
304: // Now process the outbound-resourceadapter
305: OutboundResourceadapter oRes = rAdapter
306: .getOutboundResourceadapter();
307: if (oRes != null) {
308: ConnectionDefinition cDef = null;
309: JonasConnectionDefinition jcDef = null;
310: for (Iterator it = oRes.getConnectionDefinitionList()
311: .iterator(); it.hasNext();) {
312: cDef = (ConnectionDefinition) it.next();
313: jcDef = new JonasConnectionDefinition();
314: jcDef.setId(cDef.getId());
315: jcDef.addDescription(cDef
316: .getManagedconnectionfactoryClass());
317: jcDef.setJndiName("");
318: for (Iterator i = cDef.getConfigPropertyList()
319: .iterator(); i.hasNext();) {
320: cProperty = (ConfigProperty) i.next();
321: jCfg = new JonasConfigProperty();
322: str = cProperty.getConfigPropertyName();
323: jCfg.setJonasConfigPropertyName(str);
324: jCfg.setJonasConfigPropertyValue("");
325: jcDef.addJonasConfigProperty(jCfg);
326: }
327: jConn.addJonasConnectionDefinition(jcDef);
328: }
329: }
330: // Now process the inbound-resourceadapter
331: InboundResourceadapter iRes = rAdapter
332: .getInboundResourceadapter();
333: if (iRes != null) {
334: Messageadapter mAdapter = iRes.getMessageadapter();
335: if (mAdapter != null) {
336: Messagelistener mList = null;
337: JonasActivationspec jaSpec = null;
338: Activationspec aSpec = null;
339: for (Iterator it = mAdapter
340: .getMessagelistenerList().iterator(); it
341: .hasNext();) {
342: mList = (Messagelistener) it.next();
343: aSpec = mList.getActivationspec();
344: if (aSpec != null) {
345: jaSpec = new JonasActivationspec();
346: jaSpec.setId(mList.getId());
347: jaSpec.addDescription(mList
348: .getMessagelistenerType());
349: jaSpec.setJndiName("");
350: jConn.addJonasActivationspec(jaSpec);
351: }
352: }
353: }
354: }
355: // Now process the adminobject
356: for (Iterator it = rAdapter.getAdminobjectList().iterator(); it
357: .hasNext();) {
358: Adminobject aObj = (Adminobject) it.next();
359: JonasAdminobject jaObj = null;
360: if (aObj != null) {
361: jaObj = new JonasAdminobject();
362: jaObj.setId(aObj.getId());
363: jaObj.addDescription(aObj.getAdminobjectClass());
364: jaObj.setJndiName("");
365: // Process config properties if they exist
366: for (Iterator i = aObj.getConfigPropertyList()
367: .iterator(); i.hasNext();) {
368: cProperty = (ConfigProperty) i.next();
369: jCfg = new JonasConfigProperty();
370: str = cProperty.getConfigPropertyName();
371: jCfg.setJonasConfigPropertyName(str);
372: jCfg.setJonasConfigPropertyValue("");
373: jaObj.addJonasConfigProperty(jCfg);
374: }
375: jConn.addJonasAdminobject(jaObj);
376: }
377: }
378: }
379:
380: if (securityfile != null) {
381: Properties secProps = new Properties();
382: File f = null;
383: try {
384: f = new File(securityfile);
385: FileInputStream is = new FileInputStream(f);
386: secProps.load(is);
387: } catch (FileNotFoundException e) {
388: System.err
389: .println("Cannot find security properties file: "
390: + securityfile);
391: System.err
392: .println("Continuing with creation of jonas-ra.xml");
393: } catch (IOException e) {
394: System.err.println(e);
395: }
396:
397: JonasSecurityMapping jsMap = new JonasSecurityMapping();
398: String pName = null;
399: String user = null;
400: String pass = null;
401: String tmp = null;
402: int offset = 0;
403: SecurityEntry sEnt = null;
404: for (Enumeration e = secProps.propertyNames(); e
405: .hasMoreElements();) {
406: pName = (String) e.nextElement();
407: tmp = secProps.getProperty(pName);
408: offset = tmp.indexOf("::");
409: if (offset < 0) {
410: System.err
411: .println("Error in format of file: principalName = user::password");
412: continue;
413: }
414: user = tmp.substring(0, offset);
415: pass = tmp.substring(offset + 2);
416:
417: sEnt = new SecurityEntry();
418: sEnt.setPrincipalName(pName);
419: sEnt.setUser(user);
420: if (encrypt) {
421: try {
422: pass = new String(Base64
423: .encode(pass.getBytes()));
424: sEnt.setEncrypted("" + encrypt);
425: } catch (Exception ex) {
426: System.err
427: .println("Unable to encrypt the password.");
428: }
429: }
430: sEnt.setPassword(pass);
431: jsMap.addSecurityEntry(sEnt);
432: }
433: jConn.setJonasSecurityMapping(jsMap);
434: }
435:
436: try {
437: String tmpDir = path;
438: if (path.length() == 0) {
439: tmpDir = System.getProperty("java.io.tmpdir");
440: }
441: String fileName = tmpDir;
442: int fileLen = fileName.length();
443: if (fileName.charAt(fileLen - 1) != File.separatorChar) {
444: fileName += File.separatorChar;
445: }
446: fileName += "jonas-ra.xml";
447: FileWriter fw = new FileWriter(fileName);
448: fw.write(jConn.toXML());
449: fw.close();
450: if (isVerbose) {
451: System.out.println("Build Jonas Specific DD File '"
452: + fileName + "' OK");
453: }
454:
455: } catch (Exception e) {
456: throw new RAConfigException("Error writing output", e);
457: }
458: }
459:
460: /**
461: * Mapping routine for the JDBC RA config values and the existing
462: * <DB>.properties values.
463: * @param prop Properties
464: * @param str String
465: * @return String
466: */
467: private static String getProp(Properties prop, String str) {
468: String[][] mapping = {
469: { "connCheckLevel", "jdbc.connchecklevel" },
470: { "connMaxAge", "jdbc.connmaxage" },
471: { "connTestStmt", "jdbc.connteststmt" },
472: { "URL", "datasource.url" },
473: { "dsClass", "datasource.classname" },
474: { "user", "datasource.username" },
475: { "password", "datasource.password" },
476: { "isolationLevel", "datasource.isolationlevel" },
477: { "mapperName", "datasource.mapper" }, };
478:
479: String ret = "";
480: for (int i = 0; i < mapping.length; i++) {
481: if (mapping[i][0].equalsIgnoreCase(str)) {
482: ret = (prop.getProperty(mapping[i][1]) == null ? ""
483: : prop.getProperty(mapping[i][1]).trim());
484: }
485: }
486:
487: return ret;
488: }
489:
490: /**
491: * Update the RAR file
492: * @param infile String input RAR file
493: * @param outfile String output RAR file
494: * @param updatename String update file
495: * @param rarlink String RAR link
496: * @throws RAConfigException to throw if an Exception occurs
497: */
498: private static void updateRAR(String infile, String outfile,
499: String updatename, String rarlink) throws RAConfigException {
500:
501: ZipOutputStream zipOutput = null;
502: ZipFile zipFile = null;
503: Enumeration zippedFiles = null;
504: ZipEntry currEntry = null;
505: ZipEntry entry = null;
506: byte[] buffer = new byte[BUFFER_SIZE];
507: int num = 0;
508:
509: String filename = infile;
510: String outfilename = infile;
511: boolean outNew = false;
512: if (outfile != null && outfile.length() != 0) {
513: outfilename = outfile;
514: outNew = true;
515: }
516:
517: try {
518: File file = new File(filename);
519: if (file.exists()) {
520: zipFile = new ZipFile(file.getAbsolutePath());
521: //get an enumeration of all existing entries
522: zippedFiles = zipFile.entries();
523: //create your output zip file
524: if (!outNew) {
525: zipOutput = new ZipOutputStream(
526: new FileOutputStream(new File(file
527: .getAbsolutePath()
528: + "NEW")));
529: } else {
530: File ofile = new File(outfilename);
531: if (ofile.exists()) {
532: ofile.delete();
533: }
534:
535: zipOutput = new ZipOutputStream(
536: new FileOutputStream(new File(outfilename)));
537: }
538: //Get all the data out of the previously zipped files and
539: // write
540: // it to a new ZipEntry to go into a new file archive
541: if ((outfile == null || outfile.length() == 0)
542: && rarlink.length() == 0) {
543: while (zippedFiles.hasMoreElements()) {
544: //Retrieve entry of existing files
545: currEntry = (ZipEntry) zippedFiles
546: .nextElement();
547: if (currEntry.getName().equalsIgnoreCase(
548: JONAS_RA_FILENAME)) {
549: continue;
550: }
551: //Read data from existing file
552: zipOutput.putNextEntry(new ZipEntry(currEntry
553: .getName()));
554: InputStream reader = zipFile
555: .getInputStream(currEntry);
556: while ((num = reader.read(buffer)) != -1) {
557: zipOutput.write(buffer, 0, num);
558: }
559: //Commit the data
560: zipOutput.flush();
561: zipOutput.closeEntry();
562: }
563: }
564: //Close the old zip file
565: zipFile.close();
566:
567: //Write the 'new' file to the archive, commit, and close
568: entry = new ZipEntry(JONAS_RA_FILENAME);
569: zipOutput.putNextEntry(entry);
570:
571: //Read data from existing file
572: try {
573: BufferedReader reader = new BufferedReader(
574: new FileReader(updatename));
575: int ch = 0;
576: while ((ch = reader.read()) != -1) {
577: zipOutput.write(ch);
578: }
579: } catch (Exception ex) {
580: System.out.println("Error reading input file: "
581: + updatename + " " + ex);
582: //Commit the data
583: zipOutput.flush();
584: zipOutput.closeEntry();
585: zipOutput.finish();
586: zipOutput.close();
587: }
588: //Commit the data
589: zipOutput.flush();
590: zipOutput.closeEntry();
591: zipOutput.finish();
592: zipOutput.close();
593:
594: if (!outNew) {
595: //delete the old file and rename the new one
596: File toBeDeleted = new File(file.getAbsolutePath());
597: toBeDeleted.delete();
598: File toBeRenamed = new File(file.getAbsolutePath()
599: + "NEW");
600: toBeRenamed.renameTo(file);
601: }
602: } else {
603: //create your output zip file
604: zipOutput = new ZipOutputStream(new FileOutputStream(
605: new File(outfilename)));
606:
607: //Write the file to the archive, commit, and close
608: entry = new ZipEntry(JONAS_RA_FILENAME);
609: zipOutput.putNextEntry(entry);
610: //Read data from existing file
611: BufferedReader reader = new BufferedReader(
612: new FileReader(updatename));
613: int ch = 0;
614: while ((ch = reader.read()) != -1) {
615: zipOutput.write(ch);
616: }
617:
618: //Commit the data
619: zipOutput.flush();
620: zipOutput.closeEntry();
621: zipOutput.finish();
622: zipOutput.close();
623: }
624: // finally, we delete generated jonas-ra.xml
625: new File(updatename).delete();
626: } catch (Exception ex) {
627: throw new RAConfigException(ex);
628: }
629:
630: }
631:
632: /**
633: * Extract the jonas-ra.xml file if it exists in the rar
634: *
635: * @param infile String rar file name
636: * @param path String path
637: */
638: private static boolean extractJonasRAXml(String infile, String path) {
639: ZipFile zipFile = null;
640: ZipEntry entry = null;
641: byte[] buffer = new byte[BUFFER_SIZE];
642:
643: String filename = infile;
644: File file = new File(filename);
645: if (file.exists()) {
646: try {
647: zipFile = new ZipFile(file.getAbsolutePath());
648: entry = zipFile.getEntry(JONAS_RA_FILENAME);
649: if (entry != null) {
650: InputStream input = zipFile.getInputStream(entry);
651: String tmpDir = path;
652: if (path.length() == 0) {
653: tmpDir = System.getProperty("java.io.tmpdir");
654: }
655: String fileName = tmpDir;
656: int fileLen = fileName.length();
657: if (fileName.charAt(fileLen - 1) != File.separatorChar) {
658: fileName += File.separatorChar;
659: }
660: fileName += "jonas-ra.xml";
661:
662: FileOutputStream output = new FileOutputStream(
663: fileName);
664: for (int j = 0;;) {
665: int length = input.read(buffer);
666: if (length <= 0)
667: break;
668: output.write(buffer, 0, length);
669: }
670: output.close();
671: return true;
672: }
673: } catch (Exception ex) {
674: return false;
675: }
676: }
677: return false;
678: }
679:
680: /**
681: * Usage of RAConfig
682: *
683: */
684: public static void usage() {
685: System.out.println("");
686: System.out
687: .println("Usage: java org.objectweb.jonas.resource.RAConfig <Options> <Input_File> [Output_File]");
688: System.out
689: .println(" to generate the jonas-ra.xml file for a Resource Adapter.");
690: System.out
691: .println(" The output file will be written to the default location of the java ");
692: System.out
693: .println(" system property 'java.io.tmpdir' unless the -path parameter is specified");
694: System.out.println("");
695: System.out
696: .println(" Note: for a parameter either the whole name must be used or only the");
697: System.out
698: .println(" the capital letters, e.g. -jndiname or -j are equivalent");
699: System.out.println("");
700: System.out.println(" With Options:");
701: System.out
702: .println(" -? or -help print this help message");
703: System.out
704: .println(" -DM,-DS,-PC,-XA DriverManager, Datasource, PooledConnection,");
705: System.out
706: .println(" XAConnection only used with -p. Additional");
707: System.out
708: .println(" user configuration of the jonas-ra.xml file");
709: System.out
710: .println(" will be necessary for any option but -DM.");
711: System.out
712: .println(" -Jndiname <jndiname> specifies the jndiname RA");
713: System.out
714: .println(" -Property <property file> specifies the database property file");
715: System.out
716: .println(" to process");
717: System.out
718: .println(" -Rarlink <rarlink> specifies the rar file to link to");
719: System.out
720: .println(" -Update <input name> update the input rar with the specified file");
721: System.out
722: .println(" unless an output_file named is specified");
723: System.out
724: .println(" -ENcrypt used with -sf to encrypt the passwords ");
725: System.out
726: .println(" -SecurityFile security xml file to add to jonas-ra.xml ");
727: System.out
728: .println(" -Verbose output the contents of the deployment descriptor ");
729: System.out
730: .println(" -NoValidation turn off xml dtd/schema validation ");
731: System.out
732: .println(" -PATH <path> path where jonas-ra.xml should be written to ");
733: System.out
734: .println(" default is System.property('java.io.tmpdir')");
735: System.out
736: .println(" -NEW don't extract jonas-ra.xml create a new one ");
737: System.out.println("");
738: System.out
739: .println(" Input_File Resource Adapter RAR file");
740: System.out
741: .println(" Output_File Resource Adapter RAR file only used with ");
742: System.out
743: .println(" -p(required) or -u(optional)");
744: System.out.println("");
745: }
746:
747: // Main Method
748: /**
749: * Main routine
750: * @param args String [] input arguments
751: * @throws RAConfigException if an Exception occurs
752: */
753: public static void main(String[] args) throws RAConfigException {
754: boolean isHelp = false;
755: boolean isVerbose = false;
756: boolean doProperty = false;
757: boolean doUpdate = false;
758: boolean encrypt = false;
759: boolean newXml = false;
760: String fileInputName = null;
761: String fileOutputName = null;
762: String securityfile = null;
763: String jndiname = "";
764: String rarlink = "";
765: String updatename = "";
766: String jdbcRarLink = "";
767: String jonasRarStr = "JOnASJDBC_";
768: String propertyFile = "";
769: String path = "";
770: // Get command args
771:
772: Properties dbProps = null;
773:
774: if (args.length < 1) {
775: isHelp = true;
776: }
777: for (int argn = 0; argn < args.length; argn++) {
778: String arg = args[argn].toLowerCase();
779: if (arg.equals("-help") || arg.equals("-?")) {
780: isHelp = true;
781: continue;
782: }
783: if (arg.equals("-verbose") || arg.equals("-v")) {
784: isVerbose = true;
785: continue;
786: }
787: if (arg.equals("-encrypt") || arg.equals("-en")) {
788: encrypt = true;
789: continue;
790: }
791: if (arg.equals("-securityfile") || arg.equals("-sf")) {
792: if (argn + 1 == args.length
793: || args[++argn].startsWith("-")) {
794: usage();
795: throw new RAConfigException("Error with -sf input");
796: }
797: securityfile = args[argn];
798: continue;
799: }
800: if (arg.equals("-novalidation") || arg.equals("-nv")) {
801: validation = false;
802: continue;
803: }
804: if (arg.equals("-update") || arg.equals("-u")) {
805: doUpdate = true;
806: if (argn + 1 == args.length
807: || args[++argn].startsWith("-")) {
808: usage();
809: throw new RAConfigException("Error with -u input");
810: }
811: updatename = args[argn];
812: continue;
813: }
814: if (arg.equals("-jndiname") || arg.equals("-j")) {
815: if (argn + 1 == args.length
816: || args[++argn].startsWith("-")) {
817: usage();
818: throw new RAConfigException("Error with -j input");
819: }
820: jndiname = args[argn];
821: continue;
822: }
823: if (arg.equals("-rarlink") || arg.equals("-r")) {
824: if (argn + 1 == args.length
825: || args[++argn].startsWith("-")) {
826: usage();
827: throw new RAConfigException("Error with -r input");
828: }
829: rarlink = args[argn];
830: continue;
831: }
832: if (arg.equals("-property") || arg.equals("-p")) {
833: doProperty = true;
834: if (argn + 1 == args.length
835: || args[++argn].startsWith("-")) {
836: usage();
837: throw new RAConfigException("Error with -p input");
838: }
839: propertyFile = args[argn];
840: continue;
841: }
842: if (arg.equals("-dm")) {
843: jdbcRarLink = jonasRarStr + "DM";
844: continue;
845: }
846: if (arg.equals("-ds")) {
847: jdbcRarLink = jonasRarStr + "DS";
848: continue;
849: }
850: if (arg.equals("-xa")) {
851: jdbcRarLink = jonasRarStr + "XA";
852: continue;
853: }
854: if (arg.equals("-pc")) {
855: jdbcRarLink = jonasRarStr + "PC";
856: continue;
857: }
858: if (arg.equals("-path")) {
859: if (argn + 1 == args.length
860: || args[++argn].startsWith("-")) {
861: usage();
862: throw new RAConfigException("Error with -path");
863: }
864: path = args[argn];
865: continue;
866: }
867: if (arg.equals("-new")) {
868: newXml = true;
869: continue;
870: }
871: if (fileInputName == null) {
872: fileInputName = args[argn];
873: if (!fileInputName.endsWith(".rar")) {
874: fileInputName += ".rar";
875: }
876: } else if (doProperty || doUpdate) {
877: if (fileOutputName == null) {
878: fileOutputName = args[argn];
879: if (!fileOutputName.endsWith(".rar")) {
880: fileOutputName += ".rar";
881: }
882: }
883: } else {
884: usage();
885: throw new RAConfigException(
886: "Error multiple input files specified without -property or -update");
887: }
888: }
889:
890: // Usage ?
891: if (isHelp) {
892: usage();
893: System.exit(0);
894: }
895: if (fileInputName == null) {
896: usage();
897: throw new RAConfigException("Error missing input file");
898: }
899: if (doProperty && fileOutputName == null) {
900: usage();
901: throw new RAConfigException("Error missing output file");
902: }
903: if (fileInputName.equals(fileOutputName)) {
904: throw new RAConfigException(
905: "Input and Output filenames cannot be the same");
906: }
907:
908: dbProps = new Properties();
909: if (doProperty) {
910: if (jdbcRarLink.length() > 0 && rarlink.length() == 0) {
911: rarlink = jdbcRarLink;
912: }
913: try {
914: dbProps.load(new FileInputStream(propertyFile));
915: } catch (Exception ex) {
916: if (propertyFile.endsWith(".properties")) {
917: System.out.println("Error reading " + propertyFile
918: + " " + ex);
919: } else {
920: try {
921: dbProps.load(new FileInputStream(propertyFile
922: + ".properties"));
923: } catch (Exception ex1) {
924: System.out.println("Error reading "
925: + propertyFile + " " + ex);
926: }
927: }
928: }
929: buildXML(fileInputName, jndiname, rarlink, dbProps,
930: encrypt, securityfile, path, isVerbose, true);
931: if (updatename.length() == 0) {
932: updatename = System.getProperty("java.io.tmpdir")
933: + File.separator + "jonas-ra.xml";
934: }
935: updateRAR(fileInputName, fileOutputName, updatename,
936: rarlink);
937: } else if (doUpdate) {
938: updateRAR(fileInputName, fileOutputName, updatename,
939: rarlink);
940: } else {
941: buildXML(fileInputName, jndiname, rarlink, dbProps,
942: encrypt, securityfile, path, isVerbose, newXml);
943: }
944: }
945: }
|