Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 96866c3233acc53c9315d4d652c0a68863dc4703 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2009, 2010 VMware Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * The Eclipse Public License is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 * and the Apache License v2.0 is available at 
 *   http://www.opensource.org/licenses/apache2.0.php.
 * You may elect to redistribute this code under either of these licenses.  
 *
 * Contributors:
 *   VMware Inc. - initial contribution
 *******************************************************************************/

package org.eclipse.gemini.web.tomcat.internal.loading;

import java.io.IOException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

/**
 * <strong>Concurrent Semantics</strong><br />
 * 
 * This class is immutable and therefore thread safe.
 */
public final class ChainedClassLoader extends ClassLoader {

	/** list of loaders */
	private final List<ClassLoader> loaders;

	/**
	 * Constructs a new <code>ChainedClassLoader</code> instance.
	 * 
	 * @param loaders
	 *            array of non-null class loaders
	 * @param parent
	 *            parent class loader (can be null)
	 */
	ChainedClassLoader(ClassLoader... loaders) {
		
		List<ClassLoader> l = new ArrayList<ClassLoader>();

		for (int i = 0; i < loaders.length; i++) {
			ClassLoader classLoader = loaders[i];
			if (!l.contains(classLoader)) {
				l.add(classLoader);
			}
		}
		
		this.loaders = Collections.unmodifiableList(l);
	}

	public static ChainedClassLoader create(final ClassLoader... loaders) {
		return AccessController
				.doPrivileged(new PrivilegedAction<ChainedClassLoader>() {

					public ChainedClassLoader run() {
						return new ChainedClassLoader(loaders);
					}

				});
	}

	@Override
	public URL getResource(final String name) {
		if (System.getSecurityManager() != null) {
			return AccessController.doPrivileged(new PrivilegedAction<URL>() {

				public URL run() {
					return doGetResource(name);
				}
			});
		} else {
			return doGetResource(name);
		}
	}

	@Override
	public Enumeration<URL> getResources(final String name) throws IOException {
		if (System.getSecurityManager() != null) {
			try {
				return AccessController
						.doPrivileged(new PrivilegedExceptionAction<Enumeration<URL>>() {

							public Enumeration<URL> run() throws Exception {
								return doGetResources(name);
							}

						});
			} catch (PrivilegedActionException e) {
				Exception exception = e.getException();
				if (exception instanceof RuntimeException) {
					throw (RuntimeException) exception;
				} else if (exception instanceof IOException) {
					throw (IOException) exception;
				} else {
					throw new IllegalStateException(
							"Unexpected Exception from privileged action.",
							exception);
				}
			}
		} else {
			return doGetResources(name);
		}
	}

	private Enumeration<URL> doGetResources(String name) throws IOException {
		Vector<URL> urls = new Vector<URL>();
		for (ClassLoader loader : this.loaders) {
			Enumeration<URL> resources = loader.getResources(name);
			if (resources != null) {
				while (resources.hasMoreElements()) {
					URL url = (URL) resources.nextElement();
					urls.add(url);
				}
			}
		}
		return urls.elements();
	}

	private URL doGetResource(String name) {
		URL url = null;
		for (int i = 0; i < loaders.size(); i++) {
			ClassLoader loader = (ClassLoader) loaders.get(i);
			url = loader.getResource(name);
			if (url != null)
				return url;
		}
		return url;
	}

	public Class<?> loadClass(final String name) throws ClassNotFoundException {

		if (System.getSecurityManager() != null) {
			try {
				return AccessController
						.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {

							public Class<?> run() throws Exception {
								return doLoadClass(name);
							}
						});
			} catch (PrivilegedActionException pae) {
				throw (ClassNotFoundException) pae.getException();
			}
		} else {
			return doLoadClass(name);
		}
	}

	private Class<?> doLoadClass(String name) throws ClassNotFoundException {
		Class<?> clazz = null;

		for (int i = 0; i < loaders.size(); i++) {
			ClassLoader loader = (ClassLoader) loaders.get(i);
			try {
				clazz = loader.loadClass(name);
				return clazz;
			} catch (ClassNotFoundException e) {
				// keep moving through the class loaders
			}
		}

		throw new ClassNotFoundException(name);
	}

}

Back to the top