1 25 26 package org.objectweb.jonas.jonasadmin.test.util; 27 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.net.URL ; 34 import java.util.Properties ; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.NamingException ; 39 40 import org.objectweb.jonas.adm.AdmInterface; 41 42 46 public class JProperties { 47 48 51 private static String jonasName = "jonas"; 52 53 56 private static Context ictx = null; 57 58 61 private static AdmInterface admI = null; 62 63 66 private static Properties systEnv = System.getProperties(); 67 68 71 private static String fileSeparator = systEnv.getProperty("file.separator"); 72 73 76 private static final String JONAS_TEST = "jonas.test"; 77 78 81 private static String jonasTest = systEnv.getProperty(JONAS_TEST); 82 83 86 private static final String JONAS_BASE = "jonas.base"; 87 88 91 private static String jonasBase = systEnv.getProperty(JONAS_BASE); 92 93 96 private static final String RESOURCE_DIR = "resources"; 97 98 101 private static final String CONF_DIR = "conf"; 102 103 108 private Context getInitialContext() throws NamingException { 109 return new InitialContext (); 110 } 111 112 117 public Properties getPropertiesEnv() throws Exception { 118 Properties prop = null; 119 120 try { 121 if (ictx == null) { 122 ictx = getInitialContext(); 123 } 124 if (admI == null) { 125 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 126 } 127 prop = admI.listEnv(); 128 } catch (Exception e) { 129 throw new Exception ("Cannot get list environnement : " + e.getMessage()); 130 } 131 132 return prop; 133 } 134 135 140 public String [] getServices() throws Exception { 141 Properties propertiesEnv = getPropertiesEnv(); 142 String services = propertiesEnv.getProperty("jonas.services"); 143 return services.split(","); 144 } 145 146 152 public boolean searchService(String service) throws Exception { 153 String [] services = getServices(); 154 boolean found = false; 155 int i = 0; 156 157 while (!found && i < services.length) { 158 if (services[i].equals(service)) { 159 found = true; 160 } 161 i++; 162 } 163 164 return found; 165 } 166 167 172 public boolean isDb() throws Exception { 173 boolean exist = false; 174 try { 175 if (searchService("db")) { 176 exist = true; 177 } 178 } catch (Exception e) { 179 throw new Exception ("Cannot get services : " + e.getMessage()); 180 } 181 return exist; 182 } 183 184 189 public boolean isDbm() throws Exception { 190 boolean exist = false; 191 try { 192 if (searchService("dbm")) { 193 exist = true; 194 } 195 } catch (Exception e) { 196 throw new Exception ("Cannot get services : " + e.getMessage()); 197 } 198 return exist; 199 } 200 201 206 public boolean isDiscovery() throws Exception { 207 boolean exist = false; 208 try { 209 if (searchService("discovery")) { 210 exist = true; 211 } 212 } catch (Exception e) { 213 throw new Exception ("Cannot get services : " + e.getMessage()); 214 } 215 return exist; 216 } 217 218 223 public boolean isEar() throws Exception { 224 boolean exist = false; 225 try { 226 if (searchService("ear")) { 227 exist = true; 228 } 229 } catch (Exception e) { 230 throw new Exception ("Cannot get services : " + e.getMessage()); 231 } 232 return exist; 233 } 234 235 240 public boolean isEjb() throws Exception { 241 boolean exist = false; 242 try { 243 if (searchService("ejb")) { 244 exist = true; 245 } 246 } catch (Exception e) { 247 throw new Exception ("Cannot get services : " + e.getMessage()); 248 } 249 return exist; 250 } 251 252 257 public boolean isJms() throws Exception { 258 boolean exist = false; 259 try { 260 if (searchService("jms")) { 261 exist = true; 262 } 263 } catch (Exception e) { 264 throw new Exception ("Cannot get services : " + e.getMessage()); 265 } 266 return exist; 267 } 268 269 274 public boolean isJmx() throws Exception { 275 boolean exist = false; 276 try { 277 if (searchService("jmx")) { 278 exist = true; 279 } 280 } catch (Exception e) { 281 throw new Exception ("Cannot get services : " + e.getMessage()); 282 } 283 return exist; 284 } 285 286 291 public boolean isJtm() throws Exception { 292 boolean exist = false; 293 try { 294 if (searchService("jtm")) { 295 exist = true; 296 } 297 } catch (Exception e) { 298 throw new Exception ("Cannot get services : " + e.getMessage()); 299 } 300 return exist; 301 } 302 303 308 public boolean isMail() throws Exception { 309 boolean exist = false; 310 try { 311 if (searchService("mail")) { 312 exist = true; 313 } 314 } catch (Exception e) { 315 throw new Exception ("Cannot get services : " + e.getMessage()); 316 } 317 return exist; 318 } 319 320 325 public boolean isRegistry() throws Exception { 326 boolean exist = false; 327 try { 328 if (searchService("registry")) { 329 exist = true; 330 } 331 } catch (Exception e) { 332 throw new Exception ("Cannot get services : " + e.getMessage()); 333 } 334 return exist; 335 } 336 337 342 public boolean isResource() throws Exception { 343 boolean exist = false; 344 try { 345 if (searchService("resource")) { 346 exist = true; 347 } 348 } catch (Exception e) { 349 throw new Exception ("Cannot get services : " + e.getMessage()); 350 } 351 return exist; 352 } 353 354 359 public boolean isSecurity() throws Exception { 360 boolean exist = false; 361 try { 362 if (searchService("security")) { 363 exist = true; 364 } 365 } catch (Exception e) { 366 throw new Exception ("Cannot get services : " + e.getMessage()); 367 } 368 return exist; 369 } 370 371 376 public boolean isWeb() throws Exception { 377 boolean exist = false; 378 try { 379 if (searchService("web")) { 380 exist = true; 381 } 382 } catch (Exception e) { 383 throw new Exception ("Cannot get services : " + e.getMessage()); 384 } 385 return exist; 386 } 387 388 393 public boolean isWs() throws Exception { 394 boolean exist = false; 395 try { 396 if (searchService("ws")) { 397 exist = true; 398 } 399 } catch (Exception e) { 400 throw new Exception ("Cannot get services : " + e.getMessage()); 401 } 402 return exist; 403 } 404 405 410 public boolean isJoram() throws Exception { 411 Properties propertiesEnv = getPropertiesEnv(); 412 String mom = propertiesEnv.getProperty("jonas.service.jms.mom"); 413 return mom.endsWith("JmsAdminForJoram"); 414 } 415 416 421 public boolean isCatalina() throws Exception { 422 Properties propertiesEnv = getPropertiesEnv(); 423 String webClass = propertiesEnv.getProperty("jonas.service.web.class"); 424 return webClass.endsWith("CatalinaJWebContainerServiceWrapper"); 425 } 426 427 433 public Properties getProperties(String fileName) throws Exception { 434 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 436 URL url = cl.getResource(fileName); 437 InputStream is = cl.getResourceAsStream(fileName); 438 Properties configFile = new Properties (); 439 configFile.load(is); 440 return configFile; 441 } 442 443 449 private Properties getCarolProperties() throws FileNotFoundException { 450 if (jonasBase.equalsIgnoreCase("${myenv.JONAS_BASE}")) { 452 throw new FileNotFoundException ("You must add JONAS_BASE in your environnement variables. "); 453 } else { 454 String propFileName = jonasBase + fileSeparator + CONF_DIR + fileSeparator + "carol" + ".properties"; 455 456 File f = null; 457 Properties configFile = new Properties (); 458 try { 459 f = new File (propFileName); 460 FileInputStream is = new FileInputStream (f); 461 configFile.load(is); 462 } catch (FileNotFoundException e) { 463 throw new FileNotFoundException ("Cannot find properties for " + propFileName); 464 } catch (IOException e) { 465 System.err.println(e); 466 } 467 return configFile; 468 } 469 } 470 471 475 public String getRegistryProtocol() { 476 String protocol = null; 477 try { 478 protocol = getCarolProperties().getProperty("carol.protocols"); 479 } catch (FileNotFoundException e) { 480 e.printStackTrace(); 481 } 482 return protocol; 483 } 484 485 489 public String getRegistryUrl() { 490 String protocol = null; 491 String url = null; 492 try { 493 protocol = getCarolProperties().getProperty("carol.protocols"); 494 495 try { 496 url = getCarolProperties().getProperty("carol." + protocol + ".url"); 497 } catch (FileNotFoundException e) { 498 e.printStackTrace(); 499 } 500 } catch (FileNotFoundException e) { 501 e.printStackTrace(); 502 } 503 return url; 504 } 505 506 510 public String getJvmVersion() { 511 return systEnv.getProperty("java.version"); 512 } 513 514 518 public String getJvmVendor() { 519 return systEnv.getProperty("java.vm.specification.vendor"); 520 } 521 } | Popular Tags |