KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > IOTable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.execution;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 import org.openide.windows.InputOutput;
26 import org.openide.windows.TopComponent;
27
28 /** Tasks are supposed to obey following model: every task is a ThreadGroup
29 * and the ThreadGroup is under another ThreadGroup - called "base".
30 * Systems threads are not under group base.
31 * The table keeps couples ThreadGroup:TaskIO; each task is supposed
32 * to be encapsulate by a ThreadGroup; system's threads have special
33 * handling @see #systemIO
34 * Some tasks don't require io operations. For such tasks NullTaskIO is
35 * created (at ExecutionEngine.RunClass.run()); NullTaskIOs left reusing TaskIOs
36 *
37 * @author Ales Novak
38 */

39 final class IOTable extends Hashtable JavaDoc<InputOutput,TaskIO> {
40     /** generated Serialized Version UID */
41     static final long serialVersionUID = 9096333712401558521L;
42
43     /** ThreadGroup of all tasks */
44     private ThreadGroup JavaDoc base;
45
46     /** TaskIO of system's threads */
47     private TaskIO systemIO;
48
49     /** hashtable of free TaskIOs - name:TaskIO */
50     private HashMap JavaDoc<String JavaDoc, TaskIO> freeTaskIOs;
51
52     /**
53     * @param base is a base ThreadGroup for tasks
54     * @param systemIO is a TaskIO instance that is used for system threads
55     */

56     public IOTable(ThreadGroup JavaDoc base, TaskIO systemIO) {
57         this.base = base;
58         this.systemIO = systemIO;
59         freeTaskIOs = new HashMap JavaDoc<String JavaDoc, TaskIO>(16);
60     }
61
62     /** finds top thread group of the calling thread
63     * @return null iff the calling thread is not in any exec group
64     * or exec group of calling thread
65     */

66     ThreadGroup JavaDoc findGroup () {
67         ThreadGroup JavaDoc g = Thread.currentThread().getThreadGroup ();
68         ThreadGroup JavaDoc 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     /**
79     * @return TaskIO specific for calling thread/threadgroup
80     */

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         // this piece of source is duplicated in exec engine
111
// needed when classloader defines a class with an InpuOutput
112
// but the classloader does not work on behalf of execution
113

114         // following code is executed only if a task is dead but
115
// some classes behave like Phoenix - they live again
116
if (io == null) {
117             if (inout instanceof TopComponent) {
118                 String JavaDoc allName = ((TopComponent) inout).getName();
119                 io = getTaskIO(allName);
120                 if (io == null) { // executed for the first time
121
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) { // IOThreadIfc case
129
iopc.setIO(inout);
130                 }
131                 put(inout, io);
132             } else {
133                 return new TaskIO(inout); // foreign inout - just return a TaskIO
134
}
135         }
136
137         return io;
138     }
139
140     /**
141     * @param name is a name of the tab
142     * @return TaskIO
143     */

144     synchronized TaskIO getTaskIO(String JavaDoc name) {
145         TaskIO ret;
146         if (reuseTaskIO())
147             if ((ret = getFreeTaskIO(name)) != null) return ret;
148         return null;
149     }
150
151     /**
152     * @return true iff TaskIO are to be reused
153     */

154     private boolean reuseTaskIO() {
155         return true;
156     }
157
158     /**
159     * @return true iff reused TaskIO should be reseted
160     */

161     private boolean clearTaskIO() {
162         return true;
163     }
164
165     /**
166     * @return free non-used TaskIO with given name or null
167     */

168     private TaskIO getFreeTaskIO(String JavaDoc 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 JavaDoc e) {
178             }
179         }
180         t.in = null;
181         t.getInout().flushReader();
182         freeTaskIOs.remove(name);
183         return t;
184     }
185
186     /** frees resources binded to grp
187     * @param grp is a ThreadGroup which TaskIO is to be released
188     * @param io key for freed TaskIO
189     */

190     synchronized void free(ThreadGroup JavaDoc grp, InputOutput io) {
191         TaskIO t = (TaskIO) get(io);
192         if (t == null) {
193             return; // nothing ??
194
} else if (t.foreign) {
195             return;
196         }
197         if (t.getName() != TaskIO.VOID) { // Null
198
t = (TaskIO) freeTaskIOs.put(t.getName(), t); // free it
199
if (t != null) {
200                 t.getInout().closeInputOutput(); // old one destroy
201
}
202         }
203         remove(io);
204     }
205
206     /**
207     * @return threadgroup specific Reader
208     */

209     public java.io.Reader JavaDoc getIn() {
210         TaskIO io = getIO();
211         if (io.in == null) {
212             io.initIn();
213         }
214         return io.in;
215     }
216
217     /**
218     * @return thread specific OutputWriter
219     * Two calls in the same threadgroup will return the same PrintStream
220     */

221     public java.io.Writer JavaDoc getOut() {
222         TaskIO io = getIO();
223         if (io.out == null) {
224             io.initOut();
225         }
226         return io.out;
227     }
228
229     /**
230     * @return thread specific OutputWriter
231     * Two calls in the same threadgroup will return the same PrintStream
232     */

233     public java.io.Writer JavaDoc getErr() {
234         TaskIO io = getIO();
235         if (io.err == null) {
236             io.initErr();
237         }
238         return io.err;
239     }
240     
241 }
242
Popular Tags