diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /odk/source/com/sun/star/lib/loader | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'odk/source/com/sun/star/lib/loader')
-rw-r--r-- | odk/source/com/sun/star/lib/loader/InstallationFinder.java | 590 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/Loader.java | 340 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/WinRegKey.java | 83 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/WinRegKeyException.java | 45 |
4 files changed, 1058 insertions, 0 deletions
diff --git a/odk/source/com/sun/star/lib/loader/InstallationFinder.java b/odk/source/com/sun/star/lib/loader/InstallationFinder.java new file mode 100644 index 000000000..8178e0670 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/InstallationFinder.java @@ -0,0 +1,590 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.loader; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.StringTokenizer; +import java.util.ArrayList; + +/** + * This class finds a UNO installation on the system. + * + * <p>A UNO installation can be specified by the user by either setting the + * com.sun.star.lib.loader.unopath system property or by setting the + * UNO_PATH environment variable to the program directory of a UNO + * installation. + * Note, that Java 1.3.1 and Java 1.4 don't support environment variables + * (System.getenv() throws java.lang.Error) and therefore setting the UNO_PATH + * environment variable won't work with those Java versions. + * If no UNO installation is specified by the user, the default installation + * on the system will be returned.</p> + * + * <p>On the Windows platform the default installation is read from the Windows + * Registry.</p> + * + * <p>On the Unix/Linux platforms the default installation is found from the + * PATH environment variable. Note, that for Java 1.3.1 and Java 1.4 the + * default installation is found by using the 'which' command, because + * environment variables are not supported with those Java versions. + * Both methods require that the 'soffice' executable or a symbolic + * link is in one of the directories listed in the PATH environment variable. + * For older versions than OOo 2.0 the above described methods may fail. + * In this case the default installation is taken from the .versionrc file in + * the user's home directory. Note, that the .sversionrc file will be omitted + * for OOo 2.0</p> + */ +final class InstallationFinder { + + private static final String SYSPROP_NAME = + "com.sun.star.lib.loader.unopath"; + private static final String ENVVAR_NAME = "UNO_PATH"; + private static final String SOFFICE = "libreoffice"; // Unix/Linux only + + private InstallationFinder() {} // do not instantiate + + /** + * Gets the path of a UNO installation. + * + * @return the installation path or <code>null</code>, if no installation + * was specified or found, or if an error occurred + */ + public static String getPath() { + + String path = null; + + // get the installation path from the Java system property + // com.sun.star.lib.loader.unopath + // (all platforms) + path = getPathFromProperty( SYSPROP_NAME ); + if ( path != null ) { + return path; + } + // get the installation path from the UNO_PATH environment variable + // (all platforms, not working for Java 1.3.1 and Java 1.4) + path = getPathFromEnvVar( ENVVAR_NAME ); + if ( path != null ) { + return path; + } + + String osname = null; + try { + osname = System.getProperty( "os.name" ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, + // return <code>null</code> + return null; + } + if ( osname == null ) { + return null; + } + + if ( osname.startsWith( "Windows" ) ) { + // get the installation path from the Windows Registry + // (Windows platform only) + path = getPathFromWindowsRegistry(); + } else { + // get the installation path from the PATH environment + // variable (Unix/Linux platforms only, not working for + // Java 1.3.1 and Java 1.4) + path = getPathFromPathEnvVar(); + if ( path == null ) { + // get the installation path from the 'which' + // command (Unix/Linux platforms only) + path = getPathFromWhich(); + if ( path == null ) { + // get the installation path from the + // .sversionrc file (Unix/Linux platforms only, + // for older versions than OOo 2.0) + path = getPathFromSVersionFile(); + } + } + } + + return path; + } + + /** + * Gets the installation path from a Java system property. + * + * <p>This method is called on all platforms. + * The Java system property can be passed into the application by using + * the -D flag, e.g. + * java -D<property name>=<installation path> -jar application.jar.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was specified in the Java system property or if an error occurred + */ + private static String getPathFromProperty( String prop ) { + + String path = null; + + try { + path = System.getProperty( prop ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, return <code>null</code> + } + + return path; + } + + /** + * Gets the installation path from an environment variable. + * + * <p>This method is called on all platforms. + * Note, that in Java 1.3.1 and Java 1.4 System.getenv() throws + * java.lang.Error and therefore this method returns null for those + * Java versions.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was specified in the environment variable or if an error occurred + */ + private static String getPathFromEnvVar( String var ) { + + String path = null; + + try { + path = System.getenv( var ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, return <code>null</code> + } catch ( java.lang.Error err ) { + // System.getenv() throws java.lang.Error in Java 1.3.1 and + // Java 1.4 + } + + return path; + } + + /** + * Gets the installation path from the Windows Registry. + * + * <p>This method is called on the Windows platform only.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromWindowsRegistry() { + + final String SUBKEYNAME = "\\Software\\LibreOffice\\UNO\\InstallPath"; + + String path = null; + + try { + // read the key's default value from HKEY_CURRENT_USER + WinRegKey key = new WinRegKey( "HKEY_CURRENT_USER" + SUBKEYNAME ); + path = key.getStringValue(); + } catch ( WinRegKeyException e ) { + try { + // read the key's default value from HKEY_LOCAL_MACHINE + WinRegKey key = new WinRegKey( "HKEY_LOCAL_MACHINE" + + SUBKEYNAME ); + path = key.getStringValue(); + } catch ( WinRegKeyException we ) { + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWindowsRegistry: " + + "reading key from Windows Registry failed: " + we ); + } + } + + return path; + } + + /** + * Gets the installation path from the PATH environment variable. + * + * <p>This method is called on Unix/Linux platforms only. + * An installation is found, if the executable 'soffice' or a symbolic link + * is in one of the directories listed in the PATH environment variable. + * Note, that in Java 1.3.1 and Java 1.4 System.getenv() throws + * java.lang.Error and therefore this method returns null for those + * Java versions.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromPathEnvVar() { + + final String PATH_ENVVAR_NAME = "PATH"; + + String path = null; + String str = null; + + try { + str = System.getenv( PATH_ENVVAR_NAME ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, return <code>null</code> + return null; + } catch ( java.lang.Error err ) { + // System.getenv() throws java.lang.Error in Java 1.3.1 and + // Java 1.4 + return null; + } + + if ( str != null ) { + StringTokenizer tokens = new StringTokenizer( + str, File.pathSeparator ); + while ( tokens.hasMoreTokens() ) { + File file = new File( tokens.nextToken(), SOFFICE ); + try { + if ( file.exists() ) { + try { + // resolve symlink + path = file.getCanonicalFile().getParent(); + if ( path != null ) + break; + } catch ( IOException e ) { + // if an I/O exception is thrown, ignore this + // path entry and try the next one + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromEnvVar: " + + "bad path: " + e ); + } + } + } catch ( SecurityException e ) { + // if a SecurityException was thrown, ignore this path + // entry and try the next one + } + } + } + + return path; + } + + /** + * Gets the installation path from the 'which' command on Unix/Linux + * platforms. + * + * <p>This method is called on Unix/Linux platforms only. + * An installation is found, if the executable 'soffice' or a symbolic link + * is in one of the directories listed in the PATH environment variable.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromWhich() { + + final String WHICH = "which"; + + String path = null; + + // start the which process + String[] cmdArray = new String[] { WHICH, SOFFICE }; + Process proc = null; + Runtime rt = Runtime.getRuntime(); + try { + proc = rt.exec( cmdArray ); + } catch ( SecurityException e ) { + return null; + } catch ( IOException e ) { + // if an I/O exception is thrown, return <code>null</null> + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWhich: " + + "which command failed: " + e ); + return null; + } + + // empty standard error stream in a separate thread + StreamGobbler gobbler = new StreamGobbler( proc.getErrorStream() ); + gobbler.start(); + + try { + // read the which output from standard input stream + BufferedReader br = new BufferedReader( + new InputStreamReader( proc.getInputStream(), "UTF-8" ) ); + String line = null; + try { + while ( ( line = br.readLine() ) != null ) { + if ( path == null ) { + // get the path from the which output + int index = line.lastIndexOf( SOFFICE ); + if ( index != -1 ) { + int end = index + SOFFICE.length(); + for ( int i = 0; i <= index; i++ ) { + File file = new File( line.substring( i, end ) ); + try { + if ( file.exists() ) { + // resolve symlink + path = file.getCanonicalFile().getParent(); + if ( path != null ) + break; + } + } catch ( SecurityException e ) { + return null; + } + } + } + } + } + } catch ( IOException e ) { + // if an I/O exception is thrown, return <code>null</null> + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWhich: " + + "reading which command output failed: " + e ); + return null; + } finally { + try { + br.close(); + } catch ( IOException e ) { + // closing standard input stream failed, ignore + } + } + } catch ( UnsupportedEncodingException e ) { + // if an Encoding exception is thrown, return <code>null</null> + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWhich: " + + "encoding failed: " + e ); + return null; + } + + try { + // wait until the which process has terminated + proc.waitFor(); + } catch ( InterruptedException e ) { + // the current thread was interrupted by another thread, + // kill the which process + proc.destroy(); + // set the interrupted status + Thread.currentThread().interrupt(); + } + + return path; + } + + /** + * Gets the installation path from the .sversionrc file in the user's home + * directory. + * + * <p>This method is called on Unix/Linux platforms only. + * The .sversionrc file is written during setup and will be omitted for + * OOo 2.0.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromSVersionFile() { + + final String SVERSION = ".sversionrc"; // Unix/Linux only + final String VERSIONS = "[Versions]"; + + String path = null; + + try { + File fSVersion = new File( + System.getProperty( "user.home" ) ,SVERSION ); + if ( fSVersion.exists() ) { + ArrayList<String> lines = new ArrayList<String>(); + BufferedReader br = null; + try { + br = new BufferedReader( new InputStreamReader( + new FileInputStream( fSVersion ), "UTF-8" ) ); + String line = null; + while ( ( line = br.readLine() ) != null && + !line.equals( VERSIONS ) ) { + // read lines until [Versions] is found + } + while ( ( line = br.readLine() ) != null && + line.length() != 0 ) { + if ( !line.startsWith( ";" ) ) + lines.add( line ); + } + } catch ( IOException e ) { + // if an I/O exception is thrown, try to analyze the lines + // read so far + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromSVersionFile: " + + "reading .sversionrc file failed: " + e ); + } finally { + if ( br != null ) { + try { + br.close(); + } catch ( IOException e ) { + // closing .sversionrc failed, ignore + } + } + } + for ( int i = lines.size() - 1; i >= 0; i-- ) { + StringTokenizer tokens = new StringTokenizer( + lines.get( i ), "=" ); + if ( tokens.countTokens() != 2 ) + continue; + tokens.nextToken(); // key + String url = tokens.nextToken(); + path = getCanonicalPathFromFileURL( url ); + if ( path != null ) + break; + } + } + } catch ( SecurityException e ) { + return null; + } + + return path; + } + + /** + * Translates an OOo-internal absolute file URL reference (encoded using + * UTF-8) into a Java canonical pathname. + * + * @param oooUrl any URL reference; any fragment part is ignored + * + * @return if the given URL is a valid absolute, local (that is, the host + * part is empty or equal to "localhost", ignoring case) file URL, it is + * converted into an absolute canonical pathname; otherwise, + * <code>null</code> is returned + */ + private static String getCanonicalPathFromFileURL( String oooUrl ) { + + String prefix = "file://"; + if (oooUrl.length() < prefix.length() + || !oooUrl.substring(0, prefix.length()).equalsIgnoreCase( + prefix)) + { + return null; + } + StringBuffer buf = new StringBuffer(prefix); + int n = oooUrl.indexOf('/', prefix.length()); + if (n < 0) { + n = oooUrl.length(); + } + String host = oooUrl.substring(prefix.length(), n); + if (host.length() != 0 && !host.equalsIgnoreCase("localhost")) { + return null; + } + buf.append(host); + if (n == oooUrl.length()) { + buf.append('/'); + } else { + loop: + while (n < oooUrl.length()) { + buf.append('/'); + ++n; + int n2 = oooUrl.indexOf('/', n); + if (n2 < 0) { + n2 = oooUrl.length(); + } + while (n < n2) { + char c = oooUrl.charAt(n); + switch (c) { + case '%': + byte[] bytes = new byte[(n2 - n) / 3]; + int len = 0; + while (oooUrl.length() - n > 2 + && oooUrl.charAt(n) == '%') + { + int d1 = Character.digit(oooUrl.charAt(n + 1), 16); + int d2 = Character.digit(oooUrl.charAt(n + 2), 16); + if (d1 < 0 || d2 < 0) { + break; + } + int d = 16 * d1 + d2; + if (d == '/') { + return null; + } + bytes[len++] = (byte) d; + n += 3; + } + String s; + try { + s = new String(bytes, 0, len, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return null; + } + buf.append(s); + break; + + case '#': + break loop; + + default: + buf.append(c); + ++n; + break; + } + } + } + } + URL url; + try { + url = new URL(buf.toString()); + } catch (MalformedURLException e) { + return null; + } + String path = url.getFile(); + String fragment = url.getRef(); + if (fragment != null) { + path += '#' + fragment; + } + String ret = null; + File file = new File( path, SOFFICE ); + try { + if ( file.isAbsolute() && file.exists() ) { + try { + // resolve symlink + ret = file.getCanonicalFile().getParent(); + } catch ( IOException e ) { + return null; + } + } + } catch ( SecurityException e ) { + return null; + } + + return ret; + } + + /** + This class is used for emptying any stream which is passed into it in + a separate thread. + */ + private static final class StreamGobbler extends Thread { + + InputStream m_istream; + + StreamGobbler( InputStream istream ) { + m_istream = istream; + } + + @Override + public void run() { + try { + BufferedReader br = new BufferedReader( + new InputStreamReader( m_istream, "UTF-8" ) ); + // read from input stream + while ( br.readLine() != null ) { + // don't handle line content + } + br.close(); + } catch (UnsupportedEncodingException e) { + // cannot read from input stream + } catch ( IOException e ) { + // stop reading from input stream + } + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/com/sun/star/lib/loader/Loader.java b/odk/source/com/sun/star/lib/loader/Loader.java new file mode 100644 index 000000000..bc1a3c3e7 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/Loader.java @@ -0,0 +1,340 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.loader; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Method; +import java.net.JarURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; +import java.util.jar.Attributes; +import java.util.jar.Manifest; +import java.util.StringTokenizer; +import java.util.ArrayList; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * This class can be used as a loader for application classes which use UNO. + * + * <p>The Loader class detects a UNO installation on the system and adds the + * UNO jar files to the search path of a customized class loader, which is used + * for loading the application classes.</p> + */ +public final class Loader { + + private static ClassLoader m_Loader = null; + + /** + * do not instantiate + */ + private Loader() {} + + /** + * The main method instantiates a customized class loader with the + * UNO jar files added to the search path and loads the application class, + * which is specified in the Main-Class attribute of the + * com/sun/star/lib/Loader.class entry of the manifest file or + * as first parameter in the argument list. + */ + public static void main( String[] arguments ) throws Exception { + + // 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 { + throw new 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", new Class[] { String[].class } ); + m.invoke( null, new Object[] { args } ); + } + } + + /** + * Gets the customized class loader with the UNO jar files added to the + * search path. + * + * @return the customized class loader + */ + public static synchronized ClassLoader getCustomLoader() { + if ( m_Loader == null ) { + + // 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() { + return new CustomURLClassLoader(urls); + } + }); + } + + return m_Loader; + } + + private static void 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 ); + } + } + } + + private static void 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 = new byte[1000]; + int n = 0; + try { + InputStream s = p.getInputStream(); + code = s.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 = new byte[2 * n]; + System.arraycopy(buf, 0, buf2, 0, n); + buf = buf2; + } + int k = s.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; + } + 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); + } else if (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"); + } + + private static final class Drain extends Thread { + public Drain(InputStream stream) { + super("unoinfo stderr drain"); + this.stream = stream; + } + + @Override + public void run() { + try { + while (stream.read() != -1) {} + } catch (IOException e) { /* ignored */ } + } + + private final InputStream stream; + } + + /** + * A customized class loader which is used to load classes and resources + * from a search path of user-defined URLs. + */ + private static final class CustomURLClassLoader extends URLClassLoader { + + public CustomURLClassLoader( URL[] urls ) { + super( urls ); + } + + @Override + protected Class<?> 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: + throw new ClassNotFoundException( name ); + } + + @Override + protected synchronized Class<?> loadClass( String name, boolean resolve ) + throws ClassNotFoundException + { + Class c = findLoadedClass( name ); + if ( c == null ) { + try { + c = super.findClass( name ); + } catch ( ClassNotFoundException e ) { + return super.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: + return super.loadClass( name, resolve ); + } + } + if ( resolve ) { + resolveClass( c ); + } + return c; + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/com/sun/star/lib/loader/WinRegKey.java b/odk/source/com/sun/star/lib/loader/WinRegKey.java new file mode 100644 index 000000000..3276458fc --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/WinRegKey.java @@ -0,0 +1,83 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.loader; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * This class provides functionality for reading string values from the + * Windows Registry. + */ +final class WinRegKey { + + private final String m_keyName; + + /** + * Constructs a <code>WinRegKey</code>. + */ + public WinRegKey( String keyName ) { + m_keyName = keyName; + } + + /** + * Reads the default string value. + */ + public String getStringValue() throws WinRegKeyException { + try { + Process p = Runtime.getRuntime().exec(new String[]{"reg", "QUERY", m_keyName}); + BufferedReader r = new BufferedReader( + new InputStreamReader(p.getInputStream(), Charset.defaultCharset())); + String v = null; + Pattern pt = Pattern.compile("\\s+\\(Default\\)\\s+REG_SZ\\s+(.+)"); + for (;;) { + String s = r.readLine(); + if (s == null) { + break; + } + Matcher m = pt.matcher(s); + if (m.matches()) { + if (v != null) { + throw new WinRegKeyException("reg QUERY did not provided expected output"); + } + v = m.group(1); + } + } + p.waitFor(); + int e = p.exitValue(); + if (e != 0) { + throw new WinRegKeyException("reg QUERY exited with " + e); + } + if (v == null) { + throw new WinRegKeyException("reg QUERY did not provided expected output"); + } + return v; + } catch (WinRegKeyException e) { + throw e; + } catch (Exception e) { + throw new WinRegKeyException(e); + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/com/sun/star/lib/loader/WinRegKeyException.java b/odk/source/com/sun/star/lib/loader/WinRegKeyException.java new file mode 100644 index 000000000..56abc7536 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/WinRegKeyException.java @@ -0,0 +1,45 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.loader; + +/** + * WinRegKeyException is a checked exception. + */ +final class WinRegKeyException extends java.lang.Exception { + + /** + * Constructs a <code>WinRegKeyException</code>. + */ + public WinRegKeyException(Throwable cause) { + super(cause); + } + + /** + * Constructs a <code>WinRegKeyException</code> with the specified + * detail message. + * + * @param message the detail message + */ + public WinRegKeyException( String message ) { + super( message ); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |