1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir // __________ Imports __________ 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir // structs, const, ... 38*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 39*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir // exceptions 42*cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir // interfaces 45*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 46*cdf0e10cSrcweir import com.sun.star.uno.Any; 47*cdf0e10cSrcweir import com.sun.star.uno.Exception; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir // others 52*cdf0e10cSrcweir import java.lang.String; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir // __________ Implementation __________ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /** 57*cdf0e10cSrcweir * support ONE singleton uno connection to an running office installation! 58*cdf0e10cSrcweir * Can be used to open/use/close connection to uno environment of an already running office. 59*cdf0e10cSrcweir * ctor isn't available from outside. You should call static function "getConnection()" 60*cdf0e10cSrcweir * to open or use internal set connection which is created one times only. 61*cdf0e10cSrcweir * 62*cdf0e10cSrcweir * @author Andreas Schlüns 63*cdf0e10cSrcweir * @created 7. Februar 2002 64*cdf0e10cSrcweir * @modified 05.02.2002 12:10 65*cdf0e10cSrcweir */ 66*cdf0e10cSrcweir public class OfficeConnect 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir // ____________________ 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir /** 71*cdf0e10cSrcweir * At first call we create static connection object and get the remote office 72*cdf0e10cSrcweir * context and the remote office service manager. A new office process is 73*cdf0e10cSrcweir * started if necessary. 74*cdf0e10cSrcweir * Then - and for all further requests we return these static connection member. 75*cdf0e10cSrcweir */ 76*cdf0e10cSrcweir public static synchronized OfficeConnect createConnection() 77*cdf0e10cSrcweir throws java.lang.Exception 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir if (maConnection == null) 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir maConnection = new OfficeConnect(); 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir return maConnection; 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // ____________________ 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir public static synchronized OfficeConnect getConnection() 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir return maConnection; 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir // ____________________ 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir /** 96*cdf0e10cSrcweir * ctor 97*cdf0e10cSrcweir * We try to open the connection in our ctor ... transparently for the user. 98*cdf0e10cSrcweir * We made it private to support singleton pattern of these implementation. 99*cdf0e10cSrcweir * see getConnection() for further informations 100*cdf0e10cSrcweir */ 101*cdf0e10cSrcweir private OfficeConnect() throws java.lang.Exception 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir // get the remote office context. If necessary a new office 104*cdf0e10cSrcweir // process is started 105*cdf0e10cSrcweir mxOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 106*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 107*cdf0e10cSrcweir mxServiceManager = mxOfficeContext.getServiceManager(); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir // ____________________ 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir /** 113*cdf0e10cSrcweir * create uno components inside remote office process 114*cdf0e10cSrcweir * After connection of these proccess to a running office we have access to remote service manager of it. 115*cdf0e10cSrcweir * So we can use it to create all existing services. Use this method to create components by name and 116*cdf0e10cSrcweir * get her interface. Casting of it to right target interface is part of your implementation. 117*cdf0e10cSrcweir * 118*cdf0e10cSrcweir * @param aType describe class type of created service 119*cdf0e10cSrcweir * Returned object can be casted directly to this one. 120*cdf0e10cSrcweir * Uno query was done by this method automaticly. 121*cdf0e10cSrcweir * @param sServiceSpecifier name of service which should be created 122*cdf0e10cSrcweir * @return Description of the Returned Value 123*cdf0e10cSrcweir */ 124*cdf0e10cSrcweir public Object createRemoteInstance(Class aType, String sServiceSpecifier) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir Object aResult = null; 127*cdf0e10cSrcweir try 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir aResult = UnoRuntime.queryInterface( 130*cdf0e10cSrcweir aType, mxServiceManager.createInstanceWithContext( 131*cdf0e10cSrcweir sServiceSpecifier, mxOfficeContext)); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir catch (com.sun.star.uno.Exception ex) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex); 136*cdf0e10cSrcweir ex.printStackTrace(); 137*cdf0e10cSrcweir System.exit(0); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir return aResult; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir // ____________________ 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir /** 145*cdf0e10cSrcweir * same as "createRemoteInstance()" but supports additional parameter for initializing created object 146*cdf0e10cSrcweir * 147*cdf0e10cSrcweir * @param lArguments optional arguments 148*cdf0e10cSrcweir * They are used to initialize new created service. 149*cdf0e10cSrcweir * @param aType Description of Parameter 150*cdf0e10cSrcweir * @param sServiceSpecifier Description of Parameter 151*cdf0e10cSrcweir * @return Description of the Returned Value 152*cdf0e10cSrcweir */ 153*cdf0e10cSrcweir public Object createRemoteInstanceWithArguments(Class aType, String sServiceSpecifier, Any[] lArguments) 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir Object aResult = null; 156*cdf0e10cSrcweir try 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir aResult = UnoRuntime.queryInterface( 159*cdf0e10cSrcweir aType, mxServiceManager.createInstanceWithArgumentsAndContext( 160*cdf0e10cSrcweir sServiceSpecifier, lArguments, mxOfficeContext)); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir catch (com.sun.star.uno.Exception ex) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir System.err.println("Couldn't create Service of type " + sServiceSpecifier + ": " + ex); 165*cdf0e10cSrcweir ex.printStackTrace(); 166*cdf0e10cSrcweir System.exit(0); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir return aResult; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir // ____________________ 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir /** 174*cdf0e10cSrcweir * member 175*cdf0e10cSrcweir */ 176*cdf0e10cSrcweir // singleton connection instance 177*cdf0e10cSrcweir private static OfficeConnect maConnection; 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir // reference to remote office context 180*cdf0e10cSrcweir private com.sun.star.uno.XComponentContext mxOfficeContext; 181*cdf0e10cSrcweir // reference to remote service manager 182*cdf0e10cSrcweir private com.sun.star.lang.XMultiComponentFactory mxServiceManager; 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185