001: /*
002: * Title: Container
003: * Description:
004: *
005: * This software is published under the terms of the OpenSymphony Software
006: * License version 1.1, of which a copy has been included with this
007: * distribution in the LICENSE.txt file.
008: */
009:
010: package com.opensymphony.module.sitemesh.util;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: /**
017: * Utility for determining the Servlet Container the application is running in.
018: * Currently supported containers: Tomcat, Resin, Orion, OC4J, WebLogic, HPAS, JRun,
019: * Websphere.
020: *
021: * <h3>Usage:</h3>
022: *
023: * <code>if (Container.get() == Container.TOMCAT) { .... }</code>
024: *
025: * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
026: * @version $Revision: 1.2 $
027: */
028: public final class Container {
029: public static final int UNKNOWN = 0;
030: public static final int TOMCAT = 1;
031: public static final int RESIN = 2;
032: public static final int ORION = 3; // Orion or OC4J
033: public static final int WEBLOGIC = 4;
034: public static final int HPAS = 5;
035: public static final int JRUN = 6;
036: public static final int WEBSPHERE = 7;
037:
038: private static int result = -1;
039:
040: /**
041: * A map containing classes that can be searched for,
042: * and which container they are typically found in.
043: */
044: private static Map classMappings = null;
045:
046: static {
047: // initialize the classes that can be searched for
048: classMappings = new HashMap(6);
049: classMappings.put("org.apache.jasper.runtime.JspFactoryImpl",
050: new Integer(TOMCAT));
051: classMappings.put("com.caucho.jsp.JspServlet", new Integer(
052: RESIN));
053: classMappings.put("com.evermind.server.http.JSPServlet",
054: new Integer(ORION));
055: classMappings.put("weblogic.servlet.JSPServlet", new Integer(
056: WEBLOGIC));
057: classMappings.put(
058: "com.hp.mwlabs.j2ee.containers.servlet.jsp.JspServlet",
059: new Integer(HPAS));
060: classMappings.put("jrun.servlet.WebApplicationService",
061: new Integer(JRUN));
062: classMappings.put(
063: "com.ibm.ws.webcontainer.jsp.servlet.JspServlet",
064: new Integer(WEBSPHERE));
065: }
066:
067: /** Get the current container. */
068: public static int get() {
069: if (result == -1) {
070: final String classMatch = searchForClosestClass(classMappings);
071:
072: if (classMatch == null) {
073: result = UNKNOWN;
074: } else {
075: result = ((Integer) classMappings.get(classMatch))
076: .intValue();
077: }
078: }
079: return result;
080: }
081:
082: /**
083: * Walk up the classloader hierachy and attempt to find a class in the classMappings Map
084: * that can be loaded.
085: *
086: * @return Name of the match class, or null if not found.
087: */
088: private static String searchForClosestClass(Map classMappings) {
089: // get closest classloader
090: ClassLoader loader = Container.class.getClassLoader();
091:
092: // iterate up through the classloader hierachy (through parents), until no more left.
093: while (loader != null) {
094:
095: for (Iterator iterator = classMappings.keySet().iterator(); iterator
096: .hasNext();) {
097: String className = (String) iterator.next();
098:
099: try {
100: // attempt to load current classname with current classloader
101: loader.loadClass(className);
102: // if no exception has been thrown, we're in luck.
103: return className;
104: } catch (ClassNotFoundException e) {
105: // no problem... we'll keep trying...
106: }
107: }
108: loader = loader.getParent();
109: }
110:
111: // couldn't find anything
112: return null;
113: }
114: }
|