1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir package helper; 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir //import com.sun.star.bridge.UnoUrlResolver; 30*cdf0e10cSrcweir import com.sun.star.beans.XFastPropertySet; 31*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 32*cdf0e10cSrcweir import com.sun.star.container.XEnumeration; 33*cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess; 34*cdf0e10cSrcweir import com.sun.star.frame.XDesktop; 35*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 36*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 38*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 39*cdf0e10cSrcweir import com.sun.star.util.XCloseable; 40*cdf0e10cSrcweir import com.sun.star.util.XStringSubstitution; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir import java.io.File; 43*cdf0e10cSrcweir import java.io.PrintWriter; 44*cdf0e10cSrcweir import java.util.StringTokenizer; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir import lib.TestParameters; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir import share.DescEntry; 49*cdf0e10cSrcweir import share.LogWriter; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir import util.DynamicClassLoader; 52*cdf0e10cSrcweir import util.PropertyName; 53*cdf0e10cSrcweir import util.utils; 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir /** 56*cdf0e10cSrcweir * This class will connect the office and start it if possible 57*cdf0e10cSrcweir * 58*cdf0e10cSrcweir */ 59*cdf0e10cSrcweir public class OfficeProvider implements AppProvider 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir private static boolean debug = false; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir /** 65*cdf0e10cSrcweir * copy the user layer to a safe place, usualy to $TMP/user_backup$USER 66*cdf0e10cSrcweir * @param param 67*cdf0e10cSrcweir * @param msf 68*cdf0e10cSrcweir */ 69*cdf0e10cSrcweir public void backupUserLayer(TestParameters param, XMultiServiceFactory msf) 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir try 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir final XStringSubstitution sts = createStringSubstitution(msf); 74*cdf0e10cSrcweir debug = param.getBool(PropertyName.DEBUG_IS_ACTIVE); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir String userLayer = sts.getSubstituteVariableValue("$(user)"); 77*cdf0e10cSrcweir userLayer = getDirSys(userLayer); 78*cdf0e10cSrcweir param.put("userLayer", userLayer); 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir final String copyLayer = util.utils.getUsersTempDir() + System.getProperty("file.separator") + 81*cdf0e10cSrcweir "user_backup" + 82*cdf0e10cSrcweir System.getProperty("user.name"); 83*cdf0e10cSrcweir param.put("copyLayer", copyLayer); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir dbg(" copy '" + userLayer + "' ->" + copyLayer + "'"); 87*cdf0e10cSrcweir // Slow machines the copy job could spend some time. To avoid activating of OfficeWatcher it must be pinged 88*cdf0e10cSrcweir OfficeWatcherPing owp = new OfficeWatcherPing((OfficeWatcher) param.get(PropertyName.OFFICE_WATCHER)); 89*cdf0e10cSrcweir owp.start(); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir deleteFilesAndDirector (new File(copyLayer)); 92*cdf0e10cSrcweir FileTools.copyDirectory(new File(userLayer), new File(copyLayer), new String[] 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir "temp" 95*cdf0e10cSrcweir }); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir owp.finish(); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir catch (com.sun.star.container.NoSuchElementException e) 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir System.out.println("User Variable '$(user)' not defined."); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir catch (java.io.IOException e) 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir System.out.println("Couldn't backup user layer"); 107*cdf0e10cSrcweir e.printStackTrace(); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir /** 112*cdf0e10cSrcweir * Dispose the office. 113*cdf0e10cSrcweir * This method can only be used, if the office was connected in the first 114*cdf0e10cSrcweir * place: getManager() was called first. 115*cdf0e10cSrcweir * @param param 116*cdf0e10cSrcweir * @return return true if desktop is terminates, else false 117*cdf0e10cSrcweir */ 118*cdf0e10cSrcweir public boolean disposeManager(lib.TestParameters param) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir XMultiServiceFactory msf = (XMultiServiceFactory) param.getMSF(); 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir if (msf == null) 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir return true; 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir else 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir XDesktop desk = null; 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir try 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir desk = UnoRuntime.queryInterface(XDesktop.class, msf.createInstance("com.sun.star.frame.Desktop")); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir catch (com.sun.star.uno.Exception ue) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir return false; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir msf = null; 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir if (desk != null) 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir desk.terminate(); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir return true; 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir else 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir return false; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir /** 156*cdf0e10cSrcweir * Method to get the ServiceManager of an Office 157*cdf0e10cSrcweir * @param param 158*cdf0e10cSrcweir * @return 159*cdf0e10cSrcweir */ 160*cdf0e10cSrcweir public Object getManager(lib.TestParameters param) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir String errorMessage = null; 163*cdf0e10cSrcweir boolean bAppExecutionHasWarning = false; 164*cdf0e10cSrcweir debug = param.getBool(PropertyName.DEBUG_IS_ACTIVE); 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir String additionalArgs = (String) param.get( 167*cdf0e10cSrcweir "AdditionalConnectionArguments"); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir if (additionalArgs == null) 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir additionalArgs = ";"; 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir else 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir additionalArgs = "," + additionalArgs + ";"; 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir final String cncstr = "uno:" + param.get("ConnectionString") + ";urp" + 179*cdf0e10cSrcweir additionalArgs + "StarOffice.ServiceManager"; 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir System.out.println("Connecting the Office with " + cncstr); 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir XMultiServiceFactory msf = connectOffice(cncstr); 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir // if the office is running and the office crashes while testing it could 186*cdf0e10cSrcweir // be usesfull to restart the office if possible and continuing the tests. 187*cdf0e10cSrcweir // Example: The UNO-API-Tests in the projects will be executed by calling 188*cdf0e10cSrcweir // 'damke'. This connects to an existing office. If the office crashes 189*cdf0e10cSrcweir // it is usefull to restart the office and continuing the tests. 190*cdf0e10cSrcweir if ((param.getBool(util.PropertyName.AUTO_RESTART)) && (msf != null)) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir makeAppExecCommand(msf, param); 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir if (msf == null) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir String exc = ""; 198*cdf0e10cSrcweir Exception exConnectFailed = null; 199*cdf0e10cSrcweir boolean isExecutable = false; 200*cdf0e10cSrcweir boolean isAppKnown = ((cncstr.indexOf("host=localhost") > 0) || (cncstr.indexOf("pipe,name=") > 0)); 201*cdf0e10cSrcweir isAppKnown &= !((String) param.get("AppExecutionCommand")).equals(""); 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir if (isAppKnown) 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir dbg("Local Connection trying to start the Office"); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir //ensure that a pending officewatcher gets finished before a new 208*cdf0e10cSrcweir //office is started 209*cdf0e10cSrcweir final OfficeWatcher ow_old = (OfficeWatcher) param.get("Watcher"); 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir if (ow_old != null) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir ow_old.finish = true; 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir final String cmd = (String) param.get("AppExecutionCommand"); 217*cdf0e10cSrcweir dbg("AppExecutionCommand: " + cmd); 218*cdf0e10cSrcweir // validate the AppExecutionCommand, but try it out anyway. 219*cdf0e10cSrcweir // keep the error message for later. 220*cdf0e10cSrcweir errorMessage = 221*cdf0e10cSrcweir util.utils.validateAppExecutionCommand(cmd, (String) param.get("OperatingSystem")); 222*cdf0e10cSrcweir if (errorMessage.startsWith("Error")) 223*cdf0e10cSrcweir { 224*cdf0e10cSrcweir System.out.println(errorMessage); 225*cdf0e10cSrcweir return null; 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir bAppExecutionHasWarning = !errorMessage.equals("OK"); 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir final DynamicClassLoader dcl = new DynamicClassLoader(); 230*cdf0e10cSrcweir final LogWriter log = (LogWriter) dcl.getInstance( 231*cdf0e10cSrcweir (String) param.get("LogWriter")); 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir //create empty entry 234*cdf0e10cSrcweir final DescEntry Entry = new DescEntry(); 235*cdf0e10cSrcweir Entry.entryName = "office"; 236*cdf0e10cSrcweir Entry.longName = "office"; 237*cdf0e10cSrcweir Entry.EntryType = "placebo"; 238*cdf0e10cSrcweir Entry.isOptional = false; 239*cdf0e10cSrcweir Entry.isToTest = false; 240*cdf0e10cSrcweir Entry.SubEntryCount = 0; 241*cdf0e10cSrcweir Entry.hasErrorMsg = false; 242*cdf0e10cSrcweir Entry.State = "non possible"; 243*cdf0e10cSrcweir Entry.UserDefinedParams = param; 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir log.initialize(Entry, debug); 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir final ProcessHandler ph = new ProcessHandler(cmd, (PrintWriter) log); 248*cdf0e10cSrcweir isExecutable = ph.executeAsynchronously(); 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir if (isExecutable) 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir param.put("AppProvider", ph); 253*cdf0e10cSrcweir final OfficeWatcher ow = new OfficeWatcher(param); 254*cdf0e10cSrcweir param.put("Watcher", ow); 255*cdf0e10cSrcweir ow.start(); 256*cdf0e10cSrcweir ow.ping(); 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir int k = 0; 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir // wait up to 21 seconds to get an office connection 262*cdf0e10cSrcweir while ((k < 42) && (msf == null)) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir try 265*cdf0e10cSrcweir { 266*cdf0e10cSrcweir msf = connect(cncstr); 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir catch (com.sun.star.uno.Exception ue) 269*cdf0e10cSrcweir { 270*cdf0e10cSrcweir exConnectFailed = ue; 271*cdf0e10cSrcweir exc = ue.getMessage(); 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir catch (java.lang.Exception je) 274*cdf0e10cSrcweir { 275*cdf0e10cSrcweir exConnectFailed = je; 276*cdf0e10cSrcweir exc = je.getMessage(); 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir if (msf == null) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir try 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir Thread.sleep(k * 500); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir catch (InterruptedException ex) 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir k++; 289*cdf0e10cSrcweir } 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir if (msf == null) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir System.out.println("Exception while connecting.\n" + exConnectFailed); 294*cdf0e10cSrcweir if (exc != null) 295*cdf0e10cSrcweir { 296*cdf0e10cSrcweir System.out.println(exc); 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir if (bAppExecutionHasWarning) 299*cdf0e10cSrcweir { 300*cdf0e10cSrcweir System.out.println(errorMessage); 301*cdf0e10cSrcweir } 302*cdf0e10cSrcweir } 303*cdf0e10cSrcweir else if (isExecutable) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir if (!param.getBool(util.PropertyName.DONT_BACKUP_USERLAYER)) 306*cdf0e10cSrcweir { 307*cdf0e10cSrcweir backupUserLayer(param, msf); 308*cdf0e10cSrcweir } 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir else 312*cdf0e10cSrcweir { 313*cdf0e10cSrcweir System.out.println("Could not connect an Office and cannot start one.\n".concat("please start an office with following parameter:\n"). 314*cdf0e10cSrcweir concat("\nsoffice -accept=").concat((String) param.get("ConnectionString")).concat(";urp;\n")); 315*cdf0e10cSrcweir if (bAppExecutionHasWarning) 316*cdf0e10cSrcweir { 317*cdf0e10cSrcweir System.out.println(errorMessage); 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir } 320*cdf0e10cSrcweir } 321*cdf0e10cSrcweir 322*cdf0e10cSrcweir return msf; 323*cdf0e10cSrcweir } 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir /** 326*cdf0e10cSrcweir * Connect an Office 327*cdf0e10cSrcweir * @param connectStr 328*cdf0e10cSrcweir * @return 329*cdf0e10cSrcweir * @throws com.sun.star.uno.Exception 330*cdf0e10cSrcweir * @throws com.sun.star.uno.RuntimeException 331*cdf0e10cSrcweir * @throws com.sun.star.connection.NoConnectException 332*cdf0e10cSrcweir * @throws Exception 333*cdf0e10cSrcweir */ 334*cdf0e10cSrcweir protected static XMultiServiceFactory connect(String connectStr) 335*cdf0e10cSrcweir throws com.sun.star.uno.Exception, 336*cdf0e10cSrcweir com.sun.star.uno.RuntimeException, 337*cdf0e10cSrcweir com.sun.star.connection.NoConnectException, 338*cdf0e10cSrcweir Exception 339*cdf0e10cSrcweir { 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir // Get component context 342*cdf0e10cSrcweir final XComponentContext xcomponentcontext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null); 343*cdf0e10cSrcweir 344*cdf0e10cSrcweir // initial serviceManager 345*cdf0e10cSrcweir final XMultiComponentFactory xLocalServiceManager = xcomponentcontext.getServiceManager(); 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir // create a connector, so that it can contact the office 348*cdf0e10cSrcweir // XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext); 349*cdf0e10cSrcweir final Object xUrlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xcomponentcontext); 350*cdf0e10cSrcweir final XUnoUrlResolver urlResolver = UnoRuntime.queryInterface(XUnoUrlResolver.class, xUrlResolver); 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir final Object rInitialObject = urlResolver.resolve(connectStr); 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir XMultiServiceFactory xMSF = null; 355*cdf0e10cSrcweir 356*cdf0e10cSrcweir if (rInitialObject != null) 357*cdf0e10cSrcweir { 358*cdf0e10cSrcweir // debug = true; 359*cdf0e10cSrcweir dbg("resolved url"); 360*cdf0e10cSrcweir 361*cdf0e10cSrcweir xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, rInitialObject); 362*cdf0e10cSrcweir } 363*cdf0e10cSrcweir 364*cdf0e10cSrcweir return xMSF; 365*cdf0e10cSrcweir } 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir /** 368*cdf0e10cSrcweir * Close an office. 369*cdf0e10cSrcweir * @param param The test parameters. 370*cdf0e10cSrcweir * @param closeIfPossible If true, close even if 371*cdf0e10cSrcweir * it was running before the test 372*cdf0e10cSrcweir */ 373*cdf0e10cSrcweir public boolean closeExistingOffice(lib.TestParameters param, boolean closeIfPossible) 374*cdf0e10cSrcweir { 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir XMultiServiceFactory msf = (XMultiServiceFactory) param.getMSF(); 377*cdf0e10cSrcweir final boolean alreadyConnected = (msf != null); 378*cdf0e10cSrcweir debug = param.getBool(PropertyName.DEBUG_IS_ACTIVE); 379*cdf0e10cSrcweir 380*cdf0e10cSrcweir if (alreadyConnected) 381*cdf0e10cSrcweir { 382*cdf0e10cSrcweir dbg("try to get ProcessHandler"); 383*cdf0e10cSrcweir 384*cdf0e10cSrcweir final ProcessHandler ph = (ProcessHandler) param.get("AppProvider"); 385*cdf0e10cSrcweir 386*cdf0e10cSrcweir if (ph != null) 387*cdf0e10cSrcweir { 388*cdf0e10cSrcweir dbg("ProcessHandler != null"); 389*cdf0e10cSrcweir 390*cdf0e10cSrcweir disposeOffice(msf, param); 391*cdf0e10cSrcweir 392*cdf0e10cSrcweir // dispose watcher in case it's still running. 393*cdf0e10cSrcweir dbg("try to get OfficeWatcher"); 394*cdf0e10cSrcweir 395*cdf0e10cSrcweir final OfficeWatcher ow = (OfficeWatcher) param.get("Watcher"); 396*cdf0e10cSrcweir 397*cdf0e10cSrcweir if ((ow != null) && ow.isAlive()) 398*cdf0e10cSrcweir { 399*cdf0e10cSrcweir dbg("OfficeWatcher will be finished"); 400*cdf0e10cSrcweir ow.finish = true; 401*cdf0e10cSrcweir } 402*cdf0e10cSrcweir else 403*cdf0e10cSrcweir { 404*cdf0e10cSrcweir dbg("OfficeWatcher seems to be finished"); 405*cdf0e10cSrcweir } 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir return true; 408*cdf0e10cSrcweir } 409*cdf0e10cSrcweir else 410*cdf0e10cSrcweir { 411*cdf0e10cSrcweir if (closeIfPossible) 412*cdf0e10cSrcweir { 413*cdf0e10cSrcweir return disposeOffice(msf, param); 414*cdf0e10cSrcweir } 415*cdf0e10cSrcweir } 416*cdf0e10cSrcweir } 417*cdf0e10cSrcweir else 418*cdf0e10cSrcweir { 419*cdf0e10cSrcweir final String cncstr = "uno:" + param.get("ConnectionString") + 420*cdf0e10cSrcweir ";urp;StarOffice.ServiceManager"; 421*cdf0e10cSrcweir dbg("try to connect office"); 422*cdf0e10cSrcweir msf = connectOffice(cncstr); 423*cdf0e10cSrcweir 424*cdf0e10cSrcweir if (closeIfPossible) 425*cdf0e10cSrcweir { 426*cdf0e10cSrcweir return disposeOffice(msf, param); 427*cdf0e10cSrcweir } 428*cdf0e10cSrcweir } 429*cdf0e10cSrcweir dbg("closeExistingOffice finished"); 430*cdf0e10cSrcweir return true; 431*cdf0e10cSrcweir } 432*cdf0e10cSrcweir 433*cdf0e10cSrcweir private XMultiServiceFactory connectOffice(String cncstr) 434*cdf0e10cSrcweir { 435*cdf0e10cSrcweir XMultiServiceFactory msf = null; 436*cdf0e10cSrcweir String exc = ""; 437*cdf0e10cSrcweir // debug = true; 438*cdf0e10cSrcweir 439*cdf0e10cSrcweir dbg("trying to connect to " + cncstr); 440*cdf0e10cSrcweir 441*cdf0e10cSrcweir try 442*cdf0e10cSrcweir { 443*cdf0e10cSrcweir msf = connect(cncstr); 444*cdf0e10cSrcweir } 445*cdf0e10cSrcweir catch (com.sun.star.uno.Exception ue) 446*cdf0e10cSrcweir { 447*cdf0e10cSrcweir exc = ue.getMessage(); 448*cdf0e10cSrcweir } 449*cdf0e10cSrcweir catch (java.lang.Exception je) 450*cdf0e10cSrcweir { 451*cdf0e10cSrcweir exc = je.getMessage(); 452*cdf0e10cSrcweir } 453*cdf0e10cSrcweir 454*cdf0e10cSrcweir if (debug && exc != null && exc.length() != 0) 455*cdf0e10cSrcweir { 456*cdf0e10cSrcweir if (exc == null) 457*cdf0e10cSrcweir { 458*cdf0e10cSrcweir exc = ""; 459*cdf0e10cSrcweir } 460*cdf0e10cSrcweir dbg("Could not connect an Office. " + exc); 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir return msf; 464*cdf0e10cSrcweir } 465*cdf0e10cSrcweir 466*cdf0e10cSrcweir private synchronized boolean disposeOffice(XMultiServiceFactory msf, 467*cdf0e10cSrcweir TestParameters param) 468*cdf0e10cSrcweir { 469*cdf0e10cSrcweir XDesktop desk = null; 470*cdf0e10cSrcweir 471*cdf0e10cSrcweir debug = param.getBool(PropertyName.DEBUG_IS_ACTIVE); 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir boolean result = true; 474*cdf0e10cSrcweir 475*cdf0e10cSrcweir if (msf != null) 476*cdf0e10cSrcweir { 477*cdf0e10cSrcweir 478*cdf0e10cSrcweir // disable QuickStarter 479*cdf0e10cSrcweir try 480*cdf0e10cSrcweir { 481*cdf0e10cSrcweir Object quickStarter = msf.createInstance("com.sun.star.office.Quickstart"); 482*cdf0e10cSrcweir XFastPropertySet fps = UnoRuntime.queryInterface(XFastPropertySet.class, quickStarter); 483*cdf0e10cSrcweir fps.setFastPropertyValue(0, false); 484*cdf0e10cSrcweir } 485*cdf0e10cSrcweir catch (com.sun.star.uno.Exception ex) 486*cdf0e10cSrcweir { 487*cdf0e10cSrcweir dbg("ERROR: Could not disable QuickStarter: " + ex.toString()); 488*cdf0e10cSrcweir } 489*cdf0e10cSrcweir 490*cdf0e10cSrcweir try 491*cdf0e10cSrcweir { 492*cdf0e10cSrcweir desk = UnoRuntime.queryInterface(XDesktop.class, msf.createInstance("com.sun.star.frame.Desktop")); 493*cdf0e10cSrcweir msf = null; 494*cdf0e10cSrcweir 495*cdf0e10cSrcweir if (desk != null) 496*cdf0e10cSrcweir { 497*cdf0e10cSrcweir final boolean allClosed = closeAllWindows(desk); 498*cdf0e10cSrcweir 499*cdf0e10cSrcweir if (!allClosed) 500*cdf0e10cSrcweir { 501*cdf0e10cSrcweir dbg("Couldn't close all office windows!"); 502*cdf0e10cSrcweir } 503*cdf0e10cSrcweir 504*cdf0e10cSrcweir dbg("Trying to terminate the desktop"); 505*cdf0e10cSrcweir 506*cdf0e10cSrcweir desk.terminate(); 507*cdf0e10cSrcweir dbg("Desktop terminated"); 508*cdf0e10cSrcweir 509*cdf0e10cSrcweir try 510*cdf0e10cSrcweir { 511*cdf0e10cSrcweir final int closeTime = param.getInt(util.PropertyName.OFFICE_CLOSE_TIME_OUT); 512*cdf0e10cSrcweir dbg("the Office has " + closeTime / 1000 + " seconds for closing..."); 513*cdf0e10cSrcweir Thread.sleep(closeTime); 514*cdf0e10cSrcweir } 515*cdf0e10cSrcweir catch (java.lang.InterruptedException e) 516*cdf0e10cSrcweir { 517*cdf0e10cSrcweir } 518*cdf0e10cSrcweir } 519*cdf0e10cSrcweir } 520*cdf0e10cSrcweir catch (com.sun.star.uno.Exception ue) 521*cdf0e10cSrcweir { 522*cdf0e10cSrcweir result = false; 523*cdf0e10cSrcweir } 524*cdf0e10cSrcweir catch (com.sun.star.lang.DisposedException ue) 525*cdf0e10cSrcweir { 526*cdf0e10cSrcweir result = false; 527*cdf0e10cSrcweir } 528*cdf0e10cSrcweir } 529*cdf0e10cSrcweir 530*cdf0e10cSrcweir final String AppKillCommand = (String) param.get(util.PropertyName.APP_KILL_COMMAND); 531*cdf0e10cSrcweir if (AppKillCommand != null) 532*cdf0e10cSrcweir { 533*cdf0e10cSrcweir String sAppKillCommand = StringHelper.removeSurroundQuoteIfExists(AppKillCommand); 534*cdf0e10cSrcweir final StringTokenizer aKillCommandToken = new StringTokenizer(sAppKillCommand, ";"); 535*cdf0e10cSrcweir while (aKillCommandToken.hasMoreTokens()) 536*cdf0e10cSrcweir { 537*cdf0e10cSrcweir final String sKillCommand = aKillCommandToken.nextToken(); 538*cdf0e10cSrcweir dbg("User defined an application to destroy the started process. Trying to execute: " + sKillCommand); 539*cdf0e10cSrcweir 540*cdf0e10cSrcweir final ProcessHandler pHdl = new ProcessHandler(sKillCommand, 1000); // 3000 seems to be too long 541*cdf0e10cSrcweir pHdl.runCommand(); 542*cdf0e10cSrcweir 543*cdf0e10cSrcweir pHdl.kill(); 544*cdf0e10cSrcweir } 545*cdf0e10cSrcweir } 546*cdf0e10cSrcweir 547*cdf0e10cSrcweir final ProcessHandler ph = (ProcessHandler) param.get("AppProvider"); 548*cdf0e10cSrcweir 549*cdf0e10cSrcweir if (ph != null) 550*cdf0e10cSrcweir { 551*cdf0e10cSrcweir // dispose watcher in case it's still running. 552*cdf0e10cSrcweir final OfficeWatcher ow = (OfficeWatcher) param.get("Watcher"); 553*cdf0e10cSrcweir 554*cdf0e10cSrcweir if ((ow != null) && ow.isAlive()) 555*cdf0e10cSrcweir { 556*cdf0e10cSrcweir ow.finish = true; 557*cdf0e10cSrcweir } 558*cdf0e10cSrcweir 559*cdf0e10cSrcweir ph.kill(); 560*cdf0e10cSrcweir } 561*cdf0e10cSrcweir 562*cdf0e10cSrcweir param.remove("AppProvider"); 563*cdf0e10cSrcweir param.remove("ServiceFactory"); 564*cdf0e10cSrcweir 565*cdf0e10cSrcweir if (!param.getBool(util.PropertyName.DONT_BACKUP_USERLAYER)) 566*cdf0e10cSrcweir { 567*cdf0e10cSrcweir //copy user_backup into user layer 568*cdf0e10cSrcweir try 569*cdf0e10cSrcweir { 570*cdf0e10cSrcweir final String userLayer = (String) param.get("userLayer"); 571*cdf0e10cSrcweir final String copyLayer = (String) param.get("copyLayer"); 572*cdf0e10cSrcweir if (userLayer != null && copyLayer != null) 573*cdf0e10cSrcweir { 574*cdf0e10cSrcweir deleteFilesAndDirector(new File(userLayer)); 575*cdf0e10cSrcweir final File copyFile = new File(copyLayer); 576*cdf0e10cSrcweir dbg("copy '" + copyFile + "' -> '" + userLayer + "'"); 577*cdf0e10cSrcweir FileTools.copyDirectory(copyFile, new File(userLayer), new String[] 578*cdf0e10cSrcweir { 579*cdf0e10cSrcweir "temp" 580*cdf0e10cSrcweir }); 581*cdf0e10cSrcweir dbg("copy '" + copyFile + "' -> '" + userLayer + "' finished"); 582*cdf0e10cSrcweir 583*cdf0e10cSrcweir // remove all user_backup folder in temp dir 584*cdf0e10cSrcweir // this is for the case the runner was killed and some old backup folder still stay in temp dir 585*cdf0e10cSrcweir 586*cdf0e10cSrcweir 587*cdf0e10cSrcweir } 588*cdf0e10cSrcweir else 589*cdf0e10cSrcweir { 590*cdf0e10cSrcweir System.out.println("Cannot copy layer: '" + copyLayer + "' back to user layer: '" + userLayer + "'"); 591*cdf0e10cSrcweir } 592*cdf0e10cSrcweir } 593*cdf0e10cSrcweir catch (java.io.IOException e) 594*cdf0e10cSrcweir { 595*cdf0e10cSrcweir dbg("Couldn't recover from backup\n" + e.getMessage()); 596*cdf0e10cSrcweir } 597*cdf0e10cSrcweir } 598*cdf0e10cSrcweir return result; 599*cdf0e10cSrcweir } 600*cdf0e10cSrcweir 601*cdf0e10cSrcweir protected boolean closeAllWindows(XDesktop desk) 602*cdf0e10cSrcweir { 603*cdf0e10cSrcweir final XEnumerationAccess compEnumAccess = desk.getComponents(); 604*cdf0e10cSrcweir final XEnumeration compEnum = compEnumAccess.createEnumeration(); 605*cdf0e10cSrcweir boolean res = true; 606*cdf0e10cSrcweir 607*cdf0e10cSrcweir try 608*cdf0e10cSrcweir { 609*cdf0e10cSrcweir while (compEnum.hasMoreElements()) 610*cdf0e10cSrcweir { 611*cdf0e10cSrcweir final XCloseable closer = UnoRuntime.queryInterface(XCloseable.class, compEnum.nextElement()); 612*cdf0e10cSrcweir 613*cdf0e10cSrcweir if (closer != null) 614*cdf0e10cSrcweir { 615*cdf0e10cSrcweir closer.close(true); 616*cdf0e10cSrcweir } 617*cdf0e10cSrcweir } 618*cdf0e10cSrcweir } 619*cdf0e10cSrcweir catch (com.sun.star.util.CloseVetoException cve) 620*cdf0e10cSrcweir { 621*cdf0e10cSrcweir res = false; 622*cdf0e10cSrcweir } 623*cdf0e10cSrcweir catch (com.sun.star.container.NoSuchElementException nsee) 624*cdf0e10cSrcweir { 625*cdf0e10cSrcweir res = false; 626*cdf0e10cSrcweir } 627*cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException wte) 628*cdf0e10cSrcweir { 629*cdf0e10cSrcweir res = false; 630*cdf0e10cSrcweir } 631*cdf0e10cSrcweir 632*cdf0e10cSrcweir return res; 633*cdf0e10cSrcweir } 634*cdf0e10cSrcweir 635*cdf0e10cSrcweir public static XStringSubstitution createStringSubstitution(XMultiServiceFactory xMSF) 636*cdf0e10cSrcweir { 637*cdf0e10cSrcweir Object xPathSubst = null; 638*cdf0e10cSrcweir 639*cdf0e10cSrcweir try 640*cdf0e10cSrcweir { 641*cdf0e10cSrcweir xPathSubst = xMSF.createInstance( 642*cdf0e10cSrcweir "com.sun.star.util.PathSubstitution"); 643*cdf0e10cSrcweir } 644*cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) 645*cdf0e10cSrcweir { 646*cdf0e10cSrcweir e.printStackTrace(); 647*cdf0e10cSrcweir } 648*cdf0e10cSrcweir 649*cdf0e10cSrcweir if (xPathSubst != null) 650*cdf0e10cSrcweir { 651*cdf0e10cSrcweir return UnoRuntime.queryInterface(XStringSubstitution.class, xPathSubst); 652*cdf0e10cSrcweir } 653*cdf0e10cSrcweir else 654*cdf0e10cSrcweir { 655*cdf0e10cSrcweir return null; 656*cdf0e10cSrcweir } 657*cdf0e10cSrcweir } 658*cdf0e10cSrcweir 659*cdf0e10cSrcweir /** 660*cdf0e10cSrcweir * converts directory without 'file:///' prefix. 661*cdf0e10cSrcweir * and System dependend file separator 662*cdf0e10cSrcweir * @param dir 663*cdf0e10cSrcweir * @return 664*cdf0e10cSrcweir */ 665*cdf0e10cSrcweir public static String getDirSys(String dir) 666*cdf0e10cSrcweir { 667*cdf0e10cSrcweir String sysDir = ""; 668*cdf0e10cSrcweir 669*cdf0e10cSrcweir final int idx = dir.indexOf("file://"); 670*cdf0e10cSrcweir 671*cdf0e10cSrcweir final int idx2 = dir.indexOf("file:///"); 672*cdf0e10cSrcweir 673*cdf0e10cSrcweir // remove leading 'file://' 674*cdf0e10cSrcweir if (idx < 0) 675*cdf0e10cSrcweir { 676*cdf0e10cSrcweir sysDir = dir; 677*cdf0e10cSrcweir } 678*cdf0e10cSrcweir else 679*cdf0e10cSrcweir { 680*cdf0e10cSrcweir sysDir = dir.substring("file://".length()); 681*cdf0e10cSrcweir } 682*cdf0e10cSrcweir 683*cdf0e10cSrcweir sysDir = utils.replaceAll13(sysDir, "%20", " "); 684*cdf0e10cSrcweir 685*cdf0e10cSrcweir // append '/' if not there (e.g. linux) 686*cdf0e10cSrcweir if (sysDir.charAt(sysDir.length() - 1) != '/') 687*cdf0e10cSrcweir { 688*cdf0e10cSrcweir sysDir += "/"; 689*cdf0e10cSrcweir } 690*cdf0e10cSrcweir 691*cdf0e10cSrcweir // remove leading '/' and replace others with '\' on windows machines 692*cdf0e10cSrcweir final String sep = System.getProperty("file.separator"); 693*cdf0e10cSrcweir 694*cdf0e10cSrcweir if (sep.equalsIgnoreCase("\\")) 695*cdf0e10cSrcweir { 696*cdf0e10cSrcweir if (!(idx2 < 0)) 697*cdf0e10cSrcweir { 698*cdf0e10cSrcweir sysDir = sysDir.substring(1); 699*cdf0e10cSrcweir } 700*cdf0e10cSrcweir else 701*cdf0e10cSrcweir { 702*cdf0e10cSrcweir //network path 703*cdf0e10cSrcweir sysDir = "//" + sysDir; 704*cdf0e10cSrcweir } 705*cdf0e10cSrcweir sysDir = sysDir.replace('/', '\\'); 706*cdf0e10cSrcweir } 707*cdf0e10cSrcweir 708*cdf0e10cSrcweir return sysDir; 709*cdf0e10cSrcweir } 710*cdf0e10cSrcweir 711*cdf0e10cSrcweir /** 712*cdf0e10cSrcweir * If the office is connected but the <CODE>AppExecutionCommand</CODE> is not set, 713*cdf0e10cSrcweir * this function asks the office for its location and fill the 714*cdf0e10cSrcweir * <CODE>AppExecutionCommand</CODE> with valid contet. 715*cdf0e10cSrcweir * This function was only called if parameter <CODE>AutoRestart</CODE> is set. 716*cdf0e10cSrcweir * @param msf the <CODE>MultiServiceFactory</CODE> 717*cdf0e10cSrcweir * @param param the <CODE>TestParameters</CODE> 718*cdf0e10cSrcweir */ 719*cdf0e10cSrcweir private static void makeAppExecCommand(XMultiServiceFactory msf, TestParameters param) 720*cdf0e10cSrcweir { 721*cdf0e10cSrcweir debug = param.getBool(PropertyName.DEBUG_IS_ACTIVE); 722*cdf0e10cSrcweir 723*cdf0e10cSrcweir // get existing AppExecutionCommand if available, else empty string 724*cdf0e10cSrcweir String command = (String) param.get(util.PropertyName.APP_EXECUTION_COMMAND); 725*cdf0e10cSrcweir 726*cdf0e10cSrcweir String connectionString; 727*cdf0e10cSrcweir if (param.getBool(util.PropertyName.USE_PIPE_CONNECTION) == true) 728*cdf0e10cSrcweir { 729*cdf0e10cSrcweir // This is the default behaviour 730*cdf0e10cSrcweir connectionString = (String) param.get(util.PropertyName.PIPE_CONNECTION_STRING); 731*cdf0e10cSrcweir } 732*cdf0e10cSrcweir else 733*cdf0e10cSrcweir { 734*cdf0e10cSrcweir // is used if UsePipeConnection=false 735*cdf0e10cSrcweir connectionString = (String) param.get(util.PropertyName.CONNECTION_STRING); 736*cdf0e10cSrcweir } 737*cdf0e10cSrcweir 738*cdf0e10cSrcweir String sysBinDir = ""; 739*cdf0e10cSrcweir 740*cdf0e10cSrcweir try 741*cdf0e10cSrcweir { 742*cdf0e10cSrcweir sysBinDir = utils.getSystemURL(utils.expandMacro(msf, "$SYSBINDIR")); 743*cdf0e10cSrcweir } 744*cdf0e10cSrcweir catch (java.lang.Exception e) 745*cdf0e10cSrcweir { 746*cdf0e10cSrcweir dbg("could not get system binary directory"); 747*cdf0e10cSrcweir return; 748*cdf0e10cSrcweir } 749*cdf0e10cSrcweir 750*cdf0e10cSrcweir // does the existing command show to the connected office? 751*cdf0e10cSrcweir if (command.indexOf(sysBinDir) == -1) 752*cdf0e10cSrcweir { 753*cdf0e10cSrcweir command = sysBinDir + System.getProperty("file.separator") + "soffice" + 754*cdf0e10cSrcweir " -norestore -accept=" + connectionString + ";urp;"; 755*cdf0e10cSrcweir } 756*cdf0e10cSrcweir 757*cdf0e10cSrcweir dbg("update AppExecutionCommand: " + command); 758*cdf0e10cSrcweir 759*cdf0e10cSrcweir param.put(util.PropertyName.APP_EXECUTION_COMMAND, command); 760*cdf0e10cSrcweir } 761*cdf0e10cSrcweir 762*cdf0e10cSrcweir private static void dbg(String message) 763*cdf0e10cSrcweir { 764*cdf0e10cSrcweir if (debug) 765*cdf0e10cSrcweir { 766*cdf0e10cSrcweir System.out.println(utils.getDateTime() + "OfficeProvider: " + message); 767*cdf0e10cSrcweir } 768*cdf0e10cSrcweir 769*cdf0e10cSrcweir } 770*cdf0e10cSrcweir 771*cdf0e10cSrcweir private class OfficeWatcherPing extends Thread 772*cdf0e10cSrcweir { 773*cdf0e10cSrcweir 774*cdf0e10cSrcweir private final OfficeWatcher ow; 775*cdf0e10cSrcweir private boolean bStop = false; 776*cdf0e10cSrcweir 777*cdf0e10cSrcweir public OfficeWatcherPing(OfficeWatcher ow) 778*cdf0e10cSrcweir { 779*cdf0e10cSrcweir this.ow = ow; 780*cdf0e10cSrcweir } 781*cdf0e10cSrcweir 782*cdf0e10cSrcweir @Override 783*cdf0e10cSrcweir public void run() 784*cdf0e10cSrcweir { 785*cdf0e10cSrcweir System.out.println(utils.getDateTime() + "OfficeProvider:Owp: start "); 786*cdf0e10cSrcweir 787*cdf0e10cSrcweir while (!bStop) 788*cdf0e10cSrcweir { 789*cdf0e10cSrcweir System.out.println(utils.getDateTime() + "OfficeProvider:Owp: ping "); 790*cdf0e10cSrcweir ow.ping(); 791*cdf0e10cSrcweir try 792*cdf0e10cSrcweir { 793*cdf0e10cSrcweir System.out.println(utils.getDateTime() + "OfficeProvider:Owp: sleep "); 794*cdf0e10cSrcweir OfficeWatcherPing.sleep(1000); // 5000 795*cdf0e10cSrcweir } 796*cdf0e10cSrcweir catch (InterruptedException ex) 797*cdf0e10cSrcweir { 798*cdf0e10cSrcweir ex.printStackTrace(); 799*cdf0e10cSrcweir } 800*cdf0e10cSrcweir } 801*cdf0e10cSrcweir 802*cdf0e10cSrcweir } 803*cdf0e10cSrcweir 804*cdf0e10cSrcweir public void finish() 805*cdf0e10cSrcweir { 806*cdf0e10cSrcweir synchronized(this) 807*cdf0e10cSrcweir { 808*cdf0e10cSrcweir bStop = true; 809*cdf0e10cSrcweir System.out.println(utils.getDateTime() + "OfficeProvider:Owp: stop "); 810*cdf0e10cSrcweir 811*cdf0e10cSrcweir notify(); 812*cdf0e10cSrcweir } 813*cdf0e10cSrcweir } 814*cdf0e10cSrcweir } 815*cdf0e10cSrcweir 816*cdf0e10cSrcweir private void deleteFilesAndDirector(File file) 817*cdf0e10cSrcweir { 818*cdf0e10cSrcweir File f = file; 819*cdf0e10cSrcweir if(f.isDirectory()) 820*cdf0e10cSrcweir { 821*cdf0e10cSrcweir File files[] = f.listFiles(); 822*cdf0e10cSrcweir for(int i = 0; i < files.length; i++) 823*cdf0e10cSrcweir { 824*cdf0e10cSrcweir deleteFilesAndDirector(files[i]); 825*cdf0e10cSrcweir } 826*cdf0e10cSrcweir f.delete(); 827*cdf0e10cSrcweir } 828*cdf0e10cSrcweir else if (f.isFile()) 829*cdf0e10cSrcweir { 830*cdf0e10cSrcweir f.delete(); 831*cdf0e10cSrcweir } 832*cdf0e10cSrcweir } 833*cdf0e10cSrcweir } 834