1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // __________ Imports __________
36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37*cdf0e10cSrcweir import com.sun.star.uno.Any;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir import java.lang.String;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // __________ Implementation __________
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /**
45*cdf0e10cSrcweir  * support ONE singleton uno connection to an running office installation!
46*cdf0e10cSrcweir  * Can be used to open/use/close connection to uno environment of an office. If
47*cdf0e10cSrcweir  * necessary a new office instance is started.
48*cdf0e10cSrcweir  * ctor isn't available from outside. You should call static function
49*cdf0e10cSrcweir  * "getConnection()" to open or use internal set connection which is created one
50*cdf0e10cSrcweir  * times only.
51*cdf0e10cSrcweir  *
52*cdf0e10cSrcweir  * @author      Andreas Schlüns
53*cdf0e10cSrcweir  * @created     7. Februar 2002
54*cdf0e10cSrcweir  * @modified    05.02.2002 12:10
55*cdf0e10cSrcweir  */
56*cdf0e10cSrcweir public class OfficeConnect
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     // ____________________
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     /**
61*cdf0e10cSrcweir      * At first call we create static connection object and open connection to an
62*cdf0e10cSrcweir      * office - anew offic einstance is started if necessary
63*cdf0e10cSrcweir      * Then - and for all further requests we return these static connection member.
64*cdf0e10cSrcweir      */
65*cdf0e10cSrcweir     public static synchronized void createConnection()
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir         if (maConnection == null)
68*cdf0e10cSrcweir             maConnection = new OfficeConnect();
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     // ____________________
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     /**
74*cdf0e10cSrcweir      * close connection to remote office if it exist
75*cdf0e10cSrcweir      */
76*cdf0e10cSrcweir     public static synchronized void disconnect()
77*cdf0e10cSrcweir     {
78*cdf0e10cSrcweir         if(maConnection!=null)
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir             mxServiceManager=null;
81*cdf0e10cSrcweir             mxOfficeContext=null;
82*cdf0e10cSrcweir             maConnection=null;
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     // ____________________
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     /**
89*cdf0e10cSrcweir      * ctor
90*cdf0e10cSrcweir      * We try to open the connection in our ctor ... transparently for user.
91*cdf0e10cSrcweir      * After it was successfully you will find an internal set member
92*cdf0e10cSrcweir      * m_xRemoteContext wich means remote component context of the connected office.
93*cdf0e10cSrcweir      * The context can be used to get the remote service manager from the office.
94*cdf0e10cSrcweir      * We made it private to support singleton pattern of these implementation.
95*cdf0e10cSrcweir      * see getConnection() for further informations
96*cdf0e10cSrcweir      */
97*cdf0e10cSrcweir     private OfficeConnect()
98*cdf0e10cSrcweir     {
99*cdf0e10cSrcweir         try
100*cdf0e10cSrcweir         {
101*cdf0e10cSrcweir             // get the remote office context. If necessary a new office
102*cdf0e10cSrcweir             // process is started
103*cdf0e10cSrcweir             mxOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
104*cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
105*cdf0e10cSrcweir             mxServiceManager = mxOfficeContext.getServiceManager();
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir         catch (java.lang.Exception ex)
108*cdf0e10cSrcweir         {
109*cdf0e10cSrcweir             System.err.println("connection failed" + ex);
110*cdf0e10cSrcweir             ex.printStackTrace(System.out);
111*cdf0e10cSrcweir             System.exit(1);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     // ____________________
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     /**
119*cdf0e10cSrcweir      * create uno components inside remote office process
120*cdf0e10cSrcweir      * After connection of these proccess to a running office we have access to
121*cdf0e10cSrcweir      * remote service manager of it.
122*cdf0e10cSrcweir      * So we can use it to create all existing services. Use this method to create
123*cdf0e10cSrcweir      * components by name and get her interface. Casting of it to right target
124*cdf0e10cSrcweir      * interface is part of your implementation.
125*cdf0e10cSrcweir      *
126*cdf0e10cSrcweir      * @param  aType  describe class type of created service
127*cdf0e10cSrcweir      *                Returned object can be casted directly to this one.
128*cdf0e10cSrcweir      *                Uno query was done by this method automaticly.
129*cdf0e10cSrcweir      * @param  sServiceSpecifier  name of service which should be created
130*cdf0e10cSrcweir      * @return  the new created service object
131*cdf0e10cSrcweir      */
132*cdf0e10cSrcweir     public static synchronized Object createRemoteInstance(
133*cdf0e10cSrcweir         Class aType, String sServiceSpecifier)
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         Object aResult = null;
136*cdf0e10cSrcweir         try
137*cdf0e10cSrcweir         {
138*cdf0e10cSrcweir             aResult = UnoRuntime.queryInterface(aType,
139*cdf0e10cSrcweir                     mxServiceManager.createInstanceWithContext(
140*cdf0e10cSrcweir                         sServiceSpecifier, mxOfficeContext));
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex)
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             System.err.println("Couldn't create Service of type "
145*cdf0e10cSrcweir                                + sServiceSpecifier + ": " + ex);
146*cdf0e10cSrcweir             System.exit(0);
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir         return aResult;
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     // ____________________
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     /**
154*cdf0e10cSrcweir      * same as "createRemoteInstance()" but supports additional parameter for
155*cdf0e10cSrcweir      * initializing created object
156*cdf0e10cSrcweir      *
157*cdf0e10cSrcweir      * @param  lArguments         optional arguments
158*cdf0e10cSrcweir      *                            They are used to initialize new created service.
159*cdf0e10cSrcweir      * @param  aType              Description of Parameter
160*cdf0e10cSrcweir      * @param  sServiceSpecifier  Description of Parameter
161*cdf0e10cSrcweir      * @return                    the new create service object
162*cdf0e10cSrcweir      */
163*cdf0e10cSrcweir     public static synchronized Object createRemoteInstanceWithArguments(
164*cdf0e10cSrcweir         Class aType, String sServiceSpecifier, Any[] lArguments)
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         Object aResult = null;
167*cdf0e10cSrcweir         try
168*cdf0e10cSrcweir         {
169*cdf0e10cSrcweir             aResult = UnoRuntime.queryInterface(aType,
170*cdf0e10cSrcweir                     mxServiceManager.createInstanceWithArgumentsAndContext(
171*cdf0e10cSrcweir                         sServiceSpecifier, lArguments, mxOfficeContext));
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex)
174*cdf0e10cSrcweir         {
175*cdf0e10cSrcweir             System.err.println("Couldn't create Service of type "
176*cdf0e10cSrcweir                                + sServiceSpecifier + ": " + ex);
177*cdf0e10cSrcweir             System.exit(0);
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir         return aResult;
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     // ____________________
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     /**
185*cdf0e10cSrcweir      * returns remote component context of the connected office
186*cdf0e10cSrcweir      */
187*cdf0e10cSrcweir     public static synchronized com.sun.star.uno.XComponentContext getOfficeContext()
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         return mxOfficeContext;
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     // ____________________
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     /**
195*cdf0e10cSrcweir      * member
196*cdf0e10cSrcweir      */
197*cdf0e10cSrcweir     // singleton connection instance
198*cdf0e10cSrcweir     private static OfficeConnect maConnection;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     // reference to the office component context
201*cdf0e10cSrcweir     private static com.sun.star.uno.XComponentContext  mxOfficeContext;
202*cdf0e10cSrcweir     // reference to remote service manager of singleton connection object
203*cdf0e10cSrcweir     private static com.sun.star.lang.XMultiComponentFactory  mxServiceManager;
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206