// get the name of the class to be loaded from the manifest
String className = null; Class clazz = Loader.class;
ClassLoader loader = clazz.getClassLoader();
ArrayList<URL> res = new ArrayList<URL>(); try {
Enumeration<URL> en = loader.getResources( "META-INF/MANIFEST.MF" ); while ( en.hasMoreElements() ) {
res.add( en.nextElement() );
} // the jarfile with the com/sun/star/lib/loader/Loader.class // per-entry attribute is most probably the last resource in the // list, therefore search backwards for ( int i = res.size() - 1; i >= 0; i-- ) {
URL jarurl = res.get( i ); try {
JarURLConnection jarConnection =
(JarURLConnection) jarurl.openConnection();
Manifest mf = jarConnection.getManifest();
Attributes attrs = (mf != null) ? mf.getAttributes( "com/sun/star/lib/loader/Loader.class") : null; if ( attrs != null ) {
className = attrs.getValue( "Application-Class" ); if ( className != null ) break;
}
} catch ( IOException e ) { // if an I/O error occurs when opening a new // JarURLConnection, ignore this manifest file
System.err.println( "com.sun.star.lib.loader.Loader::" + "main: bad manifest file: " + e );
}
}
} catch ( IOException e ) { // if an I/O error occurs when getting the manifest resources, // try to get the name of the class to be loaded from the argument // list
System.err.println( "com.sun.star.lib.loader.Loader::" + "main: cannot get manifest resources: " + e );
}
// if no manifest entry was found, get the name of the class // to be loaded from the argument list
String[] args; if ( className == null ) { if ( arguments.length > 0 ) {
className = arguments[0];
args = new String[arguments.length - 1];
System.arraycopy( arguments, 1, args, 0, args.length );
} else { thrownew IllegalArgumentException( "The name of the class to be loaded must be either " + "specified in the Main-Class attribute of the " + "com/sun/star/lib/loader/Loader.class entry " + "of the manifest file or as a command line argument." );
}
} else {
args = arguments;
}
// load the class with the customized class loader and // invoke the main method if ( className != null ) {
ClassLoader cl = getCustomLoader(); Thread.currentThread().setContextClassLoader(cl); Class c = cl.loadClass( className );
@SuppressWarnings("unchecked")
Method m = c.getMethod( "main", newClass[] { String[].class } );
m.invoke( null, new Object[] { args } );
}
}
// get the urls from which to load classes and resources // from the class path
ArrayList<URL> vec = new ArrayList<URL>();
String classpath = null; try {
classpath = System.getProperty( "java.class.path" );
} catch ( SecurityException e ) { // don't add the class path entries to the list of class // loader URLs
System.err.println( "com.sun.star.lib.loader.Loader::" + "getCustomLoader: cannot get system property " + "java.class.path: " + e );
} if ( classpath != null ) {
addUrls(vec, classpath, File.pathSeparator);
}
// get the urls from which to load classes and resources // from the UNO installation
String path = InstallationFinder.getPath(); if ( path != null ) {
callUnoinfo(path, vec);
} else {
System.err.println( "com.sun.star.lib.loader.Loader::" + "getCustomLoader: no UNO installation found!" );
}
// copy urls to array final URL[] urls = new URL[vec.size()];
vec.toArray( urls );
// instantiate class loader
m_Loader = AccessController.doPrivileged( new PrivilegedAction<ClassLoader>() { public ClassLoader run() { returnnew CustomURLClassLoader(urls);
}
});
}
return m_Loader;
}
privatestaticvoid addUrls(ArrayList<URL> urls, String data, String delimiter) {
StringTokenizer tokens = new StringTokenizer( data, delimiter ); while ( tokens.hasMoreTokens() ) { try {
urls.add( new File( tokens.nextToken() ).toURI().toURL() );
} catch ( MalformedURLException e ) { // don't add this class path entry to the list of class loader // URLs
System.err.println( "com.sun.star.lib.loader.Loader::" + "getCustomLoader: bad pathname: " + e );
}
}
}
privatestaticvoid close(InputStream c) { if (c == null) return; try {
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
privatestaticvoid callUnoinfo(String path, ArrayList<URL> urls) {
Process p; try {
p = Runtime.getRuntime().exec( new String[] { new File(path, "unoinfo").getPath(), "java" });
} catch (IOException e) {
System.err.println( "com.sun.star.lib.loader.Loader::getCustomLoader: exec" + " unoinfo: " + e); return;
} new Drain(p.getErrorStream()).start(); int code; byte[] buf = newbyte[1000]; int n = 0;
InputStream is = null; try {
is = p.getInputStream();
code = is.read(); for (;;) { if (n == buf.length) { if (n > Integer.MAX_VALUE / 2) {
System.err.println( "com.sun.star.lib.loader.Loader::getCustomLoader:" + " too much unoinfo output"); return;
} byte[] buf2 = newbyte[2 * n];
System.arraycopy(buf, 0, buf2, 0, n);
buf = buf2;
} int k = is.read(buf, n, buf.length - n); if (k == -1) { break;
}
n += k;
}
} catch (IOException e) {
System.err.println( "com.sun.star.lib.loader.Loader::getCustomLoader: reading" + " unoinfo output: " + e); return;
} finally {
close(is);
}
int ev; try {
ev = p.waitFor();
} catch (InterruptedException e) { Thread.currentThread().interrupt();
System.err.println( "com.sun.star.lib.loader.Loader::getCustomLoader: waiting for" + " unoinfo: " + e); return;
} if (ev != 0) {
System.err.println( "com.sun.star.lib.loader.Loader::getCustomLoader: unoinfo"
+ " exit value " + n); return;
}
String s; if (code == '0') {
s = new String(buf);
} elseif (code == '1') { try {
s = new String(buf, "UTF-16LE");
} catch (UnsupportedEncodingException e) {
System.err.println( "com.sun.star.lib.loader.Loader::getCustomLoader:" + " transforming unoinfo output: " + e); return;
}
} else {
System.err.println( "com.sun.star.lib.loader.Loader::getCustomLoader: bad unoinfo"
+ " output"); return;
}
addUrls(urls, s, "\0");
}
public CustomURLClassLoader( URL[] urls ) { super( urls );
}
@Override protectedClass<?> findClass( String name ) throws ClassNotFoundException { // This is only called via this.loadClass -> super.loadClass -> // this.findClass, after this.loadClass has already called // super.findClass, so no need to call super.findClass again: thrownew ClassNotFoundException( name );
}
@Override protectedsynchronizedClass<?> loadClass( String name, boolean resolve ) throws ClassNotFoundException
{ Class c = findLoadedClass( name ); if ( c == null ) { try {
c = super.findClass( name );
} catch ( ClassNotFoundException e ) { returnsuper.loadClass( name, resolve );
} catch ( SecurityException e ) { // A SecurityException "Prohibited package name: java.lang" // may occur when the user added the JVM's rt.jar to the // java.class.path: returnsuper.loadClass( name, resolve );
}
} if ( resolve ) {
resolveClass( c );
} return c;
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.