1 18 package com.izforge.izpack.util.os.unix; 19 20 import com.izforge.izpack.util.FileExecutor; 21 22 import java.io.BufferedWriter ; 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 27 import java.util.Date ; 28 29 34 public class ShellScript 35 { 36 37 39 41 private final static String Author = "Author: marc.eppelmann_at_gmx.de"; 42 43 44 private final static String Generator = "Generator: " + ShellScript.class.getName(); 45 46 47 private final static String SCM_ID = "$Id$"; 48 49 50 private final static String Revision = "$Revision$"; 51 52 53 private final static String CommentPre = "# "; 54 55 56 private final static String H = CommentPre; 57 58 59 private final static String lf = "\n"; 60 61 62 private final static String lh = lf + H; 63 64 65 private final static String explanation = lh + "This is an automatically generated Script." 66 + lh + "Usually this can be removed if the Generator " + lh 67 + "was unable to remove the script after execution." + lf; 68 69 70 private static String currentDateMsg = "Generated at: " + new Date ().toString(); 71 72 73 private final static String header = lf + explanation + lf + H + Generator + lf + H + SCM_ID 74 + lf + H + Author + lf + H + Revision + lf + H + currentDateMsg + lf + lf; 75 76 78 80 private StringBuffer content = new StringBuffer (); 81 82 83 private String itsLocation; 84 85 87 93 public ShellScript(String aShell) 94 { 95 content.append("#!/usr/bin/env " + aShell); 96 content.append(header); 97 } 98 99 102 public ShellScript() 103 { 104 this("sh"); 105 } 106 107 109 115 public void append(Object anObject) 116 { 117 content.append(anObject); 118 } 119 120 125 public void append(char aChar) 126 { 127 content.append(aChar); 128 } 129 130 135 public void appendln(Object anObject) 136 { 137 append(anObject); 138 append(lf); 139 } 140 141 146 public void appendln(char aChar) 147 { 148 append(aChar); 149 append(lf); 150 } 151 152 155 public void appendln() 156 { 157 append(lf); 158 } 159 160 165 public StringBuffer getContent() 166 { 167 return content; 168 } 169 170 175 public String getContentAsString() 176 { 177 return content.toString(); 178 } 179 180 186 public String toString() 187 { 188 StringBuffer result = new StringBuffer (); 189 result.append(getClass().getName()); 190 result.append('\n'); 191 result.append(itsLocation); 192 result.append('\n'); 193 result.append(content); 194 195 return result.toString(); 196 } 197 198 203 public void write(String aDestination) 204 { 205 itsLocation = aDestination; 206 207 try 208 { 209 BufferedWriter writer = new BufferedWriter (new FileWriter (aDestination)); 210 writer.write(content.toString()); 211 writer.write(lh + aDestination + lf); 212 writer.flush(); 213 writer.close(); 214 } 215 catch (IOException e) 216 { 217 e.printStackTrace(); 218 } 219 } 220 221 237 public String exec(String itsParams) 238 { 239 FileExecutor.getExecOutput(new String [] { UnixHelper.getCustomCommand("chmod"), "+x", 240 itsLocation}); 241 242 if (itsParams != null) 243 { 244 return FileExecutor.getExecOutput(new String [] { itsLocation, itsParams}); 245 } 246 else 247 { 248 return FileExecutor.getExecOutput(new String [] { itsLocation}); 249 } 250 } 251 252 257 public String exec() 258 { 259 return exec(null); 260 } 261 262 272 public static String execute(String aShell, StringBuffer lines, String aLocation, 273 String itsParams) 274 { 275 ShellScript s = new ShellScript((aShell == null) ? "sh" : aShell); 276 s.append(lines); 277 s.write(aLocation); 278 279 return (itsParams == null) ? s.exec() : s.exec(itsParams); 280 } 281 282 290 public static String execute(StringBuffer lines, String aLocation) 291 { 292 return ShellScript.execute(null, lines, aLocation, null); 293 } 294 295 308 public static String execAndDelete(String aShell, StringBuffer lines, String aLocation, 309 String itsParams) 310 { 311 String result = execute(aShell, lines, aLocation, itsParams); 312 File location = new File (aLocation); 313 314 try 315 { 316 location.delete(); 317 } 318 catch (Exception e) 319 { 320 location.deleteOnExit(); 321 } 322 323 return result; 324 } 325 326 334 public static String execAndDelete(StringBuffer lines, String aLocation) 335 { 336 return execAndDelete(null, lines, aLocation, null); 337 } 338 339 344 public static void main(String [] args) 345 { 346 350 351 356 } 357 } 358 | Popular Tags |