1 28 29 30 package org.objectweb.jonas.tools; 31 32 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.util.Enumeration ; 37 import java.util.Hashtable ; 38 import java.util.Properties ; 39 import java.util.StringTokenizer ; 40 41 import org.objectweb.jonas.common.JProp; 42 43 46 47 public class CheckEnv { 48 49 61 62 65 public static final int ENV_OK = 0; 66 67 70 public static final int ENV_WARNING = 1; 71 72 75 public static final int ENV_ERROR = 2; 76 77 80 private static final int ENV_UNKNOWN = -1; 81 82 85 private static int envStatus = ENV_UNKNOWN; 86 87 90 private static final String INSTALL_ROOT = "install.root"; 91 92 95 private static final String JONAS_BASE = "jonas.base"; 96 97 100 private static final String JAVA_NAMING_PROVIDER_URL = "java.naming.provider.url"; 101 102 105 private static final String DBM_SERVICE = "dbm"; 106 107 110 private static final String JMS_SERVICE = "jms"; 111 112 115 private static final String RESOURCE_SERVICE = "resource"; 116 117 120 private static final String JORAM_RAR = "joram_for_jonas_ra"; 121 122 125 private static final String DATASOURCES = "jonas.service.dbm.datasources"; 126 127 130 private static final String DATASOURCE_CLASS_NAME = "datasource.classname"; 131 132 135 private static Hashtable classesToCheck = new Hashtable (); 136 137 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 154 private CheckEnv() { 155 } 156 157 161 public static int getStatus() { 162 163 Properties props = null; 164 envStatus = ENV_OK; 165 int res; 166 167 Enumeration cList = classesToCheck.keys(); 169 while (cList.hasMoreElements()) { 170 String cname = (String ) 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 try { 182 String 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 fRoot = new File (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 e) { 200 System.err.println("CheckEnv ERROR: Cannot get the '" 201 + INSTALL_ROOT + "' system property (" + e + ")"); 202 envStatus = ENV_ERROR; 203 } 204 205 try { 208 String 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 fBase = new File (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 e) { 226 System.err.println("CheckEnv ERROR: Cannot get the '" 227 + JONAS_BASE + "' system property (" + e + ")"); 228 envStatus = ENV_ERROR; 229 } 230 231 if ((res = checkJonasProperties()) != ENV_OK) { 233 envStatus = res; 234 } 235 236 if (getProperties("trace.properties") == null) { 238 System.err.println("JOnAS environment ERROR: 'trace.properties' not accessible"); 239 envStatus = ENV_ERROR; 240 } 241 242 if (getProperties("carol.properties") == null) { 244 System.err.println("JOnAS environment ERROR: 'carol.properties' not accessible"); 245 envStatus = ENV_ERROR; 246 } 247 248 if ((res = checkRealm()) != ENV_OK) { 250 envStatus = res; 251 } 252 if ((res = checkJmsJoramRar()) != ENV_OK) { 255 envStatus = res; 256 } 257 258 return envStatus; 259 } 260 261 265 public static void main(String [] args) { 266 boolean isHelp = false; 267 boolean isVerbose = true; 268 for (int argn = 0; argn < args.length; argn++) { 270 String arg = args[argn]; 271 if (arg.equals("-help") || arg.equals("-?")) { 272 isHelp = true; 273 continue; 274 } 275 } 276 if (isHelp) { 278 usage(); 279 System.exit(0); 280 } 281 282 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 302 static boolean classIsAccessible(String name) { 303 try { 304 Class.forName(name); 305 return true; 306 } catch (Exception e) { 307 return false; 308 } catch (Error e) { 309 System.err.println(e); 310 return false; 311 } 312 } 313 314 320 static Properties getProperties(String name) { 321 boolean isAccessible = true; 322 Properties props = null; 323 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); 324 if (is != null) { 325 props = new Properties (); 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 pList = props.propertyNames(); 336 while (pList.hasMoreElements()) { 337 String pname = (String ) pList.nextElement(); 338 System.out.println(" " + pname + " = " + props.getProperty(pname)); 339 } 340 } 341 } catch (IOException e) { 342 props = null; 343 } 344 } else { 345 props = null; 346 } 347 return props; 348 } 349 350 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 367 private static int checkJonasProperties() { 368 try { 370 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 [] 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 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 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 String dsList = jonasProps.getValue(DATASOURCES, ""); 413 StringTokenizer st = new StringTokenizer (dsList, ","); 414 String 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 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 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 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 445 private static int checkRealm() { 446 System.out.println("\n- Check 'jonas-realm.xml':"); 447 448 try { 449 JProp jProp = JProp.getInstance(); 450 String jonasBase = JProp.getJonasBase(); 451 File f = new File (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 fe) { 460 System.err.println("Error while checking the jonas-realm.xml file : " + fe.getMessage()); 461 return ENV_ERROR; 462 } 463 } 464 465 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 e) { 481 System.err.println("JOnAS environment ERROR: 'jonas.properties' not accessible (" + e + ")"); 482 return ENV_ERROR; 483 } 484 485 try { 486 String [] 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 e) { 502 System.err.println("JOnAS environment ERROR: 'jonas.properties' not accessible (" + e + ")"); 503 return ENV_ERROR; 504 } 505 try { 506 String [] 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 e) { 523 System.err 524 .println("JOnAS environment ERROR: 'jonas.service.resource.resources' not accessible (" + e + ")"); 525 return ENV_ERROR; 526 } 527 String [] resourcesAutoloadDir = null; 528 try { 529 resourcesAutoloadDir = jonasProps.getValueAsArray("jonas.service.resource.autoloaddir"); 530 } catch (Exception e) { 531 System.err.println("JOnAS environment ERROR: 'jonas.service.resource.autoloaddir' not accessible (" + e 532 + ")"); 533 return ENV_ERROR; 534 } 535 String jonasBase = JProp.getJonasBase(); 536 if (resourcesAutoloadDir != null) { 537 for (int i = 0; i < resourcesAutoloadDir.length; i++) { 538 File dir = new File (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 [] 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 |