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