Search in sources :

Example 1 with XUnoUrlResolver

use of com.sun.star.bridge.XUnoUrlResolver in project translationstudio8 by heartsome.

the class TerminationOpenoffice method closeOpenoffice.

// public static void main(String[] args) {
/**
	 * Close openoffice.
	 * @param port
	 *            the port
	 */
public static void closeOpenoffice(String port) {
    XComponentContext xRemoteContext = null;
    XMultiComponentFactory xRemoteServiceManager = null;
    XDesktop xDesktop = null;
    try {
        XComponentContext xLocalContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
        XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
        Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", //$NON-NLS-1$
        xLocalContext);
        XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver);
        Object initialObject = xUnoUrlResolver.resolve(//$NON-NLS-1$ //$NON-NLS-2$
        "uno:socket,host=localhost,port=" + port + ";urp;StarOffice.ServiceManager");
        // Object initialObject =
        // xUnoUrlResolver.resolve("uno:pipe,name=my_app;urp;StarOffice.ServiceManager"
        // );
        XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, initialObject);
        //$NON-NLS-1$
        Object context = xPropertySet.getPropertyValue("DefaultContext");
        xRemoteContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, context);
        xRemoteServiceManager = xRemoteContext.getServiceManager();
        // get Desktop instance
        Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", //$NON-NLS-1$
        xRemoteContext);
        xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
        TerminateListener terminateListener = new TerminateListener();
        xDesktop.addTerminateListener(terminateListener);
        // try to terminate while we are at work
        atWork = true;
        boolean terminated = xDesktop.terminate();
        System.out.println(//$NON-NLS-1$
        "The Office " + //$NON-NLS-1$ //$NON-NLS-2$
        (terminated ? "has been terminated" : "is still running, we are at work"));
        // no longer at work
        atWork = false;
        // once more: try to terminate
        terminated = xDesktop.terminate();
        System.out.println(//$NON-NLS-1$
        "The Office " + (//$NON-NLS-1$
        terminated ? //$NON-NLS-1$
        "has been terminated" : //$NON-NLS-1$
        "is still running. Someone else prevents termination, e.g. the quickstarter"));
    } catch (java.lang.Exception e) {
        if (Converter.DEBUG_MODE) {
            e.printStackTrace();
        }
    }
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XComponentContext(com.sun.star.uno.XComponentContext) XDesktop(com.sun.star.frame.XDesktop) XMultiComponentFactory(com.sun.star.lang.XMultiComponentFactory) XUnoUrlResolver(com.sun.star.bridge.XUnoUrlResolver)

Aggregations

XPropertySet (com.sun.star.beans.XPropertySet)1 XUnoUrlResolver (com.sun.star.bridge.XUnoUrlResolver)1 XDesktop (com.sun.star.frame.XDesktop)1 XMultiComponentFactory (com.sun.star.lang.XMultiComponentFactory)1 XComponentContext (com.sun.star.uno.XComponentContext)1