alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.execution;

import java.security.AllPermission;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import org.openide.util.Lookup;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;

/**
 * Engine providing the environment necessary to run long-lived processes.
 * May perform tasks such as setting up thread groups, etc.
 * Modules should not implement this class.
 * @author Jaroslav Tulach, Ales Novak
 */
public abstract class ExecutionEngine extends Object {

    /**
     * Run some task in the execution engine.
     * @param name a name of the new process
     * @param run a runnable to execute
     * @param io an I/O handle to automatically redirect system I/O streams in the dynamic scope of the task to,
     *           or null if no such redirection is required
     * @return an executor task that can control the execution
     */
    public abstract ExecutorTask execute(String name, Runnable run, InputOutput io);

    /** Trap accesses to
     * Users that want to link their classes with the IDE should do this through
     * internal execution. The {@link NbClassLoader} used in internal execution will assume that calling
     * this method and giving the permission collection to the class being defined will
     * trigger automatic redirection of system output, input, and error streams into the given I/O tab.
     * Implementations of the engine should bind the tab and returned permissions.
     * Since the permission collection is on the stack when calling methods on {@link System#out} etc.,
     * it is possible to find the appropriate tab for redirection.
     * @param cs code source to construct the permission collection for
     * @param io an I/O tab
     * @return a permission collection
     */
    protected abstract PermissionCollection createPermissions(CodeSource cs, InputOutput io);

    /** Method that allows implementor of the execution engine to provide
    * class path to all libraries that one could find useful for development
    * in the system.
    *
    * @return class path to libraries
     * @deprecated There are generally no excuses to be using this method as part of a normal module;
     * its exact meaning is vague, and probably not what you want.
    */
    protected abstract NbClassPath createLibraryPath ();
    
    /**
     * Obtains default instance of the execution engine.
     * If default {@link Lookup} contains an instance of {@link ExecutionEngine},
     * that is used. Otherwise, a trivial basic implementation is returned with
     * the following behavior:
     * 
    *
  • {@link #execute} just runs the runnable immediately and pretends to be done. *
  • {@link #createPermissions} just uses {@link AllPermission}. No I/O redirection * or {@link System#exit} trapping is done. *
  • {@link #createLibraryPath} produces an empty path. *
* This basic implementation is helpful in unit tests and perhaps in standalone usage * of other libraries. * @return some execution engine implementation (never null) * @since 2.16 */ public static ExecutionEngine getDefault() { ExecutionEngine ee = (ExecutionEngine) Lookup.getDefault().lookup(ExecutionEngine.class); if (ee == null) { ee = new Trivial(); } return ee; } /** * Dummy fallback implementation, useful for unit tests. */ private static final class Trivial extends ExecutionEngine { public Trivial() {} protected NbClassPath createLibraryPath() { return new NbClassPath(new String[0]); } protected PermissionCollection createPermissions(CodeSource cs, InputOutput io) { PermissionCollection allPerms = new Permissions(); allPerms.add(new AllPermission()); allPerms.setReadOnly(); return allPerms; } public ExecutorTask execute(String name, Runnable run, InputOutput io) { int resultValue = 0; try { run.run(); } catch (RuntimeException x) { x.printStackTrace(); resultValue = 1; } return new ET(run, resultValue, name); } private static final class ET extends ExecutorTask { private final int resultValue; private final String name; public ET(Runnable run, int resultValue, String name) { super(run); this.resultValue = resultValue; this.name = name; } public void stop() {} public int result() { return resultValue; } public InputOutput getInputOutput() { return IOProvider.getDefault().getIO(name, true); } } } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.