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