KickJava   Java API By Example, From Geeks To Geeks.

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


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.logging.Level JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import org.openide.execution.ExecutorTask;
25 import org.openide.util.Exceptions;
26 import org.openide.windows.InputOutput;
27
28 /** Support for Executor beans and for their SysProcess subclasses.
29 *
30 * @author Ales Novak
31 */

32 final class DefaultSysProcess extends ExecutorTask {
33
34     /** reference count of instances */
35     static int processCount;
36     /** reference to SysProcess ThreadGroup */
37     private final TaskThreadGroup group;
38     /** flag deciding whether is the process destroyed or not */
39     private boolean destroyed = false;
40     /** InputOutput for this Context */
41     private final InputOutput io;
42     /** Name */
43     private final String JavaDoc name;
44
45     /**
46     * @param grp is a ThreadGroup of this process
47     */

48     public DefaultSysProcess(Runnable JavaDoc run, TaskThreadGroup grp, InputOutput io, String JavaDoc name) {
49         super(run);
50         group = grp;
51         this.io = io;
52         this.name = name;
53     }
54
55     /** terminates the process by killing all its thread (ThreadGroup) */
56     public synchronized void stop() {
57
58         if (destroyed) return;
59         destroyed = true;
60         try {
61             group.interrupt();
62             group.stop();
63             group.getRunClassThread().waitForEnd();
64         } catch (InterruptedException JavaDoc e) {
65             Logger.getLogger(DefaultSysProcess.class.getName()).log(Level.WARNING, null, e);
66         }
67         ExecutionEngine.closeGroup(group);
68         group.kill(); // force RunClass thread get out - end of exec is fired
69
notifyFinished();
70     }
71
72     /** waits for this process is done
73     * @return 0
74     */

75     public int result() {
76         // called by an instance of RunClass thread - kill() in previous stop() forces calling thread
77
// return from waitFor()
78
try {
79             group.waitFor();
80         } catch (InterruptedException JavaDoc e) {
81             return 4; // EINTR
82
}
83         notifyFinished();
84         return 0;
85     }
86
87     /** @return an InputOutput */
88     public InputOutput getInputOutput() {
89         return io;
90     }
91
92     public void run() {
93     }
94
95     public String JavaDoc getName() {
96         return name;
97     }
98     
99     /** destroy the thread group this process was handled from. Not that simple
100      * as it seems, since the ThreadGroup can't be destroyed from inside.
101      */

102     void destroyThreadGroup(ThreadGroup JavaDoc base) {
103         new Thread JavaDoc(base,
104                    new Runnable JavaDoc() {
105
106                        public void run() {
107                            try {
108                                while (group.activeCount() > 0) {
109                                    Thread.sleep(1000);
110                                }
111                            }
112                            catch (InterruptedException JavaDoc e) {
113                                Exceptions.printStackTrace(e);
114                            }
115                            if (!group.isDestroyed())
116                                group.destroy();
117                        }
118                    }).start();
119     }
120 }
121
Popular Tags