001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 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): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: CheckEnv.java 7697 2005-11-23 16:35:17Z pelletib $
026: * --------------------------------------------------------------------------
027: */
028:
029: package org.objectweb.jonas.tools;
030:
031: import java.io.File;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.util.Enumeration;
035: import java.util.Hashtable;
036: import java.util.Properties;
037: import java.util.StringTokenizer;
038:
039: import org.objectweb.jonas.common.JProp;
040:
041: /**
042: * This class allows to check if the environment is correct for JOnAS.
043: */
044:
045: public class CheckEnv {
046:
047: /**
048: * CheckEnv allows to check if the JOnAS environment is correct or not.
049: * <p>
050: * Usage: java org.objectweb.jonas.tools.CheckEnv -help
051: * <br>
052: * to print this help message
053: * <p>
054: * or java org.objectweb.jonas.tools.CheckEnv
055: * <br>
056: * to check if the JOnAS environment is correct or not,
057: * and to display the content of the configuration files.
058: */
059:
060: /**
061: * Constant result of JOnAS's conf check [OK]
062: */
063: public static final int ENV_OK = 0;
064:
065: /**
066: * Constant result of JOnAS's conf check [Warning]
067: */
068: public static final int ENV_WARNING = 1;
069:
070: /**
071: * Constant result of JOnAS's conf check [Error]
072: */
073: public static final int ENV_ERROR = 2;
074:
075: /**
076: * Constant esult of JOnAS's conf check [Unknown]
077: */
078: private static final int ENV_UNKNOWN = -1;
079:
080: /**
081: * Current status of the checking
082: */
083: private static int envStatus = ENV_UNKNOWN;
084:
085: /**
086: * Constant JONAS_ROOT
087: */
088: private static final String INSTALL_ROOT = "install.root";
089:
090: /**
091: * Constant JONAS_BASE
092: */
093: private static final String JONAS_BASE = "jonas.base";
094:
095: /**
096: * Constant dbm service name
097: */
098: private static final String DBM_SERVICE = "dbm";
099:
100: /**
101: * Constant jms service name
102: */
103: private static final String JMS_SERVICE = "jms";
104:
105: /**
106: * Constant resource service name
107: */
108: private static final String RESOURCE_SERVICE = "resource";
109:
110: /**
111: * Constant joram's rar name
112: */
113: private static final String JORAM_RAR = "joram_for_jonas_ra";
114:
115: /**
116: * JORAM's RAR admin configuration file
117: */
118: private static final String JORAM_RAR_ADMIN_CFG_FILE = "joramAdmin.xml";
119:
120: /**
121: * JORAM's RAR former admin configuration file
122: */
123: private static final String JORAM_RAR_ADMIN_CFG_FILE_DEPRECATED = "joram-admin.cfg";
124:
125: /**
126: * Constant dbms's datasource list in jonas.properties
127: */
128: private static final String DATASOURCES = "jonas.service.dbm.datasources";
129:
130: /**
131: * Constant datasource class name
132: */
133: private static final String DATASOURCE_CLASS_NAME = "datasource.classname";
134:
135: /**
136: * Variable classes to check
137: */
138: private static Hashtable classesToCheck = new Hashtable();
139:
140: /**
141: * Init classes to check
142: */
143: static {
144: classesToCheck.put("javax.ejb.EnterpriseBean",
145: "ejb-2_1-api.jar");
146: classesToCheck
147: .put("javax.sql.DataSource", "jdbc2_0-stdext.jar");
148: classesToCheck.put("javax.transaction.UserTransaction",
149: "jta-spec1_0_1.jar");
150: classesToCheck.put("org.apache.xerces.parsers.SAXParser",
151: "xerces.jar");
152: classesToCheck.put("javax.jms.Message", "jms.jar");
153: classesToCheck.put("org.apache.regexp.REUtil",
154: "jakarta-regexp-1.2.jar");
155: classesToCheck.put("org.objectweb.joram.mom.dest.Topic",
156: "joram-mom.jar");
157: classesToCheck.put("org.objectweb.transaction.jta.TMService",
158: "jotm.jar");
159: }
160:
161: /**
162: * Constructor
163: */
164: private CheckEnv() {
165: }
166:
167: /**
168: * check the JOnAS's configuration
169: * @return status of the configuration
170: */
171: public static int getStatus() {
172:
173: envStatus = ENV_OK;
174: int res;
175:
176: // Check classes
177: Enumeration cList = classesToCheck.keys();
178: while (cList.hasMoreElements()) {
179: String cname = (String) cList.nextElement();
180: if (!classIsAccessible(cname)) {
181: System.err
182: .println("JOnAS environment ERROR: Classes of '"
183: + classesToCheck.get(cname)
184: + "' not accessible");
185: System.err.println("(The JOnAS package is bad)");
186: envStatus = ENV_ERROR;
187: }
188: }
189:
190: // Check the 'install.root' system property
191: // (JONAS_ROOT variable environment)
192: try {
193: String sRoot = System.getProperty(INSTALL_ROOT);
194: if (sRoot == null) {
195: System.err.println("JOnAS environment ERROR: '"
196: + INSTALL_ROOT
197: + "' system property not defined");
198: envStatus = ENV_ERROR;
199: } else {
200: File fRoot = new File(sRoot);
201: if (!fRoot.isDirectory()) {
202: System.err
203: .println("JOnAS environment ERROR: Invalid 'JONAS_ROOT' value");
204: System.err.println(" '"
205: + sRoot + "' directory not exist");
206: envStatus = ENV_ERROR;
207: } else {
208: System.out.println("");
209: System.out.println("- JONAS_ROOT value: ");
210: System.out.println(" " + sRoot);
211: }
212: }
213: } catch (SecurityException e) {
214: System.err.println("CheckEnv ERROR: Cannot get the '"
215: + INSTALL_ROOT + "' system property (" + e + ")");
216: envStatus = ENV_ERROR;
217: }
218:
219: // Check the 'jonas.base' system property
220: // (JONAS_BASE variable environment)
221: try {
222: String sBase = System.getProperty(JONAS_BASE);
223: if (sBase == null) {
224: System.err.println("JOnAS environment ERROR: '"
225: + JONAS_BASE
226: + "' environment property not defined");
227: envStatus = ENV_ERROR;
228: } else {
229: File fBase = new File(sBase);
230: if (!fBase.isDirectory()) {
231: System.err
232: .println("JOnAS environment ERROR: Invalid 'JOAS_BASE' value");
233: System.err.println(" '"
234: + sBase + "' directory not exist");
235: envStatus = ENV_ERROR;
236: } else {
237: System.out.println("");
238: System.out.println("- JONAS_BASE value: ");
239: System.out.println(" " + sBase);
240: }
241: }
242: } catch (SecurityException e) {
243: System.err.println("CheckEnv ERROR: Cannot get the '"
244: + JONAS_BASE + "' system property (" + e + ")");
245: envStatus = ENV_ERROR;
246: }
247:
248: // Check jonas.properties and <datasource>.properties
249: if ((res = checkJonasProperties()) != ENV_OK) {
250: envStatus = res;
251: }
252:
253: // Check trace.properties
254: if (getProperties("trace.properties") == null) {
255: System.err
256: .println("JOnAS environment ERROR: 'trace.properties' not accessible");
257: envStatus = ENV_ERROR;
258: }
259:
260: // Check carol.properties
261: if (getProperties("carol.properties") == null) {
262: System.err
263: .println("JOnAS environment ERROR: 'carol.properties' not accessible");
264: envStatus = ENV_ERROR;
265: }
266:
267: // Check jonas-realm.xml file
268: if ((res = checkRealm()) != ENV_OK) {
269: envStatus = res;
270: }
271: // Check the JORAM's RA configuration
272: if ((res = checkJoramRar()) != ENV_OK) {
273: envStatus = res;
274: }
275:
276: return envStatus;
277: }
278:
279: /**
280: * entry point of the program
281: * @param args arguments of the program
282: */
283: public static void main(String[] args) {
284: boolean isHelp = false;
285: // Get args
286: for (int argn = 0; argn < args.length; argn++) {
287: String arg = args[argn];
288: if (arg.equals("-help") || arg.equals("-?")) {
289: isHelp = true;
290: continue;
291: }
292: }
293: // Usage ?
294: if (isHelp) {
295: usage();
296: System.exit(0);
297: }
298:
299: // Status
300: int status = getStatus();
301: System.out.println("");
302: if (status == ENV_OK) {
303: System.err.println("The JOnAS environment seems correct.");
304: System.exit(0);
305: } else if (status == ENV_WARNING) {
306: System.err
307: .println("WARNING: The JOnAS environment may be incomplete.");
308: System.exit(1);
309: } else if (status == ENV_ERROR) {
310: System.err
311: .println("ERROR: The JOnAS environment is NOT correct.");
312: System.exit(2);
313: }
314: }
315:
316: /**
317: * check if the class is accessible
318: * @param name class name
319: * @return true if class is accessible, false else
320: */
321: static boolean classIsAccessible(String name) {
322: try {
323: Class.forName(name);
324: return true;
325: } catch (Exception e) {
326: return false;
327: } catch (Error e) {
328: System.err.println(e);
329: return false;
330: }
331: }
332:
333: // Return null if properties are not accessible
334: /**
335: * get properties from a file
336: * @param name file containing properties list
337: * @return properties list
338: */
339: static Properties getProperties(String name) {
340: Properties props = null;
341: InputStream is = Thread.currentThread().getContextClassLoader()
342: .getResourceAsStream(name);
343: if (is != null) {
344: props = new Properties();
345: try {
346: props.load(is);
347: System.out.println("");
348: System.out.println("- Contents of '" + name + "':");
349: if (props.isEmpty()) {
350: System.out.println("JOnAS environment ERROR :'"
351: + name + "' is empty");
352: envStatus = ENV_ERROR;
353: } else {
354: System.out
355: .println(" ("
356: + Thread.currentThread()
357: .getContextClassLoader()
358: .getResource(name)
359: .toString() + ")");
360: Enumeration pList = props.propertyNames();
361: while (pList.hasMoreElements()) {
362: String pname = (String) pList.nextElement();
363: System.out.println(" " + pname + " = "
364: + props.getProperty(pname));
365: }
366: }
367: } catch (IOException e) {
368: props = null;
369: }
370: } else {
371: props = null;
372: }
373: return props;
374: }
375:
376: /**
377: * print program usage
378: */
379: private static void usage() {
380: System.out.println("");
381: System.out.println("Usage: CheckEnv [ -help | -? ]");
382: System.out.println(" to print this help message");
383: System.out.println("");
384: System.out.println("or CheckEnv");
385: System.out
386: .println(" to check if the JOnAS environment is correct or not,");
387: System.out
388: .println(" and to display the content of the configuration files.");
389: System.out.println("");
390: }
391:
392: /**
393: * Check jonas's Realm
394: * @return : result
395: */
396: private static int checkJonasProperties() {
397: // Check jonas.properties and <datasource>.properties
398: try {
399: // Check jonas.properties
400: boolean isServiceDbm = false;
401: JProp jonasProps = JProp.getInstance();
402:
403: System.out.println("");
404: System.out.println("- JOnAS Services:");
405: System.out.println(" "
406: + jonasProps.getValue("jonas.services", "none"));
407:
408: String[] services = jonasProps
409: .getValueAsArray("jonas.services");
410:
411: if (services != null) {
412: for (int i = 0; i < services.length; i++) {
413: if (DBM_SERVICE.equals(services[i])) {
414: isServiceDbm = true;
415: }
416: String serviceClass = jonasProps
417: .getValue("jonas.service." + services[i]
418: + ".class");
419: if (serviceClass == null) {
420: System.err
421: .println("JOnAS environment ERROR: 'jonas.service."
422: + services[i]
423: + ".class' property not defined");
424: return ENV_ERROR;
425: } else {
426: if (!classIsAccessible(serviceClass)) {
427: System.err
428: .println("JOnAS environment ERROR: '"
429: + serviceClass
430: + "' class not accessible ('"
431: + services[i]
432: + "' service class)");
433: return ENV_ERROR;
434: }
435: }
436: }
437: } else {
438: System.err
439: .println("JOnAS environment ERROR: jonas.services not defined in jonas.properties");
440: return ENV_ERROR;
441: }
442:
443: System.out.println("");
444: System.out.println("- Contents of 'jonas.properties':");
445: String jonasContent = jonasProps.toString();
446: if (jonasContent.length() > 0) {
447: System.out.println(jonasProps.toString());
448: } else {
449: System.err
450: .println("JOnAS environment ERROR : jonas.properties empty");
451: return ENV_ERROR;
452: }
453: if (isServiceDbm) {
454: // Check <datasource>.properties
455: String dsList = jonasProps.getValue(DATASOURCES, "");
456: StringTokenizer st = new StringTokenizer(dsList, ",");
457: String dsName = null;
458: while (st.hasMoreTokens()) {
459: try {
460: dsName = st.nextToken().trim();
461: JProp dsProps = JProp.getInstance(dsName);
462: System.out.println("");
463: System.out.println("- Contents of '" + dsName
464: + ".properties':");
465: System.out.println(dsProps.toString());
466: String driverName = dsProps.getValue(
467: DATASOURCE_CLASS_NAME, "");
468: if (!classIsAccessible(driverName)) {
469: System.err
470: .println("JOnAS environment ERROR: '"
471: + driverName
472: + "' class not accessible ('"
473: + dsName
474: + "' datasource's driver)");
475: return ENV_ERROR;
476: }
477: } catch (Exception e) {
478: System.err.println("JOnAS environment ERROR: '"
479: + dsName + ".properties' "
480: + "not available in JONAS_BASE/conf");
481: return ENV_ERROR;
482: }
483: }
484: }
485: } catch (Exception e) {
486: System.err
487: .println("JOnAS environment ERROR: 'jonas.properties' not accessible ("
488: + e + ")");
489: return ENV_ERROR;
490: }
491: return ENV_OK;
492: }
493:
494: /**
495: * Check jonas's Realm
496: * @return : result
497: */
498: private static int checkRealm() {
499: System.out.println("\n- Check 'jonas-realm.xml':");
500:
501: try {
502: String jonasBase = JProp.getJonasBase();
503: File f = new File(jonasBase + File.separator + "conf"
504: + File.separator + "jonas-realm.xml");
505: if (!f.exists()) {
506: System.err
507: .println("The file jonas-realm.xml was not found in your JONAS_BASE/conf directory");
508: return ENV_ERROR;
509: } else {
510: System.out.println(" File is present.");
511: return ENV_OK;
512: }
513: } catch (Exception fe) {
514: System.err
515: .println("Error while checking the jonas-realm.xml file : "
516: + fe.getMessage());
517: return ENV_ERROR;
518: }
519: }
520:
521: /**
522: * Check not presence both of jms service and joram's rar
523: * @return : result
524: */
525: private static int checkJoramRar() {
526: boolean isServiceJms = false;
527: boolean isServiceResource = false;
528: boolean isJoramRarAutoloadedFromJprop = false;
529: boolean isJoramRarAutoloadedFromAutoloadDir = false;
530: JProp jonasProps = null;
531: String jonasBase = null;
532:
533: System.out.println("\n- Check 'JORAM configuration':");
534:
535: try {
536: jonasProps = JProp.getInstance();
537: } catch (Exception e) {
538: System.err
539: .println("JOnAS environment ERROR: 'jonas.properties' not accessible ("
540: + e + ")");
541: return ENV_ERROR;
542: }
543:
544: try {
545: String[] services = jonasProps
546: .getValueAsArray("jonas.services");
547:
548: if (services != null) {
549: for (int i = 0; i < services.length; i++) {
550: if (JMS_SERVICE.equals(services[i])) {
551: isServiceJms = true;
552: }
553: if (RESOURCE_SERVICE.equals(services[i])) {
554: isServiceResource = true;
555: }
556: }
557: } else {
558: System.err
559: .println("JOnAS environment ERROR: jonas.services not defined in "
560: + "jonas.properties");
561: return ENV_ERROR;
562: }
563: } catch (Exception e) {
564: System.err
565: .println("JOnAS environment ERROR: 'jonas.properties' not accessible ("
566: + e + ")");
567: return ENV_ERROR;
568: }
569: try {
570: String[] resources = jonasProps
571: .getValueAsArray("jonas.service.resource.resources");
572: if (resources != null) {
573: for (int i = 0; i < resources.length; i++) {
574: if (JORAM_RAR.equals(resources[i])) {
575: isJoramRarAutoloadedFromJprop = true;
576: }
577: if ((JORAM_RAR + ".rar").equals(resources[i])) {
578: isJoramRarAutoloadedFromJprop = true;
579: }
580: }
581: } else {
582: System.err
583: .println("JOnAS environment ERROR: jonas.service.resource.resources not defined in "
584: + "jonas.properties");
585: return ENV_ERROR;
586: }
587:
588: } catch (Exception e) {
589: System.err
590: .println("JOnAS environment ERROR: 'jonas.service.resource.resources' not accessible ("
591: + e + ")");
592: return ENV_ERROR;
593: }
594: String[] resourcesAutoloadDir = null;
595: try {
596: resourcesAutoloadDir = jonasProps
597: .getValueAsArray("jonas.service.resource.autoloaddir");
598: } catch (Exception e) {
599: System.err
600: .println("JOnAS environment ERROR: 'jonas.service.resource.autoloaddir' not accessible ("
601: + e + ")");
602: return ENV_ERROR;
603: }
604: jonasBase = JProp.getJonasBase();
605:
606: if (resourcesAutoloadDir != null) {
607: for (int i = 0; i < resourcesAutoloadDir.length; i++) {
608: File dir = new File(jonasBase + File.separator + "rars"
609: + File.separator + resourcesAutoloadDir[i]);
610: if (!dir.exists() || !dir.isDirectory()) {
611: System.err
612: .println("JOnAS environment ERROR: 'resource service configuration error' - "
613: + dir.getName() + " access error");
614: return ENV_ERROR;
615: }
616: File[] files = dir.listFiles();
617: for (int j = 0; j < files.length; j++) {
618: if ((JORAM_RAR + ".rar").equals(files[j].getName())) {
619: isJoramRarAutoloadedFromAutoloadDir = true;
620: }
621: }
622: }
623: } else {
624: System.err
625: .println("JOnAS environment ERROR: jonas.service.resource.autoloaddir not defined in "
626: + "jonas.properties");
627: return ENV_ERROR;
628: }
629:
630: if (isJoramRarAutoloadedFromJprop
631: && isJoramRarAutoloadedFromAutoloadDir) {
632: System.err
633: .println("JOnAS environment ERROR: joram rar loaded twice in conf : jonas.properties and resource.autoloaddir");
634: return ENV_ERROR;
635: }
636:
637: if (isServiceJms
638: && isServiceResource
639: && (isJoramRarAutoloadedFromJprop || isJoramRarAutoloadedFromAutoloadDir)) {
640: System.err
641: .println("JOnAS environment ERROR: joram rar and jms service must be exclusive'");
642: return ENV_ERROR;
643: }
644:
645: // Check presence of joramAdmin.xml and not presence of joram-admin.cfg
646: if (isServiceResource) {
647: try {
648: File NewJoramAdminCfgFile = new File(jonasBase
649: + File.separator + "conf" + File.separator
650: + JORAM_RAR_ADMIN_CFG_FILE);
651: File DepJoramAdminCfgFile = new File(jonasBase
652: + File.separator + "conf" + File.separator
653: + JORAM_RAR_ADMIN_CFG_FILE_DEPRECATED);
654:
655: if (!NewJoramAdminCfgFile.exists()) {
656: System.err
657: .println("The file "
658: + JORAM_RAR_ADMIN_CFG_FILE
659: + " was not found in your JONAS_BASE/conf directory");
660: return ENV_ERROR;
661: }
662: if (DepJoramAdminCfgFile.exists()) {
663: System.err
664: .println("The file "
665: + JORAM_RAR_ADMIN_CFG_FILE_DEPRECATED
666: + " was found in your JONAS_BASE/conf directory and may cause confusing with the "
667: + JORAM_RAR_ADMIN_CFG_FILE
668: + "file.");
669: return ENV_ERROR;
670: }
671: } catch (Exception fe) {
672: System.err
673: .println("Error while checking the JORAM RA's configuration files : "
674: + fe.getMessage());
675: return ENV_ERROR;
676: }
677: }
678:
679: System.out.println("Ok");
680:
681: return ENV_OK;
682: }
683: }
|