View Javadoc

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