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 Return a flag that indicates if the constructor has been able to
47 establish a valid connection.
49 public boolean connectionIsValid ()
51 return getServiceManager() != null;
54 /** @descr Connect to an already running LibreOffice application.
56 private void connect ()
58 connect (msDefaultHost
, mnDefaultPort
);
61 private void connect (String hostname
)
63 connect (hostname
, mnDefaultPort
);
66 /** @descr Connect to a already running LibreOffice application that has
67 been started with a command line argument like
68 "--accept=socket,host=localhost,port=5678;urp;"
70 private void connect (String hostname
, int portnumber
)
73 // Set up connection string.
74 String sConnectString
= "uno:socket,host=" + hostname
+ ",port=" + portnumber
75 + ";urp;StarOffice.ServiceManager";
78 // connect to a running office and get the ServiceManager
81 // Create a URL Resolver.
82 XMultiServiceFactory aLocalServiceManager
=
83 com
.sun
.star
.comp
.helper
.Bootstrap
.createSimpleServiceManager();
84 XUnoUrlResolver aURLResolver
= UnoRuntime
.queryInterface (
85 XUnoUrlResolver
.class,
86 aLocalServiceManager
.createInstance ("com.sun.star.bridge.UnoUrlResolver")
89 maServiceManager
= UnoRuntime
.queryInterface (
90 XMultiServiceFactory
.class,
91 aURLResolver
.resolve (sConnectString
)
97 MessageArea
.println ("Could not connect with " + sConnectString
+ " : " + e
);
98 MessageArea
.println ("Please start LibreOffice with "
99 + "\"--accept=socket,host=localhost,port=5678;urp;\"");
103 private int mnDefaultPort
= 5678;
104 private final String msDefaultHost
= "localhost";
105 private XMultiServiceFactory maServiceManager
= null;
107 /** A value of true just indicates that it has been tried to establish a connection,
108 not that that has been successful.
110 private boolean mbInitialized
= false;