1 /*************************************************************************
3 * $RCSfile: OfficeConnect.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:39:37 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
42 import com
.sun
.star
.uno
.UnoRuntime
;
43 import com
.sun
.star
.uno
.Any
;
45 import java
.lang
.String
;
48 // __________ Implementation __________
51 * support ONE singleton uno connection to an running office installation!
52 * Can be used to open/use/close connection to uno environment of an office. If
53 * necessary a new office instance is started.
54 * ctor isn't available from outside. You should call static function
55 * "getConnection()" to open or use internal set connection which is created one
58 * @author Andreas Schlüns
59 * @created 7. Februar 2002
60 * @modified 05.02.2002 12:10
62 public class OfficeConnect
64 // ____________________
67 * At first call we create static connection object and open connection to an
68 * office - anew offic einstance is started if necessary
69 * Then - and for all further requests we return these static connection member.
71 public static synchronized void createConnection()
73 if (maConnection
== null)
74 maConnection
= new OfficeConnect();
77 // ____________________
80 * close connection to remote office if it exist
82 public static synchronized void disconnect()
84 if(maConnection
!=null)
86 mxServiceManager
=null;
92 // ____________________
96 * We try to open the connection in our ctor ... transparently for user.
97 * After it was successfully you will find an internal set member
98 * m_xRemoteContext wich means remote component context of the connected office.
99 * The context can be used to get the remote service manager from the office.
100 * We made it private to support singleton pattern of these implementation.
101 * see getConnection() for further informations
103 private OfficeConnect()
107 // get the remote office context. If necessary a new office
108 // process is started
109 mxOfficeContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
110 System
.out
.println("Connected to a running office ...");
111 mxServiceManager
= mxOfficeContext
.getServiceManager();
113 catch (java
.lang
.Exception ex
)
115 System
.err
.println("connection failed" + ex
);
116 ex
.printStackTrace(System
.out
);
122 // ____________________
125 * create uno components inside remote office process
126 * After connection of these proccess to a running office we have access to
127 * remote service manager of it.
128 * So we can use it to create all existing services. Use this method to create
129 * components by name and get her interface. Casting of it to right target
130 * interface is part of your implementation.
132 * @param aType describe class type of created service
133 * Returned object can be casted directly to this one.
134 * Uno query was done by this method automaticly.
135 * @param sServiceSpecifier name of service which should be created
136 * @return the new created service object
138 public static synchronized Object
createRemoteInstance(
139 Class aType
, String sServiceSpecifier
)
141 Object aResult
= null;
144 aResult
= UnoRuntime
.queryInterface(aType
,
145 mxServiceManager
.createInstanceWithContext(
146 sServiceSpecifier
, mxOfficeContext
));
148 catch (com
.sun
.star
.uno
.Exception ex
)
150 System
.err
.println("Couldn't create Service of type "
151 + sServiceSpecifier
+ ": " + ex
);
157 // ____________________
160 * same as "createRemoteInstance()" but supports additional parameter for
161 * initializing created object
163 * @param lArguments optional arguments
164 * They are used to initialize new created service.
165 * @param aType Description of Parameter
166 * @param sServiceSpecifier Description of Parameter
167 * @return the new create service object
169 public static synchronized Object
createRemoteInstanceWithArguments(
170 Class aType
, String sServiceSpecifier
, Any
[] lArguments
)
172 Object aResult
= null;
175 aResult
= UnoRuntime
.queryInterface(aType
,
176 mxServiceManager
.createInstanceWithArgumentsAndContext(
177 sServiceSpecifier
, lArguments
, mxOfficeContext
));
179 catch (com
.sun
.star
.uno
.Exception ex
)
181 System
.err
.println("Couldn't create Service of type "
182 + sServiceSpecifier
+ ": " + ex
);
188 // ____________________
191 * returns remote component context of the connected office
193 public static synchronized com
.sun
.star
.uno
.XComponentContext
getOfficeContext()
195 return mxOfficeContext
;
198 // ____________________
203 // singleton connection instance
204 private static OfficeConnect maConnection
;
206 // reference to the office component context
207 private static com
.sun
.star
.uno
.XComponentContext mxOfficeContext
;
208 // reference to remote service manager of singleton connection object
209 private static com
.sun
.star
.lang
.XMultiComponentFactory mxServiceManager
;