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