Better, Faster, Freer

The LXR Cross Referencer

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Architecture: i386 ]
Version: HEAD ]

001 /*************************************************************************
002  *
003  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004  * 
005  * Copyright 2008 by Sun Microsystems, Inc.
006  *
007  * OpenOffice.org - a multi-platform office productivity suite
008  *
009  * $RCSfile: SystemManager.java,v $
010  * $Revision: 1.7 $
011  *
012  * This file is part of OpenOffice.org.
013  *
014  * OpenOffice.org is free software: you can redistribute it and/or modify
015  * it under the terms of the GNU Lesser General Public License version 3
016  * only, as published by the Free Software Foundation.
017  *
018  * OpenOffice.org is distributed in the hope that it will be useful,
019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021  * GNU Lesser General Public License version 3 for more details
022  * (a copy is included in the LICENSE file that accompanied this code).
023  *
024  * You should have received a copy of the GNU Lesser General Public License
025  * version 3 along with OpenOffice.org.  If not, see
026  * <http://www.openoffice.org/license.html>
027  * for a copy of the LGPLv3 License.
028  *
029  ************************************************************************/
030 
031 package org.openoffice.setup.Util;
032 
033 import java.io.BufferedInputStream;
034 import java.io.BufferedOutputStream;
035 import java.io.BufferedReader;
036 import java.io.File;
037 import java.io.FileInputStream;
038 import java.io.FileNotFoundException;
039 import java.io.FileOutputStream;
040 import java.io.FileWriter;
041 import java.io.IOException;
042 import java.io.InputStreamReader;
043 import java.net.URI;
044 import java.net.URL;
045 import java.util.HashMap;
046 import java.util.Properties;
047 import java.util.Vector;
048 
049 public class SystemManager {
050     
051     private SystemManager() {
052     }
053     
054     /* the installation root is where the classes reside */
055     static public File getJarFilePath() {
056         
057         File jarFile = null;
058 
059         try {
060             Class c  = Class.forName("org.openoffice.setup.ResourceManager");
061             URL url  = c.getResource("setupfiles.properties");
062 
063             String urlString = url.toString();
064 
065             if (urlString.startsWith("jar:")) {
066                 /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
067                 urlString = urlString.substring(4, urlString.lastIndexOf("!"));
068                 jarFile = new File(new URI(urlString));
069             }
070 
071         } catch (Exception ex) {
072             /* handle URISyntaxException and ClassNotFoundException */
073             ex.printStackTrace();
074             System.exit(1);
075         }
076 
077         if ( jarFile != null ) {
078             System.err.println("Jar file: " + jarFile.getPath());
079         } else {
080             System.err.println("No jar file used for installation!");            
081         }
082 
083         return jarFile;
084     }
085 
086     /* the installation root is where the classes reside */
087     static public File getResourceRoot() {
088         
089         File dir = null;
090 
091         try {
092             Class c  = Class.forName("org.openoffice.setup.ResourceManager");
093             URL url  = c.getResource("setupfiles.properties");
094 
095             String urlString = url.toString();
096 
097             if (urlString.startsWith("jar:")) {
098                 /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
099                 urlString = urlString.substring(4, urlString.lastIndexOf("!"));
100             } else {
101                 /* ResourceManager.class resides in a directory tree. */
102                 urlString = urlString.substring(0, urlString.lastIndexOf("/org/openoffice/setup/setupfiles.properties"));
103             }
104 
105             dir = new File(new URI(urlString));
106             dir = dir.getParentFile();
107 
108         } catch (Exception ex) {
109             /* handle URISyntaxException and ClassNotFoundException */
110             ex.printStackTrace();
111             System.exit(1);
112         }
113         // }
114 
115         if ( dir != null ) {
116             // System.err.println("Resource Root: " + dir.getPath());
117         } else {
118             System.err.println("No resource root found!");            
119         }
120        
121         return dir;
122     }
123     
124     static public String getPackagePath(String subdir) {
125         
126         String path = null;
127 
128         File dir = getResourceRoot();
129         if (dir != null) {
130             // System.err.println("Resource root: " + dir.getPath());
131             dir = new File(dir, subdir);
132             if (! dir.exists()) {
133                 System.err.println("Error: Directory \"" + subdir + "\" does not exist at resouce root");
134             } else {
135                 path = dir.getPath();
136             }
137         }
138         
139         if ( path != null ) {
140             if ( ! path.endsWith("/")) {
141                 path = path + "/";
142             }
143         }
144 
145         if ( path != null ) {
146             System.err.println("Path to packages: " + path);
147         } else {
148             System.err.println("No path to packages found!");            
149         }
150         
151         return path;
152     }
153 
154     static public boolean find_file(String fileName) {
155         boolean found = false;
156         File file = new File(fileName);
157         found = file.exists();
158         return found;
159     }
160     
161     static public boolean exists_directory(String directory) {
162         File dir = new File(directory);
163         return dir.exists();
164     }
165 
166     static public boolean create_directory(String directory) throws SecurityException {
167         boolean created = false;
168         File dir = new File(directory);
169         try {
170             created = dir.mkdirs();
171         }
172         catch (SecurityException ex) {
173             throw ex;
174         } 
175                 
176         return created;
177     }
178 
179     static public String getParentDirectory(String dir) { 
180         File installFile = new File(dir);
181         String parentDir = installFile.getParent();
182         if ( parentDir == null ) {
183             parentDir = "/";
184         }
185         return parentDir;
186     }
187 
188     static public String getInstallationPrivileges() {
189         
190         String type = "";
191         String user = java.lang.System.getProperty("user.name");
192         // System.out.println("UserHome: " + java.lang.System.getProperty("user.home"));
193         
194         if ( user.equalsIgnoreCase("root")) {
195             type = "root";
196             System.err.println("Root privileges");
197         } else {
198             type = "user";
199             System.err.println("User privileges");
200         }
201         
202         return type;
203     }
204 
205     static public boolean isUserInstallation() {
206         
207         boolean isUserInstallation = false;
208         String user = java.lang.System.getProperty("user.name");
209         
210         if ( user.equalsIgnoreCase("root")) {
211             isUserInstallation = false;
212             System.err.println("Root privileges");
213         } else {
214             isUserInstallation = true;
215             System.err.println("User privileges");
216         }
217         
218         return isUserInstallation;
219     }
220 
221     static public boolean isRootInstallation() {
222         
223         boolean isRootInstallation = false;
224         String user = java.lang.System.getProperty("user.name");
225         
226         if ( user.equalsIgnoreCase("root")) {
227             isRootInstallation = true;
228         } else {
229             isRootInstallation = false;
230         }
231         
232         return isRootInstallation;
233     }
234 
235     static public String getOSType() {
236         String osVersion = java.lang.System.getProperty("os.name");
237         System.err.println("OS: " + osVersion);
238         return osVersion;
239     }
240     
241     static public String getOSArchitecture() {
242         String osArchitecture = java.lang.System.getProperty("os.arch");
243         System.out.println("OSArchitecture: " + osArchitecture);
244         return osArchitecture;
245     }
246 
247     static public String getOSVersion() {
248         String osVersion = java.lang.System.getProperty("os.version");
249         System.out.println("OSVersion: " + osVersion);
250         return osVersion;
251     }
252 
253     static public HashMap getEnvironmentHashMap() {
254         // readonly map from getenv()
255         // System.getenv only supported in Java 1.5, properties have to be set in shell script
256         // Map map = java.lang.System.getenv();
257         // HashMap myMap = new HashMap(map);
258         Properties props = System.getProperties();
259         HashMap myMap = new HashMap(props);
260         return myMap;
261     }
262     
263     static public void dumpStringArray(String[] myStringArray) {
264         for (int i = 0; i < myStringArray.length; i++) {
265             System.out.println(myStringArray[i]);
266         }
267     }
268 
269     static public void dumpFile(String baseFileName, String dumpFileName) {
270         Vector fileContent = readCharFileVector(baseFileName);
271         saveCharFileVector(dumpFileName, fileContent);
272     }
273     
274     static public Vector readCharFileVector(String fileName) {
275         Vector fileContent = new Vector();
276 
277         File file = new File(fileName);
278         if ( file.exists()) {
279             try {
280                 FileInputStream fs = new FileInputStream(file);
281                 BufferedReader bs = new BufferedReader(new InputStreamReader(fs));
282                 String zeile;
283                 while((zeile = bs.readLine())!=null) {
284                     fileContent.addElement(zeile);
285                 }
286             }
287             catch (IOException e) {
288                 System.out.println(e);
289             }
290         } else {
291             System.out.println( "Error: File not found: " +  fileName);
292         }
293 
294         return fileContent;
295     }
296     
297     
298     static public void saveCharFileVector(String fileName, Vector fileContent) {
299         FileWriter fw = null;
300         try
301         {
302             fw = new FileWriter(fileName);
303             String fileContentStr = "";
304             for (int i = 0; i < fileContent.size() ; i++) {
305                 fileContentStr = fileContentStr + fileContent.get(i) + "\n";
306                 // System.out.println(fileContent.get(i));
307             }
308             fw.write(fileContentStr);
309         }
310         catch ( IOException e ) {
311             System.out.println( "Could not create file: " +  fileName);
312         }
313         finally {
314             try {
315                 if ( fw != null ) fw.close();
316             } catch (IOException e) {}
317         }        
318     }
319 
320     static public void copyAllFiles(File source, File dest) {
321         File[] file = source.listFiles();
322         if (file != null) {
323             for (int i = 0; i < file.length; i++) {
324                 copy(file[i].getPath(), dest.getPath());
325             }
326         }
327     }
328     
329     static public void copyAllFiles(File source, File dest, String ext) {
330         File[] file = source.listFiles(new FileExtensionFilter(ext));
331         if (file != null) {
332             for (int i = 0; i < file.length; i++) {
333                 copy(file[i].getPath(), dest.getPath());
334             }
335         }
336     }
337 
338     // second parameter can be a complete file name or an existing directory
339     static public boolean copy(String source, String dest) {
340         
341         // is the second parameter a file name or a directory?
342         File dir = new File(dest);
343         if ( dir.isDirectory() ) {
344             File sourceFile = new File(source);
345             String fileName = sourceFile.getName();
346             File destFile = new File(dest, fileName);
347             dest = destFile.getPath();
348         }
349         
350         boolean file_copied = false;
351         FileInputStream fis;
352         BufferedInputStream bis;
353         FileOutputStream fos;
354         BufferedOutputStream bos;
355         byte[] b;
356         try {
357             fis = new FileInputStream(source);
358             fos = new FileOutputStream(dest);
359         } catch (FileNotFoundException ex) {
360             throw new Error("File not found");
361         }
362         // put file into buffer
363         bis = new BufferedInputStream(fis);
364         bos = new BufferedOutputStream(fos);
365         try { // read file, write and close
366             b = new byte[bis.available()];
367             bis.read(b);
368             bos.write(b);
369             bis.close();
370             bos.close();
371             file_copied = true;
372         } catch (IOException e) {
373             System.out.println("Dateien wurden nicht kopiert!");
374         }
375         
376         return file_copied;
377     }
378 
379     static public boolean deleteFile(File file) {
380         boolean success = false;
381         if ( file.exists() && file != null ) {
382             success = file.delete();
383         }        
384         return success;
385     }
386 
387     static public boolean createDirectory(File dir) throws SecurityException {
388         boolean created = false;
389         try {
390             created = dir.mkdirs();
391         }
392         catch (SecurityException ex) {
393             throw ex;
394         } 
395                 
396         return created;
397     }
398 
399     static public void removeDirectory(File dir) {
400         if ( dir.exists() && dir.isDirectory() ) {
401             File[] file = dir.listFiles();
402             if (file != null) {
403                 for (int i = 0; i < file.length; i++) {                
404                     deleteFile(file[i]);
405                 }
406             }
407             dir.delete();
408         }
409     }
410 
411     static public boolean logModuleStates() {
412         boolean logStates = false;
413         // System.getenv only supported in Java 1.5, property set in shell script
414         // String logStatesEnv = System.getenv("LOG_MODULE_STATES");
415         String logStatesEnv = System.getProperty("LOG_MODULE_STATES");
416 
417         if ( logStatesEnv != null ) {
418             logStates = true;
419         }   
420              
421         return logStates;
422     }
423 
424     static public void setUnixPrivileges(String fileName, String unixRights) {
425         // String command = "chmod " + unixRights + " " + fileName;
426         String[] commandArray = new String[3];
427         commandArray[0] = "chmod";
428         commandArray[1] = unixRights;
429         commandArray[2] = fileName;
430         int value = ExecuteProcess.executeProcessReturnValue(commandArray);
431     }
432 
433     static public void setUnixPrivilegesDirectory(File directoryName, String ext, String unixRights) {
434         File[] file = directoryName.listFiles(new FileExtensionFilter(ext));
435         if (file != null) {
436             for (int i = 0; i < file.length; i++) {
437                 setUnixPrivileges(file[i].getPath(), unixRights);
438             }
439         }
440     }
441     
442     static public int calculateDiscSpace(String directory) {
443         String command = "df -k " + directory;
444         String[] commandArray = new String[3];
445         commandArray[0] = "df";
446         commandArray[1] = "-k";
447         commandArray[2] = directory;
448 
449         int size = 0;
450         Vector returnVector = new Vector();
451         Vector returnErrorVector = new Vector();
452         int returnValue = ExecuteProcess.executeProcessReturnVector(commandArray, returnVector, returnErrorVector);
453         if ( returnValue == 0) {
454             int max = returnVector.size();
455             if ( max > 0 ) {
456                 String returnLine = (String) returnVector.get(max-1);
457 
458                 // The fourth value is the available disc space (if the first value is a path)
459                 // Otherwise it can also be the third value, if the first is not a path.
460                 // If the first value is not a path, the string starts with white spaces.
461                 
462                 int position = 3;
463                 if ( returnLine.startsWith(" ")) {
464                     position = 2;
465                 }
466                 
467                 returnLine = returnLine.trim();
468                 String[] returnArray = returnLine.split("\\s+");
469 
470                 if ( returnArray.length > 3 ) {
471                     String sizeString = returnArray[position];
472                     
473                     // Special handling for very large hard discs that cannot be converted to int
474                     if ( sizeString.length() >= Integer.toString(Integer.MAX_VALUE).length() ) {
475                         sizeString = Integer.toString(Integer.MAX_VALUE);
476                     }
477 
478                     // Converting from String to int
479                     size = Integer.parseInt(sizeString);
480                 }
481             }
482         }
483 
484         return size;
485     }
486     
487 }

source navigation ] diff markup ] identifier search ] general search ]

This page was automatically generated by the LXR engine.
The LXR team
Valid HTML 4.01!