1 18 package com.izforge.izpack.util.os.unix; 19 20 import com.izforge.izpack.util.FileExecutor; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileReader ; 26 import java.io.FileWriter ; 27 import java.io.StringReader ; 28 29 import java.util.ArrayList ; 30 31 37 public class UnixHelper 38 { 39 40 42 43 public static String whichCommand = FileExecutor.getExecOutput( 44 new String [] { "/usr/bin/env", "which", "which"}, false).trim(); 45 46 public final static String VERSION = "$Revision: 1708 $"; 47 48 50 55 public static ArrayList getEtcPasswdArray() 56 { 57 ArrayList result = new ArrayList (); 58 59 String line = ""; 60 BufferedReader reader = null; 61 62 try 63 { 64 reader = new BufferedReader (new FileReader (UnixConstants.etcPasswd)); 65 66 while ((line = reader.readLine()) != null) 67 { 68 result.add(line); 69 } 70 } 71 catch (Exception e) 72 { 73 } 75 76 return result; 77 } 78 79 84 public static ArrayList getYpPasswdArray() 85 { 86 ArrayList result = new ArrayList (); 87 88 String line = ""; 89 BufferedReader reader = null; 90 91 try 92 { 93 reader = new BufferedReader (new StringReader (FileExecutor.getExecOutput(new String [] { 94 getYpCatCommand(), "passwd"}))); 95 96 while ((line = reader.readLine()) != null) 97 { 98 result.add(line); 99 } 100 } 101 catch (Exception e) 102 { 103 } 105 106 return result; 107 } 108 109 116 public static boolean kdeIsInstalled() 117 { 118 FileExecutor fe = new FileExecutor(); 119 120 String [] execOut = new String [2]; 121 122 int execResult = fe.executeCommand( 123 new String [] { "/usr/bin/env", "konqueror", "--version"}, execOut); 124 125 return execResult == 0; 126 } 127 128 134 public static String getWhichCommand() 135 { 136 return whichCommand; 137 } 138 139 145 public static String getCpCommand() 146 { 147 return FileExecutor.getExecOutput(new String [] { getWhichCommand(), "cp"}).trim(); 148 } 149 150 156 public static String getSuCommand() 157 { 158 return FileExecutor.getExecOutput(new String [] { getWhichCommand(), "su"}).trim(); 159 } 160 161 167 public static String getRmCommand() 168 { 169 return FileExecutor.getExecOutput(new String [] { whichCommand, "rm"}).trim(); 170 } 171 172 178 public static String getYpCatCommand() 179 { 180 return FileExecutor.getExecOutput(new String [] { whichCommand, "ypcat"}).trim(); 181 } 182 183 191 public static String getCustomCommand(String aCommand) 192 { 193 return FileExecutor.getExecOutput(new String [] { whichCommand, aCommand}).trim(); 194 } 195 196 203 public static void main(String [] args) 204 { 205 System.out.println("Hallo from " + UnixHelper.class.getName() + VERSION); 206 207 210 212 System.out.println("WhichCommand: '" + getWhichCommand() + "'"); 213 System.out.println("SuCommand: " + getSuCommand()); 214 System.out.println("RmCommand: " + getRmCommand()); 215 System.out.println("CopyCommand: " + getCpCommand()); 216 System.out.println("YpCommand: " + getYpCatCommand()); 217 218 System.out.println("CustomCommand: " + getCustomCommand("cat")); 219 220 File tempFile = null; 221 222 try 223 { 224 tempFile = File.createTempFile(UnixHelper.class.getName(), Long.toString(System 225 .currentTimeMillis()) 226 + ".tmp"); 227 } 228 catch (Exception e) 229 { 230 e.printStackTrace(); 231 } 232 233 System.out.println("Tempfile: " + tempFile.toString()); 234 235 240 241 try 243 { 244 BufferedWriter w = new BufferedWriter ( new FileWriter (tempFile) ); 245 w.write("Hallo"); 246 w.flush(); 247 w.close(); 248 if( tempFile.exists() ) 249 System.out.println("Wrote: " + tempFile + ">>Hallo"); 250 else 251 System.out.println("Could not Wrote: " + tempFile + "Hallo"); 252 } 253 catch (Exception e) 254 { 255 e.printStackTrace(); 256 } 257 259 String destfilename = "/home/marc.eppelmann" + File.separator + "Desktop" ; 260 261 System.out.println("Copy: " + tempFile.toString() + " to " + destfilename); 262 263 String result = FileExecutor.getExecOutput(new String [] { getSuCommand(), "marc.eppelmann", "-c", 264 "\"" + getCpCommand() + " " + tempFile.toString() + " " + destfilename + "\""}); 265 266 System.out.println("Wrote: " + tempFile.toString() + " to " + destfilename + " > " + result); 267 268 } 271 } 272 | Popular Tags |