KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > execution > ExecutionEngine


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.openide.execution;
21
22 import java.security.AllPermission JavaDoc;
23 import java.security.CodeSource JavaDoc;
24 import java.security.PermissionCollection JavaDoc;
25 import java.security.Permissions JavaDoc;
26 import org.openide.util.Lookup;
27 import org.openide.util.RequestProcessor;
28 import org.openide.windows.IOProvider;
29 import org.openide.windows.InputOutput;
30
31 /**
32  * Engine providing the environment necessary to run long-lived processes.
33  * May perform tasks such as setting up thread groups, etc.
34  * Modules should not implement this class.
35  * @author Jaroslav Tulach, Ales Novak
36  */

37 public abstract class ExecutionEngine extends Object JavaDoc {
38
39     /**
40      * Run some task in the execution engine.
41      * @param name a name of the new process
42      * @param run a runnable to execute
43      * @param io an I/O handle to automatically redirect system I/O streams in the dynamic scope of the task to,
44      * or null if no such redirection is required
45      * @return an executor task that can control the execution
46      */

47     public abstract ExecutorTask execute(String JavaDoc name, Runnable JavaDoc run, InputOutput io);
48
49     /**
50      * Users that want to link their classes with NetBeans module classes should do this through
51      * internal execution. The {@link NbClassLoader} used in internal execution will assume that calling
52      * this method and giving the permission collection to the class being defined will
53      * trigger automatic redirection of system output, input, and error streams into the given I/O tab.
54      * Implementations of the engine should bind the tab and returned permissions.
55      * Since the permission collection is on the stack when calling methods on {@link System#out} etc.,
56      * it is possible to find the appropriate tab for redirection.
57      * @param cs code source to construct the permission collection for
58      * @param io an I/O tab
59      * @return a permission collection
60      */

61     protected abstract PermissionCollection JavaDoc createPermissions(CodeSource JavaDoc cs, InputOutput io);
62
63     /** Method that allows implementor of the execution engine to provide
64     * class path to all libraries that one could find useful for development
65     * in the system.
66     *
67     * @return class path to libraries
68      * @deprecated There are generally no excuses to be using this method as part of a normal module;
69      * its exact meaning is vague, and probably not what you want.
70     */

71     protected abstract NbClassPath createLibraryPath ();
72     
73     /**
74      * Obtains default instance of the execution engine.
75      * If default {@link Lookup} contains an instance of {@link ExecutionEngine},
76      * that is used. Otherwise, a trivial basic implementation is returned with
77      * the following behavior:
78      * <ul>
79      * <li>{@link #execute} just runs the runnable immediately and pretends to be done.
80      * <li>{@link #createPermissions} just uses {@link AllPermission}. No I/O redirection
81      * or {@link System#exit} trapping is done.
82      * <li>{@link #createLibraryPath} produces an empty path.
83      * </ul>
84      * This basic implementation is helpful in unit tests and perhaps in standalone usage
85      * of other libraries.
86      * @return some execution engine implementation (never null)
87      * @since 2.16
88      */

89     public static ExecutionEngine getDefault() {
90         ExecutionEngine ee = (ExecutionEngine) Lookup.getDefault().lookup(ExecutionEngine.class);
91         if (ee == null) {
92             ee = new Trivial();
93         }
94         return ee;
95     }
96     
97     /**
98      * Dummy fallback implementation, useful for unit tests.
99      */

100     static final class Trivial extends ExecutionEngine {
101         
102         public Trivial() {}
103
104         protected NbClassPath createLibraryPath() {
105             return new NbClassPath(new String JavaDoc[0]);
106         }
107
108         protected PermissionCollection JavaDoc createPermissions(CodeSource JavaDoc cs, InputOutput io) {
109             PermissionCollection JavaDoc allPerms = new Permissions JavaDoc();
110             allPerms.add(new AllPermission JavaDoc());
111             allPerms.setReadOnly();
112             return allPerms;
113         }
114
115         public ExecutorTask execute(String JavaDoc name, Runnable JavaDoc run, InputOutput io) {
116             return new ET(run, name, io);
117         }
118         
119         private static final class ET extends ExecutorTask {
120             private RequestProcessor.Task task;
121             private int resultValue;
122             private final String JavaDoc name;
123             private InputOutput io;
124             
125             public ET(Runnable JavaDoc run, String JavaDoc name, InputOutput io) {
126                 super(run);
127                 this.resultValue = resultValue;
128                 this.name = name;
129                 task = RequestProcessor.getDefault().post(this);
130             }
131             
132             public void stop() {
133                 task.cancel();
134             }
135             
136             public int result() {
137                 waitFinished();
138                 return resultValue;
139             }
140             
141             public InputOutput getInputOutput() {
142                 return io;
143             }
144             
145             public void run() {
146                 try {
147                     super.run();
148                 } catch (RuntimeException JavaDoc x) {
149                     x.printStackTrace();
150                     resultValue = 1;
151                 }
152             }
153             
154         }
155         
156     }
157
158 }
159
Popular Tags