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