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