Skip to main content
summaryrefslogtreecommitdiffstats
blob: ff141b82f98988a9f2415c201538a4927bc0ec08 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.commonarchivecore.internal.util;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.security.ProtectionDomain;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jst.j2ee.commonarchivecore.internal.Archive;
import org.eclipse.jst.j2ee.commonarchivecore.internal.CommonArchiveResourceHandler;
import org.eclipse.jst.j2ee.commonarchivecore.internal.EARFile;
import org.eclipse.jst.j2ee.commonarchivecore.internal.File;
import org.eclipse.jst.j2ee.commonarchivecore.internal.exception.ArchiveRuntimeException;


/**
 * Class loader which loads a given set of classes stored in byte arrays. (Assumption: System
 * classes and those in the set are the only classes needed to resolve each one)
 */

public class ArchiveFileDynamicClassLoader extends ClassLoader {    
	protected Archive archive = null;
	protected ClassLoader extraClassLoader;
	protected boolean inEARFile;
	private static final String URL_PROTOCOL = "archive";
	private ArchiveURLStreamHandler handler;
    
	protected ProtectionDomain protectionDomain;
    
    /**
     * <p>This constructor accepts a protection domain, which is used
     * by <code>findClass</code>.</p>
     * 
     * @see ArchiveFileDynamicClassLoader#findClass(String)
     */
    
    public ArchiveFileDynamicClassLoader(Archive anArchive, ClassLoader parentCl, ClassLoader extraCl, ProtectionDomain pDomain) {
        super(parentCl);
        setArchive(anArchive);
        setExtraClassLoader(extraCl);
        inEARFile = anArchive.getContainer() != null && anArchive.getContainer().isEARFile();
        handler = new ArchiveURLStreamHandler();
        protectionDomain = pDomain;
    }

    public ArchiveFileDynamicClassLoader(Archive anArchive, ClassLoader parentCl, ClassLoader extraCl) {
        this(anArchive, parentCl, extraCl, null);
	}

	/**
	 * <p>Loads a specified class.  Called only after the parent class loader has had
     * its chance to load the class, as per the Java2 delegation model.</p>
     * 
     * <p>When non-null, the receiver's protection
     * domain is passed in to the call to <code>defineClass</code>.</p>
     * 
     * @see ClassLoader#defineClass(String, byte[], int)
     * @see ClassLoader#defineClass(String, byte[], int, ProtectionDomain)
	 */
	protected Class findClass(String name) throws ClassNotFoundException {

		Class result;
		// Load class bytes from current set of class byte[]'s
		byte[] bytes = getClassBytesFor(name);

		if (bytes != null) {
            if ( protectionDomain == null ) {
                result = defineClass(name, bytes, 0, bytes.length);
            } else {
                result = defineClass(name, bytes, 0, bytes.length, protectionDomain); 
            }
			if (result == null) {
				throw new ClassNotFoundException(name);
			} // endif
		} else {
			throw new ClassNotFoundException(name);
		} // endif
		return result;
	}

	/**
	 * Insert the method's description here. Creation date: (12/17/00 9:59:57 PM)
	 * 
	 * @return com.ibm.etools.commonarchive.Archive
	 */
	public Archive getArchive() {
		return archive;
	}

	private byte[] getData(File file) {
		if (null != file) {
			try {
				return ArchiveUtil.inputStreamToBytes(file.getInputStream());
			} catch (FileNotFoundException e) {
				return null;
			} catch (IOException e) {
				throw new ArchiveRuntimeException(CommonArchiveResourceHandler.getString(CommonArchiveResourceHandler.io_ex_loading_EXC_, (new Object[]{file.getName()})), e); // = "An IO exception occurred loading "			}
			}
		}
		return null;
	}

	protected byte[] getClassBytesFor(String className) {
		if (className == null)
			return null;
		// Change the class name to a jar entry name
		String jarEntryName = ArchiveUtil.classNameToUri(className);
		return getData(getFile(jarEntryName));
	}

	protected EARFile getEARFile() {
		return (EARFile) getArchive().getContainer();
	}

	/**
	 * Insert the method's description here. Creation date: (11/21/00 6:58:05 PM)
	 * 
	 * @return java.lang.ClassLoader
	 */
	public java.lang.ClassLoader getExtraClassLoader() {
		return extraClassLoader;
	}

	/**
	 * Used for dynamic class loading in dependent jars in ears; the set is used to terminate a
	 * cycle if one exists; the cycle is invalid, but you never know what people might try...
	 */
	protected synchronized Class loadClass(String name, Set visitedArchives) throws ClassNotFoundException {
		if (visitedArchives.contains(getArchive()))
			throw new ClassNotFoundException(name);
		visitedArchives.add(getArchive());
		try {
			return super.loadClass(name, false);
		} catch (ClassNotFoundException ex) {
			return loadClassInDependentJarInEAR(name, visitedArchives);
		}
	}

	protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
		try {
			return super.loadClass(name, resolve);
		} catch (ClassNotFoundException ex) {
			Class c = loadClassInDependentJar(name);
			if (c != null && resolve)
				resolveClass(c);
			return c;
		}
	}

	protected Class loadClassInDependentJar(String name) throws ClassNotFoundException {

		if (inEARFile) {
			return loadClassInDependentJarInEAR(name);
		} else if (getExtraClassLoader() != null) {
			return getExtraClassLoader().loadClass(name);
		}
		throw new ClassNotFoundException(name);
	}

	protected Class loadClassInDependentJarInEAR(String name, Set visitedArchives) throws ClassNotFoundException {
		Object obj = getResourceInDependentJarInEAR(name, visitedArchives, CLASS_TYPE);
		if (obj == null) {
			throw new ClassNotFoundException(name);
		}
		return (Class) obj;
	}

	protected Class loadClassInDependentJarInEAR(String name) throws ClassNotFoundException {
		Object obj = getResourceInDependentJarInEAR(name, CLASS_TYPE);
		if (obj == null) {
			throw new ClassNotFoundException(name);
		}
		return (Class) obj;
	}

	protected File getFileFromDependentJar(String name) {
		Object obj = getResourceInDependentJarInEAR(name, FILE_TYPE);
		if (obj != null) {
			return (File) obj;
		}
		return null;
	}

	protected static final int CLASS_TYPE = 0;
	protected static final int FILE_TYPE = 1;

	protected Object getResourceInDependentJarInEAR(String name, int type) {
		Set visitedArchives = new HashSet(5);
		visitedArchives.add(getArchive());
		return getResourceInDependentJarInEAR(name, visitedArchives, type);
	}

	protected Object getResourceInDependentJarInEAR(String name, Set visitedArchives, int type) {
		String[] classpath = archive.getManifest().getClassPathTokenized();
		for (int i = 0; i < classpath.length; i++) {
			try {
				String uri = ArchiveUtil.deriveEARRelativeURI(classpath[i], archive);
				if (uri == null)
					continue;
				File jarFile = getEARFile().getFile(uri);
				if (jarFile.isArchive()) {
					Archive dep = (Archive) jarFile;
					switch (type) {
						case CLASS_TYPE :
							try {
								return ((ArchiveFileDynamicClassLoader) dep.getArchiveClassLoader()).loadClass(name, visitedArchives);
							} catch (ClassNotFoundException noDice) {
								continue;
							}
						case FILE_TYPE :
							try {
								return dep.getFile(name);
							} catch (FileNotFoundException noDice) {
								continue;
							}
					}
				}
			} catch (java.io.FileNotFoundException depJarNotInEAR) {
			}
		}
		return null;
	}

	/**
	 * Insert the method's description here. Creation date: (12/17/00 9:59:57 PM)
	 * 
	 * @param newArchive
	 *            com.ibm.etools.commonarchive.Archive
	 */
	public void setArchive(Archive newArchive) {
		archive = newArchive;
	}

	/**
	 * Insert the method's description here. Creation date: (11/21/00 6:58:05 PM)
	 * 
	 * @param newExtraClassLoader
	 *            java.lang.ClassLoader
	 */
	public void setExtraClassLoader(java.lang.ClassLoader newExtraClassLoader) {
		extraClassLoader = newExtraClassLoader;
	}

	public InputStream getResourceAsStream(String name) {
		try {
			File file = getFile(name);
			if (null != file) {
				return file.getInputStream();
			}
		} catch (IOException e) {
			throw new ArchiveRuntimeException(CommonArchiveResourceHandler.getString(CommonArchiveResourceHandler.io_ex_loading_EXC_, (new Object[]{name})), e); // = "An IO exception occurred loading "
		}
		return null;
	}

	protected File getFileFromArchive(String name) {
		try {
			return getArchive().getFile(name);
		} catch (FileNotFoundException e) {
		}
		return null;
	}



	protected File getFile(String name) {
		File file = getFileFromArchive(name);
		if (file == null) {
			file = getFileFromDependentJar(name);
		}
		return file;
	}

	protected URL findResource(String name) {
		if (getFile(name) != null) {
			try {
				return new URL(null, URL_PROTOCOL + "://" + name, handler);
			} catch (MalformedURLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return null;
	}

	private class ArchiveURLStreamHandler extends URLStreamHandler {
		public ArchiveURLStreamHandler() {
		}

		protected URLConnection openConnection(URL url) throws IOException {
			return new ArchiveURLConnection(url);
		}
	}

	private class ArchiveURLConnection extends URLConnection {
		private String resourceName;

		protected ArchiveURLConnection(URL url) {
			super(url);
			resourceName = url.toString().substring(URL_PROTOCOL.length() + 3);
		}

		public void connect() throws IOException {
		}

		public InputStream getInputStream() throws IOException {
			return getResourceAsStream(resourceName);
		}
	}

}

Back to the top