xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/Installer.java (revision 44cf02803b51681da4056cdf9500cc33ee29bd2f)
1180e3c91SLiu Zhe /**************************************************************
2180e3c91SLiu Zhe  *
3180e3c91SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4180e3c91SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5180e3c91SLiu Zhe  * distributed with this work for additional information
6180e3c91SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7180e3c91SLiu Zhe  * to you under the Apache License, Version 2.0 (the
8180e3c91SLiu Zhe  * "License"); you may not use this file except in compliance
9180e3c91SLiu Zhe  * with the License.  You may obtain a copy of the License at
10180e3c91SLiu Zhe  *
11180e3c91SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12180e3c91SLiu Zhe  *
13180e3c91SLiu Zhe  * Unless required by applicable law or agreed to in writing,
14180e3c91SLiu Zhe  * software distributed under the License is distributed on an
15180e3c91SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16180e3c91SLiu Zhe  * KIND, either express or implied.  See the License for the
17180e3c91SLiu Zhe  * specific language governing permissions and limitations
18180e3c91SLiu Zhe  * under the License.
19180e3c91SLiu Zhe  *
20180e3c91SLiu Zhe  *************************************************************/
216467ecbfSLiu Zhe package org.openoffice.test.common;
226467ecbfSLiu Zhe 
236467ecbfSLiu Zhe import java.io.File;
246467ecbfSLiu Zhe import java.io.IOException;
25883eb82fSLiu Zhe import java.lang.reflect.Method;
26883eb82fSLiu Zhe import java.net.URL;
27883eb82fSLiu Zhe import java.net.URLClassLoader;
286467ecbfSLiu Zhe import java.text.MessageFormat;
296467ecbfSLiu Zhe import java.util.logging.Level;
306467ecbfSLiu Zhe 
31180e3c91SLiu Zhe /**
32180e3c91SLiu Zhe  * Install openoffice from installation package before running test
33180e3c91SLiu Zhe  *
34180e3c91SLiu Zhe  */
356467ecbfSLiu Zhe public class Installer implements Runnable {
366467ecbfSLiu Zhe 	private static Logger log = Logger.getLogger(Installer.class);
376467ecbfSLiu Zhe 	File downloadDir = Testspace.getFile("download");
386467ecbfSLiu Zhe 	File downloadUrl = Testspace.getFile("download/url");
396467ecbfSLiu Zhe 	File installDir = Testspace.getFile("install");
406467ecbfSLiu Zhe 	File installTempDir = Testspace.getFile("install_temp");
416467ecbfSLiu Zhe 
426467ecbfSLiu Zhe 	@Override
run()436467ecbfSLiu Zhe 	public void run() {
446467ecbfSLiu Zhe 		String prop = System.getProperty("singleton");
456467ecbfSLiu Zhe 		if ("true".equalsIgnoreCase(prop) || "yes".equalsIgnoreCase(prop)) {
466467ecbfSLiu Zhe 			if (SystemUtil.findProcesses(".*org\\.openoffice\\.test\\.common\\.Installer.*").size() > 1) {
476467ecbfSLiu Zhe 				throw new RuntimeException("Only allow one running test instance!");
486467ecbfSLiu Zhe 			}
496467ecbfSLiu Zhe 		}
50*44cf0280SCarl Marcum 
51*44cf0280SCarl Marcum 		prop = findOpenofficeHome();
52*44cf0280SCarl Marcum 		System.setProperty("openoffice.home", prop);
53*44cf0280SCarl Marcum 		log.log(Level.INFO, MessageFormat.format("OpenOffice in {0} will be tested.", prop));
54*44cf0280SCarl Marcum 
55*44cf0280SCarl Marcum 		String[] jars = { "juh.jar", "unoil.jar", "ridl.jar", "jurt.jar" };
56*44cf0280SCarl Marcum 		for (String jar : jars) {
57*44cf0280SCarl Marcum 			File file = FileUtil.findFile(prop, jar);
58*44cf0280SCarl Marcum 
59*44cf0280SCarl Marcum 			if (file != null)
60*44cf0280SCarl Marcum 				addToClassPath(file);
61*44cf0280SCarl Marcum 		}
62*44cf0280SCarl Marcum 
63*44cf0280SCarl Marcum 	}
64*44cf0280SCarl Marcum 
findOpenofficeHome()65*44cf0280SCarl Marcum 	private String findOpenofficeHome() {
66*44cf0280SCarl Marcum 		String prop = System.getProperty("openoffice.home");
67*44cf0280SCarl Marcum 		if (prop != null) {
68*44cf0280SCarl Marcum 			validateOpenOfficeInstallation(new File(prop));
69*44cf0280SCarl Marcum 			return prop;
70*44cf0280SCarl Marcum 		}
71*44cf0280SCarl Marcum 
726467ecbfSLiu Zhe 		if ((prop = System.getProperty("openoffice.pack")) != null) {
73180e3c91SLiu Zhe 			String onlyNewProp = System.getProperty("only.new");
746467ecbfSLiu Zhe 			File packFile = null;
756467ecbfSLiu Zhe 			if (FileUtil.isUrl(prop)) {
76180e3c91SLiu Zhe 				log.log(Level.INFO, MessageFormat.format("Try to download {0}...", prop));
776467ecbfSLiu Zhe 				String url = FileUtil.readFileAsString(downloadUrl);
786467ecbfSLiu Zhe 				if (!prop.equals(url)) {
796467ecbfSLiu Zhe 					FileUtil.deleteFile(downloadDir);
806467ecbfSLiu Zhe 					downloadDir.mkdirs();
816467ecbfSLiu Zhe 					packFile = FileUtil.download(prop, downloadDir);
826467ecbfSLiu Zhe 					if (packFile == null)
836467ecbfSLiu Zhe 						throw new RuntimeException(MessageFormat.format("{0} can not be downloaded!", prop));
846467ecbfSLiu Zhe 					FileUtil.writeStringToFile(downloadUrl, prop);
856467ecbfSLiu Zhe 				} else {
86180e3c91SLiu Zhe 					boolean[] skipped = { false };
87180e3c91SLiu Zhe 					packFile = FileUtil.download(prop, downloadDir, true, skipped);
886467ecbfSLiu Zhe 					if (packFile == null)
896467ecbfSLiu Zhe 						throw new RuntimeException(MessageFormat.format("{0} can not be downloaded!", prop));
90180e3c91SLiu Zhe 					if (("true".equalsIgnoreCase(onlyNewProp) || "yes".equalsIgnoreCase(onlyNewProp)) && skipped[0])
91180e3c91SLiu Zhe 						throw new RuntimeException(MessageFormat.format("{0} is old. Test is allowed only on new build.", prop));
926467ecbfSLiu Zhe 				}
936467ecbfSLiu Zhe 			} else {
946467ecbfSLiu Zhe 				packFile = new File(prop);
956467ecbfSLiu Zhe 				if (!packFile.isFile())
966467ecbfSLiu Zhe 					throw new RuntimeException(MessageFormat.format("{0} does not exists or is not a file!", prop));
976467ecbfSLiu Zhe 			}
986467ecbfSLiu Zhe 
996467ecbfSLiu Zhe 			try {
1006467ecbfSLiu Zhe 				FileUtil.deleteFile(installDir);
1016467ecbfSLiu Zhe 				FileUtil.deleteFile(installTempDir);
1026467ecbfSLiu Zhe 				installTempDir.mkdirs();
1036467ecbfSLiu Zhe 				if (packFile.getName().endsWith(".gz")) {
1046467ecbfSLiu Zhe 					StringBuffer output = new StringBuffer();
1056467ecbfSLiu Zhe 					if (SystemUtil.exec(new String[] { "tar", "-zxpf", packFile.getAbsolutePath(), "-C", installTempDir.getAbsolutePath() }, output) != 0)
1066467ecbfSLiu Zhe 						throw new RuntimeException(MessageFormat.format("{0} can not be installed! Cause: {1}", packFile, output));
1076467ecbfSLiu Zhe 				} else {
1086467ecbfSLiu Zhe 					if (!FileUtil.unzip(packFile, installTempDir))
1096467ecbfSLiu Zhe 						throw new RuntimeException(MessageFormat.format("{0} can not be installed!", packFile));
1106467ecbfSLiu Zhe 				}
111a22b55fdSLiu Zhe 				// On windows, if path is too long, openoffice can not be
112a22b55fdSLiu Zhe 				// started.
1136467ecbfSLiu Zhe 				File[] files = installTempDir.listFiles();
1146467ecbfSLiu Zhe 				if (files != null && files.length == 1 && files[0].isDirectory()) {
1156467ecbfSLiu Zhe 					files[0].renameTo(installDir);
1166467ecbfSLiu Zhe 				}
117*44cf0280SCarl Marcum 				validateOpenOfficeInstallation(installDir);
118*44cf0280SCarl Marcum 				return installDir.getAbsolutePath();
1196467ecbfSLiu Zhe 			} finally {
1206467ecbfSLiu Zhe 				FileUtil.deleteFile(installTempDir);
1216467ecbfSLiu Zhe 			}
1226467ecbfSLiu Zhe 		}
123883eb82fSLiu Zhe 
124*44cf0280SCarl Marcum 		// Try look for the output of ./configure --with-package-format="installed"
125*44cf0280SCarl Marcum 		final String srcRoot = System.getenv("SRC_ROOT");
126*44cf0280SCarl Marcum 		final String inPath = System.getenv("INPATH");
127*44cf0280SCarl Marcum 		if (srcRoot == null || inPath == null) {
128*44cf0280SCarl Marcum 			throw new RuntimeException("Neither openoffice.home nor openoffice.pack were specified, and could not autodetect OpenOffice " +
129*44cf0280SCarl Marcum 				"from ${SRC_ROOT} and ${INPATH}. Please make sure OpenOffice was built with the --with-package-format=\"installed\" option, " +
130*44cf0280SCarl Marcum 				"and you ran \"source <platform>.set.sh\"");
131*44cf0280SCarl Marcum 		}
132*44cf0280SCarl Marcum 		final File instsetoo_native = new File(srcRoot, "instsetoo_native");
133*44cf0280SCarl Marcum 		final File buildDir = new File(instsetoo_native, inPath);
134*44cf0280SCarl Marcum 		final File apacheOpenOffice = new File(buildDir, "Apache_OpenOffice");
135*44cf0280SCarl Marcum 		final File installed = new File(apacheOpenOffice, "installed");
136*44cf0280SCarl Marcum 		final File install = new File(installed, "install");
137*44cf0280SCarl Marcum 		final File enUs = new File(install, "en-US");
138*44cf0280SCarl Marcum 		// *nix:
139*44cf0280SCarl Marcum 		File openoffice4 = new File(enUs, "openoffice4");
140*44cf0280SCarl Marcum 		if (!openoffice4.exists()) {
141*44cf0280SCarl Marcum 			// Windows:
142*44cf0280SCarl Marcum 			openoffice4 = new File(enUs, "OpenOffice 4");
143*44cf0280SCarl Marcum 		}
144*44cf0280SCarl Marcum 		if (!openoffice4.exists()) {
145*44cf0280SCarl Marcum 			throw new RuntimeException("Neither openoffice.home nor openoffice.pack were specified, and could not find OpenOffice in " + openoffice4.getAbsolutePath());
146*44cf0280SCarl Marcum 		}
147*44cf0280SCarl Marcum 		validateOpenOfficeInstallation(openoffice4);
148*44cf0280SCarl Marcum 		return openoffice4.getAbsolutePath();
149*44cf0280SCarl Marcum 	}
150*44cf0280SCarl Marcum 
validateOpenOfficeInstallation(File directory)151*44cf0280SCarl Marcum 	private static void validateOpenOfficeInstallation(File directory) {
152*44cf0280SCarl Marcum 		final File sofficeBin = FileUtil.findFile(directory, "soffice.bin", false);
153*44cf0280SCarl Marcum 		if (sofficeBin == null) {
154a22b55fdSLiu Zhe 			throw new RuntimeException(MessageFormat.format(
155*44cf0280SCarl Marcum 				"No valid OpenOffice found in {0}!", directory.getAbsolutePath()));
156883eb82fSLiu Zhe 		}
157883eb82fSLiu Zhe 	}
158883eb82fSLiu Zhe 
addToClassPath(File file)159883eb82fSLiu Zhe 	public static boolean addToClassPath(File file) {
160883eb82fSLiu Zhe 		try {
161883eb82fSLiu Zhe 			URL url = file.toURI().toURL();
162883eb82fSLiu Zhe 			URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
163883eb82fSLiu Zhe 			Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
164883eb82fSLiu Zhe 			method.setAccessible(true);
165883eb82fSLiu Zhe 			method.invoke(classLoader, new Object[] { url });
166883eb82fSLiu Zhe 			return true;
167883eb82fSLiu Zhe 		} catch (Throwable t) {
168883eb82fSLiu Zhe 			t.printStackTrace();
169883eb82fSLiu Zhe 		}
170883eb82fSLiu Zhe 
171883eb82fSLiu Zhe 		return false;
1726467ecbfSLiu Zhe 	}
1736467ecbfSLiu Zhe 
main(String... args)1746467ecbfSLiu Zhe 	public static void main(String... args) {
1756467ecbfSLiu Zhe 		new Installer().run();
1766467ecbfSLiu Zhe 	}
1776467ecbfSLiu Zhe }
178