1 package org.objectweb.celtix.tools.common.toolspec; 2 3 import java.io.InputStream ; 4 import java.lang.reflect.Constructor ; 5 import java.util.logging.Level ; 6 import java.util.logging.Logger ; 7 8 import org.objectweb.celtix.common.i18n.Message; 9 import org.objectweb.celtix.common.logging.LogUtils; 10 import org.objectweb.celtix.tools.common.ToolException; 11 public final class ToolRunner { 12 private static final Logger LOG = LogUtils.getL7dLogger(ToolRunner.class); 13 private ToolRunner() { 14 } 16 17 public static void runTool(Class clz, InputStream toolspecStream, 18 boolean validate, String [] args) throws Exception { 19 runTool(clz, toolspecStream, validate, args, true); 20 } 21 22 public static void runTool(Class clz, 23 InputStream toolspecStream, 24 boolean validate, 25 String [] args, 26 boolean exitOnFinish) throws Exception { 27 28 if (ToolContainer.class.isAssignableFrom(clz)) { 29 30 ToolContainer container = null; 31 32 try { 33 Constructor cons = clz.getConstructor( 34 new Class [] { 35 ToolSpec.class 36 }); 37 container = (ToolContainer)cons.newInstance( 38 new Object [] { 39 new ToolSpec(toolspecStream, validate) 40 }); 41 } catch (Exception ex) { 42 Message message = new Message("CLZ_CANNOT_BE_CONSTRUCTED", LOG, clz.getName()); 43 LOG.log(Level.SEVERE, message.toString()); 44 throw new ToolException(message, ex); 45 } 46 47 try { 48 container.setCommandLine(args); 49 container.init(); 50 container.execute(exitOnFinish); 51 } catch (Exception ex) { 52 throw ex; 53 } 54 } else { 55 Message message = new Message("CLZ_SHOULD_IMPLEMENT_INTERFACE", LOG, clz.getName()); 56 LOG.log(Level.SEVERE, message.toString()); 57 throw new ToolException(message); 58 } 59 60 } 61 62 } 63 | Popular Tags |