1 19 20 package org.netbeans.core.execution; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.PrintStream ; 26 import java.util.*; 27 import java.security.CodeSource ; 28 import java.security.PermissionCollection ; 29 import java.security.Policy ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 import org.openide.execution.NbClassPath; 34 import org.openide.execution.ExecutorTask; 35 import org.openide.util.Lookup; 36 import org.openide.windows.InputOutput; 37 38 import org.netbeans.core.NbTopManager; 39 40 57 public final class 58 ExecutionEngine extends org.openide.execution.ExecutionEngine { 59 60 61 public static final ThreadGroup base = new ThreadGroup ("base"); 63 64 private int number = 1; 65 66 67 public static final TaskIO systemIO = new TaskIO(); 68 69 70 private static IOTable taskIOs; 71 72 73 static private WindowTable wtable = new WindowTable(); 74 75 76 private HashSet<ExecutionListener> executionListeners = new HashSet<ExecutionListener>(); 77 78 79 private List<ExecutorTask> runningTasks = Collections.synchronizedList(new ArrayList<ExecutorTask>( 5 )); 80 81 static { 82 systemIO.out = new OutputStreamWriter(System.out); 83 systemIO.err = new OutputStreamWriter(System.err); 84 systemIO.in = new java.io.InputStreamReader (System.in); 85 } 86 87 static final long serialVersionUID =9072488605180080803L; 88 89 public ExecutionEngine () { 90 94 System.setIn(new SysIn()); 95 System.setOut(createPrintStream(true)); 96 System.setErr(createPrintStream(false)); 97 } 98 99 102 public static ExecutionEngine getExecutionEngine() { 103 ExecutionEngine ee = (ExecutionEngine)Lookup.getDefault().lookup(ExecutionEngine.class); 104 if (ee != null) return ee; 105 org.openide.execution.ExecutionEngine ee2 = (org.openide.execution.ExecutionEngine)Lookup.getDefault().lookup(org.openide.execution.ExecutionEngine.class); 106 if (ee2 instanceof ExecutionEngine) return (ExecutionEngine)ee2; 107 return null; 108 } 109 110 111 public Collection <ExecutorTask> getRunningTasks() { 112 return Arrays.asList(runningTasks.toArray(new ExecutorTask[0])); 115 } 116 117 118 public String getRunningTaskName( ExecutorTask task ) { 119 if ( !runningTasks.contains( task ) || 120 !(task instanceof DefaultSysProcess) ) { 121 return null; 122 } 123 else { 124 return ((DefaultSysProcess)task).getName(); 125 } 126 } 127 128 134 public ExecutorTask execute(String name, Runnable run, InputOutput inout) { 135 TaskThreadGroup g = new TaskThreadGroup(base, "exec_" + name + "_" + number); g.setDaemon(true); 137 ExecutorTaskImpl task = new ExecutorTaskImpl(); 138 synchronized (task.lock) { 139 try { 140 new RunClassThread(g, name, number++, inout, this, task, run); 141 task.lock.wait(); 142 } catch (InterruptedException e) { 143 throw new IllegalStateException (e.getMessage()); 144 } 145 } 146 return task; 147 } 148 149 155 protected NbClassPath createLibraryPath() { 156 @SuppressWarnings ("unchecked") List<File > l = NbTopManager.getUninitialized().getModuleJars(); 157 return new NbClassPath (l.toArray (new File [l.size ()])); 158 } 159 160 161 public final void addExecutionListener (ExecutionListener l) { 162 synchronized (executionListeners) { 163 executionListeners.add(l); 164 } 165 } 166 167 168 public final void removeExecutionListener (ExecutionListener l) { 169 synchronized (executionListeners) { 170 executionListeners.remove(l); 171 } 172 } 173 174 179 protected final PermissionCollection createPermissions(CodeSource cs, InputOutput io) { 180 PermissionCollection pc = Policy.getPolicy().getPermissions(cs); 181 ThreadGroup grp = Thread.currentThread().getThreadGroup(); 182 return new IOPermissionCollection(io, pc, (grp instanceof TaskThreadGroup ? (TaskThreadGroup) grp: null)); 183 } 184 185 186 protected final void fireExecutionStarted (ExecutionEvent ev) { 187 runningTasks.add( ev.getProcess() ); 188 @SuppressWarnings ("unchecked") 189 Iterator<ExecutionListener> iter = ((HashSet<ExecutionListener>) executionListeners.clone()).iterator(); 190 while (iter.hasNext()) { 191 ExecutionListener l = iter.next(); 192 l.startedExecution(ev); 193 } 194 } 195 196 197 protected final void fireExecutionFinished (ExecutionEvent ev) { 198 runningTasks.remove( ev.getProcess() ); 199 @SuppressWarnings ("unchecked") 200 Iterator<ExecutionListener> iter = ((HashSet<ExecutionListener>) executionListeners.clone()).iterator(); 201 while (iter.hasNext()) { 202 ExecutionListener l = iter.next(); 203 l.finishedExecution(ev); 204 } 205 ev.getProcess().destroyThreadGroup(base); 206 } 207 208 static void putWindow(java.awt.Window w, TaskThreadGroup tg) { 209 wtable.putTaskWindow(w, tg); 210 } 211 static void closeGroup(ThreadGroup tg) { 212 wtable.closeGroup(tg); 213 } 214 static boolean hasWindows(ThreadGroup tg) { 215 return wtable.hasWindows(tg); 216 } 217 218 221 static IOTable getTaskIOs() { 222 if (taskIOs == null) { 223 taskIOs = new IOTable(base, systemIO); 224 } 225 return taskIOs; 226 } 227 228 232 public static ThreadGroup findGroup () { 233 ThreadGroup g = Thread.currentThread().getThreadGroup (); 234 ThreadGroup old = null; 235 while (g != null && g != base) { 236 old = g; 237 g = g.getParent (); 238 } 239 return (g == null) ? null : old; 240 } 241 242 243 static class SysOut extends OutputStream { 244 245 246 boolean std; 247 248 249 SysOut(boolean std) { 250 this.std = std; 251 } 252 253 public void write(int b) throws IOException { 254 if (std) { 255 getTaskIOs().getOut().write(b); 256 } else { 257 getTaskIOs().getErr().write (b); 258 } 259 } 260 261 public void write(byte[] buff, int off, int len) throws IOException { 262 String s = new String (buff, off, len); 263 if (std) { 264 getTaskIOs().getOut().write(s.toCharArray(), 0, s.length()); 265 } else { 266 getTaskIOs().getErr().write(s.toCharArray(), 0, s.length()); 267 } 268 } 269 270 public void flush() throws IOException { 271 if (std) { 272 getTaskIOs().getOut().flush(); 273 } else { 274 getTaskIOs().getErr().flush(); 275 } 276 } 277 278 public void close() throws IOException { 279 if (std) { 280 getTaskIOs().getOut().close(); 281 } else { 282 getTaskIOs().getErr().close(); 283 } 284 } 285 } 286 287 static PrintStream createPrintStream(boolean stdOut) { 288 return new WriterPrintStream(new SysOut(stdOut), stdOut); 289 } 290 291 } 292 | Popular Tags |