• 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/ClassLoaderUtil.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2011-2026 Hazendaz
4
 */
5
package com.opensymphony.module.sitemesh.util;
6

7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.net.URL;
10

11
/**
12
 * This class is extremely useful for loading resources and classes in a fault tolerant manner that works across
13
 * different applications servers. It has come out of many months of frustrating use of multiple application servers at
14
 * Atlassian, please don't change things unless you're sure they're not going to break in one server or another!
15
 */
16
public class ClassLoaderUtil {
×
17

18
    /**
19
     * Load a given resource. This method will try to load the resource using the following methods (in order):
20
     * <ul>
21
     * <li>From Thread.currentThread().getContextClassLoader()
22
     * <li>From ClassLoaderUtil.class.getClassLoader()
23
     * <li>callingClass.getClassLoader()
24
     * </ul>
25
     *
26
     * @param resourceName
27
     *            The name of the resource to load
28
     * @param callingClass
29
     *            The Class object of the calling object
30
     *
31
     * @return the resource
32
     */
33
    public static URL getResource(String resourceName, Class<?> callingClass) {
34
        URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
×
35

36
        if (url == null) {
×
37
            url = ClassLoaderUtil.class.getClassLoader().getResource(resourceName);
×
38
        }
39

40
        if (url == null) {
×
41
            ClassLoader cl = callingClass.getClassLoader();
×
42

43
            if (cl != null) {
×
44
                url = cl.getResource(resourceName);
×
45
            }
46
        }
47

48
        if (url == null && resourceName != null && resourceName.charAt(0) != '/') {
×
49
            return getResource('/' + resourceName, callingClass);
×
50
        }
51

52
        return url;
×
53
    }
54

55
    /**
56
     * This is a convenience method to load a resource as a stream. The algorithm used to find the resource is given in
57
     * getResource()
58
     *
59
     * @param resourceName
60
     *            The name of the resource to load
61
     * @param callingClass
62
     *            The Class object of the calling object
63
     *
64
     * @return the resource as stream
65
     */
66
    public static InputStream getResourceAsStream(String resourceName, Class<?> callingClass) {
67
        URL url = getResource(resourceName, callingClass);
×
68

69
        try {
70
            return url != null ? url.openStream() : null;
×
71
        } catch (IOException e) {
×
72
            return null;
×
73
        }
74
    }
75

76
    /**
77
     * Load a class with a given name. It will try to load the class in the following order:
78
     * <ul>
79
     * <li>From Thread.currentThread().getContextClassLoader()
80
     * <li>Using the basic Class.forName()
81
     * <li>From ClassLoaderUtil.class.getClassLoader()
82
     * <li>From the callingClass.getClassLoader()
83
     * </ul>
84
     *
85
     * @param className
86
     *            The name of the class to load
87
     * @param callingClass
88
     *            The Class object of the calling object
89
     *
90
     * @return the class
91
     *
92
     * @throws ClassNotFoundException
93
     *             If the class cannot be found anywhere.
94
     */
95
    public static Class<?> loadClass(String className, Class<?> callingClass) throws ClassNotFoundException {
96
        try {
97
            return Thread.currentThread().getContextClassLoader().loadClass(className);
×
98
        } catch (ClassNotFoundException e) {
×
99
            try {
100
                return Class.forName(className);
×
101
            } catch (ClassNotFoundException ex) {
×
102
                try {
103
                    return ClassLoaderUtil.class.getClassLoader().loadClass(className);
×
104
                } catch (ClassNotFoundException exc) {
×
105
                    return callingClass.getClassLoader().loadClass(className);
×
106
                }
107
            }
108
        }
109
    }
110
}
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