Index: TreeClassLoader.java =================================================================== RCS file: /usr/local/cvs/case/djava/sources/koala/dynamicjava/interpreter/TreeClassLoader.java,v --- TreeClassLoader.java 3 Jun 2002 08:58:53 -0000 1.1 +++ TreeClassLoader.java 5 Jun 2002 15:45:23 -0000 @@ -1,5 +1,6 @@ /* * DynamicJava - Copyright (C) 1999-2001 + * The Rationalizer AG - Copyright (C) 2002 * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files @@ -22,7 +23,7 @@ * Except as contained in this notice, the name of Dyade shall not be * used in advertising or otherwise to promote the sale, use or other * dealings in this Software without prior written authorization from - * Dyade. + * Dyade. The same applies to the name of The Rationalizer. * */ @@ -36,15 +37,84 @@ import koala.dynamicjava.tree.*; import koala.dynamicjava.util.*; +import java.security.CodeSource; +import java.security.SecureClassLoader; + +import java.net.URL; +import java.net.MalformedURLException; + /** - * This class is responsible for loading bytecode classes + * This class is responsible for loading bytecode classes. + * + *

All classes created by TreeClassLoaders + * have an identical + * {@link java.security CodeSource}. This code source has no certificates + * but may have a location. The latter is set to the value of the system + * property with the value of {@link #CODE_SOURCE_URL_PROPERTY} as key, if + * it is a valid URL. If the property is defined but its value is not a valid + * URL, the location of the code source is set to null. If the + * property is not defined, the value of {@link #DEFAULT_CODE_SOURCE_URL} is + * used as location.

* * @author Stephane Hillion + * @author Holger Krug * @version 1.1 - 1999/05/18 */ -public class TreeClassLoader extends ClassLoader +public class TreeClassLoader extends SecureClassLoader implements ClassLoaderContainer { + + /** + * The default value for the {@link java.security.CodeSource} URL. + * May be overriden by setting the system property with name given by + * the value of {@link #CODE_SOURCE_URL_PROPERTY}. + */ + public static String DEFAULT_CODE_SOURCE_URL = + "http://koala.ilog.fr/djava/javadoc/koala/dynamicjava/interpreter/TreeClassLoader.html"; + + /** + * Name of the system property to define the value for the URL + * of the {@link java.security.CodeSource} assigned to classes + * created by this classloader. The default value is the value of + * {@link #DEFAULT_CODE_SOURCE_URL}. + * + *

If the property value is not a wellformed URL, the URL is set + * to null. + */ + public static String CODE_SOURCE_URL_PROPERTY = + "koala.dynamicjava.interpreter.TreeClassLoader.codesource.url"; + + /** + * The code source for classes defined by instances of + * TreeClassLoader. Initializes when TreeClassLoader + * is loaded the first time. + * + * @see #DEFAULT_CODE_SOURCE_URL + * @see #CODE_SOURCE_URL_PROPERTY + */ + protected static final CodeSource codeSource; + + /** + * Initializes the code source. + */ + static { + try { + String url = System.getProperty(CODE_SOURCE_URL_PROPERTY); + if ( url != null ) codeSource = new CodeSource(new URL(url), null); + } catch (java.net.MalformedURLException mfue) { + // property value malformed, return null + // [XXX]: print error message + codeSource = new CodeSource(null, null); + } + try { + codeSource = new CodeSource(new URL(DEFAULT_CODE_SOURCE_URL), null); + } catch (java.net.MalformedURLException mfue) { + // should never appear + throw new RuntimeException(mfue.getMessage()); + } + + } + /** * The place where the interpreted classes are stored */ @@ -64,22 +134,24 @@ * The auxiliary class loader */ protected ClassLoader classLoader; - + /** * Creates a new class loader * @param i the object used to interpret the classes */ public TreeClassLoader(Interpreter i) { - interpreter = i; - } + super(i.getClass().getClassLoader()); + interpreter = i; + } /** * Converts an array of bytes into an instance of class Class and * links this class. + * * @exception ClassFormatError if the class could not be defined */ public Class defineClass(String name, byte[] code) { - Class c = defineClass(name, code, 0, code.length); + Class c = defineClass(name, code, 0, code.length, codeSource); classes.put(name, c); trees.remove(name); return c; @@ -154,6 +226,6 @@ } catch (ClassNotFoundException e) { } - return interpreter.loadClass(name); + return interpreter.loadClass(name); } }