I have a multi-threaded desktop application that appears to be deadlocking
when working with open office. Each thread is a "service" that performs a
specific task. For instance, thread T1 (of class C1) might print to a
printer, and thread T2 (of class C2) might generate PDF documents and email
them. Each thread uses the same pool of template files, but that shouldn't
be the problem. The code that each thread uses was centralized in an
instance class (not that it matters). Can anyone help me out with this? I
will post whatever code I can for your review.

NOTE: I do make use of BootstrapConnector (because I always got "no office
executable found!" when running outside the Netbeans IDE):
http://user.services.openoffice.org/en/forum/viewtopic.php?f=44&t=2520


As of now, each thread creates an instance of this class (I know, I
could/should change it so that this class was static with synchronized
methods - but then would that eliminate the benefit of threads?).

FYI, my original post was here (I will updated that with whatever helpful
information I can get on this mailing list):
http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&t=8970


/************   START CODE *****************/
package com.mycompany.ooo;

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.NoSuchElementException;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.style.XStyle;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.Any;
import com.sun.star.uno.Exception;
import com.sun.star.uno.Type;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XCloseable;
import com.sun.star.view.XPrintable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import ooo.connector.BootstrapSocketConnector;

public class OOoManager {

    private XComponentContext xRemoteContext = null;
    private XMultiComponentFactory xRemoteServiceManager = null;

    public OOoManager() {    }

    public void componentExport(XComponent xComponent, PropertyValue[]
properties, String storeFile) throws com.sun.star.io.IOException {
        XStorable xStorable = (XStorable)
UnoRuntime.queryInterface(XStorable.class, xComponent);
        xStorable.storeToURL("file:///" + storeFile, properties);
    }

    public XComponent componentImport(String fName, PropertyValue[]
properties) throws com.sun.star.uno.Exception, java.lang.Exception {
        try {
            xRemoteServiceManager = this.getRemoteServiceManager();

            Object desktop =
xRemoteServiceManager.createInstanceWithContext(
                    "com.sun.star.frame.Desktop", xRemoteContext);
            XComponentLoader xComponentLoader = (XComponentLoader)
UnoRuntime.queryInterface(XComponentLoader.class, desktop);
            XComponent xTemplateComponent =
                    (XComponent)
xComponentLoader.loadComponentFromURL("file:///" + fName, "_blank", 0,
properties);
            return xTemplateComponent;
        } catch (com.sun.star.lang.DisposedException e) {
            xRemoteContext = null;
            return null;
        } catch (com.sun.star.lang.IllegalArgumentException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, fName,
e);
            throw new com.sun.star.lang.IllegalArgumentException();
        }
    }

    public void printComponent(XComponent xComponent, PropertyValue[]
printOpts, PropertyValue[] printerDesc) throws
com.sun.star.lang.IllegalArgumentException {
        XPrintable xPrintable = (XPrintable)
UnoRuntime.queryInterface(XPrintable.class, xComponent);
        xPrintable.setPrinter(printerDesc);
        xPrintable.print(printOpts);
    }

    public void closeComponent(XComponent comp) throws Exception {
        /*
         *  This routine was taken from the following:
         *  http://www.oooforum.org/forum/viewtopic.phtml?p=145079
         */
        XCloseable xCloseable = null;
        if (comp != null) {
            try {
                xCloseable = (XCloseable)
UnoRuntime.queryInterface(XCloseable.class, comp);
                if (xCloseable != null) {
                    xCloseable.close(false);
                } else {
                    comp.dispose();
                }
            } catch (com.sun.star.lang.DisposedException e) {
                e.printStackTrace();
            }
        }
    }

    protected XMultiComponentFactory getRemoteServiceManager() throws
java.lang.Exception {
        if (xRemoteContext == null && xRemoteServiceManager == null) {
            // First step: get the remote office component context

            String oooExeFolder = System.getenv("OPENOFFICE_EXE_PATH");
            xRemoteContext =
BootstrapSocketConnector.bootstrap(oooExeFolder);
            //xRemoteContext =
com.sun.star.comp.helper.Bootstrap.bootstrap();
            xRemoteServiceManager = xRemoteContext.getServiceManager();
        }
        return xRemoteServiceManager;
    }
}
/************   ENDCODE *****************/











--
Government big enough to supply everything...is big enough to take
everything you have. The course of history shows that as a government grows,
liberty decreases --- Thomas Jefferson
www.CampaignForLiberty.org

Reply via email to