Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 190279 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/servletbridge/FrameworkLauncher.java (-2 / +2 lines)
Lines 104-110 Link Here
104
	protected ServletContext context;
104
	protected ServletContext context;
105
	private File platformDirectory;
105
	private File platformDirectory;
106
	private ClassLoader frameworkContextClassLoader;
106
	private ClassLoader frameworkContextClassLoader;
107
	private URLClassLoader frameworkClassLoader;
107
	private ClassLoader frameworkClassLoader;
108
108
109
	void init(ServletConfig servletConfig) {
109
	void init(ServletConfig servletConfig) {
110
		config = servletConfig;
110
		config = servletConfig;
Lines 245-251 Link Here
245
			System.setProperty("osgi.framework.useSystemProperties", "false"); //$NON-NLS-1$ //$NON-NLS-2$
245
			System.setProperty("osgi.framework.useSystemProperties", "false"); //$NON-NLS-1$ //$NON-NLS-2$
246
246
247
			URL[] osgiURLArray = {new URL((String) initalPropertyMap.get(OSGI_FRAMEWORK))};
247
			URL[] osgiURLArray = {new URL((String) initalPropertyMap.get(OSGI_FRAMEWORK))};
248
			frameworkClassLoader = new ChildFirstURLClassLoader(osgiURLArray, this.getClass().getClassLoader());
248
			frameworkClassLoader = new JarEntryClassLoader(osgiURLArray, this.getClass().getClassLoader());
249
			Class clazz = frameworkClassLoader.loadClass(STARTER);
249
			Class clazz = frameworkClassLoader.loadClass(STARTER);
250
250
251
			Method setInitialProperties = clazz.getMethod("setInitialProperties", new Class[] {Map.class}); //$NON-NLS-1$
251
			Method setInitialProperties = clazz.getMethod("setInitialProperties", new Class[] {Map.class}); //$NON-NLS-1$
(-)src/org/eclipse/equinox/servletbridge/JarEntryURLStreamHandler.java (+17 lines)
Added Link Here
1
package org.eclipse.equinox.servletbridge;
2
3
import java.io.IOException;
4
import java.net.*;
5
6
public class JarEntryURLStreamHandler extends URLStreamHandler {
7
8
	protected URLConnection openConnection(URL url) throws IOException {
9
        URLConnection connection = new JarEntryURLConnection(url);
10
        connection.connect();
11
        return connection;
12
	}
13
14
    protected void parseURL(URL url, String spec, int start, int limit) {
15
        setURL(url, "jarentry", null, 0, null, null, spec.substring(start, limit), null, null);
16
    }
17
}
(-)src/org/eclipse/equinox/servletbridge/JarEntryClassLoader.java (+172 lines)
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
}
(-)src/org/eclipse/equinox/servletbridge/JarEntryURLConnection.java (+55 lines)
Added Link Here
1
package org.eclipse.equinox.servletbridge;
2
3
import java.io.*;
4
import java.net.URL;
5
import java.net.URLConnection;
6
import java.util.zip.ZipEntry;
7
import java.util.zip.ZipFile;
8
9
public class JarEntryURLConnection extends URLConnection {
10
	private boolean connected = false;
11
    private URL url = null;;
12
13
	public JarEntryURLConnection(URL url) {
14
		super(url);
15
		this.url = url;
16
	}
17
	
18
    public synchronized void connect() throws IOException  {
19
        if (connected) {
20
            return;
21
        }
22
        connected = true;
23
    }
24
    
25
    public InputStream getInputStream() {
26
        ZipFile zipFile = null;
27
        InputStream is = null;
28
        InputStream zis = null;
29
        try
30
        {
31
            String file = url.getFile();
32
            int bang = file.indexOf('!');
33
            String name = file.substring(bang+2);
34
            file = file.substring(5, bang);
35
            zipFile = new ZipFile(file);
36
            ZipEntry zipEntry = zipFile.getEntry(name);
37
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
38
            zis = zipFile.getInputStream(zipEntry);
39
            byte[] buf = new byte[4096];
40
            int len;
41
            while ((len = zis.read(buf)) != -1) {
42
                baos.write(buf, 0, len);
43
            }
44
            is = new ByteArrayInputStream(baos.toByteArray());
45
        }
46
        catch ( Exception e ) {
47
            e.printStackTrace();
48
        }
49
        finally {
50
            if (zis != null) try{zis.close();}catch(IOException e){}
51
            if (zipFile != null) try{zipFile.close();}catch(IOException e){}
52
        }
53
        return is;
54
    }
55
}

Return to bug 190279