Search code examples
xpagesopenoffice.orguno

Connect XPage with OpenOffice


I have a button on an XPage where I want to connect to a remote OpenOffice instance. OpenOffice is started and is listening for a socket connection.

The onclick event of the button runs following SSJS:

    oo = new com.test.OpenOffice();
    oo.init("host=127.0.0.1,port=8107");
    oo.openFile("C:\\TEMP\\Test.odt");

The code raises an excepction jva.lang.IlleagalStateException: NotesContext not initialized for the thread

The exception is raised within the method initof the class OpenOffice.

The relevant parts of the class OpenOffice is the following code:

public class DHOpenOffice implements Serializable {
    private static final long serialVersionUID = -7443191805456329135L;
    private XComponentContext xRemoteContext;
    private XMultiComponentFactory xMCF;
    private XTextDocument oTextDocument;

    public DHOpenOffice() {
        xRemoteContext = null;
        xMCF = null;
        oTextDocument = null;
    }   

    public void init(String hostAdr) throws java.lang.Exception {
        xRemoteContext = null;

        XComponentContext xLocalContext = Bootstrap.createInitialComponentContext(null);
        XUnoUrlResolver xUrlResolver = UnoUrlResolver.create(xLocalContext);

        String sConnect = "uno:socket," + hostAdr + ",tcpNoDelay=0;urp;StarOffice.ServiceManager";

        Object context = xUrlResolver.resolve(sConnect);
        xRemoteContext = UnoRuntime.queryInterface(XComponentContext.class, context);  
        xMCF = xRemoteContext.getServiceManager();  
    }

The code line Object context = xUrlResolver.resolve(sConnect); is the one that raises the exception.

Why is this happing? What is the reason for this exception and how can I resolve the situation?

N.B.: The class code runs smoothly in a standalone application. The error occurs only when the code is started by a SSJS code.


Solution

  • Thanks to the answer of @stwissel I was able to solve the problem (he pointed me to the right direction).

    I could solve the problem with a simple OSGI plug-in. The servlet approach solved the problem, too, but for me the OSGI plug-in was easier to use.

    So these are the steps to create the plug-in

    • start a new plugin project
    • copy the open office jar files into the project and include them into the build path
    • copy the custom class that uses the UNO API into the plug-in
    • create a feature project for the plugin
    • create an update site
    • deploy the plugin via an update site

    The following site where also quite helpfull: