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 package org.openoffice.test.common;
22 
23 import java.io.File;
24 import java.io.IOException;
25 import java.lang.reflect.Method;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.text.MessageFormat;
29 import java.util.logging.Level;
30 
31 /**
32  * Install openoffice from installation package before running test
33  *
34  */
35 public class Installer implements Runnable {
36 	private static Logger log = Logger.getLogger(Installer.class);
37 	File downloadDir = Testspace.getFile("download");
38 	File downloadUrl = Testspace.getFile("download/url");
39 	File installDir = Testspace.getFile("install");
40 	File installTempDir = Testspace.getFile("install_temp");
41 
42 	@Override
run()43 	public void run() {
44 		String prop = System.getProperty("singleton");
45 		if ("true".equalsIgnoreCase(prop) || "yes".equalsIgnoreCase(prop)) {
46 			if (SystemUtil.findProcesses(".*org\\.openoffice\\.test\\.common\\.Installer.*").size() > 1) {
47 				throw new RuntimeException("Only allow one running test instance!");
48 			}
49 		}
50 		if ((prop = System.getProperty("openoffice.pack")) != null) {
51 			String onlyNewProp = System.getProperty("only.new");
52 			File packFile = null;
53 			if (FileUtil.isUrl(prop)) {
54 				log.log(Level.INFO, MessageFormat.format("Try to download {0}...", prop));
55 				String url = FileUtil.readFileAsString(downloadUrl);
56 				if (!prop.equals(url)) {
57 					FileUtil.deleteFile(downloadDir);
58 					downloadDir.mkdirs();
59 					packFile = FileUtil.download(prop, downloadDir);
60 					if (packFile == null)
61 						throw new RuntimeException(MessageFormat.format("{0} can not be downloaded!", prop));
62 					FileUtil.writeStringToFile(downloadUrl, prop);
63 				} else {
64 					boolean[] skipped = { false };
65 					packFile = FileUtil.download(prop, downloadDir, true, skipped);
66 					if (packFile == null)
67 						throw new RuntimeException(MessageFormat.format("{0} can not be downloaded!", prop));
68 					if (("true".equalsIgnoreCase(onlyNewProp) || "yes".equalsIgnoreCase(onlyNewProp)) && skipped[0])
69 						throw new RuntimeException(MessageFormat.format("{0} is old. Test is allowed only on new build.", prop));
70 				}
71 			} else {
72 				packFile = new File(prop);
73 				if (!packFile.isFile())
74 					throw new RuntimeException(MessageFormat.format("{0} does not exists or is not a file!", prop));
75 			}
76 
77 			try {
78 				FileUtil.deleteFile(installDir);
79 				FileUtil.deleteFile(installTempDir);
80 				installTempDir.mkdirs();
81 				if (packFile.getName().endsWith(".gz")) {
82 					StringBuffer output = new StringBuffer();
83 					if (SystemUtil.exec(new String[] { "tar", "-zxpf", packFile.getAbsolutePath(), "-C", installTempDir.getAbsolutePath() }, output) != 0)
84 						throw new RuntimeException(MessageFormat.format("{0} can not be installed! Cause: {1}", packFile, output));
85 				} else {
86 					if (!FileUtil.unzip(packFile, installTempDir))
87 						throw new RuntimeException(MessageFormat.format("{0} can not be installed!", packFile));
88 				}
89 				// On windows, if path is too long, openoffice can not be
90 				// started.
91 				File[] files = installTempDir.listFiles();
92 				if (files != null && files.length == 1 && files[0].isDirectory()) {
93 					files[0].renameTo(installDir);
94 				}
95 				System.setProperty("openoffice.home", installDir.getAbsolutePath());
96 			} finally {
97 				FileUtil.deleteFile(installTempDir);
98 			}
99 		}
100 
101 		prop = System.getProperty("openoffice.home", installDir.getAbsolutePath());
102 		File sofficeBin = FileUtil.findFile(new File(prop), "soffice.bin", false);
103 		if (sofficeBin == null)
104 			throw new RuntimeException(MessageFormat.format(
105 					"No valid OpenOffice is found in {0}! Use system property openoffice.home to specify a valid OpenOffice installation directory.", prop));
106 		try {
107 			prop = sofficeBin.getParentFile().getParentFile().getCanonicalPath();
108 			System.setProperty("openoffice.home", prop);
109 			log.log(Level.INFO, MessageFormat.format("OpenOffice in {0} will be tested.", prop));
110 		} catch (IOException e) {
111 			// ignore, never occurs
112 		}
113 
114 		String[] jars = { "juh.jar", "unoil.jar", "ridl.jar", "jurt.jar" };
115 		for (String jar : jars) {
116 			File file = FileUtil.findFile(prop, jar);
117 
118 			if (file != null)
119 				addToClassPath(file);
120 		}
121 
122 	}
123 
addToClassPath(File file)124 	public static boolean addToClassPath(File file) {
125 		try {
126 			URL url = file.toURI().toURL();
127 			URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
128 			Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
129 			method.setAccessible(true);
130 			method.invoke(classLoader, new Object[] { url });
131 			return true;
132 		} catch (Throwable t) {
133 			t.printStackTrace();
134 		}
135 
136 		return false;
137 	}
138 
main(String... args)139 	public static void main(String... args) {
140 		new Installer().run();
141 	}
142 }
143