From 7c42535c58142db5531d9c4e58a8d419c16416bf Mon Sep 17 00:00:00 2001 From: jeremysolarz Date: Sun, 21 May 2017 01:03:52 +0200 Subject: [PATCH] First commit of bootstrap-connector --- META-INF/MANIFEST.MF | 3 + README.md | 50 ++++ pom.xml | 53 +++++ .../ooo/connector/BootstrapConnector.java | 220 ++++++++++++++++++ .../ooo/connector/BootstrapPipeConnector.java | 99 ++++++++ .../connector/BootstrapSocketConnector.java | 101 ++++++++ .../BootstrapSocketConnectorExample.java | 110 +++++++++ .../java/ooo/connector/server/OOoServer.java | 173 ++++++++++++++ 8 files changed, 809 insertions(+) create mode 100644 META-INF/MANIFEST.MF create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/ooo/connector/BootstrapConnector.java create mode 100644 src/main/java/ooo/connector/BootstrapPipeConnector.java create mode 100644 src/main/java/ooo/connector/BootstrapSocketConnector.java create mode 100644 src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java create mode 100644 src/main/java/ooo/connector/server/OOoServer.java diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..2ac9cbf --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Created-By: 1.5.0_14 (Sun Microsystems Inc.) + diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcd4ec8 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# Introduction + +This is a short How-To for quickly solving the regularly occurring error "no office executable found!". +You can read a huge number of threads in various OpenOffice.org forums dealing with this problem. +This error is caused by using the [Bootstrap Connection Mechanism](https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=1013) (introduced with OOo 2.x) in +conjunction with +moving +or copying "juh.jar" (OOo's "Java UNO Helper" JAR file containing the class file "Bootstrap.class") from the subfolder +"program\classes" of the OOo installation folder (which is for example on Windows "c:\program files\OpenOffice.org 2.3") +to another folder (mostly a subfolder, which is directly accessible from a web container or web server like for +example Tomcat). + +# How-to +So, if you're encountering **"no office executable found!"** follow these steps: + +1. Download the file "bootstrapconnector.jar" attached to this post and save it. +1. Put the JAR file "bootstrapconnector.jar" for example in the same folder, where you put "juh.jar", and add it to +your CLASSPATH or add it in your IDE to the libraries for source code compiling (for example open in NetBeans the +project properties and select there "Libraries" > tab "Compile" > press "Add JAR/Folder" > locate the "bootstrapconnector.jar" and press "Open"). +1. 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\OpenOffice.org 2.3\program\", and on *nix for example something like "/opt/openoffice.org2.3/program" or "/usr/lib/openoffice.org/program". +1. 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();``` + Perform the following steps in this Java source file: + 1. Add the following line to your import statements:
+ ```import ooo.connector.BootstrapSocketConnector;``` + 1. Add a line above "Bootstrap.bootstrap()" to assign the name of the folder of your OpenOffice.org executable to a String variable:
+ ```String oooExeFolder = "C:/Program Files/OpenOffice.org 2.3/program/";``` + 1. Change the call of "Bootstrap.bootstrap()" to "BootstrapSocketConnector.bootstrap(oooExeFolder)": + ```XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExeFolder);``` + 1. Save and compile the edited file and see if "no office executable found!" has really vanished. +1. If you want to learn more about what you can do with "bootstrapconnector.jar", stay tuned. (To be continued...) + +# Credits + +Most of the source code in the attached JAR file has been taken from the Java class "Bootstrap.java" (Revision: 1.15) +from the UDK project (Uno Software Development 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://www.openoffice.org/udk/source/browse/udk/javaunohelper/com/sun/star/comp/helper/Bootstrap.java?view=markup + +The idea to develop the BootstrapConnector, BootstrapSocketConnector and BootstrapPipeConnector 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 this forum complaining about "no office executable found!". + +## Source +Taken from +[https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=2520](https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=2520#). \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c45ee47 --- /dev/null +++ b/pom.xml @@ -0,0 +1,53 @@ + + 4.0.0 + ooo.connector + bootstrap-connector + 1.0.0 + + + 1.7 + 1.7 + UTF-8 + + + + + org.libreoffice + juh + 5.3.2 + + + org.libreoffice + jurt + 5.3.2 + + + org.libreoffice + ridl + 5.3.2 + + + org.libreoffice + unoil + 5.3.2 + + + org.libreoffice + officebean + 5.3.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + -Xlint:unchecked + + + + + \ No newline at end of file diff --git a/src/main/java/ooo/connector/BootstrapConnector.java b/src/main/java/ooo/connector/BootstrapConnector.java new file mode 100644 index 0000000..4cca65d --- /dev/null +++ b/src/main/java/ooo/connector/BootstrapConnector.java @@ -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); + } +} \ No newline at end of file diff --git a/src/main/java/ooo/connector/BootstrapPipeConnector.java b/src/main/java/ooo/connector/BootstrapPipeConnector.java new file mode 100644 index 0000000..a58d889 --- /dev/null +++ b/src/main/java/ooo/connector/BootstrapPipeConnector.java @@ -0,0 +1,99 @@ +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; + +/** + * 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&highlight=named+pipe. + */ +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); + } +} \ No newline at end of file diff --git a/src/main/java/ooo/connector/BootstrapSocketConnector.java b/src/main/java/ooo/connector/BootstrapSocketConnector.java new file mode 100644 index 0000000..c1c5824 --- /dev/null +++ b/src/main/java/ooo/connector/BootstrapSocketConnector.java @@ -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 diff --git a/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java b/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java new file mode 100644 index 0000000..5702ef5 --- /dev/null +++ b/src/main/java/ooo/connector/example/BootstrapSocketConnectorExample.java @@ -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; + } +} diff --git a/src/main/java/ooo/connector/server/OOoServer.java b/src/main/java/ooo/connector/server/OOoServer.java new file mode 100644 index 0000000..061d510 --- /dev/null +++ b/src/main/java/ooo/connector/server/OOoServer.java @@ -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