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 ) import com.sun.star.lib.util.WeakMap;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.StringTokenizer;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
final class RegistrationClassFinder {
public static Class find(String locationUrl)
throws ClassNotFoundException, IOException
{ {
// init access path synchronized (map) {
if (! s_bInit) Class c = (Class) WeakMap.getValue(map.get(locationUrl));
{ if (c != null) {
String accessPath[] = null; return c;
String env = System.getProperty( "com.sun.star.comp.loader.CPLD_ACCESSPATH" );
if (env != null)
{
int nPos = 0;
java.util.StringTokenizer tokens = new java.util.StringTokenizer( env, ";" );
accessPath = new String[ tokens.countTokens() ];
try
{
while (tokens.hasMoreTokens())
{
try
{
accessPath[ nPos ] =
(new java.io.File( tokens.nextToken() )).getCanonicalPath();
++nPos;
}
catch (java.io.IOException exc)
{
}
}
}
catch (java.util.NoSuchElementException exc)
{
}
if (nPos != accessPath.length)
{
// realloc accessPath to nPos
String ar[] = new String[ nPos ];
System.arraycopy( accessPath, 0, ar, 0, nPos );
accessPath = ar;
}
}
s_accessPath = accessPath;
s_bInit = true;
if (DEBUG && s_accessPath != null)
{
System.err.print( "> CPLD_ACCESSPATH: " );
for ( int nPos = 0; nPos < s_accessPath.length; ++nPos )
{
System.err.print( "\"" + s_accessPath[ nPos ] + "\" " );
}
System.err.println();
} }
} }
URL url = new URL(locationUrl);
if (s_accessPath == null) checkAccess(url);
return true; // no CPLD_ACCESSPATH set String name = null;
Manifest mf = new JarInputStream(url.openStream()).getManifest();
if (! url.getProtocol().equals( "file" )) if (mf != null) {
{ name = mf.getMainAttributes().getValue("RegistrationClassName");
if (DEBUG)
System.err.println( "> \"" + url.toExternalForm() + "\" is no file url!" );
return false;
}
String surl;
try
{
surl = (new java.io.File( url.getFile() )).getCanonicalPath();
} }
catch (java.io.IOException exc) if (name == null) {
{ return null;
if (DEBUG)
System.err.println( "> \"" + url.toExternalForm() + "\" cannot be resolved!" );
return false;
} }
Class c = new URLClassLoader(new URL[] { url }).loadClass(name);
if (DEBUG) synchronized (map) {
System.err.print( "> java loader looking up: \"" + surl + "\"..." ); Class c2 = (Class) WeakMap.getValue(map.get(locationUrl));
if (c2 != null) {
// check if jar is in access path return c2;
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;
} }
map.put(locationUrl, c);
} }
return c;
if (DEBUG)
System.err.println( "failed!" );
return false;
} }
public Class getRegistrationClass() private RegistrationClassFinder() {} // do not instantiate
throws java.io.IOException,
java.lang.ClassNotFoundException, private static void checkAccess(URL url) throws ClassNotFoundException {
java.net.MalformedURLException // The system property com.sun.star.comp.loader.CPLD_ACCESSPATH was
{ // introduced as a hack to restrict which UNO components can be
Class ret = null; // instantiated. It seems to be unused nowadays, and should probably be
// replaced by the native Java security features, anyway.
if (m_context != null) { if (accessPath != null) {
String className = null; if (!url.getProtocol().equals("file")) {
throw new ClassNotFoundException(
java.net.URL url = new java.net.URL(m_locationUrl); "Access restriction: <" + url + "> is not a file URL");
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();
java.util.jar.Manifest manifest = new java.util.jar.Manifest( inManifest );
java.util.jar.Attributes attributes = manifest.getMainAttributes();
className = attributes.getValue( "RegistrationClassName" );
if (className != null) {
ret = m_context.loadClass( className );
} }
String p;
try {
p = new File(url.getFile()).getCanonicalPath();
} catch (IOException e) {
throw new ClassNotFoundException(
"Access restriction: <" + url + "> is bad: " + e);
}
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))
{
return;
}
}
throw new ClassNotFoundException(
"Access restriction: <" + url + "> is restricted");
} }
else
ret = Class.forName(m_locationUrl);
if(DEBUG) System.err.println("##### " + getClass().getName() + ".getRegistrationClass:" + ret);
return ret;
} }
public Class loadClass( String className ) private static final WeakMap map = new WeakMap();
throws java.lang.ClassNotFoundException,
java.io.IOException, private static final String[] accessPath;
java.net.MalformedURLException static {
{ String[] ap = null;
Class ret = null; String p = System.getProperty(
"com.sun.star.comp.loader.CPLD_ACCESSPATH");
if (m_context != null) { if (p != null) {
java.net.URL url = new java.net.URL( m_locationUrl ); StringTokenizer t = new StringTokenizer(p, ";");
if (! checkAccessPath( url )) ap = new String[t.countTokens()];
throw new ClassNotFoundException( "jar access failed!" ); int i = 0;
Resource resource = ResourceProxy.load(url, null); while (t.hasMoreTokens()) {
resource.loadJar(url); try {
ap[i] = new File(t.nextToken()).getCanonicalPath();
ret = m_context.loadClass(className); ++i;
} else { } catch (IOException e) {}
ret = Class.forName(className); }
if (i != ap.length) {
String[] ap2 = new String[i];
System.arraycopy(ap, 0, ap2, 0, i);
ap = ap2;
}
} }
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