View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001-2006 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.app.util;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.net.MalformedURLException;
28  import java.net.URL;
29  import java.net.URLConnection;
30  import java.util.Enumeration;
31  import java.util.Hashtable;
32  import java.util.NoSuchElementException;
33  import java.util.jar.JarEntry;
34  import java.util.jar.JarInputStream;
35  
36  /**
37   * @deprecated use MIDletClassLoader 
38   */
39  public class MIDletClassLoader /*extends SystemClassLoader*/ {
40  
41  //	protected Hashtable entries;
42  //	
43  //	private ResURLStreamHandler resUrlStreamHandler;
44  //	
45  //	public static final boolean debug = false;
46  //	
47  //	public MIDletClassLoader(ClassLoader parent) {
48  //		super(parent);
49  //		entries = new Hashtable();
50  //		resUrlStreamHandler = new ResURLStreamHandler(entries);
51  //	}
52  //
53  //	
54  //	public void addURL(URL midletSource) throws IOException {
55  //		 String path = midletSource.toExternalForm();
56  //		if (path.endsWith(".jar")) {
57  //			addJarURL(midletSource);
58  //		} else if (path.startsWith("file:")) {
59  //			addPathURL(midletSource);
60  //		} else {
61  //			throw new IOException("URL Type not supported: " + midletSource);
62  //		}
63  //	}
64  //	
65  //	private void addPathURL(URL url) throws IOException {
66  //		String path = url.toExternalForm();
67  //		path = path.substring("file:".length(), path.length());
68  //		File classesDir = new File(path);
69  //		if ((!classesDir.exists()) || (!classesDir.isDirectory())) {
70  //			throw new IOException("URL Type not supported: " + url);
71  //		}
72  //		int baseLen = path.length();
73  //		for (Enumeration en = new DirectoryEnumeration(classesDir); en.hasMoreElements();) {
74  //			File file = (File) en.nextElement();
75  //			if (!file.isDirectory()) {
76  //				String name = file.getAbsolutePath().substring(baseLen);
77  //				if (!allowEntryName(name)) {
78  //					continue;
79  //				}
80  //				InputStream is = new FileInputStream(file);
81  //				byte[] tmp = new byte[(int)file.length()];
82  //				is.read(tmp);
83  //				if (debug) {
84  //					System.out.println("add entry: " + name);
85  //				}
86  //				entries.put(name, tmp);
87  //			}
88  //		}
89  //	}
90  //	
91  //	private class DirectoryEnumeration implements Enumeration {
92  //
93  //		File[] files;
94  //		
95  //		int processing;
96  //
97  //		Enumeration child = null;
98  //		
99  //		DirectoryEnumeration(File dir) {
100 //			files = dir.listFiles();
101 //			if (files == null) {
102 //				throw new Error(dir.getAbsolutePath() + " path does not denote a directory");
103 //			}
104 //			processing = 0;
105 //		}
106 //
107 //		public boolean hasMoreElements() {
108 //			return ((child != null) && (child.hasMoreElements())) || (processing < files.length);
109 //		}
110 //
111 //		public Object nextElement() {
112 //			if (child != null) {
113 //				try {
114 //					return child.nextElement();
115 //				} catch (NoSuchElementException e) {
116 //					child = null;
117 //				}
118 //			}
119 //			if (processing >= files.length) {
120 //				throw new NoSuchElementException();
121 //			}
122 //			File next = files[processing++];
123 //			if (next.isDirectory()) {
124 //				child = new DirectoryEnumeration(next);
125 //			}
126 //			return next;
127 //		}
128 //
129 //	}
130 //	
131 //	private void addJarURL(URL midletSource) throws IOException {
132 //		byte[] cache = new byte[1024];
133 //		JarInputStream jis = null;
134 //		try {
135 //			URLConnection conn = midletSource.openConnection();
136 //			jis = new JarInputStream(conn.getInputStream());
137 //			while (true) {
138 //				JarEntry entry = jis.getNextJarEntry();
139 //				if (entry != null) {
140 //					if (!entry.isDirectory()) {
141 //						if (!allowEntryName(entry.getName())) {
142 //							continue;
143 //						}
144 //						int offset = 0;
145 //						int i = 0;
146 //						while ((i = jis.read(cache, offset, cache.length - offset)) != -1) {
147 //							offset += i;
148 //							if (offset >= cache.length) {
149 //								byte newcache[] = new byte[cache.length + 1024];
150 //								System.arraycopy(cache, 0, newcache, 0, cache.length);
151 //								cache = newcache;
152 //							}
153 //						}
154 //						byte[] tmp = new byte[offset];
155 //						System.arraycopy(cache, 0, tmp, 0, offset);
156 //						if (debug) {
157 //							System.out.println("add entry: " + entry.getName());
158 //						}
159 //						entries.put(entry.getName(), tmp);
160 //					}
161 //				} else {
162 //					break;
163 //				}
164 //			}
165 //		} finally {
166 //			if (jis != null) {
167 //				try {
168 //					jis.close();
169 //				} catch (IOException ignore) {
170 //				}
171 //			}
172 //		}
173 //	}
174 //
175 //    /**
176 //     * Loads the class with the specified <a href="#name">binary name</a>.
177 //     * This implementation of this method searches for classes in the
178 //     * following order:
179 //     *
180 //     * <p><ol>
181 //     *
182 //     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
183 //     *   has already been loaded.  </p></li>
184 //     *
185 //     *   <li><p> Invoke the {@link #findClass(String)} method to find the
186 //     *   class in this class loader.  </p></li>
187 //     *
188 //     *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
189 //     *   on the parent class loader.  If the parent is <tt>null</tt> the class
190 //     *   loader built-in to the virtual machine is used, instead.  </p></li>
191 //     *
192 //     * </ol>
193 //     *
194 //     */
195 //	protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
196 //		// First, check if the class has already been loaded
197 //		Class result = findLoadedClass(name);
198 //		if (result == null) {
199 //			if (hasClassData(name)) {
200 //				result = findClass(name);
201 //			} else {
202 //				result = super.loadClass(name, false);
203 //			}
204 //		}
205 //		if (resolve) {
206 //		    resolveClass(result);
207 //		}
208 //		return result;
209 //	}
210 //	
211 //	public Class findClass(String name) throws ClassNotFoundException {
212 //		Class result = findLoadedClass(name);
213 //		if (result == null) {
214 //			byte[] b = loadClassData(name);
215 //			result = defineClass(name, b, 0, b.length);
216 //		}
217 //		return result;
218 //	}
219 //
220 //	public InputStream getResourceAsStream(String name) {
221 //		String newname;
222 //		if (name.startsWith("/")) {
223 //			newname = name.substring(1);
224 //		} else {
225 //			newname = name;
226 //		}
227 //		byte[] tmp = (byte[]) entries.get(newname);
228 //		if (tmp != null) {
229 //			InputStream is = new ByteArrayInputStream(tmp);
230 //			return is;
231 //		}
232 //
233 //		return getClass().getResourceAsStream(name);
234 //	}
235 //
236 //	private boolean allowEntryName(String name) {
237 //		return !((name.startsWith("javax/") || name.startsWith("java/")));
238 //	}
239 //	
240 //	private boolean allowClass(String name) {
241 //		return !((name.startsWith("javax.") || name.startsWith("java.")));
242 //	}
243 //	
244 //	private boolean hasClassData(String name) {
245 //		if (!allowClass(name)) {
246 //			return false;
247 //		}
248 //		name = name.replace('.', '/') + ".class";
249 //		return (entries.get(name) != null);
250 //	}
251 //
252 //	protected byte[] loadClassData(String name) throws ClassNotFoundException {
253 //		name = name.replace('.', '/') + ".class";
254 //		byte[] result = (byte[]) entries.get(name);
255 //		if (result == null) {
256 //			throw new ClassNotFoundException(name);
257 //		}
258 //
259 //		return result;
260 //	}
261 //
262 //	protected URL findResource(String name) {
263 //		if (entries.containsKey(name)) {
264 //			try {
265 //				return new URL(null, "res:" + name, resUrlStreamHandler);
266 //			} catch (MalformedURLException ex) {
267 //				ex.printStackTrace();
268 //				return null;
269 //			}
270 //		}
271 //		return null;
272 //	}
273 
274 }