xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/Util/SystemManager.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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.BufferedInputStream;
31 import java.io.BufferedOutputStream;
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.io.FileWriter;
38 import java.io.IOException;
39 import java.io.InputStreamReader;
40 import java.net.URI;
41 import java.net.URL;
42 import java.util.HashMap;
43 import java.util.Properties;
44 import java.util.Vector;
45 
46 public class SystemManager {
47 
48     private SystemManager() {
49     }
50 
51     /* the installation root is where the classes reside */
52     static public File getJarFilePath() {
53 
54         File jarFile = null;
55 
56         try {
57             Class c  = Class.forName("org.openoffice.setup.ResourceManager");
58             URL url  = c.getResource("setupfiles.properties");
59 
60             String urlString = url.toString();
61 
62             if (urlString.startsWith("jar:")) {
63                 /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
64                 urlString = urlString.substring(4, urlString.lastIndexOf("!"));
65                 jarFile = new File(new URI(urlString));
66             }
67 
68         } catch (Exception ex) {
69             /* handle URISyntaxException and ClassNotFoundException */
70             ex.printStackTrace();
71             System.exit(1);
72         }
73 
74         if ( jarFile != null ) {
75             System.err.println("Jar file: " + jarFile.getPath());
76         } else {
77             System.err.println("No jar file used for installation!");
78         }
79 
80         return jarFile;
81     }
82 
83     /* the installation root is where the classes reside */
84     static public File getResourceRoot() {
85 
86         File dir = null;
87 
88         try {
89             Class c  = Class.forName("org.openoffice.setup.ResourceManager");
90             URL url  = c.getResource("setupfiles.properties");
91 
92             String urlString = url.toString();
93 
94             if (urlString.startsWith("jar:")) {
95                 /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
96                 urlString = urlString.substring(4, urlString.lastIndexOf("!"));
97             } else {
98                 /* ResourceManager.class resides in a directory tree. */
99                 urlString = urlString.substring(0, urlString.lastIndexOf("/org/openoffice/setup/setupfiles.properties"));
100             }
101 
102             dir = new File(new URI(urlString));
103             dir = dir.getParentFile();
104 
105         } catch (Exception ex) {
106             /* handle URISyntaxException and ClassNotFoundException */
107             ex.printStackTrace();
108             System.exit(1);
109         }
110         // }
111 
112         if ( dir != null ) {
113             // System.err.println("Resource Root: " + dir.getPath());
114         } else {
115             System.err.println("No resource root found!");
116         }
117 
118         return dir;
119     }
120 
121     static public String getPackagePath(String subdir) {
122 
123         String path = null;
124 
125         File dir = getResourceRoot();
126         if (dir != null) {
127             // System.err.println("Resource root: " + dir.getPath());
128             dir = new File(dir, subdir);
129             if (! dir.exists()) {
130                 System.err.println("Error: Directory \"" + subdir + "\" does not exist at resouce root");
131             } else {
132                 path = dir.getPath();
133             }
134         }
135 
136         if ( path != null ) {
137             if ( ! path.endsWith("/")) {
138                 path = path + "/";
139             }
140         }
141 
142         if ( path != null ) {
143             System.err.println("Path to packages: " + path);
144         } else {
145             System.err.println("No path to packages found!");
146         }
147 
148         return path;
149     }
150 
151     static public boolean find_file(String fileName) {
152         boolean found = false;
153         File file = new File(fileName);
154         found = file.exists();
155         return found;
156     }
157 
158     static public boolean exists_directory(String directory) {
159         File dir = new File(directory);
160         return dir.exists();
161     }
162 
163     static public boolean create_directory(String directory) throws SecurityException {
164         boolean created = false;
165         File dir = new File(directory);
166         try {
167             created = dir.mkdirs();
168         }
169         catch (SecurityException ex) {
170             throw ex;
171         }
172 
173         return created;
174     }
175 
176     static public String getParentDirectory(String dir) {
177         File installFile = new File(dir);
178         String parentDir = installFile.getParent();
179         if ( parentDir == null ) {
180             parentDir = "/";
181         }
182         return parentDir;
183     }
184 
185     static public String getInstallationPrivileges() {
186 
187         String type = "";
188         String user = java.lang.System.getProperty("user.name");
189         // System.out.println("UserHome: " + java.lang.System.getProperty("user.home"));
190 
191         if ( user.equalsIgnoreCase("root")) {
192             type = "root";
193             System.err.println("Root privileges");
194         } else {
195             type = "user";
196             System.err.println("User privileges");
197         }
198 
199         return type;
200     }
201 
202     static public boolean isUserInstallation() {
203 
204         boolean isUserInstallation = false;
205         String user = java.lang.System.getProperty("user.name");
206 
207         if ( user.equalsIgnoreCase("root")) {
208             isUserInstallation = false;
209             System.err.println("Root privileges");
210         } else {
211             isUserInstallation = true;
212             System.err.println("User privileges");
213         }
214 
215         return isUserInstallation;
216     }
217 
218     static public boolean isRootInstallation() {
219 
220         boolean isRootInstallation = false;
221         String user = java.lang.System.getProperty("user.name");
222 
223         if ( user.equalsIgnoreCase("root")) {
224             isRootInstallation = true;
225         } else {
226             isRootInstallation = false;
227         }
228 
229         return isRootInstallation;
230     }
231 
232     static public String getOSType() {
233         String osVersion = java.lang.System.getProperty("os.name");
234         System.err.println("OS: " + osVersion);
235         return osVersion;
236     }
237 
238     static public String getOSArchitecture() {
239         String osArchitecture = java.lang.System.getProperty("os.arch");
240         System.out.println("OSArchitecture: " + osArchitecture);
241         return osArchitecture;
242     }
243 
244     static public String getOSVersion() {
245         String osVersion = java.lang.System.getProperty("os.version");
246         System.out.println("OSVersion: " + osVersion);
247         return osVersion;
248     }
249 
250     static public HashMap getEnvironmentHashMap() {
251         // readonly map from getenv()
252         // System.getenv only supported in Java 1.5, properties have to be set in shell script
253         // Map map = java.lang.System.getenv();
254         // HashMap myMap = new HashMap(map);
255         Properties props = System.getProperties();
256         HashMap myMap = new HashMap(props);
257         return myMap;
258     }
259 
260     static public void dumpStringArray(String[] myStringArray) {
261         for (int i = 0; i < myStringArray.length; i++) {
262             System.out.println(myStringArray[i]);
263         }
264     }
265 
266     static public void dumpFile(String baseFileName, String dumpFileName) {
267         Vector fileContent = readCharFileVector(baseFileName);
268         saveCharFileVector(dumpFileName, fileContent);
269     }
270 
271     static public Vector readCharFileVector(String fileName) {
272         Vector fileContent = new Vector();
273 
274         File file = new File(fileName);
275         if ( file.exists()) {
276             try {
277                 FileInputStream fs = new FileInputStream(file);
278                 BufferedReader bs = new BufferedReader(new InputStreamReader(fs));
279                 String zeile;
280                 while((zeile = bs.readLine())!=null) {
281                     fileContent.addElement(zeile);
282                 }
283             }
284             catch (IOException e) {
285                 System.out.println(e);
286             }
287         } else {
288             System.out.println( "Error: File not found: " +  fileName);
289         }
290 
291         return fileContent;
292     }
293 
294 
295     static public void saveCharFileVector(String fileName, Vector fileContent) {
296         FileWriter fw = null;
297         try
298         {
299             fw = new FileWriter(fileName);
300             String fileContentStr = "";
301             for (int i = 0; i < fileContent.size() ; i++) {
302                 fileContentStr = fileContentStr + fileContent.get(i) + "\n";
303                 // System.out.println(fileContent.get(i));
304             }
305             fw.write(fileContentStr);
306         }
307         catch ( IOException e ) {
308             System.out.println( "Could not create file: " +  fileName);
309         }
310         finally {
311             try {
312                 if ( fw != null ) fw.close();
313             } catch (IOException e) {}
314         }
315     }
316 
317     static public void copyAllFiles(File source, File dest) {
318         File[] file = source.listFiles();
319         if (file != null) {
320             for (int i = 0; i < file.length; i++) {
321                 copy(file[i].getPath(), dest.getPath());
322             }
323         }
324     }
325 
326     static public void copyAllFiles(File source, File dest, String ext) {
327         File[] file = source.listFiles(new FileExtensionFilter(ext));
328         if (file != null) {
329             for (int i = 0; i < file.length; i++) {
330                 copy(file[i].getPath(), dest.getPath());
331             }
332         }
333     }
334 
335     // second parameter can be a complete file name or an existing directory
336     static public boolean copy(String source, String dest) {
337 
338         // is the second parameter a file name or a directory?
339         File dir = new File(dest);
340         if ( dir.isDirectory() ) {
341             File sourceFile = new File(source);
342             String fileName = sourceFile.getName();
343             File destFile = new File(dest, fileName);
344             dest = destFile.getPath();
345         }
346 
347         boolean file_copied = false;
348         FileInputStream fis;
349         BufferedInputStream bis;
350         FileOutputStream fos;
351         BufferedOutputStream bos;
352         byte[] b;
353         try {
354             fis = new FileInputStream(source);
355             fos = new FileOutputStream(dest);
356         } catch (FileNotFoundException ex) {
357             throw new Error("File not found");
358         }
359         // put file into buffer
360         bis = new BufferedInputStream(fis);
361         bos = new BufferedOutputStream(fos);
362         try { // read file, write and close
363             b = new byte[bis.available()];
364             bis.read(b);
365             bos.write(b);
366             bis.close();
367             bos.close();
368             file_copied = true;
369         } catch (IOException e) {
370             System.out.println("Dateien wurden nicht kopiert!");
371         }
372 
373         return file_copied;
374     }
375 
376     static public boolean deleteFile(File file) {
377         boolean success = false;
378         if ( file.exists() && file != null ) {
379             success = file.delete();
380         }
381         return success;
382     }
383 
384     static public boolean createDirectory(File dir) throws SecurityException {
385         boolean created = false;
386         try {
387             created = dir.mkdirs();
388         }
389         catch (SecurityException ex) {
390             throw ex;
391         }
392 
393         return created;
394     }
395 
396     static public void removeDirectory(File dir) {
397         if ( dir.exists() && dir.isDirectory() ) {
398             File[] file = dir.listFiles();
399             if (file != null) {
400                 for (int i = 0; i < file.length; i++) {
401                     deleteFile(file[i]);
402                 }
403             }
404             dir.delete();
405         }
406     }
407 
408     static public boolean logModuleStates() {
409         boolean logStates = false;
410         // System.getenv only supported in Java 1.5, property set in shell script
411         // String logStatesEnv = System.getenv("LOG_MODULE_STATES");
412         String logStatesEnv = System.getProperty("LOG_MODULE_STATES");
413 
414         if ( logStatesEnv != null ) {
415             logStates = true;
416         }
417 
418         return logStates;
419     }
420 
421     static public void setUnixPrivileges(String fileName, String unixRights) {
422         // String command = "chmod " + unixRights + " " + fileName;
423         String[] commandArray = new String[3];
424         commandArray[0] = "chmod";
425         commandArray[1] = unixRights;
426         commandArray[2] = fileName;
427         int value = ExecuteProcess.executeProcessReturnValue(commandArray);
428     }
429 
430     static public void setUnixPrivilegesDirectory(File directoryName, String ext, String unixRights) {
431         File[] file = directoryName.listFiles(new FileExtensionFilter(ext));
432         if (file != null) {
433             for (int i = 0; i < file.length; i++) {
434                 setUnixPrivileges(file[i].getPath(), unixRights);
435             }
436         }
437     }
438 
439     static public int calculateDiscSpace(String directory) {
440         String command = "df -k " + directory;
441         String[] commandArray = new String[3];
442         commandArray[0] = "df";
443         commandArray[1] = "-k";
444         commandArray[2] = directory;
445 
446         int size = 0;
447         Vector returnVector = new Vector();
448         Vector returnErrorVector = new Vector();
449         int returnValue = ExecuteProcess.executeProcessReturnVector(commandArray, returnVector, returnErrorVector);
450         if ( returnValue == 0) {
451             int max = returnVector.size();
452             if ( max > 0 ) {
453                 String returnLine = (String) returnVector.get(max-1);
454 
455                 // The fourth value is the available disc space (if the first value is a path)
456                 // Otherwise it can also be the third value, if the first is not a path.
457                 // If the first value is not a path, the string starts with white spaces.
458 
459                 int position = 3;
460                 if ( returnLine.startsWith(" ")) {
461                     position = 2;
462                 }
463 
464                 returnLine = returnLine.trim();
465                 String[] returnArray = returnLine.split("\\s+");
466 
467                 if ( returnArray.length > 3 ) {
468                     String sizeString = returnArray[position];
469 
470                     // Special handling for very large hard discs that cannot be converted to int
471                     if ( sizeString.length() >= Integer.toString(Integer.MAX_VALUE).length() ) {
472                     	sizeString = Integer.toString(Integer.MAX_VALUE);
473                     }
474 
475                     // Converting from String to int
476                     size = Integer.parseInt(sizeString);
477                 }
478             }
479         }
480 
481         return size;
482     }
483 
484 }
485