Author: cmarcum Date: Sat Jan 23 19:49:46 2016 New Revision: 1726442 URL: http://svn.apache.org/viewvc?rev=1726442&view=rev Log: #i126807# initial import of bootstrap-connector as a gradle project
Added: openoffice/devtools/bootstrap-connector/ openoffice/devtools/bootstrap-connector/.gradle/ openoffice/devtools/bootstrap-connector/.gradle/2.7/ openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/ openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties (with props) openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties.lock (with props) openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileHashes.bin (with props) openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileSnapshots.bin (with props) openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/outputFileStates.bin (with props) openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/taskArtifacts.bin (with props) openoffice/devtools/bootstrap-connector/README.md openoffice/devtools/bootstrap-connector/build.gradle openoffice/devtools/bootstrap-connector/gradle.properties (with props) openoffice/devtools/bootstrap-connector/src/ openoffice/devtools/bootstrap-connector/src/main/ openoffice/devtools/bootstrap-connector/src/main/java/ openoffice/devtools/bootstrap-connector/src/main/java/ooo/ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapConnector.java (with props) openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapPipeConnector.java (with props) openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapSocketConnector.java (with props) openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/example/ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java (with props) openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/server/ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/server/OOoServer.java (with props) openoffice/devtools/bootstrap-connector/src/main/resources/ openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/ openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/CREDIT openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/LICENSE (with props) openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/NOTICE (with props) openoffice/devtools/bootstrap-connector/src/test/ openoffice/devtools/bootstrap-connector/src/test/java/ Added: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties (added) +++ openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties Sat Jan 23 19:49:46 2016 @@ -0,0 +1 @@ +#Sat Jan 23 07:31:56 EST 2016 Propchange: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties.lock URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties.lock?rev=1726442&view=auto ============================================================================== Binary file - no diff available. Propchange: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/cache.properties.lock ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileHashes.bin URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileHashes.bin?rev=1726442&view=auto ============================================================================== Binary file - no diff available. Propchange: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileHashes.bin ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileSnapshots.bin URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileSnapshots.bin?rev=1726442&view=auto ============================================================================== Binary file - no diff available. Propchange: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/fileSnapshots.bin ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/outputFileStates.bin URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/outputFileStates.bin?rev=1726442&view=auto ============================================================================== Binary file - no diff available. Propchange: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/outputFileStates.bin ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/taskArtifacts.bin URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/taskArtifacts.bin?rev=1726442&view=auto ============================================================================== Binary file - no diff available. Propchange: openoffice/devtools/bootstrap-connector/.gradle/2.7/taskArtifacts/taskArtifacts.bin ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: openoffice/devtools/bootstrap-connector/README.md URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/README.md?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/README.md (added) +++ openoffice/devtools/bootstrap-connector/README.md Sat Jan 23 19:49:46 2016 @@ -0,0 +1,45 @@ +BootstrapConnector +================== + +Introduction +------------ +This software can be used by Java applications to bootstrap Apache OpenOffice when the Java UNO jars are downloaded from a Maven repository or otherwise in a different location than the soffice executable. + +How-To +------ +The original How-to can be found here [OpenOffice.org Forum][1]. + +* Add the jar file from building this software to the classpath. +* Determine the folder of your OpenOffice.org executable "soffice.exe" (on Windows systems) or "soffice" (on *nix systems). On Windows it might be something like "C:\Program Files (x86)\OpenOffice 4\program\", and on *nix for example something like "/opt/openoffice4/program". +* Edit your Java source code file, that tries to get the connection by calling "Bootstrap.bootstrap()". This is mostly done with a Java source code line looking like this: +> XComponentContext xContext = Bootstrap.bootstrap(); +* Add the following line to your import statements: +> import ooo.connector.BootstrapSocketConnector; +* Add a line above "Bootstrap.bootstrap()" to assign the name of the folder of your OpenOffice.org executable to a String variable (note slash direction for Windows): +> String oooExeFolder = "C:/Program Files (x86)/OpenOffice 4/program/"; +* Change the call of "Bootstrap.bootstrap()" to "BootstrapSocketConnector.bootstrap(oooExeFolder)": +> XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExeFolder); + +Building +-------- +This software uses the Gradle build system and can be built with: +> gradle clean jar +Javadoc can be built with: +> gradle javadoc +Or all at once: +> gradle clean javadoc jar + +Credits +------- +This software is based on source code found in bootstrapconnector.jar authored +by OpenOffice.org forum user hol.sten in a OpenOffice.org forum post [OpenOffice.org Forum][1]. + +License +------- +This software and the application of the Apache License 2.0 is by permission granted by hol.sten and documented with the Apache OpenOffice PMC. + +See src/main/resources/META-INF/CREDIT, LICENSE, and NOTICE files for more information. + +[1]: https://forum.openoffice.org/en/forum/viewtopic.php?t=2520 "OpenOffice.org Forum" + + Added: openoffice/devtools/bootstrap-connector/build.gradle URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/build.gradle?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/build.gradle (added) +++ openoffice/devtools/bootstrap-connector/build.gradle Sat Jan 23 19:49:46 2016 @@ -0,0 +1,17 @@ +apply plugin: "java" +apply plugin: "application" + +repositories { + mavenCentral() +} + +version "0.1.0" + +dependencies { + compile "org.openoffice:juh:4.1.2" + compile "org.openoffice:ridl:4.1.2" + compile "org.openoffice:unoil:4.1.2" + compile "org.openoffice:jurt:4.1.2" + +} + Added: openoffice/devtools/bootstrap-connector/gradle.properties URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/gradle.properties?rev=1726442&view=auto ============================================================================== (empty) Propchange: openoffice/devtools/bootstrap-connector/gradle.properties ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapConnector.java URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapConnector.java?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapConnector.java (added) +++ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapConnector.java Sat Jan 23 19:49:46 2016 @@ -0,0 +1,220 @@ +package ooo.connector; + +import com.sun.star.bridge.UnoUrlResolver; +import com.sun.star.bridge.XUnoUrlResolver; +import com.sun.star.comp.helper.Bootstrap; +import com.sun.star.comp.helper.BootstrapException; +import com.sun.star.connection.ConnectionSetupException; +import com.sun.star.connection.NoConnectException; +import com.sun.star.frame.XDesktop; +import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import ooo.connector.server.OOoServer; + +/** + * A bootstrap connector which establishes a connection to an OOo server. + * + * Most of the source code in this class has been taken from the Java class + * "Bootstrap.java" (Revision: 1.15) from the UDK projekt (Uno Software Develop- + * ment Kit) from OpenOffice.org (http://udk.openoffice.org/). The source code + * is available for example through a browser based online version control + * access at http://udk.openoffice.org/source/browse/udk/. The Java class + * "Bootstrap.java" is there available at + * http://udk.openoffice.org/source/browse/udk/javaunohelper/com/sun/star/comp/helper/Bootstrap.java?view=markup + * + * The idea to develop this BootstrapConnector comes from the blog "Getting + * started with the OpenOffice.org API part III : starting OpenOffice.org with + * jars not in the OOo install dir by Wouter van Reeven" + * (http://technology.amis.nl/blog/?p=1284) and from various posts in the + * "(Unofficial) OpenOffice.org Forum" at http://www.oooforum.org/ and the + * "OpenOffice.org Community Forum" at http://user.services.openoffice.org/ + * complaining about "no office executable found!". + */ +public class BootstrapConnector { + + /** The OOo server. */ + private OOoServer oooServer; + + /** The connection string which has ben used to establish the connection. */ + private String oooConnectionString; + + /** + * Constructs a bootstrap connector which uses the folder of the OOo + * installation containing the soffice executable. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + */ + public BootstrapConnector(String oooExecFolder) { + + this.oooServer = new OOoServer(oooExecFolder); + this.oooConnectionString = null; + } + + /** + * Constructs a bootstrap connector which connects to the specified + * OOo server. + * + * @param oooServer The OOo server + */ + public BootstrapConnector(OOoServer oooServer) { + + this.oooServer = oooServer; + this.oooConnectionString = null; + } + + /** + * Connects to an OOo server using the specified accept option and + * connection string and returns a component context for using the + * connection to the OOo server. + * + * The accept option and the connection string should match to get a + * connection. OOo provides to different types of connections: + * 1) The socket connection + * 2) The named pipe connection + * + * To create a socket connection a host and port must be provided. + * For example using the host "localhost" and the port "8100" the + * accept option and connection string looks like this: + * - accept option : -accept=socket,host=localhost,port=8100;urp; + * - connection string: uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext + * + * To create a named pipe a pipe name must be provided. For example using + * the pipe name "oooPipe" the accept option and connection string looks + * like this: + * - accept option : -accept=pipe,name=oooPipe;urp; + * - connection string: uno:pipe,name=oooPipe;urp;StarOffice.ComponentContext + * + * @param oooAcceptOption The accept option + * @param oooConnectionString The connection string + * @return The component context + */ + public XComponentContext connect(String oooAcceptOption, String oooConnectionString) throws BootstrapException { + + this.oooConnectionString = oooConnectionString; + + XComponentContext xContext = null; + try { + // get local context + XComponentContext xLocalContext = getLocalContext(); + + oooServer.start(oooAcceptOption); + + // initial service manager + XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager(); + if ( xLocalServiceManager == null ) + throw new BootstrapException("no initial service manager!"); + + // create a URL resolver + XUnoUrlResolver xUrlResolver = UnoUrlResolver.create(xLocalContext); + + // wait until office is started + for (int i = 0;; ++i) { + try { + xContext = getRemoteContext(xUrlResolver); + break; + } catch ( com.sun.star.connection.NoConnectException ex ) { + // Wait 500 ms, then try to connect again, but do not wait + // longer than 5 min (= 600 * 500 ms) total: + if (i == 600) { + throw new BootstrapException(ex.toString()); + } + Thread.sleep(500); + } + } + } catch (java.lang.RuntimeException e) { + throw e; + } catch (java.lang.Exception e) { + throw new BootstrapException(e); + } + return xContext; + } + + /** + * Disconnects from an OOo server using the connection string from the + * previous connect. + * + * If there has been no previous connect, the disconnects does nothing. + * + * If there has been a previous connect, disconnect tries to terminate + * the OOo server and kills the OOo server process the connect started. + */ + public void disconnect() { + + if (oooConnectionString == null) + return; + + // call office to terminate itself + try { + // get local context + XComponentContext xLocalContext = getLocalContext(); + + // create a URL resolver + XUnoUrlResolver xUrlResolver = UnoUrlResolver.create(xLocalContext); + + // get remote context + XComponentContext xRemoteContext = getRemoteContext(xUrlResolver); + + // get desktop to terminate office + Object desktop = xRemoteContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop",xRemoteContext); + XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop); + xDesktop.terminate(); + } + catch (Exception e) { + // Bad luck, unable to terminate office + } + + oooServer.kill(); + oooConnectionString = null; + } + + /** + * Create default local component context. + * + * @return The default local component context + */ + private XComponentContext getLocalContext() throws BootstrapException, Exception { + + XComponentContext xLocalContext = Bootstrap.createInitialComponentContext(null); + if (xLocalContext == null) { + throw new BootstrapException("no local component context!"); + } + return xLocalContext; + } + + /** + * Try to connect to office. + * + * @return The remote component context + */ + private XComponentContext getRemoteContext(XUnoUrlResolver xUrlResolver) throws BootstrapException, ConnectionSetupException, IllegalArgumentException, NoConnectException { + + Object context = xUrlResolver.resolve(oooConnectionString); + XComponentContext xContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, context); + if (xContext == null) { + throw new BootstrapException("no component context!"); + } + return xContext; + } + + /** + * Bootstraps a connection to an OOo server in the specified soffice + * executable folder of the OOo installation using the specified accept + * option and connection string and returns a component context for using + * the connection to the OOo server. + * + * The accept option and the connection string should match in connection + * type and pipe name or host and port to get a connection. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + * @param oooAcceptOption The accept option + * @param oooConnectionString The connection string + * @return The component context + */ + public static final XComponentContext bootstrap(String oooExecFolder, String oooAcceptOption, String oooConnectionString) throws BootstrapException { + + BootstrapConnector bootstrapConnector = new BootstrapConnector(oooExecFolder); + return bootstrapConnector.connect(oooAcceptOption, oooConnectionString); + } +} Propchange: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapConnector.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapPipeConnector.java URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapPipeConnector.java?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapPipeConnector.java (added) +++ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapPipeConnector.java Sat Jan 23 19:49:46 2016 @@ -0,0 +1,100 @@ +package ooo.connector; + +import com.sun.star.comp.helper.BootstrapException; +import com.sun.star.uno.XComponentContext; +import java.util.Random; +import ooo.connector.server.OOoServer; + +//removed &highlight=named+pipe from end of url below for javadoc +/** + * A bootstrap connector which uses a named pipe to connect to an OOo server. + * + * Very helpful in getting the named pipe connection working has been the posts + * of mnasato in the thread "Correct FilterName to open RTF from bytestream?" at + * http://www.oooforum.org/forum/viewtopic.phtml?t=40263 + */ +public class BootstrapPipeConnector extends BootstrapConnector { + + /** + * Constructs a bootstrap pipe connector which uses the specified folder of + * the OOo installation containing the soffice executable. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + */ + public BootstrapPipeConnector(String oooExecFolder) { + + super(oooExecFolder); + } + + /** + * Constructs a bootstrap pipe connector which connects to the specified + * OOo server. + * + * @param oooServer The OOo server + */ + public BootstrapPipeConnector(OOoServer oooServer) { + + super(oooServer); + } + + /** + * Connects to an OOo server using a random pipe name and returns a + * component context for using the connection to the OOo server. + * + * @return The component context + */ + public XComponentContext connect() throws BootstrapException { + + // create random pipe name + String sPipeName = "uno" + Long.toString((new Random()).nextLong() & 0x7fffffffffffffffL); + + return connect(sPipeName); + } + + /** + * Connects to an OOo server using the specified pipe name and returns a + * component context for using the connection to the OOo server. + * + * @param pipeName The pipe name + * @return The component context + */ + public XComponentContext connect(String pipeName) throws BootstrapException { + + // accept option + String oooAcceptOption = "-accept=pipe,name=" + pipeName + ";urp;"; + + // connection string + String unoConnectString = "uno:pipe,name=" + pipeName + ";urp;StarOffice.ComponentContext"; + + return connect(oooAcceptOption, unoConnectString); + } + + /** + * Bootstraps a connection to an OOo server in the specified soffice + * executable folder of the OOo installation using a random pipe name and + * returns a component context for using the connection to the OOo server. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + * @return The component context + */ + public static final XComponentContext bootstrap(String oooExecFolder) throws BootstrapException { + + BootstrapPipeConnector bootstrapPipeConnector = new BootstrapPipeConnector(oooExecFolder); + return bootstrapPipeConnector.connect(); + } + + /** + * Bootstraps a connection to an OOo server in the specified soffice + * executable folder of the OOo installation using the specified pipe name + * and returns a component context for using the connection to OOo server. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + * @param pipeName The pipe name + * @return The component context + */ + public static final XComponentContext bootstrap(String oooExecFolder, String pipeName) throws BootstrapException { + + BootstrapPipeConnector bootstrapPipeConnector = new BootstrapPipeConnector(oooExecFolder); + return bootstrapPipeConnector.connect(pipeName); + } +} Propchange: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapPipeConnector.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapSocketConnector.java URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapSocketConnector.java?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapSocketConnector.java (added) +++ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapSocketConnector.java Sat Jan 23 19:49:46 2016 @@ -0,0 +1,101 @@ +package ooo.connector; + +import com.sun.star.comp.helper.BootstrapException; +import com.sun.star.uno.XComponentContext; +import ooo.connector.server.OOoServer; + +/** + * A Bootstrap Connector which uses a socket to connect to an OOo server. + */ +public class BootstrapSocketConnector extends BootstrapConnector { + + /** + * Constructs a bootstrap socket connector which uses the folder of the OOo installation containing the soffice executable. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + */ + public BootstrapSocketConnector(String oooExecFolder) { + + super(oooExecFolder); + } + + /** + * Constructs a bootstrap socket connector which connects to the specified + * OOo server. + * + * @param oooServer The OOo server + */ + public BootstrapSocketConnector(OOoServer oooServer) { + + super(oooServer); + } + + /** + * Connects to an OOo server using a default socket and returns a + * component context for using the connection to the OOo server. + * + * @return The component context + */ + public XComponentContext connect() throws BootstrapException { + + // create random pipe name + String host = "localhost"; + int port = 8100; + + return connect(host,port); + } + + /** + * Connects to an OOo server using the specified host and port for the + * socket and returns a component context for using the connection to the + * OOo server. + * + * @param host The host + * @param port The port + * @return The component context + */ + public XComponentContext connect(String host, int port) throws BootstrapException { + + // host and port + String hostAndPort = "host="+host+",port="+port; + + // accept option + String oooAcceptOption = "-accept=socket,"+hostAndPort+";urp;"; + + // connection string + String unoConnectString = "uno:socket,"+hostAndPort+";urp;StarOffice.ComponentContext"; + + return connect(oooAcceptOption, unoConnectString); + } + + /** + * Bootstraps a connection to an OOo server in the specified soffice + * executable folder of the OOo installation using a default socket and + * returns a component context for using the connection to the OOo server. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + * @return The component context + */ + public static final XComponentContext bootstrap(String oooExecFolder) throws BootstrapException { + + BootstrapSocketConnector bootstrapSocketConnector = new BootstrapSocketConnector(oooExecFolder); + return bootstrapSocketConnector.connect(); + } + + /** + * Bootstraps a connection to an OOo server in the specified soffice + * executable folder of the OOo installation using the specified host and + * port for the socket and returns a component context for using the + * connection to the OOo server. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + * @param host The host + * @param port The port + * @return The component context + */ + public static final XComponentContext bootstrap(String oooExecFolder, String host, int port) throws BootstrapException { + + BootstrapSocketConnector bootstrapSocketConnector = new BootstrapSocketConnector(oooExecFolder); + return bootstrapSocketConnector.connect(host,port); + } +} \ No newline at end of file Propchange: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/BootstrapSocketConnector.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java (added) +++ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java Sat Jan 23 19:49:46 2016 @@ -0,0 +1,110 @@ +package ooo.connector.example; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.comp.helper.BootstrapException; +import com.sun.star.connection.NoConnectException; +import com.sun.star.frame.XComponentLoader; +import com.sun.star.frame.XStorable; +import com.sun.star.io.IOException; +import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; +import java.util.List; +import ooo.connector.BootstrapSocketConnector; +import ooo.connector.server.OOoServer; + +public class BootstrapSocketConnectorExample { + + private static final String OOO_EXEC_FOLDER = "C:/Programme/OpenOffice.org 2.3/program/"; + + private static final String TEMPLATE_FOLDER = "C:/Temp/"; + private static final String TEXT_DOCUMENT_NAME = "Text"; + + private static final String FILE_URL_PREFIX = "file:///"; + private static final String TEXT_DOCUMENT_EXTENSION = ".odt"; + private static final String PDF_DOCUMENT_EXTENSION = ".pdf"; + + /** + * Converts an OOo text document (.odt) to a PDF file using a + * BootstrapConnector. + * + * @param args The command line arguments + */ + public static void main(String[] args) { + + String textDocumentName = (args.length > 0)? args[0]: TEXT_DOCUMENT_NAME; + + String loadUrl=FILE_URL_PREFIX+TEMPLATE_FOLDER+textDocumentName+TEXT_DOCUMENT_EXTENSION; + String storeUrl; + + try { + storeUrl=FILE_URL_PREFIX+TEMPLATE_FOLDER+textDocumentName+"SC"+PDF_DOCUMENT_EXTENSION; + convertWithStaticConnector(loadUrl, storeUrl); + + storeUrl=FILE_URL_PREFIX+TEMPLATE_FOLDER+textDocumentName+"C"+PDF_DOCUMENT_EXTENSION; + convertWithConnector(loadUrl, storeUrl); + } + catch (NoConnectException e) { + System.out.println("OOo is not responding"); + e.printStackTrace(); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + System.exit(0); + } + } + + private static void convertWithStaticConnector(String loadUrl, String storeUrl) throws Exception, IllegalArgumentException, IOException, BootstrapException { + + // Connect to OOo + XComponentContext remoteContext = BootstrapSocketConnector.bootstrap(OOO_EXEC_FOLDER); + + // Convert text document to PDF + convert(loadUrl, storeUrl, remoteContext); + } + + private static void convertWithConnector(String loadUrl, String storeUrl) throws Exception, IllegalArgumentException, IOException, BootstrapException { + + // Create OOo server with additional -nofirststartwizard option + List oooOptions = OOoServer.getDefaultOOoOptions(); + oooOptions.add("-nofirststartwizard"); + OOoServer oooServer = new OOoServer(OOO_EXEC_FOLDER, oooOptions); + + // Connect to OOo + BootstrapSocketConnector bootstrapSocketConnector = new BootstrapSocketConnector(oooServer); + XComponentContext remoteContext = bootstrapSocketConnector.connect(); + + // Convert text document to PDF + convert(loadUrl, storeUrl, remoteContext); + + // Disconnect and terminate OOo server + bootstrapSocketConnector.disconnect(); + } + + protected static void convert(String loadUrl, String storeUrl, XComponentContext remoteContext) throws IllegalArgumentException, IOException, Exception { + + XComponentLoader xcomponentloader = getComponentLoader(remoteContext); + + Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(loadUrl, "_blank", 0, new PropertyValue[0]); + + PropertyValue[] conversionProperties = new PropertyValue[1]; + conversionProperties[0] = new PropertyValue(); + conversionProperties[0].Name = "FilterName"; + conversionProperties[0].Value = "writer_pdf_Export"; + + XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class,objectDocumentToStore); + xstorable.storeToURL(storeUrl, conversionProperties); + } + + private static XComponentLoader getComponentLoader(XComponentContext remoteContext) throws Exception { + + XMultiComponentFactory remoteServiceManager = remoteContext.getServiceManager(); + Object desktop = remoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", remoteContext); + XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,desktop); + + return xcomponentloader; + } +} Propchange: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/server/OOoServer.java URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/server/OOoServer.java?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/server/OOoServer.java (added) +++ openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/server/OOoServer.java Sat Jan 23 19:49:46 2016 @@ -0,0 +1,173 @@ +package ooo.connector.server; + +import com.sun.star.comp.helper.BootstrapException; +import com.sun.star.lib.util.NativeLibraryLoader; +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.PrintStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; + +/** + * Starts and stops an OOo server. + * + * Most of the source code in this class has been taken from the Java class + * "Bootstrap.java" (Revision: 1.15) from the UDK projekt (Uno Software Develop- + * ment Kit) from OpenOffice.org (http://udk.openoffice.org/). The source code + * is available for example through a browser based online version control + * access at http://udk.openoffice.org/source/browse/udk/. The Java class + * "Bootstrap.java" is there available at + * http://udk.openoffice.org/source/browse/udk/javaunohelper/com/sun/star/comp/helper/Bootstrap.java?view=markup + */ +public class OOoServer { + + /** The OOo server process. */ + private Process oooProcess; + + /** The folder of the OOo installation containing the soffice executable. */ + private String oooExecFolder; + + /** The options for starting the OOo server. */ + private List oooOptions; + + /** + * Constructs an OOo server which uses the folder of the OOo installation + * containing the soffice executable and a list of default options to start + * OOo. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + */ + public OOoServer(String oooExecFolder) { + + this.oooProcess = null; + this.oooExecFolder = oooExecFolder; + this.oooOptions = getDefaultOOoOptions(); + } + + /** + * Constructs an OOo server which uses the folder of the OOo installation + * containing the soffice executable and a given list of options to start + * OOo. + * + * @param oooExecFolder The folder of the OOo installation containing the soffice executable + * @param oooOptions The list of options + */ + public OOoServer(String oooExecFolder, List oooOptions) { + + this.oooProcess = null; + this.oooExecFolder = oooExecFolder; + this.oooOptions = oooOptions; + } + + /** + * Starts an OOo server which uses the specified accept option. + * + * The accept option can be used for two different types of connections: + * 1) The socket connection + * 2) The named pipe connection + * + * To create a socket connection a host and port must be provided. + * For example using the host "localhost" and the port "8100" the + * accept option looks like this: + * - accept option : -accept=socket,host=localhost,port=8100;urp; + * + * To create a named pipe a pipe name must be provided. For example using + * the pipe name "oooPipe" the accept option looks like this: + * - accept option : -accept=pipe,name=oooPipe;urp; + * + * @param oooAcceptOption The accept option + */ + public void start(String oooAcceptOption) throws BootstrapException, IOException, MalformedURLException { + + // find office executable relative to this class's class loader + String sOffice = System.getProperty("os.name").startsWith("Windows")? "soffice.exe": "soffice"; + + URL[] oooExecFolderURL = new URL[] {new File(oooExecFolder).toURI().toURL()}; + URLClassLoader loader = new URLClassLoader(oooExecFolderURL); + File fOffice = NativeLibraryLoader.getResource(loader, sOffice); + if (fOffice == null) + throw new BootstrapException("no office executable found!"); + + // create call with arguments + int arguments = (oooOptions != null)? oooOptions.size()+1: 1; + if (oooAcceptOption != null) + arguments++; + + String[] oooCommand = new String[arguments]; + oooCommand[0] = fOffice.getPath(); + + for (int i = 0; i < oooOptions.size(); i++) { + oooCommand[i+1] = (String) oooOptions.get(i); + } + + if (oooAcceptOption != null) + oooCommand[arguments-1] = oooAcceptOption; + + // start office process + oooProcess = Runtime.getRuntime().exec(oooCommand); + + pipe(oooProcess.getInputStream(), System.out, "CO> "); + pipe(oooProcess.getErrorStream(), System.err, "CE> "); + } + + /** + * Kills the OOo server process from the previous start. + * + * If there has been no previous start of the OOo server, the kill does + * nothing. + * + * If there has been a previous start, kill destroys the process. + */ + public void kill() { + + if (oooProcess != null) + { + oooProcess.destroy(); + oooProcess = null; + } + } + + private static void pipe(final InputStream in, final PrintStream out, final String prefix ) { + new Thread( "Pipe: " + prefix) { + @Override + public void run() { + BufferedReader r = new BufferedReader(new InputStreamReader(in)); + try { + for ( ; ; ) { + String s = r.readLine(); + if (s == null) { + break; + } + out.println(prefix + s); + } + } catch (java.io.IOException e) { + e.printStackTrace(System.err); + } + } + }.start(); + } + + /** + * Returns the list of default options. + * + * @return The list of default options + */ + public static List getDefaultOOoOptions() { + + ArrayList options = new ArrayList(); + + options.add("-nologo"); + options.add("-nodefault"); + options.add("-norestore"); + options.add("-nocrashreport"); + options.add("-nolockcheck"); + + return options; + } +} \ No newline at end of file Propchange: openoffice/devtools/bootstrap-connector/src/main/java/ooo/connector/server/OOoServer.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/CREDIT URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/CREDIT?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/CREDIT (added) +++ openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/CREDIT Sat Jan 23 19:49:46 2016 @@ -0,0 +1,6 @@ +This software is based on source code found in bootstrapconnector.jar authored +by OpenOffice.org forum user hol.sten in a OpenOffice.org forum post +https://forum.openoffice.org/en/forum/viewtopic.php?t=2520 + +This software and the application of the Apache License 2.0 is by permission granted by hol.sten. + Added: openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/LICENSE URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/LICENSE?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/LICENSE (added) +++ openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/LICENSE Sat Jan 23 19:49:46 2016 @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. Propchange: openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/LICENSE ------------------------------------------------------------------------------ svn:eol-style = native Added: openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/NOTICE URL: http://svn.apache.org/viewvc/openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/NOTICE?rev=1726442&view=auto ============================================================================== --- openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/NOTICE (added) +++ openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/NOTICE Sat Jan 23 19:49:46 2016 @@ -0,0 +1,9 @@ +Apache OpenOffice (http://www.openoffice.org) +Copyright 2011, 2015 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +Portions of this software copyright (c) 2000-2011, +Oracle and/or its affiliates <http://www.oracle.com/>. + Propchange: openoffice/devtools/bootstrap-connector/src/main/resources/META-INF/NOTICE ------------------------------------------------------------------------------ svn:eol-style = native