1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // __________ Imports __________
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // structs, const, ...
27cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir // exceptions
31cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // interfaces
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.uno.Any;
36cdf0e10cSrcweir import com.sun.star.uno.Exception;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir // others
41cdf0e10cSrcweir import java.lang.String;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // __________ Implementation __________
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /**
46cdf0e10cSrcweir  * support ONE singleton uno connection to an running office installation!
47cdf0e10cSrcweir  * Can be used to open/use/close connection to uno environment of an already running office.
48cdf0e10cSrcweir  * ctor isn't available from outside. You should call static function "getConnection()"
49cdf0e10cSrcweir  * to open or use internal set connection which is created one times only.
50cdf0e10cSrcweir  *
51cdf0e10cSrcweir  * @author      Andreas Schlüns
52cdf0e10cSrcweir  * @created     7. Februar 2002
53cdf0e10cSrcweir  * @modified    05.02.2002 12:10
54cdf0e10cSrcweir  */
55cdf0e10cSrcweir public class OfficeConnect
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     // ____________________
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     /**
60cdf0e10cSrcweir      * At first call we create static connection object and get the remote office
61cdf0e10cSrcweir      * context and the remote office service manager. A new office process is
62cdf0e10cSrcweir      * started if necessary.
63cdf0e10cSrcweir      * Then - and for all further requests we return these static connection member.
64cdf0e10cSrcweir      */
65cdf0e10cSrcweir     public static synchronized OfficeConnect createConnection()
66cdf0e10cSrcweir         throws java.lang.Exception
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         if (maConnection == null)
69cdf0e10cSrcweir         {
70cdf0e10cSrcweir             maConnection = new OfficeConnect();
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         return maConnection;
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     // ____________________
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     public static synchronized OfficeConnect getConnection()
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         return maConnection;
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     // ____________________
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     /**
85cdf0e10cSrcweir      * ctor
86cdf0e10cSrcweir      * We try to open the connection in our ctor ... transparently for the user.
87cdf0e10cSrcweir      * We made it private to support singleton pattern of these implementation.
88cdf0e10cSrcweir      * see getConnection() for further informations
89cdf0e10cSrcweir      */
90cdf0e10cSrcweir     private OfficeConnect() throws java.lang.Exception
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         // get the remote office context. If necessary a new office
93cdf0e10cSrcweir         // process is started
94cdf0e10cSrcweir         mxOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
95cdf0e10cSrcweir         System.out.println("Connected to a running office ...");
96cdf0e10cSrcweir         mxServiceManager = mxOfficeContext.getServiceManager();
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     // ____________________
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     /**
102cdf0e10cSrcweir      * create uno components inside remote office process
103cdf0e10cSrcweir      * After connection of these proccess to a running office we have access to remote service manager of it.
104cdf0e10cSrcweir      * So we can use it to create all existing services. Use this method to create components by name and
105cdf0e10cSrcweir      * get her interface. Casting of it to right target interface is part of your implementation.
106cdf0e10cSrcweir      *
107cdf0e10cSrcweir      * @param  aType              describe class type of created service
108cdf0e10cSrcweir      *                              Returned object can be casted directly to this one.
109cdf0e10cSrcweir      *                              Uno query was done by this method automaticly.
110cdf0e10cSrcweir      * @param  sServiceSpecifier  name of service which should be created
111cdf0e10cSrcweir      * @return                    Description of the Returned Value
112cdf0e10cSrcweir      */
113cdf0e10cSrcweir     public Object createRemoteInstance(Class aType, String sServiceSpecifier)
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         Object aResult = null;
116cdf0e10cSrcweir         try
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             aResult = UnoRuntime.queryInterface(
119cdf0e10cSrcweir                     aType, mxServiceManager.createInstanceWithContext(
120cdf0e10cSrcweir                         sServiceSpecifier, mxOfficeContext));
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex)
123cdf0e10cSrcweir         {
124cdf0e10cSrcweir             System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
125cdf0e10cSrcweir             ex.printStackTrace();
126cdf0e10cSrcweir             System.exit(0);
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir         return aResult;
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     // ____________________
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     /**
134cdf0e10cSrcweir      * same as "createRemoteInstance()" but supports additional parameter for initializing created object
135cdf0e10cSrcweir      *
136cdf0e10cSrcweir      * @param  lArguments         optional arguments
137cdf0e10cSrcweir      *                      They are used to initialize new created service.
138cdf0e10cSrcweir      * @param  aType              Description of Parameter
139cdf0e10cSrcweir      * @param  sServiceSpecifier  Description of Parameter
140cdf0e10cSrcweir      * @return                    Description of the Returned Value
141cdf0e10cSrcweir      */
142cdf0e10cSrcweir     public Object createRemoteInstanceWithArguments(Class aType, String sServiceSpecifier, Any[] lArguments)
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         Object aResult = null;
145cdf0e10cSrcweir         try
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             aResult = UnoRuntime.queryInterface(
148cdf0e10cSrcweir                 aType, mxServiceManager.createInstanceWithArgumentsAndContext(
149cdf0e10cSrcweir                     sServiceSpecifier, lArguments, mxOfficeContext));
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex)
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex);
154cdf0e10cSrcweir             ex.printStackTrace();
155cdf0e10cSrcweir             System.exit(0);
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir         return aResult;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     // ____________________
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     /**
163cdf0e10cSrcweir      * member
164cdf0e10cSrcweir      */
165cdf0e10cSrcweir     // singleton connection instance
166cdf0e10cSrcweir     private static OfficeConnect maConnection;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     // reference to remote office context
169cdf0e10cSrcweir     private com.sun.star.uno.XComponentContext  mxOfficeContext;
170cdf0e10cSrcweir     // reference to remote service manager
171cdf0e10cSrcweir     private com.sun.star.lang.XMultiComponentFactory  mxServiceManager;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174