1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package helper; 28 29 import com.sun.star.comp.helper.Bootstrap; 30 import com.sun.star.lang.XMultiComponentFactory; 31 import com.sun.star.lang.XMultiServiceFactory; 32 import com.sun.star.uno.UnoRuntime; 33 import com.sun.star.uno.XComponentContext; 34 import java.util.Hashtable; 35 import lib.TestParameters; 36 import util.PropertyName; 37 import util.utils; 38 39 /** 40 * Bootstrap UNO from a Java environment. 41 * Needed parameters: 42 * <ol> 43 * <li> 44 * <ul> 45 * <li>UNORC - complete path to the unorc file</li> 46 * </ul> 47 * </li> 48 * <li> 49 * <ul> 50 * <li>AppExecutionCommand - path to the soffice executable</li> 51 * <li>OS - the operating system in case it's Windows, because the 52 * unorc is called uno.ini</li> 53 * </ul> 54 * </li> 55 * </ol> 56 */ 57 public class UnoProvider implements AppProvider { 58 59 public UnoProvider(){ 60 61 } 62 63 /** 64 * Close existing office: calls disposeManager() 65 * @param param The test parameters. 66 * @param closeIfPossible Not needed, since UNO is bootstrapped by this 67 * class in every case. 68 * @return True, if bootstrapping worked. 69 */ 70 public boolean closeExistingOffice(TestParameters param, 71 boolean closeIfPossible) { 72 return disposeManager(param); 73 } 74 75 /** 76 * Dispose the UNO environment: just clears the bootstrapped 77 * MultiServiceFactory 78 * @param param The test parameters. 79 * @return True, if bootstrapping worked. 80 */ 81 public boolean disposeManager(TestParameters param) { 82 XMultiServiceFactory xMSF = 83 (XMultiServiceFactory)param.remove("ServiceManager"); 84 xMSF = null; 85 System.gc(); 86 try { 87 Thread.sleep(1000); 88 } 89 catch(java.lang.InterruptedException e) {} 90 return true; 91 } 92 93 /** 94 * Bootstrap UNO and return the created MultiServiceFactory. 95 * @param param The test parameters. 96 * @return A created MultiServiceFactory. 97 */ 98 public Object getManager(TestParameters param) { 99 XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF(); 100 if (xMSF == null) { 101 // bootstrap UNO. 102 String unorcName = getUnorcName(param); 103 Hashtable env = new Hashtable(); 104 env.put("SYSBINDIR", getSysBinDir(param)); 105 106 XComponentContext xContext = null; 107 try { 108 xContext = Bootstrap.defaultBootstrap_InitialComponentContext( 109 unorcName, env); 110 } 111 catch(Exception e) { 112 e.printStackTrace(); 113 System.out.println("Could not get XComponentContext. Maybe you must add program folder to LD_LIBRARY_PATH"); 114 return null; 115 } 116 XMultiComponentFactory xMCF = xContext.getServiceManager(); 117 xMSF = (XMultiServiceFactory)UnoRuntime.queryInterface( 118 XMultiServiceFactory.class, xMCF); 119 } 120 return xMSF; 121 } 122 123 private String getUnorcName(TestParameters param) { 124 String unorcName = (String)param.get("UNORC"); 125 if (unorcName == null) { 126 String office = (String)param.get("AppExecutionCommand"); 127 // determine unorc name: unorc or uno.ini on windows 128 String opSystem = (String)param.get(PropertyName.OPERATING_SYSTEM); 129 if ( opSystem != null && opSystem.equalsIgnoreCase(PropertyName.WNTMSCI)) { 130 unorcName = "uno.ini"; 131 } 132 else { 133 unorcName = "unorc"; 134 } 135 if (office == null) 136 return null; 137 // use '/', because this will be a URL in any case. 138 unorcName = office.substring(0, office.indexOf("program")+7) + 139 "/" + unorcName; 140 } 141 unorcName = utils.getFullURL(unorcName); 142 if (param.DebugIsActive) { 143 System.out.println("UnoUcr: " + unorcName); 144 } 145 return unorcName; 146 } 147 148 private String getSysBinDir(TestParameters param) { 149 String base = (String)param.get("AppExecutionCommand"); 150 if (base == null) 151 base = (String)param.get("UNORC"); 152 153 if (base == null) 154 return null; 155 156 String sysbindir = base.substring(0, 157 base.indexOf("program")+7); 158 159 sysbindir = utils.getFullURL(sysbindir); 160 if (param.DebugIsActive) { 161 System.out.println("SysBinDir: " + sysbindir); 162 } 163 return sysbindir; 164 } 165 } 166