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