20package com.sun.star.lib.loader;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.UnsupportedEncodingException;
26import java.lang.reflect.Method;
27import java.net.JarURLConnection;
28import java.net.MalformedURLException;
30import java.net.URLClassLoader;
31import java.util.Enumeration;
32import java.util.jar.Attributes;
33import java.util.jar.Manifest;
34import java.util.StringTokenizer;
35import java.util.ArrayList;
36import java.security.AccessController;
37import java.security.PrivilegedAction;
67 ClassLoader
loader = clazz.getClassLoader();
68 ArrayList<URL> res =
new ArrayList<URL>();
70 Enumeration<URL> en =
loader.getResources(
"META-INF/MANIFEST.MF" );
71 while ( en.hasMoreElements() ) {
72 res.add( en.nextElement() );
77 for (
int i = res.size() - 1;
i >= 0;
i-- ) {
78 URL jarurl = res.get(
i );
80 JarURLConnection jarConnection =
81 (JarURLConnection) jarurl.openConnection();
82 Manifest mf = jarConnection.getManifest();
83 Attributes attrs = (mf !=
null) ? mf.getAttributes(
84 "com/sun/star/lib/loader/Loader.class") :
null;
85 if ( attrs !=
null ) {
86 className = attrs.getValue(
"Application-Class" );
87 if ( className !=
null )
90 }
catch ( IOException e ) {
93 System.err.println(
"com.sun.star.lib.loader.Loader::" +
94 "main: bad manifest file: " + e );
97 }
catch ( IOException e ) {
101 System.err.println(
"com.sun.star.lib.loader.Loader::" +
102 "main: cannot get manifest resources: " + e );
108 if ( className ==
null ) {
109 if ( arguments.length > 0 ) {
110 className = arguments[0];
114 throw new IllegalArgumentException(
115 "The name of the class to be loaded must be either " +
116 "specified in the Main-Class attribute of the " +
117 "com/sun/star/lib/loader/Loader.class entry " +
118 "of the manifest file or as a command line argument." );
126 if ( className !=
null ) {
128 Thread.currentThread().setContextClassLoader(cl);
129 Class c = cl.loadClass( className );
130 @SuppressWarnings(
"unchecked")
147 ArrayList<URL> vec =
new ArrayList<URL>();
150 classpath =
System.getProperty(
"java.class.path" );
151 }
catch ( SecurityException e ) {
154 System.err.println(
"com.sun.star.lib.loader.Loader::" +
155 "getCustomLoader: cannot get system property " +
156 "java.class.path: " + e );
158 if ( classpath !=
null ) {
164 String path = InstallationFinder.getPath();
165 if ( path !=
null ) {
168 System.err.println(
"com.sun.star.lib.loader.Loader::" +
169 "getCustomLoader: no UNO installation found!" );
173 final URL[] urls =
new URL[vec.size()];
177 m_Loader = AccessController.doPrivileged(
178 new PrivilegedAction<ClassLoader>() {
179 public ClassLoader
run() {
188 private static void addUrls(ArrayList<URL> urls, String data, String delimiter) {
189 StringTokenizer
tokens =
new StringTokenizer( data, delimiter );
190 while (
tokens.hasMoreTokens() ) {
192 urls.add(
new File(
tokens.nextToken() ).toURI().toURL() );
193 }
catch ( MalformedURLException e ) {
196 System.err.println(
"com.sun.star.lib.loader.Loader::" +
197 "getCustomLoader: bad pathname: " + e );
202 private static void close(InputStream c) {
203 if (c ==
null)
return;
211 private static void callUnoinfo(String path, ArrayList<URL> urls) {
215 new String[] {
new File(path,
"unoinfo").getPath(),
"java" });
216 }
catch (IOException e) {
218 "com.sun.star.lib.loader.Loader::getCustomLoader: exec" +
222 new Drain(
p.getErrorStream()).start();
224 byte[] buf =
new byte[1000];
226 InputStream is =
null;
228 is =
p.getInputStream();
231 if (
n == buf.length) {
234 "com.sun.star.lib.loader.Loader::getCustomLoader:" +
235 " too much unoinfo output");
238 byte[] buf2 =
new byte[2 *
n];
239 System.arraycopy(buf, 0, buf2, 0,
n);
242 int k = is.read(buf,
n, buf.length -
n);
248 }
catch (IOException e) {
250 "com.sun.star.lib.loader.Loader::getCustomLoader: reading" +
251 " unoinfo output: " + e);
260 }
catch (InterruptedException e) {
261 Thread.currentThread().interrupt();
263 "com.sun.star.lib.loader.Loader::getCustomLoader: waiting for" +
269 "com.sun.star.lib.loader.Loader::getCustomLoader: unoinfo"
270 +
" exit value " +
n);
276 }
else if (
code ==
'1') {
278 s =
new String(buf,
"UTF-16LE");
279 }
catch (UnsupportedEncodingException e) {
281 "com.sun.star.lib.loader.Loader::getCustomLoader:" +
282 " transforming unoinfo output: " + e);
287 "com.sun.star.lib.loader.Loader::getCustomLoader: bad unoinfo"
294 private static final class Drain extends Thread {
296 super(
"unoinfo stderr drain");
303 while (
stream.read() != -1) {}
304 }
catch (IOException e) { }
321 protected Class<?>
findClass( String name )
throws ClassNotFoundException {
325 throw new ClassNotFoundException(
name );
329 protected synchronized Class<?>
loadClass( String name,
boolean resolve )
330 throws ClassNotFoundException
335 c = super.findClass(
name );
336 }
catch ( ClassNotFoundException e ) {
337 return super.loadClass(
name, resolve );
338 }
catch ( SecurityException e ) {
342 return super.loadClass(
name, resolve );
A customized class loader which is used to load classes and resources from a search path of user-defi...
synchronized Class<?> loadClass(String name, boolean resolve)
CustomURLClassLoader(URL[] urls)
Class<?> findClass(String name)
Drain(InputStream stream)
This class can be used as a loader for application classes which use UNO.
static void callUnoinfo(String path, ArrayList< URL > urls)
static ClassLoader m_Loader
static void main(String[] arguments)
The main method instantiates a customized class loader with the UNO jar files added to the search pat...
static synchronized ClassLoader getCustomLoader()
Gets the customized class loader with the UNO jar files added to the search path.
static void close(InputStream c)
Loader()
do not instantiate
static void addUrls(ArrayList< URL > urls, String data, String delimiter)
def run(arg=None, arg2=-1)