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