1 23 24 package com.sun.enterprise.server; 25 26 import java.io.File ; 27 import java.io.FilenameFilter ; 28 import java.lang.reflect.Method ; 29 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 33 import java.util.ArrayList ; 34 import java.util.Arrays ; 35 import java.util.List ; 36 import java.util.jar.*; 37 import java.util.StringTokenizer ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 import com.sun.appserv.server.util.ASURLClassLoader; 42 import com.sun.appserv.server.util.ClassLoaderChain; 43 import com.sun.enterprise.util.ASenvPropertyReader; 44 import com.sun.enterprise.util.SystemPropertyConstants; 45 46 47 60 61 public class PELaunch { 62 63 public static final String USE_NEW_CLASSLOADER_PROPERTY 66 = "com.sun.aas.useNewClassLoader"; 67 68 public static final String PROPERTIES_FILES 69 = "processLauncher.properties"; 70 71 private static final String CLASSPATH_PREFIX_PROPERTY 76 = "com.sun.aas.ClassPathPrefix"; 77 private static final String CLASSPATH_SUFFIX_PROPERTY 78 = "com.sun.aas.ClassPathSuffix"; 79 private static final String SERVER_CLASSPATH_PROPERTY 80 = "com.sun.aas.ServerClassPath"; 81 82 private static final String fileSeparator = File.separator; 83 private static final String pathSeparator = File.pathSeparator; 84 85 private static String installRoot = null; 86 private static String javaHome = null; 87 88 private static List <String > _appserverClasspath = null; 89 private static List <String > _sharedClasspath = null; 90 private static List <String > _optionalClasspath = null; 91 92 private static ASURLClassLoader _sharedClassLoader = null; 93 private static ClassLoaderChain _optionalChain = null; 94 private static ClassLoaderChain _asChain = null; 95 96 private static final boolean bDebug = new Boolean ( 97 System.getProperty("com.sun.aas.useNewClassLoader.debug", "false")).booleanValue(); 98 99 100 static { 102 if (System.getProperty(USE_NEW_CLASSLOADER_PROPERTY) == null) { 103 System.setProperty(USE_NEW_CLASSLOADER_PROPERTY, "true"); 104 } 105 } 106 107 public static ClassLoader getSharedChain(){ 108 return _sharedClassLoader; 109 } 110 111 public static ClassLoader getOptionalChain(){ 112 return _optionalChain; 113 } 114 115 public static ClassLoader getAppServerChain(){ 116 return _asChain; 117 } 118 119 public static List <String > getSharedClasspath() { 120 if(_sharedClasspath!=null) 121 return _sharedClasspath; 122 String asLib = installRoot + fileSeparator + "lib" + fileSeparator; 124 String sharedJarsList = System.getProperty("com.sun.aas.classloader.sharedChainJars"); 125 if (isEE()) { 126 String eeSharedJarsList = System.getProperty("com.sun.aas.classloader.sharedChainJars.ee"); 127 sharedJarsList += ("," + eeSharedJarsList); 128 } 129 logFine("shared jar list " + sharedJarsList); 130 131 List <String > shr = getLibraryList(asLib, sharedJarsList); 132 133 String prefixString = System.getProperty(CLASSPATH_PREFIX_PROPERTY); 135 logFine(" prefixString " + prefixString ); 136 String [] classpathPrefix = null; 137 if (prefixString != null) classpathPrefix = prefixString.split("" +File.pathSeparatorChar); 138 139 String suffixString = System.getProperty(CLASSPATH_SUFFIX_PROPERTY); 140 logFine(" suffixString " + suffixString); 141 String [] classpathSuffix = null; 142 if (suffixString != null) classpathSuffix = suffixString.split("" +File.pathSeparatorChar); 143 144 String serverClassPathString = System.getProperty(SERVER_CLASSPATH_PROPERTY); 145 logFine(" serverClassPathString " + serverClassPathString); 146 String [] serverClassPath = null; 147 if (serverClassPathString != null) serverClassPath = serverClassPathString.split("" +File.pathSeparatorChar); 148 149 List <String > sharedChainList = new ArrayList <String >(); 151 if (classpathPrefix != null) sharedChainList.addAll(Arrays.asList(classpathPrefix)); 152 sharedChainList.addAll(shr); 153 if (serverClassPath != null) sharedChainList.addAll(Arrays.asList(serverClassPath)); 154 if (classpathSuffix != null) sharedChainList.addAll(Arrays.asList(classpathSuffix)); 155 156 List manifestaddonJars = getManifestAddonJars(); 157 if(manifestaddonJars.size() != 0) 158 sharedChainList.addAll(manifestaddonJars); 159 _sharedClasspath=sharedChainList; 160 return _sharedClasspath; 161 } 162 163 164 public static List getAppServerClasspath() { 165 return _appserverClasspath; 166 } 167 168 public static List <String > getOptionalClasspath() { 169 return _optionalClasspath; 170 } 171 172 177 public static List getServerClasspath() { 178 String asLib = installRoot + fileSeparator + "lib" + fileSeparator; 179 String serverJarsList = System.getProperty("com.sun.aas.classloader.serverClassPath"); 180 181 if (isEE()) { 182 String eeServerJarsList = System.getProperty("com.sun.aas.classloader.serverClassPath.ee"); 183 serverJarsList += ("," + eeServerJarsList); 184 } 185 logFine("serverClassPathJarsList " + serverJarsList); 186 187 List <String > serverClasspathList = getLibraryList(asLib, serverJarsList); 188 return serverClasspathList; 189 } 190 191 194 private static List getManifestAddonJars() { 195 List <String > manifestJars = new ArrayList <String >(); 196 JarFile file = null; 197 try { 198 String addonDir = installRoot + fileSeparator + "lib" + fileSeparator + "addons"; 199 File libAddonDirectory = new File (addonDir); 200 if(!libAddonDirectory.isDirectory()) 201 return manifestJars; 202 203 File [] fileArray = libAddonDirectory.listFiles(); 204 for(int i = 0;i<fileArray.length;i++) { 205 String addonJar = fileArray[i].getName(); 206 String jarExtension = ""; 207 int dotLastIndex = addonJar.lastIndexOf("."); 208 if(dotLastIndex != -1) 209 jarExtension = addonJar.substring(dotLastIndex + 1); 210 if(jarExtension.equalsIgnoreCase("jar")) { 211 manifestJars.add(fileArray[i].getAbsolutePath()); 212 file = new JarFile(fileArray[i].getAbsolutePath()); 213 Manifest mf = file.getManifest(); 214 Attributes attributes = null; 215 if(mf != null) { 216 attributes = mf.getMainAttributes(); 217 if(attributes != null) { 218 String classPath = attributes.getValue(Attributes.Name.CLASS_PATH); 219 if(classPath != null && !classPath.trim().equals("")) { 220 StringTokenizer stoken = new StringTokenizer (classPath); 221 while(stoken.hasMoreTokens()) { 222 String classPathJar = addonDir + fileSeparator + stoken.nextElement(); 223 manifestJars.add(classPathJar); 224 } 225 } 226 } 228 file.close(); 229 } 230 } 231 } 232 }catch(Exception e) { 233 e.printStackTrace(System.err); 234 }finally { 235 try { 236 if(file != null) 237 file.close(); 238 }catch(Exception ex) { 239 ex.printStackTrace(System.err); 240 } 241 } 242 return manifestJars; 244 } 245 248 public static void main(String [] args) { 249 try{ 250 Class peMainClass = null; 251 252 if(Boolean.getBoolean(USE_NEW_CLASSLOADER_PROPERTY)){ 253 ASenvPropertyReader reader = new ASenvPropertyReader( 254 System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY), 255 false); 256 reader.setSystemProperties(); 257 installRoot = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY); 258 javaHome = System.getProperty(SystemPropertyConstants.JAVA_ROOT_PROPERTY); 259 260 setupClassloaders(); 261 peMainClass = _asChain.loadClass( 263 "com.sun.enterprise.server.PEMain", true); 264 Thread.currentThread().setContextClassLoader(_asChain); 265 } else { 266 peMainClass = Class.forName("com.sun.enterprise.server.PEMain"); 267 } 268 269 Class [] argTypes = new Class []{String [].class}; 270 Method m = peMainClass.getMethod("main", argTypes); 271 Object [] argListForInvokedMain = new Object []{args}; 272 m.invoke(null, argListForInvokedMain); 273 } catch(Exception e) { 274 e.printStackTrace(System.err); 275 System.exit(1); 276 } 277 } 278 279 282 private static void setupClassloaders(){ 283 prepareAppServerJars(); 284 prepareOptionalOverrideableJars(); 285 appendOtherJarsToSharedChain(); 286 setupSharedChain(); 287 setupOptionalOverrideableChain(); 288 setupAppServerChain(); 289 290 List <String > cp = getSharedClasspath(); 291 StringBuilder classpath = new StringBuilder (System.getProperty("java.class.path")); 294 for(String s:cp){ 295 classpath.append(s); 296 classpath.append(pathSeparator); 297 } 298 System.setProperty("java.class.path", classpath.toString()); 299 } 300 301 306 public static List <String > getServerClassPath(String configDir, 307 String domainDir){ 308 ASenvPropertyReader reader = new ASenvPropertyReader(configDir,false); 309 reader.setSystemProperties(); 310 installRoot = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY); 311 javaHome = System.getProperty(SystemPropertyConstants.JAVA_ROOT_PROPERTY); 312 313 String asLib = installRoot + fileSeparator ; 315 List <String >serverClassPathList = new ArrayList <String >(); 316 File [] fls= getAllLibrariesInLib(asLib); 317 318 for (File element : fls) { 319 serverClassPathList.add(element.getAbsolutePath()); 320 } 321 322 String mqlib = System.getProperty(SystemPropertyConstants.IMQ_LIB_PROPERTY); 324 String antlib = System.getProperty(SystemPropertyConstants.ANT_LIB_PROPERTY); 325 String jdmklib = System.getProperty(SystemPropertyConstants.JDMK_HOME_PROPERTY); 326 String hadbRoot = System.getProperty(SystemPropertyConstants.HADB_ROOT_PROPERTY); 327 328 String [] peServerClassPath = {installRoot + "/lib/install/applications/jmsra/imqjmsra.jar", 329 mqlib + "/jaxm-api.jar" , mqlib + "/fscontext.jar", 330 mqlib + "/imqbroker.jar", mqlib + "/imqjmx.jar", 331 mqlib + "/imqxm.jar", 332 antlib + "/ant.jar", jdmklib + "/lib/jdmkrt.jar"} ; 333 String [] eeServerClassPath = {hadbRoot + "/lib/hadbjdbc4.jar", 334 jdmklib + "/lib/jdmkrt.jar",hadbRoot + "/lib/dbstate.jar", 335 hadbRoot + "/lib/hadbm.jar", hadbRoot + "/lib/hadbmgt.jar"} ; 336 for (String element : peServerClassPath) { 337 File f = new File (element); 338 if(f.exists()) { 339 serverClassPathList.add(f.getAbsolutePath()); 340 } 341 } 342 for (String element : eeServerClassPath) { 343 File f = new File (element); 344 if(f.exists()) { 345 serverClassPathList.add(f.getAbsolutePath()); 346 } 347 } 348 349 logFine("Server Classpath for verifier " + serverClassPathList + "\n"); 350 Logger.getAnonymousLogger().log(Level.FINE, "Server Classpath for verifier " + serverClassPathList); 351 return serverClassPathList; 352 } 353 354 359 private static void setupSharedChain(){ 360 List <String > sharedChainJarList = getSharedClasspath(); 361 logFine("shared classpath jars : " + sharedChainJarList + "\n"); 362 URL [] urls = getURLList(sharedChainJarList); 363 logFine(" SharedChain URL List " + urls); 364 _sharedClassLoader = new ASURLClassLoader(urls, 365 ClassLoader.getSystemClassLoader()); 366 _sharedClassLoader.setName("Shared ClassLoader Chain"); 367 } 368 369 370 377 private static void setupOptionalOverrideableChain(){ 378 379 _optionalChain = new ClassLoaderChain(_sharedClassLoader); 380 _optionalChain.setName("optionalChain"); 381 382 URL [] urls = getURLList(_optionalClasspath); 383 ASURLClassLoader optionalJarsLoader = new ASURLClassLoader(urls, 385 _sharedClassLoader); 386 _optionalChain.addToList(optionalJarsLoader); 387 388 } 389 390 private static void prepareOptionalOverrideableJars(){ 391 String asLib = installRoot + fileSeparator + "lib" + fileSeparator; 392 String optionalJarsString = System.getProperty("com.sun.aas.classloader.optionalOverrideableChain"); 393 if (isEE()) { 394 String eeOptionalJarsList = System.getProperty("com.sun.aas.classloader.optionalOverrideableChain.ee"); 395 optionalJarsString += ("," + eeOptionalJarsList); 396 } 397 398 logFine(" optionalOverrideableChain" + optionalJarsString ); 399 400 _optionalClasspath = getLibraryList(asLib, optionalJarsString); 401 logFine("Optional overrideable chain classpath : " + _optionalClasspath + "\n"); 402 } 403 404 410 private static void setupAppServerChain(){ 411 URL [] urls = getURLList(_appserverClasspath); 412 413 _asChain = new ClassLoaderChain(_sharedClassLoader); 415 _asChain.setName("ASChain"); 416 417 ASURLClassLoader asimplloader = new ASURLClassLoader(urls, _asChain); 418 asimplloader.setName("asimpl"); 419 _asChain.addToList(asimplloader); 420 _asChain.addToList(_optionalChain); 421 } 422 423 private static void prepareAppServerJars(){ 424 String asLib = installRoot + fileSeparator + "lib"; 426 String appserverJarsStr = System.getProperty("com.sun.aas.classloader.appserverChainJars"); 427 if (isEE()) { 428 String eeAppserverJarsList = System.getProperty("com.sun.aas.classloader.appserverChainJars.ee"); 429 appserverJarsStr += ("," + eeAppserverJarsList); 430 } 431 logFine("appserverJarsString " + appserverJarsStr ); 432 433 _appserverClasspath = getLibraryList(asLib, appserverJarsStr); 434 logFine("Application server classpath : " + _appserverClasspath + "\n"); 435 } 436 437 441 private static boolean isEE() { 442 boolean isEE = false; 443 final String eepffc = "com.sun.enterprise.ee.server.pluggable.EEPluggableFeatureImpl"; 444 final String pn = "com.sun.appserv.pluggable.features"; 445 final String pv = System.getProperty(pn); 446 if (eepffc.equals(pv)) { 447 isEE = true; 448 } 449 return ( isEE ); 450 } 451 452 private static List <String > getLibraryList(String libraryRoot, String librariesString) { 453 String [] libraries = librariesString.split(","); 454 List <String > shr = new ArrayList <String >(); 455 for(String library: libraries){ 456 library = library.trim(); 457 File file = new File (library); 458 if (!file.isAbsolute()) { 459 shr.add(libraryRoot+library); 460 } else { 461 shr.add(library); 462 } 463 } 464 return shr; 465 } 466 467 private static URL [] getURLList(List <String > librariesList) { 468 int i=0; 469 String [] sharedJars = librariesList.toArray(new String [] {}); 470 URL [] urls = new URL [sharedJars.length]; 471 for(String s:sharedJars){ 472 try{ 473 URL url = (new File (s)).toURI().toURL(); 474 urls[i++] = url; 475 }catch(MalformedURLException e){ 476 Logger.getAnonymousLogger().warning(e.getMessage()); 477 Logger.getAnonymousLogger().log(Level.WARNING, "Exception while" + 478 "setting up shared chain", e); 479 } 480 } 481 return urls; 482 } 483 484 private static void logFine(String s) { 485 if(bDebug) { 486 System.err.println(s); 487 } 488 } 489 490 private static void appendOtherJarsToSharedChain(){ 491 List <String > list = new ArrayList <String >(); 492 list.addAll(getSharedClasspath()); 493 list.addAll(getAppServerClasspath()); 494 list.addAll(getOptionalClasspath()); 495 File [] files = getAllLibrariesInLib(installRoot); 496 for(File file:files){ 497 try{ 498 if(!list.contains(file.getCanonicalPath())){ 499 _sharedClasspath.add(file.getCanonicalPath()); 500 } 501 }catch(java.io.IOException ioe){ 502 System.err.println("Error getting "+file.getAbsolutePath()+" "+ioe.getMessage()); 503 } 504 } 505 } 506 507 private static File [] getAllLibrariesInLib(String asLib) { 508 File installLib = new File (asLib,"lib"); 509 File [] files = installLib.listFiles(new FilenameFilter () { 510 public boolean accept(File dir, String name) { 511 if(name.endsWith(".jar") || name.endsWith(".zip")) { 512 if (name.equals("j2ee.jar")) { 515 return false; 516 } 517 return true; 518 } 519 return false; 520 } 521 }); 522 return files; 523 } 524 } 525 | Popular Tags |