1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // base classes
23 import com.sun.star.uno.UnoRuntime;
24 
25 // factory for creating components
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.bridge.XUnoUrlResolver;
28 import com.sun.star.frame.XComponentLoader;
29 import com.sun.star.frame.XDesktop;
30 import com.sun.star.frame.XModel;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.XInterface;
33 
34 // Exceptions
35 import com.sun.star.uno.RuntimeException;
36 
37 
38 /** @descr  This class establishes a connection to a StarOffice application.
39  */
40 public class OfficeConnection
41 {
OfficeConnection(int nPortNumber)42     public OfficeConnection (int nPortNumber)
43     {
44         mnDefaultPort = nPortNumber;
45         connect ();
46     }
47 
48     /** @descr Return the service manager that represents the connected
49                 StarOffice application
50     */
getServiceManager()51     public XMultiServiceFactory getServiceManager ()
52     {
53         if ( ! mbInitialized)
54             connect ();
55         return maServiceManager;
56     }
57 
58     /** @descr  Return a flag that indicates if the constructor has been able to
59                 establish a valid connection.
60     */
connectionIsValid()61     public boolean connectionIsValid ()
62     {
63         return getServiceManager() != null;
64     }
65 
66     /** @descr  Connect to a already running StarOffice application.
67     */
connect()68     private void connect ()
69     {
70         connect (msDefaultHost, mnDefaultPort);
71     }
72 
connect(String hostname)73     private void connect (String hostname)
74     {
75         connect (hostname, mnDefaultPort);
76     }
77 
78     /** @descr  Connect to a already running StarOffice application that has
79                 been started with a command line argument like
80                 "-accept=socket,host=localhost,port=5678;urp;"
81     */
connect(String hostname, int portnumber)82     private void connect (String hostname, int portnumber)
83     {
84         mbInitialized = true;
85         //  Set up connection string.
86         String sConnectString = "uno:socket,host=" + hostname + ",port=" + portnumber
87             + ";urp;StarOffice.ServiceManager";
88 
89 
90         // connect to a running office and get the ServiceManager
91         try
92         {
93             //  Create a URL Resolver.
94             XMultiServiceFactory aLocalServiceManager =
95                 com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager();
96             XUnoUrlResolver aURLResolver = (XUnoUrlResolver) UnoRuntime.queryInterface (
97                 XUnoUrlResolver.class,
98                 aLocalServiceManager.createInstance ("com.sun.star.bridge.UnoUrlResolver")
99                 );
100 
101             maServiceManager = (XMultiServiceFactory) UnoRuntime.queryInterface (
102                     XMultiServiceFactory.class,
103                     aURLResolver.resolve (sConnectString)
104                     );
105         }
106 
107         catch (Exception e)
108         {
109             MessageArea.println ("Could not connect with " + sConnectString + " : " + e);
110             MessageArea.println ("Please start OpenOffice/StarOffice with "
111                 + "\"-accept=socket,host=localhost,port=5678;urp;\"");
112         }
113     }
114 
115     private int mnDefaultPort = 5678;
116     private final String msDefaultHost = "localhost";
117     private XMultiServiceFactory  maServiceManager = null;
118 
119     /** A value of true just indicates that it has been tried to establish a connection,
120         not that that has been successful.
121     */
122     private boolean mbInitialized = false;
123 }
124