CheckEnv.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999-2004 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): ____________________________________.
00022  * Contributor(s): ______________________________________.
00023  *
00024  * --------------------------------------------------------------------------
00025  * $Id: CheckEnv.java,v 1.22 2004/03/24 15:11:23 benoitf Exp $
00026  * --------------------------------------------------------------------------
00027  */
00028 
00029 
00030 package org.objectweb.jonas.tools;
00031 
00032 
00033 import java.io.File;
00034 import java.io.IOException;
00035 import java.io.InputStream;
00036 import java.util.Enumeration;
00037 import java.util.Hashtable;
00038 import java.util.Properties;
00039 import java.util.StringTokenizer;
00040 
00041 import org.objectweb.jonas.common.JProp;
00042 
00047 public class CheckEnv {
00048 
00062     public  static final int ENV_OK      = 0;
00063     public  static final int ENV_WARNING = 1;
00064     public  static final int ENV_ERROR   = 2;
00065     private static final int ENV_UNKNOWN = -1;
00066     private static int envStatus = ENV_UNKNOWN;
00067 
00068     private static final String INSTALL_ROOT = "install.root";
00069     private static final String JONAS_BASE   = "jonas.base";
00070     private static final String JAVA_NAMING_PROVIDER_URL = "java.naming.provider.url";
00071     private static final String DBM_SERVICE = "dbm";
00072     private static final String DATASOURCES = "jonas.service.dbm.datasources";
00073     private static final String DATASOURCE_CLASS_NAME = "datasource.classname";
00074 
00075     private static Hashtable classesToCheck = new Hashtable();
00076     static {
00077         classesToCheck.put("javax.ejb.EnterpriseBean", "ejb-2_1-api.jar");
00078         classesToCheck.put("javax.sql.DataSource", "jdbc2_0-stdext.jar");
00079         classesToCheck.put("javax.transaction.UserTransaction", "jta-spec1_0_1.jar");
00080         classesToCheck.put("org.apache.xerces.parsers.SAXParser", "xerces.jar");
00081         classesToCheck.put("javax.jms.Message", "jms.jar");
00082         classesToCheck.put("org.apache.regexp.REUtil", "jakarta-regexp-1.2.jar");
00083         classesToCheck.put("org.objectweb.joram.mom.dest.Topic", "joram-mom.jar");
00084         classesToCheck.put("org.objectweb.transaction.jta.TMService", "jotm.jar");
00085     }
00086 
00087     public static int getStatus() {
00088 
00089         Properties props = null;
00090         envStatus = ENV_OK;
00091 
00092         // Check classes
00093         Enumeration cList = classesToCheck.keys();
00094         while (cList.hasMoreElements()) {
00095             String cname = (String) cList.nextElement();
00096             if (!classIsAccessible(cname)) {
00097                 System.err.println("JOnAS environment ERROR: Classes of '"
00098                                    + classesToCheck.get(cname) + "' not accessible");
00099                 System.err.println("(The JOnAS package is bad)");
00100                 envStatus = ENV_ERROR;
00101             }
00102         }
00103 
00104         // Check the 'install.root' system property
00105         // (JONAS_ROOT variable environment)
00106         try {
00107             String sRoot = System.getProperty(INSTALL_ROOT);
00108             if (sRoot == null) {
00109                 System.err.println("JOnAS environment ERROR: '"
00110                                    + INSTALL_ROOT + "' system property not defined");
00111                 envStatus = ENV_ERROR;
00112             } else {
00113                 File fRoot = new File(sRoot);
00114                 if (!fRoot.isDirectory()) {
00115                     System.err.println("JOnAS environment ERROR: Invalid 'JONAS_ROOT' value");
00116                     System.err.println("                         '" + sRoot + "' directory not exist");
00117                     envStatus = ENV_ERROR;
00118                 } else {
00119                     System.out.println("");
00120                     System.out.println("- JONAS_ROOT value: ");
00121                     System.out.println("   " + sRoot);
00122                 }
00123             }
00124         } catch (SecurityException e) {
00125             System.err.println("CheckEnv ERROR: Cannot get the '"
00126                                + INSTALL_ROOT + "' system property (" + e + ")");
00127             envStatus = ENV_ERROR;
00128         }
00129 
00130         // Check the 'jonas.base' system property
00131         // (JONAS_BASE variable environment)
00132         try {
00133             String sBase = System.getProperty(JONAS_BASE);
00134             if (sBase == null) {
00135                 System.err.println("JOnAS environment ERROR: '"
00136                                    + JONAS_BASE + "' environment property not defined");
00137                 envStatus = ENV_ERROR;
00138             } else {
00139                 File fBase = new File(sBase);
00140                 if (!fBase.isDirectory()) {
00141                     System.err.println("JOnAS environment ERROR: Invalid 'JOAS_BASE' value");
00142                     System.err.println("                         '" + sBase + "' directory not exist");
00143                     envStatus = ENV_ERROR;
00144                 } else {
00145                     System.out.println("");
00146                     System.out.println("- JONAS_BASE value: ");
00147                     System.out.println("   " + sBase);
00148                 }
00149             }
00150         } catch (SecurityException e) {
00151             System.err.println("CheckEnv ERROR: Cannot get the '"
00152                                + JONAS_BASE + "' system property (" + e + ")");
00153             envStatus = ENV_ERROR;
00154         }
00155 
00156         // Check jonas.properties and <datasource>.properties
00157         try {
00158             // Check jonas.properties
00159             boolean isServiceDbm = false;
00160             JProp jonasProps = JProp.getInstance();
00161  
00162             System.out.println("");
00163             System.out.println("- JOnAS Services:");
00164             System.out.println("   " + jonasProps.getValue("jonas.services", "none"));
00165 
00166             String[] services = jonasProps.getValueAsArray("jonas.services");
00167            
00168             if (services != null) {
00169                 for (int i = 0; i < services.length; i++) {
00170                     if (DBM_SERVICE.equals(services[i])) {
00171                         isServiceDbm = true;
00172                     }
00173                     String serviceClass = jonasProps.getValue("jonas.service." + services[i] + ".class");
00174                     if (serviceClass == null) {
00175                         System.err.println("JOnAS environment ERROR: 'jonas.service."
00176                                            + services[i] + ".class' property not defined");
00177                         envStatus = ENV_ERROR;
00178                     } else {
00179                         if (!classIsAccessible(serviceClass)) {
00180                             System.err.println("JOnAS environment ERROR: '"
00181                                                + serviceClass + "' class not accessible ('"
00182                                                + services[i] + "' service class)");
00183                             envStatus = ENV_ERROR;
00184                         } 
00185                     }
00186                 } 
00187             } else {
00188                 envStatus = ENV_ERROR;
00189                 System.err.println("JOnAS environment ERROR: jonas.services not defined in " 
00190                                    + "jonas.properties");
00191             }
00192             
00193             System.out.println("");
00194             System.out.println("- Contents of 'jonas.properties':");
00195             String jonasContent = jonasProps.toString();
00196             if (jonasContent.length() > 0) {  
00197                 System.out.println(jonasProps.toString());
00198             } else {
00199                 envStatus = ENV_ERROR;
00200                 System.err.println("JOnAS environment ERROR : jonas.properties empty");
00201             }
00202             if (isServiceDbm) {
00203                 // Check <datasource>.properties
00204                 String dsList = jonasProps.getValue(DATASOURCES, "");
00205                 StringTokenizer st = new StringTokenizer(dsList, ",");
00206                 String dsName = null;
00207                 while (st.hasMoreTokens()) {
00208                     try {
00209                         dsName = st.nextToken().trim();
00210                         JProp dsProps = JProp.getInstance(dsName);
00211                         System.out.println("");
00212                         System.out.println("- Contents of '" + dsName + ".properties':");
00213                         System.out.println(dsProps.toString());
00214                         String driverName = dsProps.getValue(DATASOURCE_CLASS_NAME, "");
00215                         if (!classIsAccessible(driverName)) {
00216                             System.err.println("JOnAS environment ERROR: '"
00217                                                + driverName + "' class not accessible ('"
00218                                                + dsName + "' datasource's driver)");
00219                             envStatus = ENV_ERROR;
00220                         }
00221                     } catch (Exception e) {
00222                         System.err.println("JOnAS environment ERROR: '"
00223                                            + dsName + ".properties' "
00224                                            + "not available in JONAS_BASE/conf");
00225                         envStatus = ENV_ERROR;
00226                     }
00227                 }
00228             }
00229         } catch (Exception e) {
00230             System.err.println("JOnAS environment ERROR: 'jonas.properties' not accessible (" + e + ")");
00231             envStatus = ENV_ERROR;
00232         }
00233 
00234         // Check trace.properties
00235         if (getProperties("trace.properties") == null) {
00236             System.err.println("JOnAS environment ERROR: 'trace.properties' not accessible");
00237             envStatus = ENV_ERROR;
00238         }
00239 
00240         // Check carol.properties
00241         if (getProperties("carol.properties") == null) {
00242             System.err.println("JOnAS environment ERROR: 'carol.properties' not accessible");
00243             envStatus = ENV_ERROR;
00244         }
00245 
00246         // Check jonas-realm.xml file
00247         System.out.println("\n- Check 'jonas-realm.xml':");
00248         try {
00249             JProp jProp = JProp.getInstance();
00250             String jonasBase = JProp.getJonasBase();
00251             File f = new File(jonasBase + File.separator + "conf" + File.separator + "jonas-realm.xml");
00252             if (!f.exists()) {
00253                 System.err.println("The file jonas-realm.xml was not found in your JONAS_BASE/conf directory");
00254                 envStatus = ENV_ERROR;
00255             } else {
00256                 System.out.println("  File is present.");
00257             }
00258         } catch (Exception fe) {
00259             System.err.println("Error while checking the jonas-realm.xml file : " + fe.getMessage());
00260             envStatus = ENV_ERROR;
00261         }
00262 
00263         return envStatus;
00264     }
00265 
00266     public static void main(String args[]) {
00267         boolean isHelp    = false;
00268         boolean isVerbose = true;
00269         // Get args
00270         for (int argn = 0; argn < args.length; argn++) {
00271             String arg = args[argn];
00272             if (arg.equals("-help") || arg.equals("-?")) {
00273                 isHelp = true;
00274                 continue;
00275             }
00276         }
00277         // Usage ?
00278         if (isHelp) {
00279             usage();
00280             System.exit(0);
00281         }
00282 
00283         // Status
00284         int status = getStatus();
00285         System.out.println("");
00286         if (status == ENV_OK) {
00287             System.err.println("The JOnAS environment seems correct.");
00288             System.exit(0);
00289         } else if (status == ENV_WARNING) {
00290             System.err.println("WARNING: The JOnAS environment may be incomplete.");
00291             System.exit(1);
00292         } else if (status == ENV_ERROR) {
00293             System.err.println("ERROR: The JOnAS environment is NOT correct.");
00294             System.exit(2);
00295         }
00296     }
00297 
00298     static boolean classIsAccessible(String name) {
00299         try {
00300             Class.forName(name);
00301             return true;
00302         } catch (Exception e) {
00303             return false;
00304         } catch (Error e) {
00305             System.err.println(e);
00306             return false;
00307         }
00308     }
00309 
00310     // Return null if properties are not accessible
00311     static Properties getProperties(String name) {
00312         boolean isAccessible = true;
00313         Properties props = null;
00314         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
00315         if (is != null) {
00316             props = new Properties();
00317             try {
00318                 props.load(is);
00319                 
00320                 System.out.println("");
00321                 System.out.println("- Contents of '" + name + "':");
00322                 if (props.isEmpty()) {
00323                     System.out.println("JOnAS environment ERROR :'" + name + "' is empty");
00324                     envStatus = ENV_ERROR;
00325                 } else {
00326                     System.out.println("  (" + Thread.currentThread().getContextClassLoader().getResource(name).toString() + ")");
00327                     Enumeration pList = props.propertyNames();
00328                     while (pList.hasMoreElements()) {
00329                         String pname = (String) pList.nextElement();
00330                         System.out.println("   " + pname + " = " + props.getProperty(pname));
00331                     }
00332                 }
00333             } catch (IOException e) {
00334                 props = null;
00335             }
00336         } else {
00337             props = null;
00338         }
00339         return props;
00340     }
00341 
00342     private static void usage() {
00343         System.out.println("");
00344         System.out.println("Usage: CheckEnv [ -help | -? ]");
00345         System.out.println("    to print this help message");
00346         System.out.println("");
00347         System.out.println("or     CheckEnv");
00348         System.out.println("    to check if the JOnAS environment is correct or not,");
00349         System.out.println("    and to display the content of the configuration files.");
00350         System.out.println("");
00351     }
00352 
00353 }
00354 
00355 

Generated on Tue Feb 15 15:05:24 2005 for JOnAS by  doxygen 1.3.9.1