Kaydet (Commit) 1d3164df authored tarafından Rüdiger Timm's avatar Rüdiger Timm

INTEGRATION: CWS sb20 (1.6.136); FILE MERGED

2004/06/14 11:26:47 sb 1.6.136.1: #i29119# Replaced sandbox.jar-based class loader with an own one.
üst 462f3d3f
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
* *
* $RCSfile: RegistrationClassFinder.java,v $ * $RCSfile: RegistrationClassFinder.java,v $
* *
* $Revision: 1.6 $ * $Revision: 1.7 $
* *
* last change: $Author: dbo $ $Date: 2002-09-04 09:20:04 $ * last change: $Author: rt $ $Date: 2004-07-23 14:43:52 $
* *
* The Contents of this file are made available subject to the terms of * The Contents of this file are made available subject to the terms of
* either of the following licenses * either of the following licenses
...@@ -58,196 +58,105 @@ ...@@ -58,196 +58,105 @@
* *
* *
************************************************************************/ ************************************************************************/
package com.sun.star.comp.loader;
import com.sun.star.lib.sandbox.ClassContext;
import com.sun.star.lib.sandbox.ClassContextProxy;
import com.sun.star.lib.sandbox.Resource;
import com.sun.star.lib.sandbox.ResourceProxy;
public class RegistrationClassFinder {
final static boolean DEBUG = false;
protected ClassContext m_context = null;
protected String m_locationUrl = null;
protected String m_manifest = null;
protected String m_className = null;
public RegistrationClassFinder( String locationUrl )
throws java.io.IOException,
java.net.MalformedURLException
{
if(DEBUG) System.err.println("##### " + getClass().getName() + ".<init>:" + locationUrl);
m_locationUrl = locationUrl;
if(locationUrl.endsWith(".jar")) { package com.sun.star.comp.loader;
// The class loader is to be regarded as secure. Therefore the security manager
// should not restrict the execution of Java components.
m_context = ClassContextProxy.create(new java.net.URL(m_locationUrl), null, null, true);
m_manifest = locationUrl + "/META-INF/MANIFEST.MF";
}
}
private static String s_accessPath[];
private static boolean s_bInit = false;
private final static boolean checkAccessPath( java.net.URL url )
{
// init access path
if (! s_bInit)
{
String accessPath[] = null;
String env = System.getProperty( "com.sun.star.comp.loader.CPLD_ACCESSPATH" ); import com.sun.star.lib.util.WeakMap;
if (env != null) import java.io.File;
{ import java.io.IOException;
int nPos = 0; import java.net.URL;
java.util.StringTokenizer tokens = new java.util.StringTokenizer( env, ";" ); import java.net.URLClassLoader;
accessPath = new String[ tokens.countTokens() ]; import java.util.StringTokenizer;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
try final class RegistrationClassFinder {
{ public static Class find(String locationUrl)
while (tokens.hasMoreTokens()) throws ClassNotFoundException, IOException
{
try
{
accessPath[ nPos ] =
(new java.io.File( tokens.nextToken() )).getCanonicalPath();
++nPos;
}
catch (java.io.IOException exc)
{ {
synchronized (map) {
Class c = (Class) WeakMap.getValue(map.get(locationUrl));
if (c != null) {
return c;
} }
} }
URL url = new URL(locationUrl);
checkAccess(url);
String name = null;
Manifest mf = new JarInputStream(url.openStream()).getManifest();
if (mf != null) {
name = mf.getMainAttributes().getValue("RegistrationClassName");
} }
catch (java.util.NoSuchElementException exc) if (name == null) {
{ return null;
}
if (nPos != accessPath.length)
{
// realloc accessPath to nPos
String ar[] = new String[ nPos ];
System.arraycopy( accessPath, 0, ar, 0, nPos );
accessPath = ar;
}
} }
Class c = new URLClassLoader(new URL[] { url }).loadClass(name);
s_accessPath = accessPath; synchronized (map) {
s_bInit = true; Class c2 = (Class) WeakMap.getValue(map.get(locationUrl));
if (c2 != null) {
if (DEBUG && s_accessPath != null) return c2;
{
System.err.print( "> CPLD_ACCESSPATH: " );
for ( int nPos = 0; nPos < s_accessPath.length; ++nPos )
{
System.err.print( "\"" + s_accessPath[ nPos ] + "\" " );
} }
System.err.println(); map.put(locationUrl, c);
} }
return c;
} }
if (s_accessPath == null) private RegistrationClassFinder() {} // do not instantiate
return true; // no CPLD_ACCESSPATH set
if (! url.getProtocol().equals( "file" )) private static void checkAccess(URL url) throws ClassNotFoundException {
{ // The system property com.sun.star.comp.loader.CPLD_ACCESSPATH was
if (DEBUG) // introduced as a hack to restrict which UNO components can be
System.err.println( "> \"" + url.toExternalForm() + "\" is no file url!" ); // instantiated. It seems to be unused nowadays, and should probably be
return false; // replaced by the native Java security features, anyway.
if (accessPath != null) {
if (!url.getProtocol().equals("file")) {
throw new ClassNotFoundException(
"Access restriction: <" + url + "> is not a file URL");
} }
String p;
String surl; try {
p = new File(url.getFile()).getCanonicalPath();
try } catch (IOException e) {
{ throw new ClassNotFoundException(
surl = (new java.io.File( url.getFile() )).getCanonicalPath(); "Access restriction: <" + url + "> is bad: " + e);
} }
catch (java.io.IOException exc) for (int i = 0; i < accessPath.length; ++i) {
String p2 = accessPath[i];
if (p.startsWith(p2) && p.length() > p2.length()
&& (p2.charAt(p2.length() - 1) == File.separatorChar
|| p.charAt(p2.length()) == File.separatorChar))
{ {
if (DEBUG) return;
System.err.println( "> \"" + url.toExternalForm() + "\" cannot be resolved!" );
return false;
} }
if (DEBUG)
System.err.print( "> java loader looking up: \"" + surl + "\"..." );
// check if jar is in access path
for ( int nPos = 0; nPos < s_accessPath.length; ++nPos )
{
String path = s_accessPath[ nPos ];
if (0 == surl.indexOf( path ) &&
surl.length() > path.length() &&
(path.charAt( path.length() -1 ) == java.io.File.separatorChar ||
surl.charAt( path.length() ) == java.io.File.separatorChar)) // dir boundary
{
if (DEBUG)
System.err.println( "succeeded!" );
return true;
} }
throw new ClassNotFoundException(
"Access restriction: <" + url + "> is restricted");
} }
if (DEBUG)
System.err.println( "failed!" );
return false;
} }
public Class getRegistrationClass() private static final WeakMap map = new WeakMap();
throws java.io.IOException,
java.lang.ClassNotFoundException,
java.net.MalformedURLException
{
Class ret = null;
if (m_context != null) {
String className = null;
java.net.URL url = new java.net.URL(m_locationUrl);
if (! checkAccessPath( url ))
throw new ClassNotFoundException( "jar access failed!" );
Resource resource = ResourceProxy.load(url, null);
resource.loadJar(url);
m_context.addCargo( resource );
java.io.InputStream inManifest = ResourceProxy.load(new java.net.URL(m_manifest), null).getInputStream(); private static final String[] accessPath;
java.util.jar.Manifest manifest = new java.util.jar.Manifest( inManifest ); static {
java.util.jar.Attributes attributes = manifest.getMainAttributes(); String[] ap = null;
className = attributes.getValue( "RegistrationClassName" ); String p = System.getProperty(
"com.sun.star.comp.loader.CPLD_ACCESSPATH");
if (className != null) { if (p != null) {
ret = m_context.loadClass( className ); StringTokenizer t = new StringTokenizer(p, ";");
} ap = new String[t.countTokens()];
int i = 0;
while (t.hasMoreTokens()) {
try {
ap[i] = new File(t.nextToken()).getCanonicalPath();
++i;
} catch (IOException e) {}
} }
else if (i != ap.length) {
ret = Class.forName(m_locationUrl); String[] ap2 = new String[i];
System.arraycopy(ap, 0, ap2, 0, i);
if(DEBUG) System.err.println("##### " + getClass().getName() + ".getRegistrationClass:" + ret); ap = ap2;
return ret;
} }
public Class loadClass( String className )
throws java.lang.ClassNotFoundException,
java.io.IOException,
java.net.MalformedURLException
{
Class ret = null;
if (m_context != null) {
java.net.URL url = new java.net.URL( m_locationUrl );
if (! checkAccessPath( url ))
throw new ClassNotFoundException( "jar access failed!" );
Resource resource = ResourceProxy.load(url, null);
resource.loadJar(url);
ret = m_context.loadClass(className);
} else {
ret = Class.forName(className);
} }
accessPath = ap;
return ret;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment