xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/Util/Controller.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.setup.Util;
29 
30 import java.io.File;
31 import java.util.Vector;
32 import org.openoffice.setup.InstallData;
33 import org.openoffice.setup.ResourceManager;
34 import org.openoffice.setup.SetupData.SetupDataProvider;
35 import org.openoffice.setup.Util.LogManager;
36 
37 public class Controller {
38 
39     private Controller() {
40     }
41 
42     static public void checkPackagePathExistence(InstallData installData) {
43         String packagePath = installData.getPackagePath();
44         if (( packagePath == null ) || ( packagePath.equals(""))) {
45             String message = ResourceManager.getString("String_InstallationOngoing_PackagePath_Not_Found");
46             String title = ResourceManager.getString("String_Error");
47             Informer.showErrorMessage(message, title);
48             System.exit(1);
49         }
50     }
51 
52     static public void checkPackageFormat(InstallData installData) {
53         String packageFormat = installData.getPackageFormat();
54         String os = installData.getOSType();
55 
56         boolean notSupportedPackageFormat = true;
57 
58         // Show warnings for currently not supported combinations of OS and package format.
59         // This has to be adapted if further OS or package formats are supported.
60 
61         if (( os.equalsIgnoreCase("SunOS") ) && ( packageFormat.equalsIgnoreCase("pkg") )) {
62             notSupportedPackageFormat = false;
63         }
64 
65         if (( os.equalsIgnoreCase("Linux") ) && ( packageFormat.equalsIgnoreCase("rpm") )) {
66             notSupportedPackageFormat = false;
67         }
68 
69         // Inform user about not supported package format and exit program
70 
71         if ( notSupportedPackageFormat ) {
72             System.err.println("Error: Package format not supported by this OS!");
73             String mainmessage = ResourceManager.getString("String_Packageformat_Not_Supported");
74             String osstring = ResourceManager.getString("String_Operating_System");
75             String formatstring = ResourceManager.getString("String_Packageformat");
76             String message = mainmessage + "\n" + osstring + ": " + os + "\n" + formatstring + ": " + packageFormat;
77             String title = ResourceManager.getString("String_Error");
78             Informer.showErrorMessage(message, title);
79             System.exit(1);
80         }
81     }
82 
83     static public void collectSystemLanguages(InstallData installData) {
84         String pkgCommand = "";
85         String[] pkgCommandArray;
86         String adminFileName = "";
87         String log = "";
88         Vector returnVector = new Vector();
89         Vector returnErrorVector = new Vector();
90         int returnValue;
91 
92         pkgCommand = "locale -a";
93         pkgCommandArray = new String[2];
94         pkgCommandArray[0] = "locale";
95         pkgCommandArray[1] = "-a";
96         returnValue = ExecuteProcess.executeProcessReturnVector(pkgCommandArray, returnVector, returnErrorVector);
97 
98         if ( returnValue == 0 ) {
99             log = pkgCommand + "<br><b>Returns: " + returnValue + " Successful command</b><br>";
100             LogManager.addCommandsLogfileComment(log);
101 
102             // System.err.println("Available languages 1: ");
103             // for (int i = 0; i < returnVector.size(); i++) {
104             //     System.err.println(returnVector.get(i));
105             // }
106 
107             // Collecting "en-US" instead of "en-US.UTF8"
108             Vector realVector = new Vector();
109 
110             for (int i = 0; i < returnVector.size(); i++) {
111                 String oneLang = (String)returnVector.get(i);
112                 int position = oneLang.indexOf(".");
113                 if ( position > -1 ) {
114                     oneLang = oneLang.substring(0, position);
115                 }
116                 if ( ! realVector.contains(oneLang)) {
117                     realVector.add(oneLang);
118                 }
119             }
120 
121             // System.err.println("Available languages 2: ");
122             // for (int i = 0; i < realVector.size(); i++) {
123             //     System.err.println(realVector.get(i));
124             // }
125 
126             installData.setSystemLanguages(realVector);
127         } else {    // an error occured
128             log = pkgCommand + "<br><b>Returns: " + returnValue + " An error occured</b><br>";
129             LogManager.addCommandsLogfileComment(log);
130             System.err.println("Error in command: " + pkgCommand);
131             for (int i = 0; i < returnErrorVector.size(); i++) {
132                 LogManager.addCommandsLogfileComment((String)returnErrorVector.get(i));
133                 System.err.println(returnErrorVector.get(i));
134             }
135         }
136     }
137 
138     static public boolean createdSubDirectory(String dir) {
139         boolean createdDirectory = false;
140         boolean errorShown = false;
141         String subDirName = "testdir";
142         File testDir = new File(dir, subDirName);
143         try {
144             createdDirectory = SystemManager.create_directory(testDir.getPath());
145         }
146         catch (SecurityException ex) {
147             String message = ResourceManager.getString("String_ChooseDirectory_No_Write_Access") + ": " + dir;
148             String title = ResourceManager.getString("String_Error");
149             Informer.showErrorMessage(message, title);
150             errorShown = true;
151         }
152 
153         if (( ! createdDirectory ) && ( ! errorShown )) {
154             String message = ResourceManager.getString("String_ChooseDirectory_No_Write_Access") + ": " + dir;
155             String title = ResourceManager.getString("String_Error");
156             Informer.showErrorMessage(message, title);
157             errorShown = true;
158         }
159 
160         if ( SystemManager.exists_directory(testDir.getPath()) ) {
161             testDir.delete();
162         }
163 
164         return createdDirectory;
165     }
166 
167     static public boolean createdDirectory(String dir) {
168         boolean createdDirectory = false;
169         try {
170             createdDirectory = SystemManager.create_directory(dir);
171         }
172         catch (SecurityException ex) {
173             // message = ResourceManager.getString("String_ChooseDirectory_Not_Allowed") + ": " + dir;
174             // title = ResourceManager.getString("String_Error");
175             // Informer.showErrorMessage(message, title);
176         }
177 
178         if ( ! createdDirectory ) {
179             String message = ResourceManager.getString("String_ChooseDirectory_No_Success") + ": " + dir;
180             String title = ResourceManager.getString("String_Error");
181             Informer.showErrorMessage(message, title);
182         }
183 
184         return createdDirectory;
185     }
186 
187     static public boolean reducedRootWritePrivileges() {
188         Vector vec = new Vector();
189         File dir = new File("/usr");
190         vec.add(dir);
191         dir = new File("/etc");
192         vec.add(dir);
193 
194         boolean restrictedWritePrivilges = false;
195 
196         // Check for zones. If "zonename" is successful and the name is not "global",
197         // this is a "sparse zone".
198         // Alternative: Simply always check, if root has write access in selected directories.
199 
200         for (int i = 0; i < vec.size(); i++) {
201             File directory = (File)vec.get(i);
202             if ( directory.exists() ) {
203                 // do we have write privileges inside the directory
204                 String tempDirName = "temptestdir";
205                 File tempDir = new File(directory, tempDirName);
206 
207                 if ( SystemManager.createDirectory(tempDir) ) {
208                     SystemManager.removeDirectory(tempDir);
209                 } else {
210                     restrictedWritePrivilges = true;
211                     System.err.println("Restricted Root privileges. No write access in " + directory.getPath());
212                     break;
213                 }
214             }
215         }
216 
217         return restrictedWritePrivilges;
218     }
219 
220     static public void checkForNewerVersion(InstallData installData) {
221         LogManager.setCommandsHeaderLine("Checking change installation");
222         InstallChangeCtrl.checkInstallChange(installData);
223 
224         if ( installData.newerVersionExists() ) {
225             // Inform user about a newer version installed
226             SetupDataProvider.setNewMacro("DIR", installData.getInstallDefaultDir()); // important for string replacement
227 
228             System.err.println("Error: A newer version is already installed in " + installData.getInstallDefaultDir() + " !");
229             String message1 = ResourceManager.getString("String_Newer_Version_Installed_Found")
230                             + "\n" + installData.getInstallDefaultDir() + "\n";
231             String message2 = ResourceManager.getString("String_Newer_Version_Installed_Remove");
232             String message = message1 + "\n" + message2;
233             String title = ResourceManager.getString("String_Error");
234             Informer.showErrorMessage(message, title);
235             System.exit(1);
236         }
237     }
238 
239     static public void checkForUidFile(InstallData installData) {
240         // check existence of getuid.so
241         File getuidFile = Controller.findUidFile(installData);
242 
243         if (( getuidFile == null ) || (! getuidFile.exists()) ) {
244             // Root privileges required -> abort installation
245             System.err.println("Root privileges required for installation!");
246             String message = ResourceManager.getString("String_Root_Privileges_Required_1") + "\n"
247                            + ResourceManager.getString("String_Root_Privileges_Required_2");
248             String title = ResourceManager.getString("String_Error");
249             Informer.showErrorMessage(message, title);
250             System.exit(1);
251         } else {
252             installData.setGetUidPath(getuidFile.getPath());
253         }
254     }
255 
256     static private File findUidFile(InstallData data) {
257 
258         File getuidFile = null;
259 
260         if ( data.isInstallationMode()) {
261             String getuidpath = System.getProperty("GETUID_PATH");
262 
263             if ( getuidpath != null ) {
264                 getuidFile = new File(getuidpath);
265 
266                 if (( getuidFile.isDirectory() ) && ( ! getuidFile.isFile() )) {
267                     // Testing, if GETUID_PATH only contains the path, not the filename
268                     String defaultfilename = "getuid.so";
269                     getuidFile = new File(getuidpath, defaultfilename);
270 
271                     if ( ! getuidFile.exists() ) {
272                         getuidFile = null;
273                     }
274                 }
275             }
276 
277             // File resourceRoot = data.getResourceRoot();
278             // String getuidString = "getuid.so";
279             // if ( resourceRoot != null ) {
280             //     File getuidDir = new File (data.getInfoRoot(), "getuid");
281             //     getuidFile = new File(getuidDir, getuidString);
282             // }
283 
284         } else {
285             getuidFile = new File(data.getGetUidPath());
286         }
287 
288         return getuidFile;
289     }
290 
291 }
292