2
* Copyright Terracotta, Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package net.sf.ehcache.management;
18
import java.io.ByteArrayOutputStream;
20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.lang.reflect.InvocationTargetException;
23
import java.lang.reflect.Method;
25
import java.net.URLConnection;
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.Enumeration;
29
import java.util.List;
30
import java.util.jar.Attributes;
31
import java.util.jar.Manifest;
32
import java.util.jar.Attributes.Name;
34
import net.sf.ehcache.util.MergedEnumeration;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
40
* ResourceClassLoader can load classes nested in a subdirectory of a jar
42
* ehcache.jar!/net/sf/ehcache/CacheManager is in the "normal" classpath and will be loaded by any typical classloader
43
* ehcache.jar!/subdirectory/net/sf/ehcache/CacheManager can only be loaded by the ResourceClassLoader, with prefix "subdirectory"
45
* @author Anthony Dahanne
48
public class ResourceClassLoader extends ClassLoader {
50
private static final int BUFFER_SIZE = 1024;
51
private static final Logger LOG = LoggerFactory.getLogger(ResourceClassLoader.class);
52
private final String prefix;
53
private final String implementationVersion;
56
* Given a parent classloader and the prefix to apply to the lookup path
57
* Creates a ResourceClassLoader able to load classes from resources prefixed with "prefix"
63
public ResourceClassLoader(String prefix, ClassLoader parent) {
66
String temporaryImplementationVersion = null;
67
InputStream in = null;
68
// looking up the version of our jar, from the Manifest in the private package (prefix)
70
URL manifestResource = getParent().getResource(prefix + "/META-INF/MANIFEST.MF");
71
in = manifestResource.openStream();
72
Manifest man = new Manifest(in);
73
Attributes attributes = man.getMainAttributes();
74
temporaryImplementationVersion = attributes.getValue(Name.IMPLEMENTATION_VERSION);
75
} catch (Exception e) {
76
LOG.debug("Could not read the Manifest", e);
82
} catch (Exception e) {
86
this.implementationVersion = temporaryImplementationVersion;
90
public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
91
// changing the order of delegation to prefer the resourceClassLoader over its parents
92
Class c = findLoadedClass(name);
96
} catch (ClassNotFoundException e) {
97
c = super.loadClass(name, resolve);
107
public URL getResource(String name) {
109
url = findResource(name);
111
return super.getResource(name);
117
protected URL findResource(String name) {
118
URL resource = getParent().getResource(prefix + "/" + name);
124
* very similar to what OracleJDK classloader does,
125
* except the first resources (more important) are the ones found with our ResourceClassLoader
127
public Enumeration<URL> getResources(String resourceName) throws IOException {
128
Enumeration[] tmp = new Enumeration[2];
129
tmp[0] = findResources(resourceName);
130
tmp[1] = getParent().getResources(resourceName);
131
return new MergedEnumeration<URL>(tmp[0], tmp[1]);
136
protected Enumeration<URL> findResources(String name) throws IOException {
137
Enumeration<URL> resources = getParent().getResources(prefix + "/" + name);
138
// DEV-8100 add support for Jboss AS, translating vfs URLs
139
List<URL> urls = new ArrayList<URL>();
140
while (resources.hasMoreElements()) {
142
URL nextElement = resources.nextElement();
143
if (nextElement.toExternalForm().startsWith("vfs")) {
144
elementToAdd = translateFromVFSToPhysicalURL(nextElement);
146
elementToAdd = nextElement;
148
urls.add(elementToAdd);
150
return Collections.enumeration(urls);
154
protected Class<?> findClass(String className) throws ClassNotFoundException {
157
String classRealName = prefix + "/" + className.replace('.', '/') + ".class";
158
URL classResource = getParent().getResource(classRealName);
160
if (classResource != null) {
161
// classresource ok, let's define its package too
162
int index = className.lastIndexOf('.');
164
String pkgname = className.substring(0, index);
165
if (getPackage(pkgname) == null) {
166
definePackage(pkgname, null, null, null, null, implementationVersion, null, null);
169
InputStream in = null;
171
byte[] array = new byte[BUFFER_SIZE];
172
in = classResource.openStream();
173
ByteArrayOutputStream out = new ByteArrayOutputStream(array.length);
174
int length = in.read(array);
176
out.write(array, 0, length);
177
length = in.read(array);
179
Class<?> defineClass = defineClass(className, out.toByteArray(), 0, out.size());
181
} catch (IOException e) {
182
LOG.warn("Impossible to open " + classRealName + " for loading", e);
188
} catch (Exception e) {
193
throw new ClassNotFoundException(className);
198
* DEV-8100 add support for Jboss AS
199
* jersey does not understand Jboss VFS URLs , so we use Jboss VFS classes to translate those URLs to file: URLs
201
private URL translateFromVFSToPhysicalURL(URL vfsUrl) throws IOException {
202
URL physicalUrl = null;
203
URLConnection vfsURLConnection = vfsUrl.openConnection();
204
Object vfsVirtualFile = vfsURLConnection.getContent();
206
Class vfsUtilsClass = Class.forName("org.jboss.vfs.VFSUtils");
207
Class virtualFileClass = Class.forName("org.jboss.vfs.VirtualFile");
208
Method getPathName = virtualFileClass.getDeclaredMethod("getPathName", new Class[0]);
209
Method getPhysicalURL = vfsUtilsClass.getDeclaredMethod("getPhysicalURL", virtualFileClass);
210
Method recursiveCopy = vfsUtilsClass.getDeclaredMethod("recursiveCopy", virtualFileClass, File.class);
211
String pathName = (String) getPathName.invoke(vfsVirtualFile, (Object[]) null);
212
physicalUrl = (URL) getPhysicalURL.invoke(null, vfsVirtualFile);
213
File physicalURLAsFile = new File(physicalUrl.getFile());
214
// https://issues.jboss.org/browse/JBAS-8786
215
if (physicalURLAsFile.isDirectory() && physicalURLAsFile.list().length == 0) {
216
// jboss does not unpack the libs in WEB-INF/lib, we have to unpack them (partially) ourselves
217
unpackVfsResourceToPhysicalURLLocation(physicalUrl, vfsVirtualFile, recursiveCopy);
219
} catch (ClassNotFoundException e) {
220
// jboss-5 and below doesn't have this class, so just return the vfsUrl,
221
// in case the library loading this resource knows how to handle it
222
physicalUrl = vfsUrl;
223
} catch (NoSuchMethodException e) {
224
throw new RuntimeException(e);
225
} catch (InvocationTargetException e) {
226
throw new RuntimeException(e.getCause());
227
} catch (IllegalAccessException e) {
228
throw new RuntimeException(e);
229
} catch (IllegalArgumentException e) {
230
throw new RuntimeException(e);
235
private void unpackVfsResourceToPhysicalURLLocation(URL physicalUrl, Object vfsVirtualFile, Method recursiveCopy)
236
throws IllegalAccessException, InvocationTargetException {
237
String physicalPath = physicalUrl.getFile() + "/../";
238
recursiveCopy.invoke(null, vfsVirtualFile, new File(physicalPath));