1 28 29 package org.objectweb.util.launcher ; 30 31 32 import java.lang.reflect.Method ; 33 34 import java.util.Properties ; 35 36 import org.objectweb.util.trace.TraceSystem; 37 38 39 40 57 public class CommandJava 58 { 59 60 protected String name_ ; 61 62 protected String class_ ; 63 64 protected XBootClassLoader loader_ ; 65 66 protected Properties properties_ ; 67 68 protected StringList args_ ; 69 70 protected Mode mode_; 71 72 75 public CommandJava() { 76 this(null); 77 } 78 79 83 public CommandJava(XBootClassLoader loader) { 84 name_ = "default" ; 85 class_ = ""; 86 args_ = new StringList(); 87 properties_ = new Properties (); 88 mode_ = new ModeThread(); 89 loader_ = loader; 90 } 91 92 97 public void setName(String value) { 98 TraceSystem.get("launcher").debug(getName()+": Setting Command name to "+value); 99 this.name_ = value ; 100 } 101 102 107 public String getName() { 108 return this.name_ ; 109 } 110 111 116 public void setClassname(String value) { 117 TraceSystem.get("launcher").debug(getName()+": Setting classname to "+value); 118 this.class_ = value ; 119 } 120 121 126 public String getClassname() { 127 return this.class_ ; 128 } 129 130 131 136 public void setLoader(XBootClassLoader value) { 137 this.loader_ = value ; 138 } 139 140 145 public XBootClassLoader getLoader() { 146 return this.loader_ ; 147 } 148 149 150 155 public void addProperties(Properties value) { 156 TraceSystem.get("launcher").debug(getName()+": Adding properties list"+value); 157 this.properties_.putAll(value) ; 158 } 159 160 165 public Properties getProperties() { 166 return this.properties_ ; 167 } 168 169 170 175 public void addArguments(StringList value) { 176 TraceSystem.get("launcher").debug(getName()+": Adding argument list "+value); 177 this.args_.addAll(value) ; 178 } 179 180 185 public StringList getArguments() { 186 return this.args_ ; 187 } 188 189 190 195 public void setMode(String value) { 196 TraceSystem.get("launcher").debug(getName()+": Setting mode to "+value); 197 this.mode_ = org.objectweb.util.launcher.ModeFactory.create(value); 198 } 199 200 205 public Mode getMode() { 206 return this.mode_ ; 207 } 208 209 210 protected ClassLoader systemLoader_ ; 211 212 217 private Class getStringArrayType() { 218 return (Class )(new String [0]).getClass(); 219 } 220 221 227 protected void execute() 228 { Class class_instance = null; 230 TraceSystem.get("launcher").info(getName()+": Launching the "+getClassname()+" class"); 231 try { 232 class_instance = Class.forName(getClassname(), false, getLoader()) ; 233 } catch (java.lang.ClassNotFoundException ex) { 234 throw new LauncherException(ex); 235 } 236 Method method = null; 237 try { 238 Class [] params = {getStringArrayType()} ; 239 method = class_instance.getMethod("main", params); 240 } catch (java.lang.NoSuchMethodException ex) { 241 throw new LauncherException(ex); 242 } 243 244 this.systemLoader_ = Thread.currentThread().getContextClassLoader(); 246 Thread.currentThread().setContextClassLoader(getLoader()); 247 System.getProperties().putAll(getProperties()); 248 249 try { Object [] params = {getArguments().toStringArray()} ; 251 TraceSystem.get("launcher").debug("Arguments used: "+getArguments()); 252 method.invoke(null, params); 253 } catch (java.lang.IllegalAccessException ex) { 254 throw new LauncherException(ex); 255 } catch (java.lang.reflect.InvocationTargetException ex) { 256 throw new LauncherException(ex); 257 } 258 } 259 260 263 public void run() { 264 this.mode_.execute(this); 265 } 266 267 270 protected void finalize() { 271 Thread.currentThread().setContextClassLoader(this.systemLoader_); 272 } 273 274 279 public boolean equals(Object obj) { 280 if (obj instanceof CommandJava) { 281 CommandJava cmd = (CommandJava) obj ; 282 return ((getName().equals(cmd.getName())) 283 && (getClassname().equals(cmd.getClassname())) 284 && (getLoader().equals(cmd.getLoader())) 285 && (getProperties().equals(cmd.getProperties())) 286 && (getArguments().equals(cmd.getArguments()))); 287 } 288 return false ; 289 } 290 } 291 | Popular Tags |