1 55 package org.lateralnz.common.util; 56 57 import java.io.File ; 58 import java.io.FileFilter ; 59 import java.io.FileInputStream ; 60 import java.io.FilenameFilter ; 61 import java.io.FileOutputStream ; 62 import java.io.InputStreamReader ; 63 import java.io.IOException ; 64 import java.lang.reflect.InvocationTargetException ; 65 import java.lang.reflect.Method ; 66 import java.lang.reflect.Modifier ; 67 import java.net.InetAddress ; 68 import java.net.NetworkInterface ; 69 import java.nio.channels.FileChannel ; 70 import java.util.Arrays ; 71 import java.util.Enumeration ; 72 import java.util.List ; 73 74 import org.apache.log4j.Logger; 75 76 81 public class SystemUtils { 82 private static final Logger log = Logger.getLogger(SystemUtils.class.getName()); 83 private static final String GET = "get"; 84 private static final String SET = "set"; 85 private static final Class MAIN_ARGS = (new String []{ }).getClass(); 86 87 private static final byte[] LOCALHOST_LOOPBACK = new byte[]{ (byte)127, (byte)0, (byte)0, (byte)1 }; 88 89 private SystemUtils() { 90 } 91 92 public static final void copyFile(String f1, String f2) throws IOException { 93 FileChannel source = new FileInputStream (f1).getChannel(); 94 FileChannel dest = new FileOutputStream (f2).getChannel(); 95 dest.transferFrom(source, 0, source.size()); 96 97 source.close(); 98 dest.close(); 99 } 100 101 110 public static final String [] exec(String [] cmd, String [] env, File f, long waitPeriod) throws IOException , InterruptedException { 111 Process p = Runtime.getRuntime().exec(cmd, env, f); 112 113 ProcessMonitor pm = new ProcessMonitor(p, waitPeriod); 114 p.waitFor(); 115 116 InputStreamReader is = null; 117 InputStreamReader es = null; 118 try { 119 is = new InputStreamReader (p.getInputStream()); 120 es = new InputStreamReader (p.getErrorStream()); 121 122 if (pm.rtn[0] == null) { 123 pm.rtn[0] = StringUtils.readFrom(is).trim(); 124 } 125 if (pm.rtn[1] == null) { 126 pm.rtn[1] = StringUtils.readFrom(es).trim(); 127 } 128 } 129 finally { 130 IOUtils.close(is); 131 IOUtils.close(es); 132 } 133 134 return pm.rtn; 135 } 136 137 141 public static final String getArgument(String [] args, int idx, String defaultValue) { 142 if (args == null || idx >= args.length || StringUtils.isEmpty(args[idx])) { 143 return defaultValue; 144 } 145 else { 146 return args[idx]; 147 } 148 } 149 150 153 public static final String [] getFileList(String directory, final String regex) { 154 File f = new File (directory); 155 if (!f.exists() || !f.isDirectory()) { 156 return new String []{ }; 157 } 158 else { 159 return f.list(new FilenameFilter () { 160 public boolean accept(File f, String name) { 161 if (StringUtils.matches(name, regex)) { 162 return true; 163 } 164 return false; 165 } 166 }); 167 } 168 } 169 170 173 public static final void getFileList(List filelist, String directory, final String includesRegex, final String excludesRegex, boolean recursive) { 174 File f = new File (directory); 175 if (f.exists() && f.isDirectory()) { 176 File [] farray = f.listFiles(new FileFilter () { 177 public boolean accept(File f) { 178 if (f.isDirectory()) { 179 return true; 180 } 181 else { 182 String name; 183 try { 184 name = f.getCanonicalPath(); 185 if (StringUtils.matches(name, includesRegex) && !StringUtils.matches(name, excludesRegex)) { 186 return true; 187 } 188 } 189 catch (IOException ioe) { 190 ioe.printStackTrace(); 191 } 192 } 193 return false; 194 } 195 }); 196 for (int i = 0; i < farray.length; i++) { 197 if (farray[i].isDirectory() && recursive) { 198 getFileList(filelist, farray[i].getAbsolutePath(), includesRegex, excludesRegex, recursive); 199 } 200 else { 201 filelist.add(farray[i]); 202 } 203 } 204 } 205 } 206 207 212 public static final byte[] getLocalhostIP() { 213 try { 214 InetAddress ia = InetAddress.getLocalHost(); 215 if (!Arrays.equals(ia.getAddress(), LOCALHOST_LOOPBACK)) { 216 return ia.getAddress(); 217 } 218 else { 219 Enumeration en = NetworkInterface.getNetworkInterfaces(); 220 while (en.hasMoreElements()) { 221 NetworkInterface ni = (NetworkInterface )en.nextElement(); 222 Enumeration en2 = ni.getInetAddresses(); 223 while (en2.hasMoreElements()) { 224 ia = (InetAddress )en2.nextElement(); 225 if (!ia.isLoopbackAddress()) { 226 return ia.getAddress(); 227 } 228 } 229 } 230 } 231 } 232 catch (Exception e) { 233 e.printStackTrace(); 234 } 235 return (byte[])LOCALHOST_LOOPBACK.clone(); 236 } 237 238 241 public static final Method getSetterMethod(Class c, String property, Class [] paramTypes) throws NoSuchMethodException { 242 if (StringUtils.isEmpty(property)) { 243 return null; 244 } 245 246 String tmp = SET + initCaps(property); 247 248 return c.getMethod(tmp, paramTypes); 249 } 250 251 private static final String initCaps(String s) { 252 return s.substring(0,1).toUpperCase() + s.substring(1); 253 } 254 255 258 public static final Object invoke(Object obj, String method, Object [] args) throws IllegalAccessException , InvocationTargetException , NoSuchMethodException { 259 Class c = obj.getClass(); 260 Class [] params = null; 261 if (args != null) { 262 params = new Class [args.length]; 263 for (int i = 0; i < args.length; i++) { 264 params[i] = args[i].getClass(); 265 } 266 } 267 Method m = c.getMethod(method, params); 268 return m.invoke(obj, args); 269 } 270 271 274 public static final void invokeMain(Class c, String [] args) throws IllegalAccessException , InvocationTargetException , NoSuchMethodException { 275 Class argsClass; 276 if (args == null) { 277 argsClass = MAIN_ARGS; 278 } 279 else { 280 argsClass = args.getClass(); 281 } 282 Method m = c.getMethod("main", new Class [] { argsClass }); 283 m.setAccessible(true); 284 int mods = m.getModifiers(); 285 if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) { 286 throw new NoSuchMethodException ("main"); 287 } 288 m.invoke(null, new Object [] { args }); 289 } 290 291 294 static class ProcessMonitor extends Thread { 295 private String [] rtn = new String [2]; 296 private boolean running = true; 297 private int count = 10; 298 private long waitPeriod; 299 private Process p; 300 301 public ProcessMonitor(Process p, long waitPeriod) { 302 this.p = p; 303 this.waitPeriod = waitPeriod; 304 this.start(); 305 } 306 307 public void run() { 308 while (running) { 309 try { 310 this.sleep(waitPeriod); 311 p.exitValue(); 312 } 313 catch (InterruptedException ie) { } 314 catch (IllegalThreadStateException itse) { 315 count--; 316 InputStreamReader iisr = null; 317 InputStreamReader eisr = null; 318 try { 319 int incount = p.getInputStream().available(); 320 int errcount = p.getErrorStream().available(); 321 if (incount > 0 || errcount > 0 || count <= 0) { 322 log.warn("stdin(" + incount + "), stderr(" + errcount + ")"); 323 log.warn("destroying hung process " + p); 324 325 iisr = new InputStreamReader (p.getInputStream()); 326 eisr = new InputStreamReader (p.getErrorStream()); 327 rtn[0] = StringUtils.readFrom(iisr, incount).trim(); 328 rtn[1] = StringUtils.readFrom(eisr, errcount).trim(); 329 p.destroy(); 330 running = false; 331 } 332 else if (log.isInfoEnabled()) { 333 log.info("SystemUtils.exec: process has still not completed after 10 seconds"); 334 } 335 } 336 catch (Exception e) { 337 log.error(e); 338 e.printStackTrace(); 339 } 340 finally { 341 IOUtils.close(iisr); 342 IOUtils.close(eisr); 343 } 344 } 345 } 346 } 347 } 348 349 public static final void sleep(long time) { 350 try { 351 Thread.currentThread().sleep(time); 352 } 353 catch (InterruptedException ie) { } 354 } 355 } | Popular Tags |