KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > tools > CheckEnv


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: CheckEnv.java,v 1.23 2005/03/07 14:36:19 pelletib Exp $
26  * --------------------------------------------------------------------------
27  */

28
29
30 package org.objectweb.jonas.tools;
31
32
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40
41 import org.objectweb.jonas.common.JProp;
42
43 /**
44  * This class allows to check if the environment is correct for JOnAS.
45  */

46
47 public class CheckEnv {
48
49     /**
50      * CheckEnv allows to check if the JOnAS environment is correct or not.
51      * <p>
52      * Usage: java org.objectweb.jonas.tools.CheckEnv -help
53      * <br>
54      * to print this help message
55      * <p>
56      * or java org.objectweb.jonas.tools.CheckEnv
57      * <br>
58      * to check if the JOnAS environment is correct or not,
59      * and to display the content of the configuration files.
60      */

61
62     /**
63      * Constant result of JOnAS's conf check [OK]
64      */

65     public static final int ENV_OK = 0;
66
67     /**
68      * Constant result of JOnAS's conf check [Warning]
69      */

70     public static final int ENV_WARNING = 1;
71
72     /**
73      * Constant result of JOnAS's conf check [Error]
74      */

75     public static final int ENV_ERROR = 2;
76
77     /**
78      * Constant esult of JOnAS's conf check [Unknown]
79      */

80     private static final int ENV_UNKNOWN = -1;
81
82     /**
83      * Current status of the checking
84      */

85     private static int envStatus = ENV_UNKNOWN;
86
87     /**
88      * Constant JONAS_ROOT
89      */

90     private static final String JavaDoc INSTALL_ROOT = "install.root";
91
92     /**
93      * Constant JONAS_BASE
94      */

95     private static final String JavaDoc JONAS_BASE = "jonas.base";
96
97     /**
98      * Constant java.naming.provider.url
99      */

100     private static final String JavaDoc JAVA_NAMING_PROVIDER_URL = "java.naming.provider.url";
101
102     /**
103      * Constant dbm service name
104      */

105     private static final String JavaDoc DBM_SERVICE = "dbm";
106
107     /**
108      * Constant jms service name
109      */

110     private static final String JavaDoc JMS_SERVICE = "jms";
111
112     /**
113      * Constant resource service name
114      */

115     private static final String JavaDoc RESOURCE_SERVICE = "resource";
116
117     /**
118      * Constant joram's rar name
119      */

120     private static final String JavaDoc JORAM_RAR = "joram_for_jonas_ra";
121
122     /**
123      * Constant dbms's datasource list in jonas.properties
124      */

125     private static final String JavaDoc DATASOURCES = "jonas.service.dbm.datasources";
126
127     /**
128      * Constant datasource class name
129      */

130     private static final String JavaDoc DATASOURCE_CLASS_NAME = "datasource.classname";
131
132     /**
133      * Variable classes to check
134      */

135     private static Hashtable JavaDoc classesToCheck = new Hashtable JavaDoc();
136
137     /**
138      * Init classes to check
139      */

140     static {
141         classesToCheck.put("javax.ejb.EnterpriseBean", "ejb-2_1-api.jar");
142         classesToCheck.put("javax.sql.DataSource", "jdbc2_0-stdext.jar");
143         classesToCheck.put("javax.transaction.UserTransaction", "jta-spec1_0_1.jar");
144         classesToCheck.put("org.apache.xerces.parsers.SAXParser", "xerces.jar");
145         classesToCheck.put("javax.jms.Message", "jms.jar");
146         classesToCheck.put("org.apache.regexp.REUtil", "jakarta-regexp-1.2.jar");
147         classesToCheck.put("org.objectweb.joram.mom.dest.Topic", "joram-mom.jar");
148         classesToCheck.put("org.objectweb.transaction.jta.TMService", "jotm.jar");
149     }
150
151     /**
152      * Constructor
153      */

154     private CheckEnv() {
155     }
156
157     /**
158      * check the JOnAS's configuration
159      * @return status of the configuration
160      */

161     public static int getStatus() {
162
163         Properties JavaDoc props = null;
164         envStatus = ENV_OK;
165         int res;
166
167         // Check classes
168
Enumeration JavaDoc cList = classesToCheck.keys();
169         while (cList.hasMoreElements()) {
170             String JavaDoc cname = (String JavaDoc) cList.nextElement();
171             if (!classIsAccessible(cname)) {
172                 System.err.println("JOnAS environment ERROR: Classes of '"
173                                    + classesToCheck.get(cname) + "' not accessible");
174                 System.err.println("(The JOnAS package is bad)");
175                 envStatus = ENV_ERROR;
176             }
177         }
178
179         // Check the 'install.root' system property
180
// (JONAS_ROOT variable environment)
181
try {
182             String JavaDoc sRoot = System.getProperty(INSTALL_ROOT);
183             if (sRoot == null) {
184                 System.err.println("JOnAS environment ERROR: '"
185                                    + INSTALL_ROOT + "' system property not defined");
186                 envStatus = ENV_ERROR;
187             } else {
188                 File JavaDoc fRoot = new File JavaDoc(sRoot);
189                 if (!fRoot.isDirectory()) {
190                     System.err.println("JOnAS environment ERROR: Invalid 'JONAS_ROOT' value");
191                     System.err.println(" '" + sRoot + "' directory not exist");
192                     envStatus = ENV_ERROR;
193                 } else {
194                     System.out.println("");
195                     System.out.println("- JONAS_ROOT value: ");
196                     System.out.println(" " + sRoot);
197                 }
198             }
199         } catch (SecurityException JavaDoc e) {
200             System.err.println("CheckEnv ERROR: Cannot get the '"
201                                + INSTALL_ROOT + "' system property (" + e + ")");
202             envStatus = ENV_ERROR;
203         }
204
205         // Check the 'jonas.base' system property
206
// (JONAS_BASE variable environment)
207
try {
208             String JavaDoc sBase = System.getProperty(JONAS_BASE);
209             if (sBase == null) {
210                 System.err.println("JOnAS environment ERROR: '"
211                                    + JONAS_BASE + "' environment property not defined");
212                 envStatus = ENV_ERROR;
213             } else {
214                 File JavaDoc fBase = new File JavaDoc(sBase);
215                 if (!fBase.isDirectory()) {
216                     System.err.println("JOnAS environment ERROR: Invalid 'JOAS_BASE' value");
217                     System.err.println(" '" + sBase + "' directory not exist");
218                     envStatus = ENV_ERROR;
219                 } else {
220                     System.out.println("");
221                     System.out.println("- JONAS_BASE value: ");
222                     System.out.println(" " + sBase);
223                 }
224             }
225         } catch (SecurityException JavaDoc e) {
226             System.err.println("CheckEnv ERROR: Cannot get the '"
227                                + JONAS_BASE + "' system property (" + e + ")");
228             envStatus = ENV_ERROR;
229         }
230
231         // Check jonas.properties and <datasource>.properties
232
if ((res = checkJonasProperties()) != ENV_OK) {
233             envStatus = res;
234         }
235
236         // Check trace.properties
237
if (getProperties("trace.properties") == null) {
238             System.err.println("JOnAS environment ERROR: 'trace.properties' not accessible");
239             envStatus = ENV_ERROR;
240         }
241
242         // Check carol.properties
243
if (getProperties("carol.properties") == null) {
244             System.err.println("JOnAS environment ERROR: 'carol.properties' not accessible");
245             envStatus = ENV_ERROR;
246         }
247
248         // Check jonas-realm.xml file
249
if ((res = checkRealm()) != ENV_OK) {
250             envStatus = res;
251         }
252         // Check presence of jms svc without JORAM's rar or
253
// presence of Joram's rar without jms svc
254
if ((res = checkJmsJoramRar()) != ENV_OK) {
255             envStatus = res;
256         }
257
258         return envStatus;
259     }
260
261     /**
262      * entry point of the program
263      * @param args arguments of the program
264      */

265     public static void main(String JavaDoc[] args) {
266         boolean isHelp = false;
267         boolean isVerbose = true;
268         // Get args
269
for (int argn = 0; argn < args.length; argn++) {
270             String JavaDoc arg = args[argn];
271             if (arg.equals("-help") || arg.equals("-?")) {
272                 isHelp = true;
273                 continue;
274             }
275         }
276         // Usage ?
277
if (isHelp) {
278             usage();
279             System.exit(0);
280         }
281
282         // Status
283
int status = getStatus();
284         System.out.println("");
285         if (status == ENV_OK) {
286             System.err.println("The JOnAS environment seems correct.");
287             System.exit(0);
288         } else if (status == ENV_WARNING) {
289             System.err.println("WARNING: The JOnAS environment may be incomplete.");
290             System.exit(1);
291         } else if (status == ENV_ERROR) {
292             System.err.println("ERROR: The JOnAS environment is NOT correct.");
293             System.exit(2);
294         }
295     }
296
297     /**
298      * check if the class is accessible
299      * @param name class name
300      * @return true if class is accessible, false else
301      */

302     static boolean classIsAccessible(String JavaDoc name) {
303         try {
304             Class.forName(name);
305             return true;
306         } catch (Exception JavaDoc e) {
307             return false;
308         } catch (Error JavaDoc e) {
309             System.err.println(e);
310             return false;
311         }
312     }
313
314     // Return null if properties are not accessible
315
/**
316      * get properties from a file
317      * @param name file containing properties list
318      * @return properties list
319      */

320     static Properties JavaDoc getProperties(String JavaDoc name) {
321         boolean isAccessible = true;
322         Properties JavaDoc props = null;
323         InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
324         if (is != null) {
325             props = new Properties JavaDoc();
326             try {
327                 props.load(is);
328                 System.out.println("");
329                 System.out.println("- Contents of '" + name + "':");
330                 if (props.isEmpty()) {
331                     System.out.println("JOnAS environment ERROR :'" + name + "' is empty");
332                     envStatus = ENV_ERROR;
333                 } else {
334                     System.out.println(" (" + Thread.currentThread().getContextClassLoader().getResource(name).toString() + ")");
335                     Enumeration JavaDoc pList = props.propertyNames();
336                     while (pList.hasMoreElements()) {
337                         String JavaDoc pname = (String JavaDoc) pList.nextElement();
338                         System.out.println(" " + pname + " = " + props.getProperty(pname));
339                     }
340                 }
341             } catch (IOException JavaDoc e) {
342                 props = null;
343             }
344         } else {
345             props = null;
346         }
347         return props;
348     }
349
350     /**
351      * print program usage
352      */

353     private static void usage() {
354         System.out.println("");
355         System.out.println("Usage: CheckEnv [ -help | -? ]");
356         System.out.println(" to print this help message");
357         System.out.println("");
358         System.out.println("or CheckEnv");
359         System.out.println(" to check if the JOnAS environment is correct or not,");
360         System.out.println(" and to display the content of the configuration files.");
361         System.out.println("");
362     }
363     /**
364      * Check jonas's Realm
365      * @return : result
366      */

367     private static int checkJonasProperties() {
368         // Check jonas.properties and <datasource>.properties
369
try {
370             // Check jonas.properties
371
boolean isServiceDbm = false;
372             JProp jonasProps = JProp.getInstance();
373
374             System.out.println("");
375             System.out.println("- JOnAS Services:");
376             System.out.println(" " + jonasProps.getValue("jonas.services", "none"));
377
378             String JavaDoc[] services = jonasProps.getValueAsArray("jonas.services");
379
380             if (services != null) {
381                 for (int i = 0; i < services.length; i++) {
382                     if (DBM_SERVICE.equals(services[i])) {
383                         isServiceDbm = true;
384                     }
385                     String JavaDoc serviceClass = jonasProps.getValue("jonas.service." + services[i] + ".class");
386                     if (serviceClass == null) {
387                         System.err.println("JOnAS environment ERROR: 'jonas.service." + services[i] + ".class' property not defined");
388                         return ENV_ERROR;
389                     } else {
390                         if (!classIsAccessible(serviceClass)) {
391                             System.err.println("JOnAS environment ERROR: '" + serviceClass + "' class not accessible ('" + services[i] + "' service class)");
392                             return ENV_ERROR;
393                         }
394                     }
395                 }
396             } else {
397                 System.err.println("JOnAS environment ERROR: jonas.services not defined in jonas.properties");
398                 return ENV_ERROR;
399             }
400
401             System.out.println("");
402             System.out.println("- Contents of 'jonas.properties':");
403             String JavaDoc jonasContent = jonasProps.toString();
404             if (jonasContent.length() > 0) {
405                 System.out.println(jonasProps.toString());
406             } else {
407                 System.err.println("JOnAS environment ERROR : jonas.properties empty");
408                 return ENV_ERROR;
409             }
410             if (isServiceDbm) {
411                 // Check <datasource>.properties
412
String JavaDoc dsList = jonasProps.getValue(DATASOURCES, "");
413                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(dsList, ",");
414                 String JavaDoc dsName = null;
415                 while (st.hasMoreTokens()) {
416                     try {
417                         dsName = st.nextToken().trim();
418                         JProp dsProps = JProp.getInstance(dsName);
419                         System.out.println("");
420                         System.out.println("- Contents of '" + dsName + ".properties':");
421                         System.out.println(dsProps.toString());
422                         String JavaDoc driverName = dsProps.getValue(DATASOURCE_CLASS_NAME, "");
423                         if (!classIsAccessible(driverName)) {
424                             System.err.println("JOnAS environment ERROR: '" + driverName + "' class not accessible ('" + dsName + "' datasource's driver)");
425                             return ENV_ERROR;
426                         }
427                     } catch (Exception JavaDoc e) {
428                         System.err.println("JOnAS environment ERROR: '" + dsName + ".properties' "
429                                 + "not available in JONAS_BASE/conf");
430                         return ENV_ERROR;
431                     }
432                 }
433             }
434         } catch (Exception JavaDoc e) {
435             System.err.println("JOnAS environment ERROR: 'jonas.properties' not accessible (" + e + ")");
436             return ENV_ERROR;
437         }
438         return ENV_OK;
439     }
440
441     /**
442      * Check jonas's Realm
443      * @return : result
444      */

445     private static int checkRealm() {
446         System.out.println("\n- Check 'jonas-realm.xml':");
447
448         try {
449             JProp jProp = JProp.getInstance();
450             String JavaDoc jonasBase = JProp.getJonasBase();
451             File JavaDoc f = new File JavaDoc(jonasBase + File.separator + "conf" + File.separator + "jonas-realm.xml");
452             if (!f.exists()) {
453                 System.err.println("The file jonas-realm.xml was not found in your JONAS_BASE/conf directory");
454                 return ENV_ERROR;
455             } else {
456                 System.out.println(" File is present.");
457                 return ENV_OK;
458             }
459         } catch (Exception JavaDoc fe) {
460             System.err.println("Error while checking the jonas-realm.xml file : " + fe.getMessage());
461             return ENV_ERROR;
462         }
463     }
464
465     /**
466      * Check not presence both of jms service and joram's rar
467      * @return : result
468      */

469     private static int checkJmsJoramRar() {
470         boolean isServiceJms = false;
471         boolean isServiceResource = false;
472         boolean isJoramRarAutoloadedFromJprop = false;
473         boolean isJoramRarAutoloadedFromAutoloadDir = false;
474         JProp jonasProps = null;
475
476         System.out.println("\n- Check 'not presence both joram rar and jms service':");
477
478         try {
479             jonasProps = JProp.getInstance();
480         } catch (Exception JavaDoc e) {
481             System.err.println("JOnAS environment ERROR: 'jonas.properties' not accessible (" + e + ")");
482             return ENV_ERROR;
483         }
484
485         try {
486             String JavaDoc[] services = jonasProps.getValueAsArray("jonas.services");
487
488             if (services != null) {
489                 for (int i = 0; i < services.length; i++) {
490                     if (JMS_SERVICE.equals(services[i])) {
491                         isServiceJms = true;
492                     }
493                     if (RESOURCE_SERVICE.equals(services[i])) {
494                         isServiceResource = true;
495                     }
496                 }
497             } else {
498                 System.err.println("JOnAS environment ERROR: jonas.services not defined in " + "jonas.properties");
499                 return ENV_ERROR;
500             }
501         } catch (Exception JavaDoc e) {
502             System.err.println("JOnAS environment ERROR: 'jonas.properties' not accessible (" + e + ")");
503             return ENV_ERROR;
504         }
505         try {
506             String JavaDoc[] resources = jonasProps.getValueAsArray("jonas.service.resource.resources");
507             if (resources != null) {
508                 for (int i = 0; i < resources.length; i++) {
509                     if (JORAM_RAR.equals(resources[i])) {
510                         isJoramRarAutoloadedFromJprop = true;
511                     }
512                     if ((JORAM_RAR + ".rar").equals(resources[i])) {
513                         isJoramRarAutoloadedFromJprop = true;
514                     }
515                 }
516             } else {
517                 System.err.println("JOnAS environment ERROR: jonas.service.resource.resources not defined in "
518                         + "jonas.properties");
519                 return ENV_ERROR;
520             }
521
522         } catch (Exception JavaDoc e) {
523             System.err
524                     .println("JOnAS environment ERROR: 'jonas.service.resource.resources' not accessible (" + e + ")");
525             return ENV_ERROR;
526         }
527         String JavaDoc[] resourcesAutoloadDir = null;
528         try {
529             resourcesAutoloadDir = jonasProps.getValueAsArray("jonas.service.resource.autoloaddir");
530         } catch (Exception JavaDoc e) {
531             System.err.println("JOnAS environment ERROR: 'jonas.service.resource.autoloaddir' not accessible (" + e
532                     + ")");
533             return ENV_ERROR;
534         }
535         String JavaDoc jonasBase = JProp.getJonasBase();
536         if (resourcesAutoloadDir != null) {
537             for (int i = 0; i < resourcesAutoloadDir.length; i++) {
538                 File JavaDoc dir = new File JavaDoc(jonasBase + File.separator + "rars" + File.separator + resourcesAutoloadDir[i]);
539                 if (!dir.exists() || !dir.isDirectory()) {
540                     System.err.println("JOnAS environment ERROR: 'resource service configuration error' - "
541                             + dir.getName() + " access error");
542                     return ENV_ERROR;
543                 }
544                 File JavaDoc[] files = dir.listFiles();
545                 for (int j = 0; j < files.length; j++) {
546                     if ((JORAM_RAR + ".rar").equals(files[j].getName())) {
547                         isJoramRarAutoloadedFromAutoloadDir = true;
548                     }
549                 }
550             }
551         } else {
552             System.err.println("JOnAS environment ERROR: jonas.service.resource.autoloaddir not defined in "
553                     + "jonas.properties");
554             return ENV_ERROR;
555         }
556
557         if (isJoramRarAutoloadedFromJprop && isJoramRarAutoloadedFromAutoloadDir) {
558             System.err
559                     .println("JOnAS environment ERROR: joram rar loaded twice in conf : jonas.properties and resource.autoloaddir");
560             return ENV_ERROR;
561         }
562
563         if (isServiceJms && isServiceResource && (isJoramRarAutoloadedFromJprop || isJoramRarAutoloadedFromAutoloadDir)) {
564             System.err.println("JOnAS environment ERROR: joram rar and jms service must be exclusive'");
565             return ENV_ERROR;
566         }
567         System.out.println("Ok");
568
569         return ENV_OK;
570     }
571 }
572
Popular Tags