xref: /AOO41X/main/qadevOOo/runner/helper/UnoProvider.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.comp.helper.Bootstrap;
30*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
31*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
34*cdf0e10cSrcweir import java.util.Hashtable;
35*cdf0e10cSrcweir import lib.TestParameters;
36*cdf0e10cSrcweir import util.PropertyName;
37*cdf0e10cSrcweir import util.utils;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir /**
40*cdf0e10cSrcweir  * Bootstrap UNO from a Java environment.
41*cdf0e10cSrcweir  * Needed parameters:
42*cdf0e10cSrcweir  * <ol>
43*cdf0e10cSrcweir  *  <li>
44*cdf0e10cSrcweir  *      <ul>
45*cdf0e10cSrcweir  *          <li>UNORC - complete path to the unorc file</li>
46*cdf0e10cSrcweir  *      </ul>
47*cdf0e10cSrcweir  *  </li>
48*cdf0e10cSrcweir  *  <li>
49*cdf0e10cSrcweir  *      <ul>
50*cdf0e10cSrcweir  *          <li>AppExecutionCommand - path to the soffice executable</li>
51*cdf0e10cSrcweir  *          <li>OS - the operating system in case it's Windows, because the
52*cdf0e10cSrcweir  *              unorc is called uno.ini</li>
53*cdf0e10cSrcweir  *      </ul>
54*cdf0e10cSrcweir  *  </li>
55*cdf0e10cSrcweir  * </ol>
56*cdf0e10cSrcweir  */
57*cdf0e10cSrcweir public class UnoProvider implements AppProvider {
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     public UnoProvider(){
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     /**
64*cdf0e10cSrcweir      * Close existing office: calls disposeManager()
65*cdf0e10cSrcweir      * @param param The test parameters.
66*cdf0e10cSrcweir      * @param closeIfPossible Not needed, since UNO is bootstrapped by this
67*cdf0e10cSrcweir      * class in every case.
68*cdf0e10cSrcweir      * @return True, if bootstrapping worked.
69*cdf0e10cSrcweir      */
70*cdf0e10cSrcweir     public boolean closeExistingOffice(TestParameters param,
71*cdf0e10cSrcweir                                                     boolean closeIfPossible) {
72*cdf0e10cSrcweir         return disposeManager(param);
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     /**
76*cdf0e10cSrcweir      * Dispose the UNO environment: just clears the bootstrapped
77*cdf0e10cSrcweir      * MultiServiceFactory
78*cdf0e10cSrcweir      * @param param The test parameters.
79*cdf0e10cSrcweir      * @return True, if bootstrapping worked.
80*cdf0e10cSrcweir      */
81*cdf0e10cSrcweir     public boolean disposeManager(TestParameters param) {
82*cdf0e10cSrcweir         XMultiServiceFactory xMSF =
83*cdf0e10cSrcweir                     (XMultiServiceFactory)param.remove("ServiceManager");
84*cdf0e10cSrcweir         xMSF = null;
85*cdf0e10cSrcweir         System.gc();
86*cdf0e10cSrcweir         try {
87*cdf0e10cSrcweir             Thread.sleep(1000);
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir         catch(java.lang.InterruptedException e) {}
90*cdf0e10cSrcweir         return true;
91*cdf0e10cSrcweir     }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     /**
94*cdf0e10cSrcweir      * Bootstrap UNO and return the created MultiServiceFactory.
95*cdf0e10cSrcweir      * @param param The test parameters.
96*cdf0e10cSrcweir      * @return A created MultiServiceFactory.
97*cdf0e10cSrcweir      */
98*cdf0e10cSrcweir     public Object getManager(TestParameters param) {
99*cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF();
100*cdf0e10cSrcweir         if (xMSF == null) {
101*cdf0e10cSrcweir             // bootstrap UNO.
102*cdf0e10cSrcweir             String unorcName = getUnorcName(param);
103*cdf0e10cSrcweir             Hashtable env = new Hashtable();
104*cdf0e10cSrcweir             env.put("SYSBINDIR", getSysBinDir(param));
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             XComponentContext xContext = null;
107*cdf0e10cSrcweir             try {
108*cdf0e10cSrcweir                 xContext = Bootstrap.defaultBootstrap_InitialComponentContext(
109*cdf0e10cSrcweir                                                                  unorcName, env);
110*cdf0e10cSrcweir             }
111*cdf0e10cSrcweir             catch(Exception e) {
112*cdf0e10cSrcweir                 e.printStackTrace();
113*cdf0e10cSrcweir                 System.out.println("Could not get XComponentContext. Maybe you must add program folder to LD_LIBRARY_PATH");
114*cdf0e10cSrcweir                 return null;
115*cdf0e10cSrcweir             }
116*cdf0e10cSrcweir             XMultiComponentFactory xMCF = xContext.getServiceManager();
117*cdf0e10cSrcweir             xMSF = (XMultiServiceFactory)UnoRuntime.queryInterface(
118*cdf0e10cSrcweir                                 XMultiServiceFactory.class, xMCF);
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir         return xMSF;
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     private String getUnorcName(TestParameters param) {
124*cdf0e10cSrcweir         String unorcName = (String)param.get("UNORC");
125*cdf0e10cSrcweir         if (unorcName == null) {
126*cdf0e10cSrcweir             String office = (String)param.get("AppExecutionCommand");
127*cdf0e10cSrcweir             // determine unorc name: unorc or uno.ini on windows
128*cdf0e10cSrcweir             String opSystem = (String)param.get(PropertyName.OPERATING_SYSTEM);
129*cdf0e10cSrcweir             if ( opSystem != null && opSystem.equalsIgnoreCase(PropertyName.WNTMSCI)) {
130*cdf0e10cSrcweir                 unorcName = "uno.ini";
131*cdf0e10cSrcweir             }
132*cdf0e10cSrcweir             else {
133*cdf0e10cSrcweir                 unorcName = "unorc";
134*cdf0e10cSrcweir             }
135*cdf0e10cSrcweir             if (office == null)
136*cdf0e10cSrcweir                 return null;
137*cdf0e10cSrcweir             // use '/', because this will be a URL in any case.
138*cdf0e10cSrcweir             unorcName = office.substring(0, office.indexOf("program")+7) +
139*cdf0e10cSrcweir                         "/" + unorcName;
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir         unorcName = utils.getFullURL(unorcName);
142*cdf0e10cSrcweir         if (param.DebugIsActive) {
143*cdf0e10cSrcweir             System.out.println("UnoUcr: " + unorcName);
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir         return unorcName;
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     private String getSysBinDir(TestParameters param) {
149*cdf0e10cSrcweir         String base = (String)param.get("AppExecutionCommand");
150*cdf0e10cSrcweir         if (base == null)
151*cdf0e10cSrcweir             base = (String)param.get("UNORC");
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         if (base == null)
154*cdf0e10cSrcweir             return null;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         String sysbindir = base.substring(0,
157*cdf0e10cSrcweir                                 base.indexOf("program")+7);
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         sysbindir = utils.getFullURL(sysbindir);
160*cdf0e10cSrcweir         if (param.DebugIsActive) {
161*cdf0e10cSrcweir             System.out.println("SysBinDir: " + sysbindir);
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         return sysbindir;
164*cdf0e10cSrcweir     }
165*cdf0e10cSrcweir }
166