2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.uno
.UnoRuntime
;
22 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
23 import com
.sun
.star
.lang
.XMultiServiceFactory
;
26 /** @descr This class establishes a connection to a LibreOffice application.
28 public class OfficeConnection
30 public OfficeConnection (int nPortNumber
)
32 mnDefaultPort
= nPortNumber
;
36 /** @descr Return the service manager that represents the connected
37 LibreOffice application
39 public XMultiServiceFactory
getServiceManager ()
43 return maServiceManager
;
46 /** @descr Connect to an already running LibreOffice application.
48 private void connect ()
50 connect (msDefaultHost
, mnDefaultPort
);
53 /** @descr Connect to an already running LibreOffice application that has
54 been started with a command line argument like
55 "--accept=socket,host=localhost,port=5678;urp;"
57 private void connect (String hostname
, int portnumber
)
60 // Set up connection string.
61 String sConnectString
= "uno:socket,host=" + hostname
+ ",port=" + portnumber
62 + ";urp;StarOffice.ServiceManager";
65 // connect to a running office and get the ServiceManager
68 // Create a URL Resolver.
69 XMultiServiceFactory aLocalServiceManager
=
70 com
.sun
.star
.comp
.helper
.Bootstrap
.createSimpleServiceManager();
71 XUnoUrlResolver aURLResolver
= UnoRuntime
.queryInterface (
72 XUnoUrlResolver
.class,
73 aLocalServiceManager
.createInstance ("com.sun.star.bridge.UnoUrlResolver")
76 maServiceManager
= UnoRuntime
.queryInterface (
77 XMultiServiceFactory
.class,
78 aURLResolver
.resolve (sConnectString
)
84 MessageArea
.println ("Could not connect with " + sConnectString
+ " : " + e
);
85 MessageArea
.println ("Please start LibreOffice with "
86 + "\"--accept=socket,host=localhost,port=5678;urp;\"");
90 private final int mnDefaultPort
;
91 private static final String msDefaultHost
= "localhost";
92 private XMultiServiceFactory maServiceManager
= null;
94 /** A value of true just indicates that it has been tried to establish a connection,
95 not that has been successful.
97 private boolean mbInitialized
= false;