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 
37 // structs, const, ...
38 import com.sun.star.beans.PropertyValue;
39 import com.sun.star.bridge.XUnoUrlResolver;
40 
41 // exceptions
42 import com.sun.star.container.NoSuchElementException;
43 
44 // interfaces
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.uno.Any;
47 import com.sun.star.uno.Exception;
48 
49 import com.sun.star.uno.UnoRuntime;
50 
51 // others
52 import java.lang.String;
53 
54 // __________ Implementation __________
55 
56 /**
57  * support ONE singleton uno connection to an running office installation!
58  * Can be used to open/use/close connection to uno environment of an already running office.
59  * ctor isn't available from outside. You should call static function "getConnection()"
60  * to open or use internal set connection which is created one times only.
61  *
62  * @author      Andreas Schlüns
63  * @created     7. Februar 2002
64  * @modified    05.02.2002 12:10
65  */
66 public class OfficeConnect
67 {
68     // ____________________
69 
70     /**
71      * At first call we create static connection object and get the remote office
72      * context and the remote office service manager. A new office process is
73      * started if necessary.
74      * Then - and for all further requests we return these static connection member.
75      */
76     public static synchronized OfficeConnect createConnection()
77         throws java.lang.Exception
78     {
79         if (maConnection == null)
80         {
81             maConnection = new OfficeConnect();
82         }
83         return maConnection;
84     }
85 
86     // ____________________
87 
88     public static synchronized OfficeConnect getConnection()
89     {
90         return maConnection;
91     }
92 
93     // ____________________
94 
95     /**
96      * ctor
97      * We try to open the connection in our ctor ... transparently for the user.
98      * We made it private to support singleton pattern of these implementation.
99      * see getConnection() for further informations
100      */
101     private OfficeConnect() throws java.lang.Exception
102     {
103         // get the remote office context. If necessary a new office
104         // process is started
105         mxOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
106         System.out.println("Connected to a running office ...");
107         mxServiceManager = mxOfficeContext.getServiceManager();
108     }
109 
110     // ____________________
111 
112     /**
113      * create uno components inside remote office process
114      * After connection of these proccess to a running office we have access to remote service manager of it.
115      * So we can use it to create all existing services. Use this method to create components by name and
116      * get her interface. Casting of it to right target interface is part of your implementation.
117      *
118      * @param  aType              describe class type of created service
119      *                              Returned object can be casted directly to this one.
120      *                              Uno query was done by this method automaticly.
121      * @param  sServiceSpecifier  name of service which should be created
122      * @return                    Description of the Returned Value
123      */
124     public Object createRemoteInstance(Class aType, String sServiceSpecifier)
125     {
126         Object aResult = null;
127         try
128         {
129             aResult = UnoRuntime.queryInterface(
130                     aType, mxServiceManager.createInstanceWithContext(
131                         sServiceSpecifier, mxOfficeContext));
132         }
133         catch (com.sun.star.uno.Exception ex)
134         {
135             System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
136             ex.printStackTrace();
137             System.exit(0);
138         }
139         return aResult;
140     }
141 
142     // ____________________
143 
144     /**
145      * same as "createRemoteInstance()" but supports additional parameter for initializing created object
146      *
147      * @param  lArguments         optional arguments
148      *                      They are used to initialize new created service.
149      * @param  aType              Description of Parameter
150      * @param  sServiceSpecifier  Description of Parameter
151      * @return                    Description of the Returned Value
152      */
153     public Object createRemoteInstanceWithArguments(Class aType, String sServiceSpecifier, Any[] lArguments)
154     {
155         Object aResult = null;
156         try
157         {
158             aResult = UnoRuntime.queryInterface(
159                 aType, mxServiceManager.createInstanceWithArgumentsAndContext(
160                     sServiceSpecifier, lArguments, mxOfficeContext));
161         }
162         catch (com.sun.star.uno.Exception ex)
163         {
164             System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
165             ex.printStackTrace();
166             System.exit(0);
167         }
168         return aResult;
169     }
170 
171     // ____________________
172 
173     /**
174      * member
175      */
176     // singleton connection instance
177     private static OfficeConnect maConnection;
178 
179     // reference to remote office context
180     private com.sun.star.uno.XComponentContext  mxOfficeContext;
181     // reference to remote service manager
182     private com.sun.star.lang.XMultiComponentFactory  mxServiceManager;
183 }
184 
185