diff options
Diffstat (limited to 'odk/source')
-rw-r--r-- | odk/source/com/sun/star/lib/loader/InstallationFinder.java | 587 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/Loader.java | 388 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/WinRegKey.java | 203 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/WinRegKeyException.java | 54 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/makefile.mk | 60 | ||||
-rw-r--r-- | odk/source/unoapploader/unx/makefile.mk | 64 | ||||
-rw-r--r-- | odk/source/unoapploader/unx/unoapploader.c | 304 | ||||
-rw-r--r-- | odk/source/unoapploader/win/makefile.mk | 61 | ||||
-rw-r--r-- | odk/source/unoapploader/win/unoapploader.c | 426 | ||||
-rw-r--r-- | odk/source/unowinreg/win/makefile.mk | 122 | ||||
-rw-r--r-- | odk/source/unowinreg/win/unowinreg.cxx | 188 | ||||
-rw-r--r-- | odk/source/unowinreg/win/unowinreg.dxp | 9 |
12 files changed, 2466 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 000000000000..67390a005037 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/InstallationFinder.java @@ -0,0 +1,587 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: InstallationFinder.java,v $ + * $Revision: 1.4 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +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.Vector; + +/** + * 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 + * enviroment 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 .sversionrc 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 = "soffice"; // 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 occured + */ + 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 ) { + // 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 ) { + 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 ) { + 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 occured + */ + 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 occured + */ + 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 occured + */ + private static String getPathFromWindowsRegistry() { + + final String SUBKEYNAME = "Software\\OpenOffice.org\\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( "" ); // default + } 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( "" ); // default + } 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 occured + */ + 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 occured + */ + private static String getPathFromWhich() { + + final String WHICH = "which"; + + String path = null; + + // start the which process + String[] cmdArray = new String[2]; + cmdArray[0] = WHICH; + cmdArray[1] = 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 seperate thread + StreamGobbler gobbler = new StreamGobbler( proc.getErrorStream() ); + gobbler.start(); + + // read the which output from standard input stream + BufferedReader br = new BufferedReader( + new InputStreamReader( proc.getInputStream() ) ); + 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 { + if ( br != null ) { + try { + br.close(); + } catch ( IOException e ) { + // closing standard input stream failed, ignore + } + } + } + + 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 .sverionrc 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 occured + */ + 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() ) { + Vector lines = new Vector(); + 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 ) ) != true ) { + // 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( + (String)lines.elementAt( i ), "=" ); + if ( tokens.countTokens() != 2 ) + continue; + String key = tokens.nextToken(); + 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()).toLowerCase().equals( + 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.toLowerCase().equals("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; + } + + public void run() { + try { + BufferedReader br = new BufferedReader( + new InputStreamReader( m_istream ) ); + // read from input stream + while ( br.readLine() != null ) { + // don't handle line content + } + br.close(); + } catch ( IOException e ) { + // stop reading from input stream + } + } + } +} 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 000000000000..1e116c399f87 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/Loader.java @@ -0,0 +1,388 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: Loader.java,v $ + * $Revision: 1.6 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +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.InvocationTargetException; +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.Vector; + +/** + * 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(); + Vector res = new Vector(); + try { + Enumeration en = loader.getResources( "META-INF/MANIFEST.MF" ); + while ( en.hasMoreElements() ) { + res.add( (URL) 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 = (URL) res.elementAt( i ); + try { + JarURLConnection jarConnection = + (JarURLConnection) jarurl.openConnection(); + Manifest mf = jarConnection.getManifest(); + Attributes attrs = (Attributes) mf.getAttributes( + "com/sun/star/lib/loader/Loader.class" ); + 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 ); + 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() { + + final String CLASSESDIR = "classes"; + final String JUHJAR = "juh.jar"; + + if ( m_Loader == null ) { + + // get the urls from which to load classes and resources + // from the class path + Vector vec = new Vector(); + 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 ) { + File fClassesDir = new File( path, CLASSESDIR ); + File fJuh = new File( fClassesDir, JUHJAR ); + if ( fJuh.exists() ) { + URL[] clurls = new URL[1]; + try { + clurls[0] = fJuh.toURL(); + ClassLoader cl = new CustomURLClassLoader( clurls ); + Class c = cl.loadClass( + "com.sun.star.comp.helper.UnoInfo" ); + Method m = c.getMethod( "getJars", (Class[]) null ); + URL[] jarurls = (URL[]) m.invoke( + null, (Object[]) null ); + for ( int i = 0; i < jarurls.length; i++ ) { + vec.add( jarurls[i] ); + } + } catch ( MalformedURLException e ) { + // don't add the UNO jar files to the list of class + // loader URLs + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: cannot add UNO jar files: " + e ); + } catch ( ClassNotFoundException e ) { + // don't add the UNO jar files to the list of class + // loader URLs + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: cannot add UNO jar files: " + e ); + } catch ( NoSuchMethodException e ) { + // don't add the UNO jar files to the list of class + // loader URLs + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: cannot add UNO jar files: " + e ); + } catch ( IllegalAccessException e ) { + // don't add the UNO jar files to the list of class + // loader URLs + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: cannot add UNO jar files: " + e ); + } catch ( InvocationTargetException e ) { + // don't add the UNO jar files to the list of class + // loader URLs + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: cannot add UNO jar files: " + e ); + } + } else { + callUnoinfo(path, vec); + } + } else { + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: no UNO installation found!" ); + } + + // copy urls to array + URL[] urls = new URL[vec.size()]; + vec.toArray( urls ); + + // instantiate class loader + m_Loader = new CustomURLClassLoader( urls ); + } + + return m_Loader; + } + + private static void addUrls(Vector urls, String data, String delimiter) { + StringTokenizer tokens = new StringTokenizer( data, delimiter ); + while ( tokens.hasMoreTokens() ) { + try { + urls.add( new File( tokens.nextToken() ).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, Vector 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]; + for (int i = 0; i < n; ++i) { + buf2[i] = buf[i]; + } + 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; + } + + 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 ); + } + + 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 ); + } + + protected 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; + } + } +} 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 000000000000..4b2a91e50505 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/WinRegKey.java @@ -0,0 +1,203 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WinRegKey.java,v $ + * $Revision: 1.4 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +package com.sun.star.lib.loader; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; + + +/** + * This class provides functionality for reading string values from the + * Windows Registry. It requires the native library unowinreg.dll. + */ +final class WinRegKey { + + private String m_rootKeyName; + private String m_subKeyName; + + // native methods to access the windows registry + private static native boolean winreg_RegOpenClassesRoot( long[] hkresult ); + private static native boolean winreg_RegOpenCurrentConfig( + long[] hkresult ); + private static native boolean winreg_RegOpenCurrentUser( long[] hkresult ); + private static native boolean winreg_RegOpenLocalMachine( long[] hkresult ); + private static native boolean winreg_RegOpenUsers( long[] hkresult ); + private static native boolean winreg_RegOpenKeyEx( long parent, String name, + long[] hkresult ); + private static native boolean winreg_RegCloseKey( long hkey ); + private static native boolean winreg_RegQueryValueEx( + long hkey, String value, long[] type, + byte[] data, long[] size ); + private static native boolean winreg_RegQueryInfoKey( + long hkey, long[] subkeys, long[] maxSubkeyLen, + long[] values, long[] maxValueNameLen, + long[] maxValueLen, long[] secDescriptor ); + + // load the native library unowinreg.dll + static { + try { + ClassLoader cl = WinRegKey.class.getClassLoader(); + InputStream is = cl.getResourceAsStream( "win/unowinreg.dll" ); + if ( is != null ) { + // generate a temporary name for lib file and write to temp + // location + BufferedInputStream istream = new BufferedInputStream( is ); + File libfile = File.createTempFile( "unowinreg", ".dll" ); + libfile.deleteOnExit(); // ensure deletion + BufferedOutputStream ostream = new BufferedOutputStream( + new FileOutputStream( libfile ) ); + int bsize = 2048; int n = 0; + byte[] buffer = new byte[bsize]; + while ( ( n = istream.read( buffer, 0, bsize ) ) != -1 ) { + ostream.write( buffer, 0, n ); + } + istream.close(); + ostream.close(); + // load library + System.load( libfile.getPath() ); + } else { + // If the library cannot be found as a class loader resource, + // try the global System.loadLibrary(). The JVM will look for + // it in the java.library.path. + System.loadLibrary( "unowinreg" ); + } + } catch ( java.lang.Exception e ) { + System.err.println( "com.sun.star.lib.loader.WinRegKey: " + + "loading of native library failed!" + e ); + } + } + + /** + * Constructs a <code>WinRegKey</code>. + */ + public WinRegKey( String rootKeyName, String subKeyName ) { + m_rootKeyName = rootKeyName; + m_subKeyName = subKeyName; + } + + /** + * Reads a string value for the specified value name. + */ + public String getStringValue( String valueName ) throws WinRegKeyException { + byte[] data = getValue( valueName ); + // remove terminating null character + return new String( data, 0, data.length - 1 ); + } + + /** + * Reads a value for the specified value name. + */ + private byte[] getValue( String valueName ) throws WinRegKeyException { + + byte[] result = null; + long[] hkey = {0}; + + // open the specified registry key + boolean bRet = false; + long[] hroot = {0}; + if ( m_rootKeyName.equals( "HKEY_CLASSES_ROOT" ) ) { + bRet = winreg_RegOpenClassesRoot( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_CURRENT_CONFIG" ) ) { + bRet = winreg_RegOpenCurrentConfig( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_CURRENT_USER" ) ) { + bRet = winreg_RegOpenCurrentUser( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_LOCAL_MACHINE" ) ) { + bRet = winreg_RegOpenLocalMachine( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_USERS" ) ) { + bRet = winreg_RegOpenUsers( hroot ); + } else { + throw new WinRegKeyException( "unknown root registry key!"); + } + if ( !bRet ) { + throw new WinRegKeyException( "opening root registry key " + + "failed!" ); + } + if ( !winreg_RegOpenKeyEx( hroot[0], m_subKeyName, hkey ) ) { + if ( !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "opening registry key and " + + "releasing root registry key handle failed!" ); + } + throw new WinRegKeyException( "opening registry key failed!" ); + } + + // get the size of the longest data component among the key's values + long[] subkeys = {0}; + long[] maxSubkeyLen = {0}; + long[] values = {0}; + long[] maxValueNameLen = {0}; + long[] maxValueLen = {0}; + long[] secDescriptor = {0}; + if ( !winreg_RegQueryInfoKey( hkey[0], subkeys, maxSubkeyLen, + values, maxValueNameLen, maxValueLen, secDescriptor ) ) { + if ( !winreg_RegCloseKey( hkey[0] ) || + !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "retrieving information about " + + "the registry key and releasing registry key handles " + + "failed!" ); + } + throw new WinRegKeyException( "retrieving information about " + + "the registry key failed!" ); + } + + // get the data for the specified value name + byte[] buffer = new byte[ (int) maxValueLen[0] ]; + long[] size = new long[1]; + size[0] = buffer.length; + long[] type = new long[1]; + type[0] = 0; + if ( !winreg_RegQueryValueEx( hkey[0], valueName, type, buffer, + size ) ) { + if ( !winreg_RegCloseKey( hkey[0] ) || + !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "retrieving data for the " + + "specified value name and releasing registry key handles " + + "failed!" ); + } + throw new WinRegKeyException( "retrieving data for the " + + "specified value name failed!" ); + } + + // release registry key handles + if ( !winreg_RegCloseKey( hkey[0] ) || + !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "releasing registry key handles " + + "failed!" ); + } + + result = new byte[ (int) size[0] ]; + System.arraycopy( buffer, 0, result, 0, (int)size[0] ); + + return result; + } +} 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 000000000000..fea82f3b0d8d --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/WinRegKeyException.java @@ -0,0 +1,54 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: WinRegKeyException.java,v $ + * $Revision: 1.4 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +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() { + super(); + } + + /** + * Constructs a <code>WinRegKeyException</code> with the specified + * detail message. + * + * @param message the detail message + */ + public WinRegKeyException( String message ) { + super( message ); + } +} diff --git a/odk/source/com/sun/star/lib/loader/makefile.mk b/odk/source/com/sun/star/lib/loader/makefile.mk new file mode 100644 index 000000000000..e86150730eb6 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/makefile.mk @@ -0,0 +1,60 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.5 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/..$/..$/..$/..$/.. + +PRJNAME = odk +PACKAGE = com$/sun$/star$/lib$/loader +TARGET = com_sun_star_lib_loader + + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +.IF "$(SOLAR_JAVA)"=="" +nojava: + @echo "Not building javaunohelper because Java is disabled" +.ENDIF + +# Files -------------------------------------------------------- + +JAVAFILES= \ + Loader.java\ + InstallationFinder.java\ + WinRegKey.java\ + WinRegKeyException.java + +JAVACLASSFILES= $(foreach,i,$(JAVAFILES) $(CLASSDIR)$/$(PACKAGE)$/$(i:b).class) + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/odk/source/unoapploader/unx/makefile.mk b/odk/source/unoapploader/unx/makefile.mk new file mode 100644 index 000000000000..92eac422b210 --- /dev/null +++ b/odk/source/unoapploader/unx/makefile.mk @@ -0,0 +1,64 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.8 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/..$/.. + +PRJNAME=odk +TARGET=unoapploader +LIBTARGET=NO + +LIBSALCPPRT= + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +# --- Files -------------------------------------------------------- + +.IF "$(COM)"=="GCC" +LINK=$(CC) +.ENDIF + +.IF "$(OS)"!="FREEBSD" +STDLIB= -ldl +.ENDIF + + +APP1NOSAL=TRUE +APP1RPATH=NONE + +APP1TARGET= $(TARGET) + +APP1OBJS= $(OBJ)$/unoapploader.obj $(SOLARLIBDIR)$/findsofficepath.obj + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/odk/source/unoapploader/unx/unoapploader.c b/odk/source/unoapploader/unx/unoapploader.c new file mode 100644 index 000000000000..738e4d24787b --- /dev/null +++ b/odk/source/unoapploader/unx/unoapploader.c @@ -0,0 +1,304 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: unoapploader.c,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> + +#ifdef LINUX +#define __USE_GNU +#endif +#include <dlfcn.h> + +#include "cppuhelper/findsofficepath.h" +#include "rtl/string.h" +#include "sal/types.h" + +char const* getPath(); +char* createCommandName( char* argv0 ); + +const int SEPARATOR = '/'; +const char* PATHSEPARATOR = ":"; + + +/* + * The main function implements a loader for applications which use UNO. + * + * <p>This code runs on the Unix/Linux platforms only.</p> + * + * <p>The main function detects a UNO installation on the system and adds the + * relevant directories of the installation to the LD_LIBRARY_PATH environment + * variable. After that, the application process is loaded and started, whereby + * the new process inherits the environment of the calling process, including + * the modified LD_LIBRARY_PATH environment variable. The application's + * executable name must be the same as the name of this executable, prefixed + * by '_'.</p> + * <p>On MACOSX DYLD_LIBRARY_PATH is used instead of LD_LIBRARY_PATH!<p> + * + * <p>A UNO installation can be specified by the user by setting the UNO_PATH + * environment variable to the program directory of the UNO installation. + * If no installation is specified by the user, the default installation on + * the system will be taken. The default installation is found from the + * PATH environment variable. This requires that the 'soffice' executable or + * a symbolic link is in one of the directories listed in the PATH environment + * variable.</p> + */ +int main( int argc, char *argv[] ) +{ + char const* path; + char* cmdname; + + (void) argc; /* avoid warning about unused parameter */ + + /* get the path of the UNO installation */ + path = getPath(); + + if ( path != NULL ) + { +#ifdef MACOSX + static const char* ENVVARNAME = "DYLD_LIBRARY_PATH"; +#else + static const char* ENVVARNAME = "LD_LIBRARY_PATH"; +#endif + char * libpath; + int freeLibpath; + + char* value; + char* envstr; + int size; + + size_t pathlen = strlen(path); + struct stat stat; + int ret; + char * unoinfo = malloc( + pathlen + RTL_CONSTASCII_LENGTH("/unoinfo") + 1); + /*TODO: overflow */ + if (unoinfo == NULL) { + fprintf(stderr, "Error: out of memory!\n"); + exit(EXIT_FAILURE); + } + strcpy(unoinfo, path); + strcpy( + unoinfo + pathlen, + "/unoinfo" + (pathlen == 0 || path[pathlen - 1] != '/' ? 0 : 1)); + ret = lstat(unoinfo, &stat); + free(unoinfo); + + if (ret == 0) { + char * cmd = malloc( + 2 * pathlen + RTL_CONSTASCII_LENGTH("/unoinfo c++") + 1); + /*TODO: overflow */ + char const * p; + char * q; + FILE * f; + size_t n = 1000; + size_t old = 0; + if (cmd == NULL) { + fprintf(stderr, "Error: out of memory!\n"); + exit(EXIT_FAILURE); + } + p = path; + q = cmd; + while (*p != '\0') { + *q++ = '\\'; + *q++ = *p++; + } + if (p == path || p[-1] != '/') { + *q++ = '/'; + } + strcpy(q, "unoinfo c++"); + f = popen(cmd, "r"); + free(cmd); + if (f == NULL) + { + fprintf(stderr, "Error: calling unoinfo failed!\n"); + exit(EXIT_FAILURE); + } + libpath = NULL; + for (;;) { + size_t m; + libpath = realloc(libpath, n); + if (libpath == NULL) { + fprintf( + stderr, + "Error: out of memory reading unoinfo output!\n"); + exit(EXIT_FAILURE); + } + m = fread(libpath + old, 1, n - old - 1, f); + if (m != n - old - 1) { + if (ferror(f)) { + fprintf(stderr, "Error: cannot read unoinfo output!\n"); + exit(EXIT_FAILURE); + } + libpath[old + m] = '\0'; + break; + } + if (n >= SAL_MAX_SIZE / 2) { + fprintf( + stderr, + "Error: out of memory reading unoinfo output!\n"); + exit(EXIT_FAILURE); + } + old = n - 1; + n *= 2; + } + if (pclose(f) != 0) { + fprintf(stderr, "Error: executing unoinfo failed!\n"); + exit(EXIT_FAILURE); + } + freeLibpath = 1; + } + else + { + /* Assume an old OOo 2.x installation without unoinfo: */ + libpath = (char *) path; + freeLibpath = 0; + } + + value = getenv( ENVVARNAME ); + + size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( libpath ) + 1; + if ( value != NULL ) + size += strlen( PATHSEPARATOR ) + strlen( value ); + envstr = (char*) malloc( size ); + strcpy( envstr, ENVVARNAME ); + strcat( envstr, "=" ); + strcat( envstr, libpath ); + if ( freeLibpath != 0 ) + { + free( libpath ); + } + if ( value != NULL ) + { + strcat( envstr, PATHSEPARATOR ); + strcat( envstr, value ); + } + putenv( envstr ); + } + else + { + fprintf( stderr, "Warning: no UNO installation found!\n" ); + fflush( stderr ); + } + + /* set the executable name for the application process */ + cmdname = createCommandName( argv[0] ); + argv[0] = cmdname; + + /* + * create the application process; + * if successful, execvp doesn't return to the calling process + */ + execvp( cmdname, argv ); + fprintf( stderr, "Error: execvp failed!\n" ); + fflush( stderr ); + + return 0; +} + +/* + * Gets the path of a UNO installation. + * + * @return the installation path or NULL, if no installation was specified or + * found, or if an error occured + */ +char const* getPath() +{ + char const* path = cppuhelper_detail_findSofficePath(); + + if ( path == NULL ) + { + fprintf( stderr, "Warning: getting path from PATH environment " + "variable failed!\n" ); + fflush( stderr ); + } + + return path; +} + +/* + * Creates the application's executable file name. + * + * <p>The application's executable file name is the name of this executable + * prefixed by '_'.</p> + * + * @param argv0 specifies the argv[0] parameter of the main function + * + * @return the application's executable file name or NULL, if an error occured + */ +char* createCommandName( char* argv0 ) +{ + const char* CMDPREFIX = "_"; + const char* prgname = NULL; + + char* cmdname = NULL; + char* sep = NULL; + Dl_info dl_info; + int pos; + + /* get the executable file name from argv0 */ + prgname = argv0; + + /* + * if argv0 doesn't contain an absolute path name, try to get the absolute + * path name from dladdr; note that this only works for Solaris, not for + * Linux + */ + if ( argv0 != NULL && *argv0 != SEPARATOR && + dladdr( (void*) &createCommandName, &dl_info ) && + dl_info.dli_fname != NULL && *dl_info.dli_fname == SEPARATOR ) + { + prgname = dl_info.dli_fname; + } + + /* prefix the executable file name by '_' */ + if ( prgname != NULL ) + { + cmdname = (char*) malloc( strlen( prgname ) + strlen( CMDPREFIX ) + 1 ); + sep = strrchr( prgname, SEPARATOR ); + if ( sep != NULL ) + { + pos = ++sep - prgname; + strncpy( cmdname, prgname, pos ); + cmdname[ pos ] = '\0'; + strcat( cmdname, CMDPREFIX ); + strcat( cmdname, sep ); + } + else + { + strcpy( cmdname, CMDPREFIX ); + strcat( cmdname, prgname ); + } + } + + return cmdname; +} diff --git a/odk/source/unoapploader/win/makefile.mk b/odk/source/unoapploader/win/makefile.mk new file mode 100644 index 000000000000..b9447da0aa1d --- /dev/null +++ b/odk/source/unoapploader/win/makefile.mk @@ -0,0 +1,61 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.6 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/..$/.. + +PRJNAME=odk +TARGET=unoapploader +TARGETTYPE=GUI +LIBTARGET=NO + +DYNAMIC_CRT= +UWINAPILIB= +NO_DEFAULT_STL=true + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +# --- Files -------------------------------------------------------- + +APP1NOSAL=TRUE +APP1RPATH=NONE + +APP1TARGET= $(TARGET) + +APP1OBJS= $(OBJ)$/unoapploader.obj $(SOLARLIBDIR)$/findsofficepath.obj + +APP1STDLIBS=\ + $(ADVAPI32LIB) + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/odk/source/unoapploader/win/unoapploader.c b/odk/source/unoapploader/win/unoapploader.c new file mode 100644 index 000000000000..3ee0aa96f15a --- /dev/null +++ b/odk/source/unoapploader/win/unoapploader.c @@ -0,0 +1,426 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: unoapploader.c,v $ + * $Revision: 1.7 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <process.h> + +#if defined _MSC_VER +#pragma warning(push, 1) +#endif +#include <windows.h> +#if defined _MSC_VER +#pragma warning(pop) +#endif + +#include "cppuhelper/findsofficepath.h" +#include "sal/types.h" + +#define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1) + +char const* getPath(); +char* createCommandLine( char* lpCmdLine ); +FILE* getErrorFile( int create ); +void writeError( const char* errstr ); +void closeErrorFile(); + +/* + * The main function implements a loader for applications which use UNO. + * + * <p>This code runs on the Windows platform only.</p> + * + * <p>The main function detects a UNO installation on the system and adds the + * program directory of the UNO installation to the PATH environment variable. + * After that, the application process is loaded and started, whereby the + * new process inherits the environment of the calling process, including + * the modified PATH environment variable. The application's executable name + * must be the same as the name of this executable, prefixed by '_'.</p> + * + * <p>A UNO installation can be specified by the user by setting the UNO_PATH + * environment variable to the program directory of the UNO installation. + * If no installation is specified by the user, the default installation on + * the system will be taken. The default installation is read from the + * default value of the key "Software\OpenOffice.org\UNO\InstallPath" from the + * root key HKEY_CURRENT_USER in the Windows Registry. If this key is missing, + * the key is read from the root key HKEY_LOCAL_MACHINE.</p> + */ +int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, + LPSTR lpCmdLine, int nCmdShow ) +{ + const char* ENVVARNAME = "PATH"; + const char* PATHSEPARATOR = ";"; + + char const* path = NULL; + char path2[MAX_PATH]; + char* value = NULL; + char* envstr = NULL; + char* cmdline = NULL; + int size; + STARTUPINFO startup_info; + PROCESS_INFORMATION process_info; + BOOL bCreate; + + (void) hInstance; /* unused */ + (void) hPrevInstance; /* unused */ + (void) nCmdShow; /* unused */ + + /* get the path of the UNO installation */ + path = getPath(); + + if ( path != NULL ) + { + wchar_t cmd[ + MY_LENGTH(L"\"") + MAX_PATH + + MY_LENGTH(L"\\unoinfo.exe\" c++")]; + /* hopefully does not overflow */ + int pathsize; + SECURITY_ATTRIBUTES sec; + HANDLE temp; + HANDLE stdoutRead; + HANDLE stdoutWrite; + STARTUPINFOW startinfo; + PROCESS_INFORMATION procinfo; + int ret; + cmd[0] = L'"'; + pathsize = MultiByteToWideChar(CP_ACP, 0, path, -1, cmd + 1, MAX_PATH); + if (pathsize == 0) { + writeError("Error: MultiByteToWideChar failed!\n"); + closeErrorFile(); + return 1; + } + if (wcschr(cmd + 1, L'"') != NULL) { + writeError("Error: bad characters in UNO installation path!\n"); + closeErrorFile(); + return 1; + } + wcscpy( + cmd + pathsize, + (L"\\unoinfo.exe\" c++" + + (pathsize == 1 || cmd[pathsize - 1] != L'\\' ? 0 : 1))); + sec.nLength = sizeof (SECURITY_ATTRIBUTES); + sec.lpSecurityDescriptor = NULL; + sec.bInheritHandle = TRUE; + if (CreatePipe(&temp, &stdoutWrite, &sec, 0) == 0 || + DuplicateHandle( + GetCurrentProcess(), temp, GetCurrentProcess(), &stdoutRead, 0, + FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS) == 0) + { + writeError("Error: CreatePipe/DuplicateHandle failed!\n"); + closeErrorFile(); + return 1; + } + memset(&startinfo, 0, sizeof (STARTUPINFOW)); + startinfo.cb = sizeof (STARTUPINFOW); + startinfo.lpDesktop = L""; + startinfo.dwFlags = STARTF_USESTDHANDLES; + startinfo.hStdOutput = stdoutWrite; + ret = CreateProcessW( + NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startinfo, &procinfo); + if (ret != 0) { + char * buf = NULL; + size_t n = 1000; + size_t k = 0; + DWORD exitcode; + int path2size; + CloseHandle(stdoutWrite); + CloseHandle(procinfo.hThread); + for (;;) { + DWORD m; + buf = realloc(buf, n); + if (buf == NULL) { + writeError( + "Error: out of memory reading unoinfo output!\n"); + closeErrorFile(); + return 1; + } + if (!ReadFile(stdoutRead, buf + k, n - k, &m, NULL)) + { + DWORD err = GetLastError(); + if (err == ERROR_HANDLE_EOF || err == ERROR_BROKEN_PIPE) { + break; + } + writeError("Error: cannot read unoinfo output!\n"); + closeErrorFile(); + return 1; + } + if (m == 0) { + break; + } + k += m; + if (k >= n) { + if (n >= SAL_MAX_SIZE / 2) { + writeError( + "Error: out of memory reading unoinfo output!\n"); + closeErrorFile(); + return 1; + } + n *= 2; + } + } + if ((k & 1) == 1) { + writeError("Error: bad unoinfo output!\n"); + closeErrorFile(); + return 1; + } + CloseHandle(stdoutRead); + if (!GetExitCodeProcess(procinfo.hProcess, &exitcode) || + exitcode != 0) + { + writeError("Error: executing unoinfo failed!\n"); + closeErrorFile(); + return 1; + } + if (k == 0) { + path2size = 0; + } else { + path2size = WideCharToMultiByte( + CP_ACP, 0, (wchar_t *) buf, k / 2, path2, MAX_PATH - 1, + NULL, NULL); + if (path2size == 0) { + writeError("Error: converting unoinfo output failed!\n"); + closeErrorFile(); + return 1; + } + } + path2[path2size] = '\0'; + path = path2; + } else { + if (GetLastError() != ERROR_FILE_NOT_FOUND) { + writeError("Error: calling unoinfo failed!\n"); + closeErrorFile(); + return 1; + } + CloseHandle(stdoutRead); + CloseHandle(stdoutWrite); + } + + /* get the value of the PATH environment variable */ + value = getenv( ENVVARNAME ); + + /* + * add the UNO installation path to the PATH environment variable; + * note that this only affects the environment variable of the current + * process, the command processor's environment is not changed + */ + size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( path ) + 1; + if ( value != NULL ) + size += strlen( PATHSEPARATOR ) + strlen( value ); + envstr = (char*) malloc( size ); + strcpy( envstr, ENVVARNAME ); + strcat( envstr, "=" ); + strcat( envstr, path ); + if ( value != NULL ) + { + strcat( envstr, PATHSEPARATOR ); + strcat( envstr, value ); + } + _putenv( envstr ); + free( envstr ); + } + else + { + writeError( "Warning: no UNO installation found!\n" ); + } + + /* create the command line for the application process */ + cmdline = createCommandLine( lpCmdLine ); + if ( cmdline == NULL ) + { + writeError( "Error: cannot create command line!\n" ); + closeErrorFile(); + return 1; + } + + /* create the application process */ + memset( &startup_info, 0, sizeof( STARTUPINFO ) ); + startup_info.cb = sizeof( STARTUPINFO ); + bCreate = CreateProcess( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, + &startup_info, &process_info ); + free( cmdline ); + if ( !bCreate ) + { + writeError( "Error: cannot create process!\n" ); + closeErrorFile(); + return 1; + } + + /* close the error file */ + closeErrorFile(); + + return 0; +} + +/* + * Gets the path of a UNO installation. + * + * @return the installation path or NULL, if no installation was specified or + * found, or if an error occured + */ +char const* getPath() +{ + char const* path = cppuhelper_detail_findSofficePath(); + + if ( path == NULL ) + writeError( "Warning: getting path from Windows Registry failed!\n" ); + + return path; +} + +/* + * Creates the command line for the application process including the absolute + * path of the executable. + * + * <p>The application's executable file name is the name of this executable + * prefixed by '_'.</p> + * + * @param appendix specifies the command line for the application excluding + * the executable name + * + * @return the command line for the application process or NULL, if an error + * occured + */ +char* createCommandLine( char* appendix ) +{ + const char* CMDPREFIX = "_"; + const char* DQUOTE = "\""; + const char* SPACE = " "; + + char* cmdline = NULL; + + char cmdname[ _MAX_PATH ]; + char drive[ _MAX_DRIVE ]; + char dir[ _MAX_PATH ]; + char base[ _MAX_FNAME ]; + char newbase[ _MAX_FNAME ]; + char ext[ _MAX_EXT ]; + + /* get the absolute path of the executable file */ + if ( GetModuleFileName( NULL, cmdname, sizeof( cmdname ) ) ) + { + /* prefix the executable file name by '_' */ + _splitpath( cmdname, drive, dir, base, ext ); + strcpy( newbase, CMDPREFIX ); + strcat( newbase, base ); + _makepath( cmdname, drive, dir, newbase, ext ); + + /* create the command line */ + cmdline = (char*) malloc( strlen( DQUOTE ) + strlen( cmdname ) + + strlen ( DQUOTE ) + strlen( SPACE ) + strlen( appendix ) + 1 ); + strcpy( cmdline, DQUOTE ); + strcat( cmdline, cmdname ); + strcat( cmdline, DQUOTE ); + strcat( cmdline, SPACE ); + strcat( cmdline, appendix ); + } + + return cmdline; +} + +/* + * Gets the pointer to the error file. + * + * <p>The error file will only be created, if create != 0.</p> + * + * <p>The error file has the name <executable file name>-error.log and is + * created in the same directory as the executable file. If this fails, + * the error file is created in the directory designated for temporary files. + * </p> + + * @param create specifies, if the error file should be created (create != 0) + * + * @return the pointer to the open error file or NULL, if no error file is + * open or can be created + */ +FILE* getErrorFile( int create ) +{ + const char* MODE = "w"; + const char* BASEPOSTFIX = "-error"; + const char* EXTENSION = ".log"; + + static FILE* ferr = NULL; + + char fname[ _MAX_PATH ]; + char drive[ _MAX_DRIVE ]; + char dir[ _MAX_PATH ]; + char base[ _MAX_FNAME ]; + char newbase[ _MAX_FNAME ]; + char ext[ _MAX_EXT ]; + + if ( ferr == NULL && create ) + { + /* get the absolute path of the executable file */ + if ( GetModuleFileName( NULL, fname, sizeof( fname ) ) ) + { + /* create error file in the directory of the executable file */ + _splitpath( fname, drive, dir, base, ext ); + strcpy( newbase, base ); + strcat( newbase, BASEPOSTFIX ); + _makepath( fname, drive, dir, newbase, EXTENSION ); + ferr = fopen( fname, MODE ); + + if ( ferr == NULL ) + { + /* create error file in the temp directory */ + GetTempPath( sizeof( fname ), fname ); + strcat( fname, newbase ); + strcat( fname, EXTENSION ); + ferr = fopen( fname, MODE ); + } + } + } + + return ferr; +} + +/* + * Writes an error message to the error file. + * + * @param errstr specifies the error message + */ +void writeError( const char* errstr ) +{ + FILE* ferr = getErrorFile( 1 ); + if ( ferr != NULL ) + { + fprintf( ferr, errstr ); + fflush( ferr ); + } +} + +/* + * Closes the error file. + */ +void closeErrorFile() +{ + FILE* ferr = getErrorFile( 0 ); + if ( ferr != NULL ) + fclose( ferr ); +} diff --git a/odk/source/unowinreg/win/makefile.mk b/odk/source/unowinreg/win/makefile.mk new file mode 100644 index 000000000000..7c826ddb265c --- /dev/null +++ b/odk/source/unowinreg/win/makefile.mk @@ -0,0 +1,122 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.12.34.1 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/..$/.. + +PRJNAME=odk +TARGET=unowinreg +ENABLE_EXCEPTIONS=TRUE + +NO_DEFAULT_STL=TRUE +NO_BSYMBOLIC=TRUE +USE_DEFFILE=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +JAVA_INCLUDES:= -I$(JAVA_HOME)/include + +# values taken from set_soenv.in +.IF "$(JDK)" != "gcj" +.IF "$(OS)" == "LINUX" +JAVA_INCLUDES+= -I$(JAVA_HOME)/include/linux +.ELIF "$(OS)" == "FREEBSD" +JAVA_INCLUDES+= -I$(JAVA_HOME)/include/freebsd +JAVA_INCLUDES+= -I$(JAVA_HOME)/include/bsd +JAVA_INCLUDES+= -I$(JAVA_HOME)/include/linux +.ELIF "$(OS)" == "NETBSD" +JAVA_INCLUDES+= -I$(JAVA_HOME)/include/netbsd +.ELIF "$(OS)" == "IRIX" +JAVA_INCLUDES+= -I$(JAVA_HOME)/include/solaris +.ENDIF +.ENDIF + +.IF "$(SOLAR_JAVA)"=="" +nojava: + @echo "Not building javaunohelper because Java is disabled" +.ENDIF + +.IF "$(OS)" != "WNT" + +.IF "$(BUILD_UNOWINREG)" == "YES" + +$(BIN)$/unowinreg.dll : unowinreg.cxx + $(MINGWCXX) -Wall -D_JNI_IMPLEMENTATION_ $(JAVA_INCLUDES) \ + -I$(PRJ)/inc/pch -shared -o $(BIN)$/unowinreg.dll unowinreg.cxx \ + -Wl,--kill-at -lkernel32 -ladvapi32 + $(MINGWSTRIP) $(BIN)$/unowinreg.dll + +.ELSE + +$(BIN)$/unowinreg.dll : $(SOLARVERSION)$/$(INPATH)$/bin$(UPDMINOREXT)$/unowinreg.dll + @@-rm -f $@ + $(GNUCOPY) $< $@ + +.ENDIF + +.ELSE # "$(OS)" != "WNT" +# Always build unowinreg.dll on windows + +# --- Files -------------------------------------------------------- + +LINKFLAGS+=-MANIFEST:NO +SLOFILES = \ + $(SLO)$/unowinreg.obj +SHL1TARGET=$(TARGET) +SHL1LIBS=$(SLB)$/$(TARGET).lib + +#No default libraries +STDSHL= +.IF "$(COM)"=="GCC" +SHL1STDLIBS += -lstdc++ +.IF "$(MINGW_GCCLIB_EH)"=="YES" +SHL1STDLIBS += -lgcc_eh +.ENDIF +SHL1STDLIBS += -lgcc -lmingw32 -lmoldname -lmsvcrt +.ENDIF + +SHL1STDLIBS +=\ + $(KERNEL32LIB)\ + $(ADVAPI32LIB) + +SHL1DEF=$(MISC)$/$(SHL1TARGET).def + +DEF1NAME=$(SHL1TARGET) +DEF1EXPORTFILE=$(TARGET).dxp +DEF1DES=unowinreg + +.ENDIF # "$(OS)" != "WNT" + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/odk/source/unowinreg/win/unowinreg.cxx b/odk/source/unowinreg/win/unowinreg.cxx new file mode 100644 index 000000000000..312b7bb8f27a --- /dev/null +++ b/odk/source/unowinreg/win/unowinreg.cxx @@ -0,0 +1,188 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2008 by Sun Microsystems, Inc. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * $RCSfile: unowinreg.cxx,v $ + * $Revision: 1.4 $ + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_odk.hxx" + +#if defined _MSC_VER +#pragma warning(push, 1) +#endif +#include <windows.h> +#if defined _MSC_VER +#pragma warning(pop) +#endif + +#include <jni.h> + +extern "C" BOOL __stdcall _DllMainCRTStartup(HINSTANCE, DWORD, LPVOID) +{ + return TRUE; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenClassesRoot( + JNIEnv *env, jclass, jlongArray hkresult) +{ + jboolean ret = JNI_FALSE; + PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0); + if (RegOpenKeyEx(HKEY_CLASSES_ROOT, NULL, 0, KEY_READ, phkey) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentConfig( + JNIEnv *env, jclass, jlongArray hkresult) +{ + jboolean ret = JNI_FALSE; + PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0); + if (RegOpenKeyEx(HKEY_CURRENT_CONFIG, NULL, 0, KEY_READ, phkey) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentUser( + JNIEnv *env, jclass, jlongArray hkresult) +{ + jboolean ret = JNI_FALSE; + PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0); + if (RegOpenKeyEx(HKEY_CURRENT_USER, NULL, 0, KEY_READ, phkey) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenLocalMachine( + JNIEnv *env, jclass, jlongArray hkresult) +{ + jboolean ret = JNI_FALSE; + PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0); + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_READ, phkey) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenUsers( + JNIEnv *env, jclass, jlongArray hkresult) +{ + jboolean ret = JNI_FALSE; + PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0); + if (RegOpenKeyEx(HKEY_USERS, NULL, 0, KEY_READ, phkey) == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenKeyEx( + JNIEnv *env, jclass, jlong parent, jstring name, jlongArray hkresult) +{ + jboolean ret = JNI_FALSE; + const char *namestr = env->GetStringUTFChars(name, 0); + PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0); + if (RegOpenKeyEx((HKEY)parent, namestr, 0, KEY_READ, phkey) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseStringUTFChars(name, namestr); + env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0); + return ret; +} + + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegCloseKey( + JNIEnv *, jclass, jlong hkey) +{ + jboolean ret = JNI_FALSE; + if (RegCloseKey((HKEY)hkey) == ERROR_SUCCESS) + ret = JNI_TRUE; + return ret; +} + +extern "C" JNIEXPORT jboolean + JNICALL Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryValueEx( + JNIEnv *env, jclass, jlong hkey, jstring value, jlongArray type, + jbyteArray data, jlongArray size) +{ + jboolean ret = JNI_FALSE; + const char* valuestr = env->GetStringUTFChars(value, 0); + LPDWORD ptype = (LPDWORD)env->GetLongArrayElements(type, 0); + LPBYTE pdata = (LPBYTE)env->GetByteArrayElements(data, 0); + LPDWORD psize = (LPDWORD)env->GetLongArrayElements(size, 0); + if (RegQueryValueEx((HKEY)hkey, valuestr, NULL, ptype, pdata, psize) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseStringUTFChars(value, valuestr); + env->ReleaseLongArrayElements(type, (jlong *)ptype, 0); + env->ReleaseByteArrayElements(data, (jbyte *)pdata, 0); + env->ReleaseLongArrayElements(size, (jlong *)psize, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryInfoKey( + JNIEnv *env, jclass, jlong hkey, jlongArray subkeys, + jlongArray maxSubkeyLen, jlongArray values, jlongArray maxValueNameLen, + jlongArray maxValueLen, jlongArray secDescriptor) +{ + jboolean ret = JNI_FALSE; + LPDWORD psubkeys = (LPDWORD)env->GetLongArrayElements(subkeys, 0); + LPDWORD pmaxSubkeyLen = + (LPDWORD)env->GetLongArrayElements(maxSubkeyLen, 0); + LPDWORD pvalues = (LPDWORD)env->GetLongArrayElements(values, 0); + LPDWORD pmaxValueNameLen = + (LPDWORD)env->GetLongArrayElements(maxValueNameLen, 0); + LPDWORD pmaxValueLen = + (LPDWORD)env->GetLongArrayElements(maxValueLen, 0); + LPDWORD psecDescriptor = + (LPDWORD)env->GetLongArrayElements(secDescriptor, 0); + FILETIME ft; + if (RegQueryInfoKey((HKEY)hkey, NULL, NULL, NULL, psubkeys, pmaxSubkeyLen, + NULL, pvalues, pmaxValueNameLen, pmaxValueLen, + psecDescriptor, &ft) == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(subkeys, (jlong*)psubkeys, 0); + env->ReleaseLongArrayElements(maxSubkeyLen, (jlong*)pmaxSubkeyLen, 0); + env->ReleaseLongArrayElements(values, (jlong*)pvalues, 0); + env->ReleaseLongArrayElements(maxValueNameLen, (jlong*)pmaxValueNameLen, 0); + env->ReleaseLongArrayElements(maxValueLen, (jlong*)pmaxValueLen, 0); + env->ReleaseLongArrayElements(secDescriptor, (jlong*)psecDescriptor, 0); + return ret; +} diff --git a/odk/source/unowinreg/win/unowinreg.dxp b/odk/source/unowinreg/win/unowinreg.dxp new file mode 100644 index 000000000000..af44919d42a9 --- /dev/null +++ b/odk/source/unowinreg/win/unowinreg.dxp @@ -0,0 +1,9 @@ +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenClassesRoot +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentConfig +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentUser +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenLocalMachine +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenUsers +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenKeyEx +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegCloseKey +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryValueEx +Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryInfoKey |