1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package helper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
26cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
30cdf0e10cSrcweir import java.util.Hashtable;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir import util.PropertyName;
33cdf0e10cSrcweir import util.utils;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /**
36cdf0e10cSrcweir  * Bootstrap UNO from a Java environment.
37cdf0e10cSrcweir  * Needed parameters:
38cdf0e10cSrcweir  * <ol>
39cdf0e10cSrcweir  *  <li>
40cdf0e10cSrcweir  *      <ul>
41cdf0e10cSrcweir  *          <li>UNORC - complete path to the unorc file</li>
42cdf0e10cSrcweir  *      </ul>
43cdf0e10cSrcweir  *  </li>
44cdf0e10cSrcweir  *  <li>
45cdf0e10cSrcweir  *      <ul>
46cdf0e10cSrcweir  *          <li>AppExecutionCommand - path to the soffice executable</li>
47cdf0e10cSrcweir  *          <li>OS - the operating system in case it's Windows, because the
48cdf0e10cSrcweir  *              unorc is called uno.ini</li>
49cdf0e10cSrcweir  *      </ul>
50cdf0e10cSrcweir  *  </li>
51cdf0e10cSrcweir  * </ol>
52cdf0e10cSrcweir  */
53cdf0e10cSrcweir public class UnoProvider implements AppProvider {
54cdf0e10cSrcweir 
UnoProvider()55cdf0e10cSrcweir     public UnoProvider(){
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     /**
60cdf0e10cSrcweir      * Close existing office: calls disposeManager()
61cdf0e10cSrcweir      * @param param The test parameters.
62cdf0e10cSrcweir      * @param closeIfPossible Not needed, since UNO is bootstrapped by this
63cdf0e10cSrcweir      * class in every case.
64cdf0e10cSrcweir      * @return True, if bootstrapping worked.
65cdf0e10cSrcweir      */
closeExistingOffice(TestParameters param, boolean closeIfPossible)66cdf0e10cSrcweir     public boolean closeExistingOffice(TestParameters param,
67cdf0e10cSrcweir                                                     boolean closeIfPossible) {
68cdf0e10cSrcweir         return disposeManager(param);
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /**
72cdf0e10cSrcweir      * Dispose the UNO environment: just clears the bootstrapped
73cdf0e10cSrcweir      * MultiServiceFactory
74cdf0e10cSrcweir      * @param param The test parameters.
75cdf0e10cSrcweir      * @return True, if bootstrapping worked.
76cdf0e10cSrcweir      */
disposeManager(TestParameters param)77cdf0e10cSrcweir     public boolean disposeManager(TestParameters param) {
78cdf0e10cSrcweir         XMultiServiceFactory xMSF =
79cdf0e10cSrcweir                     (XMultiServiceFactory)param.remove("ServiceManager");
80cdf0e10cSrcweir         xMSF = null;
81cdf0e10cSrcweir         System.gc();
82cdf0e10cSrcweir         try {
83cdf0e10cSrcweir             Thread.sleep(1000);
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir         catch(java.lang.InterruptedException e) {}
86cdf0e10cSrcweir         return true;
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     /**
90cdf0e10cSrcweir      * Bootstrap UNO and return the created MultiServiceFactory.
91cdf0e10cSrcweir      * @param param The test parameters.
92cdf0e10cSrcweir      * @return A created MultiServiceFactory.
93cdf0e10cSrcweir      */
getManager(TestParameters param)94cdf0e10cSrcweir     public Object getManager(TestParameters param) {
95cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF();
96cdf0e10cSrcweir         if (xMSF == null) {
97cdf0e10cSrcweir             // bootstrap UNO.
98cdf0e10cSrcweir             String unorcName = getUnorcName(param);
99cdf0e10cSrcweir             Hashtable env = new Hashtable();
100cdf0e10cSrcweir             env.put("SYSBINDIR", getSysBinDir(param));
101cdf0e10cSrcweir 
102cdf0e10cSrcweir             XComponentContext xContext = null;
103cdf0e10cSrcweir             try {
104cdf0e10cSrcweir                 xContext = Bootstrap.defaultBootstrap_InitialComponentContext(
105cdf0e10cSrcweir                                                                  unorcName, env);
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir             catch(Exception e) {
108cdf0e10cSrcweir                 e.printStackTrace();
109cdf0e10cSrcweir                 System.out.println("Could not get XComponentContext. Maybe you must add program folder to LD_LIBRARY_PATH");
110cdf0e10cSrcweir                 return null;
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir             XMultiComponentFactory xMCF = xContext.getServiceManager();
113cdf0e10cSrcweir             xMSF = (XMultiServiceFactory)UnoRuntime.queryInterface(
114cdf0e10cSrcweir                                 XMultiServiceFactory.class, xMCF);
115cdf0e10cSrcweir         }
116cdf0e10cSrcweir         return xMSF;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
getUnorcName(TestParameters param)119cdf0e10cSrcweir     private String getUnorcName(TestParameters param) {
120cdf0e10cSrcweir         String unorcName = (String)param.get("UNORC");
121cdf0e10cSrcweir         if (unorcName == null) {
122cdf0e10cSrcweir             String office = (String)param.get("AppExecutionCommand");
123cdf0e10cSrcweir             // determine unorc name: unorc or uno.ini on windows
124cdf0e10cSrcweir             String opSystem = (String)param.get(PropertyName.OPERATING_SYSTEM);
125cdf0e10cSrcweir             if ( opSystem != null && opSystem.equalsIgnoreCase(PropertyName.WNTMSCI)) {
126cdf0e10cSrcweir                 unorcName = "uno.ini";
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir             else {
129cdf0e10cSrcweir                 unorcName = "unorc";
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir             if (office == null)
132cdf0e10cSrcweir                 return null;
133cdf0e10cSrcweir             // use '/', because this will be a URL in any case.
134cdf0e10cSrcweir             unorcName = office.substring(0, office.indexOf("program")+7) +
135cdf0e10cSrcweir                         "/" + unorcName;
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir         unorcName = utils.getFullURL(unorcName);
138cdf0e10cSrcweir         if (param.DebugIsActive) {
139cdf0e10cSrcweir             System.out.println("UnoUcr: " + unorcName);
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir         return unorcName;
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
getSysBinDir(TestParameters param)144cdf0e10cSrcweir     private String getSysBinDir(TestParameters param) {
145cdf0e10cSrcweir         String base = (String)param.get("AppExecutionCommand");
146cdf0e10cSrcweir         if (base == null)
147cdf0e10cSrcweir             base = (String)param.get("UNORC");
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         if (base == null)
150cdf0e10cSrcweir             return null;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         String sysbindir = base.substring(0,
153cdf0e10cSrcweir                                 base.indexOf("program")+7);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         sysbindir = utils.getFullURL(sysbindir);
156cdf0e10cSrcweir         if (param.DebugIsActive) {
157cdf0e10cSrcweir             System.out.println("SysBinDir: " + sysbindir);
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir         return sysbindir;
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir }
162