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