1 19 20 package org.netbeans.core.execution; 21 22 import java.util.HashMap ; 23 import java.util.Hashtable ; 24 25 import org.openide.windows.InputOutput; 26 import org.openide.windows.TopComponent; 27 28 39 final class IOTable extends Hashtable <InputOutput,TaskIO> { 40 41 static final long serialVersionUID = 9096333712401558521L; 42 43 44 private ThreadGroup base; 45 46 47 private TaskIO systemIO; 48 49 50 private HashMap <String , TaskIO> freeTaskIOs; 51 52 56 public IOTable(ThreadGroup base, TaskIO systemIO) { 57 this.base = base; 58 this.systemIO = systemIO; 59 freeTaskIOs = new HashMap <String , TaskIO>(16); 60 } 61 62 66 ThreadGroup findGroup () { 67 ThreadGroup g = Thread.currentThread().getThreadGroup (); 68 ThreadGroup old = null; 69 while (g != null && g != base) { 70 old = g; 71 g = g.getParent (); 72 } 73 return (g == null) ? null : old; 74 } 75 76 private boolean searchingIO = false; 77 78 81 private synchronized TaskIO getIO() { 82 83 if (searchingIO) { 84 return systemIO; 85 } 86 87 InputOutput inout = null; 88 89 if (Thread.currentThread() instanceof IOThreadIfc) { 90 inout = ((IOThreadIfc) Thread.currentThread()).getInputOutput(); 91 } 92 93 IOPermissionCollection iopc = null; 94 95 if (inout == null) { 96 try { 97 searchingIO = true; 98 iopc = AccController.getIOPermissionCollection(); 99 } finally { 100 searchingIO = false; 101 } 102 if (iopc == null) { 103 return systemIO; 104 } 105 inout = iopc.getIO(); 106 } 107 108 TaskIO io = (TaskIO) get(inout); 109 110 114 if (io == null) { 117 if (inout instanceof TopComponent) { 118 String allName = ((TopComponent) inout).getName(); 119 io = getTaskIO(allName); 120 if (io == null) { inout = org.openide.windows.IOProvider.getDefault().getIO(allName, true); 122 io = new TaskIO(inout, allName); 123 } else { 124 inout = io.getInout(); 125 } 126 inout.select(); 127 inout.setFocusTaken(true); 128 if ( iopc!= null) { iopc.setIO(inout); 130 } 131 put(inout, io); 132 } else { 133 return new TaskIO(inout); } 135 } 136 137 return io; 138 } 139 140 144 synchronized TaskIO getTaskIO(String name) { 145 TaskIO ret; 146 if (reuseTaskIO()) 147 if ((ret = getFreeTaskIO(name)) != null) return ret; 148 return null; 149 } 150 151 154 private boolean reuseTaskIO() { 155 return true; 156 } 157 158 161 private boolean clearTaskIO() { 162 return true; 163 } 164 165 168 private TaskIO getFreeTaskIO(String name) { 169 TaskIO t = (TaskIO) freeTaskIOs.get(name); 170 if (t == null) { 171 return null; 172 } 173 if (clearTaskIO()) { 174 try { 175 t.getInout().getOut().reset(); 176 t.getInout().getErr().reset(); 177 } catch (java.io.IOException e) { 178 } 179 } 180 t.in = null; 181 t.getInout().flushReader(); 182 freeTaskIOs.remove(name); 183 return t; 184 } 185 186 190 synchronized void free(ThreadGroup grp, InputOutput io) { 191 TaskIO t = (TaskIO) get(io); 192 if (t == null) { 193 return; } else if (t.foreign) { 195 return; 196 } 197 if (t.getName() != TaskIO.VOID) { t = (TaskIO) freeTaskIOs.put(t.getName(), t); if (t != null) { 200 t.getInout().closeInputOutput(); } 202 } 203 remove(io); 204 } 205 206 209 public java.io.Reader getIn() { 210 TaskIO io = getIO(); 211 if (io.in == null) { 212 io.initIn(); 213 } 214 return io.in; 215 } 216 217 221 public java.io.Writer getOut() { 222 TaskIO io = getIO(); 223 if (io.out == null) { 224 io.initOut(); 225 } 226 return io.out; 227 } 228 229 233 public java.io.Writer getErr() { 234 TaskIO io = getIO(); 235 if (io.err == null) { 236 io.initErr(); 237 } 238 return io.err; 239 } 240 241 } 242 | Popular Tags |