• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / sitemesh2 / 59

22 Mar 2026 02:30AM UTC coverage: 40.347%. Remained the same
59

push

github

hazendaz
[mvn] Update maven wrapper

698 of 1891 branches covered (36.91%)

Branch coverage included in aggregate %.

1555 of 3693 relevant lines covered (42.11%)

0.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/opensymphony/module/sitemesh/util/Container.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2011-2026 Hazendaz
4
 */
5
/*
6
 * Title:        Container
7
 * Description:
8
 *
9
 * This software is published under the terms of the OpenSymphony Software
10
 * License version 1.1, of which a copy has been included with this
11
 * distribution in the LICENSE.txt file.
12
 */
13

14
package com.opensymphony.module.sitemesh.util;
15

16
import java.util.HashMap;
17
import java.util.Iterator;
18
import java.util.Map;
19

20
/**
21
 * Utility for determining the Servlet Container the application is running in. Currently supported containers: Tomcat,
22
 * Resin, Orion, OC4J, WebLogic, HPAS, JRun, Websphere.
23
 * <h2>Usage:</h2> <code>if (Container.get() == Container.TOMCAT) { .... }</code>
24
 *
25
 * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
26
 */
27
public final class Container {
×
28

29
    /** The Constant UNKNOWN. */
30
    public static final int UNKNOWN = 0;
31

32
    /** The Constant TOMCAT. */
33
    public static final int TOMCAT = 1;
34

35
    /** The Constant RESIN. */
36
    public static final int RESIN = 2;
37

38
    /** The Constant ORION. */
39
    public static final int ORION = 3; // Orion or OC4J
40

41
    /** The Constant WEBLOGIC. */
42
    public static final int WEBLOGIC = 4;
43

44
    /** The Constant HPAS. */
45
    public static final int HPAS = 5;
46

47
    /** The Constant JRUN. */
48
    public static final int JRUN = 6;
49

50
    /** The Constant WEBSPHERE. */
51
    public static final int WEBSPHERE = 7;
52

53
    /** The result. */
54
    private static int result = -1;
×
55

56
    /**
57
     * A map containing classes that can be searched for, and which container they are typically found in.
58
     */
59
    private static Map<String, Integer> classMappings = null;
×
60

61
    static {
62
        // initialize the classes that can be searched for
63
        classMappings = new HashMap<String, Integer>(6);
×
64
        classMappings.put("org.apache.jasper.runtime.JspFactoryImpl", Integer.valueOf(TOMCAT));
×
65
        classMappings.put("com.caucho.jsp.JspServlet", Integer.valueOf(RESIN));
×
66
        classMappings.put("com.evermind.server.http.JSPServlet", Integer.valueOf(ORION));
×
67
        classMappings.put("weblogic.servlet.JSPServlet", Integer.valueOf(WEBLOGIC));
×
68
        classMappings.put("com.hp.mwlabs.j2ee.containers.servlet.jsp.JspServlet", Integer.valueOf(HPAS));
×
69
        classMappings.put("jrun.servlet.WebApplicationService", Integer.valueOf(JRUN));
×
70
        classMappings.put("com.ibm.ws.webcontainer.jsp.servlet.JspServlet", Integer.valueOf(WEBSPHERE));
×
71
    }
×
72

73
    /**
74
     * Get the current container.
75
     *
76
     * @return the int
77
     */
78
    public static int get() {
79
        if (result == -1) {
×
80
            final String classMatch = searchForClosestClass(classMappings);
×
81

82
            if (classMatch == null) {
×
83
                result = UNKNOWN;
×
84
            } else {
85
                result = ((Integer) classMappings.get(classMatch)).intValue();
×
86
            }
87
        }
88
        return result;
×
89
    }
90

91
    /**
92
     * Walk up the classloader hierachy and attempt to find a class in the classMappings Map that can be loaded.
93
     *
94
     * @param classMappings
95
     *            the class mappings
96
     *
97
     * @return Name of the match class, or null if not found.
98
     */
99
    private static String searchForClosestClass(Map<String, Integer> classMappings) {
100
        // get closest classloader
101
        ClassLoader loader = Container.class.getClassLoader();
×
102

103
        // iterate up through the classloader hierachy (through parents), until no more left.
104
        while (loader != null) {
×
105

106
            for (Iterator<String> iterator = classMappings.keySet().iterator(); iterator.hasNext();) {
×
107
                String className = (String) iterator.next();
×
108

109
                try {
110
                    // attempt to load current classname with current classloader
111
                    loader.loadClass(className);
×
112
                    // if no exception has been thrown, we're in luck.
113
                    return className;
×
114
                } catch (ClassNotFoundException e) {
×
115
                    // no problem... we'll keep trying...
116
                }
117
            }
×
118
            loader = loader.getParent();
×
119
        }
120

121
        // couldn't find anything
122
        return null;
×
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc