|
Added
Link Here
|
| 1 |
package org.eclipse.equinox.servletbridge; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.net.MalformedURLException; |
| 5 |
import java.net.URL; |
| 6 |
import java.security.*; |
| 7 |
import java.util.*; |
| 8 |
import java.util.jar.JarEntry; |
| 9 |
import java.util.jar.JarFile; |
| 10 |
|
| 11 |
public class JarEntryClassLoader extends SecureClassLoader { |
| 12 |
private List entries = null; |
| 13 |
private URL osgiFrameworkURL = null; |
| 14 |
private JarEntryURLStreamHandler jarEntryURLStreamHandler = null; |
| 15 |
|
| 16 |
public JarEntryClassLoader(URL[] osgiFrameworkURLs, ClassLoader parent) { |
| 17 |
super(parent); |
| 18 |
jarEntryURLStreamHandler = new JarEntryURLStreamHandler(); |
| 19 |
this.osgiFrameworkURL = osgiFrameworkURLs[0]; // Expect only one framework jar |
| 20 |
entries = new ArrayList(); |
| 21 |
JarFile jarFile = null; |
| 22 |
|
| 23 |
try { |
| 24 |
jarFile = new JarFile(new File(osgiFrameworkURL.toURI())); |
| 25 |
JarEntry je = null; |
| 26 |
Enumeration e = jarFile.entries(); |
| 27 |
while (e.hasMoreElements()) { |
| 28 |
je = (JarEntry)e.nextElement(); |
| 29 |
if (!je.isDirectory()) { |
| 30 |
entries.add(je.getName()); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
catch (Exception e) { |
| 35 |
e.printStackTrace(); |
| 36 |
} |
| 37 |
finally { |
| 38 |
try {jarFile.close();} catch (IOException e) {} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
protected void addURL(URL url) { |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
protected synchronized Class findClass(String name) throws ClassNotFoundException { |
| 47 |
String className = name.replace('.', '/'); |
| 48 |
if (entries.contains(className+".class")) { |
| 49 |
InputStream is = null; |
| 50 |
try { |
| 51 |
URL url = new URL("jarentry", null, 0, osgiFrameworkURL.toExternalForm()+"!/"+className+".class", jarEntryURLStreamHandler); |
| 52 |
is = url.openStream(); |
| 53 |
byte[] buffer = new byte[4096]; |
| 54 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 55 |
int len = 0; |
| 56 |
while ((len = is.read(buffer, 0, 4096)) != -1) { |
| 57 |
baos.write(buffer, 0, len); |
| 58 |
} |
| 59 |
byte[] bytes = baos.toByteArray(); |
| 60 |
return defineClass(name, bytes, 0, bytes.length); |
| 61 |
} catch (IOException e) { |
| 62 |
e.printStackTrace(); |
| 63 |
} finally { |
| 64 |
try {if (is != null) { is.close(); } }catch(IOException e){} |
| 65 |
} |
| 66 |
} |
| 67 |
throw new ClassNotFoundException(name); |
| 68 |
} |
| 69 |
|
| 70 |
protected URL findResource(String name) { |
| 71 |
URL resource = null; |
| 72 |
|
| 73 |
if (entries.contains(name) || entries.contains("/"+name)) { |
| 74 |
if (name.charAt(0) != '/') |
| 75 |
name = "/"+name; |
| 76 |
try { |
| 77 |
resource = new URL("jarentry", null, 0, osgiFrameworkURL.toExternalForm()+"!"+name, jarEntryURLStreamHandler); |
| 78 |
} catch (MalformedURLException e) { |
| 79 |
resource = null; |
| 80 |
} |
| 81 |
} |
| 82 |
return resource; |
| 83 |
} |
| 84 |
|
| 85 |
public Enumeration findResources (String name) throws IOException { |
| 86 |
List resources = new ArrayList(); |
| 87 |
for (Iterator itr = entries.iterator(); itr.hasNext();) { |
| 88 |
String resource = (String)itr.next(); |
| 89 |
if (resource.endsWith(name)) { |
| 90 |
if (resource.charAt(0) != '/') { |
| 91 |
resource = "/"+resource; |
| 92 |
} |
| 93 |
URL url = new URL("jarentry", null, 0, osgiFrameworkURL.toExternalForm()+"!"+resource, jarEntryURLStreamHandler); |
| 94 |
resources.add(url); |
| 95 |
} |
| 96 |
} |
| 97 |
return Collections.enumeration(resources); |
| 98 |
} |
| 99 |
|
| 100 |
public URL getResource(String name) { |
| 101 |
URL resource = findResource(name); |
| 102 |
if (resource == null) { |
| 103 |
ClassLoader parent = getParent(); |
| 104 |
if (parent != null) |
| 105 |
resource = parent.getResource(name); |
| 106 |
} |
| 107 |
return resource; |
| 108 |
} |
| 109 |
|
| 110 |
protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { |
| 111 |
Class clazz = findLoadedClass(name); |
| 112 |
if (clazz == null) { |
| 113 |
try { |
| 114 |
clazz = findClass(name); |
| 115 |
} catch (ClassNotFoundException e) { |
| 116 |
ClassLoader parent = getParent(); |
| 117 |
if (parent != null) |
| 118 |
clazz = parent.loadClass(name); |
| 119 |
else |
| 120 |
clazz = getSystemClassLoader().loadClass(name); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (resolve) |
| 125 |
resolveClass(clazz); |
| 126 |
|
| 127 |
return clazz; |
| 128 |
} |
| 129 |
|
| 130 |
static final PermissionCollection allPermissions = new PermissionCollection() { |
| 131 |
private static final long serialVersionUID = 482874725021998286L; |
| 132 |
// The AllPermission permission |
| 133 |
Permission allPermission = new AllPermission(); |
| 134 |
|
| 135 |
// A simple PermissionCollection that only has AllPermission |
| 136 |
public void add(Permission permission) { |
| 137 |
// do nothing |
| 138 |
} |
| 139 |
|
| 140 |
public boolean implies(Permission permission) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
public Enumeration elements() { |
| 145 |
return new Enumeration() { |
| 146 |
int cur = 0; |
| 147 |
|
| 148 |
public boolean hasMoreElements() { |
| 149 |
return cur < 1; |
| 150 |
} |
| 151 |
|
| 152 |
public Object nextElement() { |
| 153 |
if (cur == 0) { |
| 154 |
cur = 1; |
| 155 |
return allPermission; |
| 156 |
} |
| 157 |
throw new NoSuchElementException(); |
| 158 |
} |
| 159 |
}; |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
static { |
| 164 |
// We do this to ensure the anonymous Enumeration class in allPermissions is pre-loaded |
| 165 |
if (allPermissions.elements() == null) |
| 166 |
throw new IllegalStateException(); |
| 167 |
} |
| 168 |
|
| 169 |
protected PermissionCollection getPermissions(CodeSource codesource) { |
| 170 |
return allPermissions; |
| 171 |
} |
| 172 |
} |