1 25 26 package org.objectweb.jonas.server; 27 28 29 import java.io.File ; 30 import java.util.Hashtable ; 31 import java.util.Map ; 32 import java.util.StringTokenizer ; 33 34 import org.objectweb.jonas.common.JProp; 35 36 41 public class LoaderManager { 42 43 46 private static LoaderManager instance = null; 47 48 51 private static final String COMMONS = "loader.commons"; 52 53 56 private static final String TOOLS = "loader.tools"; 57 58 61 private static final String APPS = "loader.apps"; 62 63 66 private static final String CATALINA = "loader.catalina"; 67 68 71 private static final String CATALINA_BOOTSTRAP_JAR = "bin" + File.separator + "bootstrap.jar"; 72 73 76 private static final String MORTBAY_JETTY_JAR = "lib" + File.separator + "org.mortbay.jetty.jar"; 77 78 81 private JProp jp = null; 82 83 86 private Map loaders = null; 87 88 91 private static File jonasLib = null; 92 93 96 private static File catalinaLib = null; 97 98 101 private static File jettyLib = null; 102 103 106 private static String jonasBase = null; 107 108 111 private static String catalinaHome = null; 112 113 116 private static String jettyHome = null; 117 118 121 private boolean hasWeb = false; 122 123 126 private boolean useDefaultClassPath = false; 127 128 131 private boolean isCatalina = false; 132 133 136 private File jonasCommonsDir; 137 138 141 private File jonasToolsDir; 142 143 146 private File jonasAppsDir; 147 148 151 private LoaderManager() { 152 String jonasRoot = System.getProperty("install.root"); 154 if (jonasRoot == null) { 155 System.err.println("JONAS_ROOT is not set"); 156 System.exit(2); 157 } 158 159 jonasLib = new File (jonasRoot, "lib"); 160 jonasCommonsDir = new File (jonasLib, "commons"); 161 jonasToolsDir = new File (jonasLib, "tools"); 162 jonasAppsDir = new File (jonasLib, "apps"); 163 catalinaLib = new File (jonasLib, "catalina"); 164 jettyLib = new File (jonasLib, "jetty"); 165 166 loaders = new Hashtable (); 168 } 169 170 173 public static LoaderManager getInstance() { 174 if (instance == null) { 175 instance = new LoaderManager(); 176 } 177 return instance; 178 } 179 180 185 public void init(JProp props) { 186 jp = props; 187 jonasBase = JProp.getJonasBase(); 189 190 useDefaultClassPath = jp.getValueAsBoolean("jonas.default.classloader", false); 191 192 String [] services = props.getValueAsArray("jonas.services"); 194 if (services != null) { 195 for (int i = 0; i < services.length; i++) { 196 if (services[i].equals("web")) { 197 hasWeb = true; 198 String classname = props.getValue("jonas.service.web.class"); 199 if (classname.matches(".*Catalina.*")) { 200 isCatalina = true; 201 } 202 } 203 } 204 } 205 206 catalinaHome = System.getProperty("catalina.home"); 207 jettyHome = System.getProperty("jetty.home"); 208 209 if (hasEmbeddedCatalina()) { 214 System.setProperty("catalina.home", catalinaLib.getPath()); 215 System.setProperty("catalina.base", jonasBase); 216 catalinaHome = catalinaLib.getPath(); 217 } 218 219 if (jettyHome == null) { 222 if (hasEmbeddedJetty()) { 223 System.setProperty("jetty.home", jettyLib.getPath()); 224 jettyHome = jettyLib.getPath(); 225 } 226 } 227 228 } 229 230 233 private boolean hasEmbeddedCatalina() { 234 return new File (catalinaLib, CATALINA_BOOTSTRAP_JAR).exists(); 235 } 236 237 240 private boolean hasEmbeddedJetty() { 241 return new File (jettyLib, MORTBAY_JETTY_JAR).exists(); 242 } 243 244 248 public JClassLoader getCommonsLoader() throws Exception { 249 JClassLoader commons = getLoader(COMMONS); 250 if (commons == null) { 251 commons = createCommonsClassLoader(); 252 setLoader(COMMONS, commons); 253 } 254 return commons; 255 } 256 257 261 public JClassLoader getToolsLoader() throws Exception { 262 JClassLoader tools = getLoader(TOOLS); 263 if (tools == null) { 264 tools = createToolsClassLoader(); 265 setLoader(TOOLS, tools); 266 } 267 return tools; 268 } 269 270 274 public JClassLoader getAppsLoader() throws Exception { 275 JClassLoader apps = getLoader(APPS); 276 if (apps == null) { 277 apps = createAppsClassLoader(); 278 setLoader(APPS, apps); 279 } 280 return apps; 281 } 282 283 287 public JClassLoader getCatalinaLoader() throws Exception { 288 JClassLoader catalina = getLoader(CATALINA); 289 if (catalina == null) { 290 catalina = createCatalinaClassLoader(); 291 setLoader(CATALINA, catalina); 292 } 293 return catalina; 294 } 295 296 307 private static JURLs getClassPath(boolean xtracp) throws Exception { 308 String cpprop = "java.class.path"; 309 if (xtracp) { 310 cpprop = "jonas.classpath"; 311 } 312 String cpValue = System.getProperty(cpprop); 313 String sep = File.pathSeparator; 315 JURLs jurls = new JURLs(); 316 StringTokenizer tokenizer = new StringTokenizer (cpValue, sep); 317 while (tokenizer.hasMoreTokens()) { 318 File file = new File (tokenizer.nextToken()); 319 if (file.isDirectory()) { 320 jurls.addDir(file); 321 } else { 322 if (!file.getName().equals("ow_jonas_bootstrap.jar")) { 325 jurls.add(file); 326 } 327 } 328 } 329 return jurls; 330 } 331 332 339 private JClassLoader getLoader(String name) { 340 return (JClassLoader) loaders.get(name); 341 } 342 343 349 private void setLoader(String name, JClassLoader loader) { 350 loaders.put(name, loader); 351 } 352 353 359 private JClassLoader createCommonsClassLoader() throws Exception { 360 361 JURLs jurls = new JURLs(); 362 363 jurls.merge(getClassPath(useDefaultClassPath)); 365 366 jurls.add(jonasCommonsDir, ".jar"); 368 369 if (!System.getProperty("install.root").toLowerCase().equals(jonasBase.toLowerCase())) { 372 File jonasBaseLibExt = new File (jonasBase + File.separator + "lib" + File.separator + "ext"); 373 if (jonasBaseLibExt.exists()) { 374 jurls.add(jonasBaseLibExt, ".jar"); 375 } 376 } 377 378 jurls.add(new File (jonasLib, "ext"), ".jar"); 380 381 if (hasWeb) { 382 384 if (jettyHome != null && !isCatalina) { 386 File jettyLibDir = new File (jettyHome, "lib"); 387 File jettyExtDir = new File (jettyHome, "ext"); 388 File jettyExtraDir = new File (new File (jettyHome, "extra"), "lib"); 389 jurls.add(jettyLibDir, ".jar"); 390 jurls.add(jettyExtDir, ".jar"); 391 jurls.add(jettyExtraDir, "jsr77.jar"); 392 if (!hasEmbeddedJetty()) { 393 File jonasJetty5Glue = new File (new File (jettyLib, "lib"), "ow_jonas_jetty.jar"); 395 jurls.add(jonasJetty5Glue); 396 } 397 } 398 } 400 401 return new JClassLoader("Commons", jurls.toURLs()); 402 } 403 404 411 private JClassLoader createToolsClassLoader() throws Exception { 412 JURLs jurls = new JURLs(); 413 414 jurls.add(jonasToolsDir, ".jar"); 415 416 return new JClassLoader("Tools", jurls.toURLs(), getCommonsLoader()); 417 } 418 419 427 private JClassLoader createAppsClassLoader() throws Exception { 428 JURLs jurls = new JURLs(); 429 430 jurls.add(jonasAppsDir, ".jar"); 432 433 if (hasWeb) { 434 if (catalinaHome != null && isCatalina) { 436 File catalinaBinDir = new File (catalinaHome, "bin"); 437 File catalinaCommonLibDir = new File (catalinaHome, "common" + File.separator + "lib"); 438 File catalinaSharedLibDir = new File (catalinaHome, "shared" + File.separator + "lib"); 439 jurls.add(new File (catalinaBinDir, "bootstrap.jar")); 440 jurls.add(catalinaCommonLibDir, ".jar"); 441 jurls.add(catalinaSharedLibDir, ".jar"); 442 } 443 } 444 return new JClassLoader("Apps", jurls.toURLs(), getCommonsLoader()); 445 } 446 447 454 private JClassLoader createCatalinaClassLoader() throws Exception { 455 JURLs jurls = new JURLs(); 456 457 File catalinaServerLibDir = new File (catalinaHome, "server" + File.separator + "lib"); 459 jurls.add(catalinaServerLibDir, ".jar"); 460 461 if (!hasEmbeddedCatalina()) { 463 File jonasTomcat5Glue = new File (catalinaLib, "server" + File.separator + "lib" + File.separator + "ow_jonas_tomcat5.jar"); 465 File jonasTomcat55Glue = new File (catalinaLib, "server" + File.separator + "lib" + File.separator + "ow_jonas_tomcat55.jar"); 466 if (jonasTomcat5Glue.exists()) { 467 jurls.add(jonasTomcat5Glue); 468 } else if (jonasTomcat55Glue.exists()) { 469 jurls.add(jonasTomcat55Glue); 470 } else { 471 System.out.println("WARN : No ow_jonas_tomcat5.jar found in " + catalinaLib + "/server/lib directory."); 472 } 473 } 474 return new JClassLoader("Catalina", jurls.toURLs(), getAppsLoader()); 475 } 476 } 477 | Popular Tags |