org.microemu.app.classloader.ExtensionsClassLoader Maven / Gradle / Ivy
/**
* MicroEmulator
* Copyright (C) 2006-2007 Bartek Teodorczyk
* Copyright (C) 2006-2007 Vlad Skarzhevskyy
*
* It is licensed under the following two licenses as alternatives:
* 1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
* 2. Apache License (the "AL") Version 2.0
*
* You may not use this file except in compliance with at least one of
* the above two licenses.
*
* You may obtain a copy of the LGPL at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*
* You may obtain a copy of the AL at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the LGPL or the AL for the specific language governing permissions and
* limitations.
*
* @version $Id: ExtensionsClassLoader.java 1605 2008-02-25 21:07:14Z barteo $
*/
package org.microemu.app.classloader;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.StringTokenizer;
import org.microemu.app.util.IOUtils;
import org.microemu.log.Logger;
/**
*
* Class loader for device and other Extensions
*
* @author vlads
*
*/
public class ExtensionsClassLoader extends URLClassLoader {
private final static boolean debug = false;
/* The context to be used when loading classes and resources */
private AccessControlContext acc;
public ExtensionsClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
acc = AccessController.getContext();
}
public void addURL(URL url) {
super.addURL(url);
}
public void addClasspath(String classpath) {
StringTokenizer st = new StringTokenizer(classpath, ";");
while (st.hasMoreTokens()) {
try {
String path = st.nextToken();
if (path.startsWith("file:")) {
addURL(new URL(path));
} else {
addURL(new URL(IOUtils.getCanonicalFileURL(new File(path))));
}
} catch (MalformedURLException e) {
throw new Error(e);
}
}
}
/**
* Finds the resource with the given name. A resource is some data (images,
* audio, text, etc) that can be accessed by class code in a way that is
* independent of the location of the code.
*
*
* The name of a resource is a '/'-separated path name that
* identifies the resource.
*
*
* Search order is reverse to standard implemenation
*
*
*
* This method will first use {@link #findResource(String)} to find the
* resource. That failing, this method will NOT invoke the parent class
* loader.
*
*
* @param name
* The resource name
*
* @return A URL object for reading the resource, or
* null if the resource could not be found or the invoker
* doesn't have adequate privileges to get the resource.
*
*/
public URL getResource(final String name) {
try {
URL url = (URL) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
return findResource(name);
}
}, acc);
if (url != null) {
return url;
}
} catch (PrivilegedActionException e) {
if (debug) {
Logger.error("Unable to find resource " + name + " ", e);
}
}
return super.getResource(name);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy