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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.compiler;

import java.io.IOException;
import java.util.List;
import java.util.LinkedList;
import org.openide.LifecycleManager;

import org.openide.awt.StatusDisplayer;
import org.openide.execution.*;
import org.openide.filesystems.*;
import org.openide.loaders.DataObject;
import org.openide.util.io.FoldingIOException;
import org.openide.util.NbBundle;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;

/** For external compilation - runs external compiler through std
* execution interface.
*
* @author Ales Novak
*/
final class CompilerExecutor extends ProcessExecutor {
    /** generated Serialized Version UID */
    static final long serialVersionUID = -2611259508166125914L;

    /** files to compile (String) */
    private List files;

    /** ExternalCompiler reference */
    private ExternalCompilerGroup eCompiler;
    /** description of external compiler - it is the "class" of this compiler*/ // NOI18N
    private NbProcessDescriptor nbDescriptor;
    /** ErrorExpression */
    private ExternalCompiler.ErrorExpression errorExpression;
    /** type of compiler */
    private Object compilerType;

    /**
    * @param ec is a reference to external compiler
    * @param filePath is compiled file
    */
    CompilerExecutor(
        ExternalCompilerGroup ecg,
        NbProcessDescriptor nbDescriptor,
        ExternalCompiler.ErrorExpression err,
        Object compilerType
    ) {
        files = new LinkedList ();
        this.eCompiler = ecg;
        this.nbDescriptor = nbDescriptor;
        this.errorExpression = err;
        this.compilerType = compilerType;
    }

    /** add a new file to files */
    public void addFile(String file) {
        files.add(file);
    }
    
    private static InputOutput getCompilerIO() {
        String outName = NbBundle.getBundle("org.openide.compiler.Bundle").getString ("CTL_CompileTab");
        return IOProvider.getDefault().getIO(outName, false);
    }

    /** Executes a compilation
    * @param ignoreme is ignored
    */
    public ExecutorTask execute(DataObject ignoreme) throws IOException {
        CERunnable run = new CERunnable(eCompiler, errorExpression, nbDescriptor, files, compilerType);
        synchronized (run) {
            ExecutorTask et = ExecutionEngine.getDefault().execute(
                NbBundle.getBundle(CompilerExecutor.class).getString("CTL_CompilationTask"),
                run, getCompilerIO());

            try {
                if (Thread.interrupted()) {
                    throw new InterruptedException();
                }
                run.wait(); // let the runnable be executed
                Throwable e = run.getException();
                if (e != null) {
                    if (e instanceof Error) {
                        throw (Error) e;
                    } else if (e instanceof RuntimeException) {
                        throw (RuntimeException) e;
                    } else {
                        throw new FoldingIOException(e);
                    }
                }
                CompilerSysProcess csp = run.getCompilerSysProcess();
                csp.setExecutorTask(et);
                return csp;
            } catch (InterruptedException e) {
                return new CompilerSysProcess.InterruptedProcess();
            }
        }
    }

    /** Getter for error expresions.
    */
    ExternalCompiler.ErrorExpression getErrorExpression () {
        return errorExpression;
    }

    /** @return descriptor
    */
    NbProcessDescriptor getDescriptor () {
        return nbDescriptor;
    }

    /** Instance are executed in ExecutionEngine */
    static class CERunnable implements Runnable {

        private ExternalCompilerGroup eeg;
        private ExternalCompiler.ErrorExpression errorExpression;
        private NbProcessDescriptor nbDescriptor;
        private Throwable t;
        private CompilerSysProcess csp;
        private List files;
        private Object compilerType;

        CERunnable(ExternalCompilerGroup eeg, ExternalCompiler.ErrorExpression errorExpression, NbProcessDescriptor nbDescriptor, List files, Object compilerType) {
            this.eeg = eeg;
            this.errorExpression = errorExpression;
            this.nbDescriptor = nbDescriptor;
            this.files = files;
            this.compilerType = compilerType;
        }

        public void run() {
            try {
                // Save all modified files
                LifecycleManager.getDefault().saveAll();
                String msg = eeg.getStatusLineText();

                StatusDisplayer.getDefault().setStatusText(msg);

                csp = new CompilerSysProcess(
                          this,
                          eeg,
                          eeg.createProcess(nbDescriptor,(String[]) files.toArray(new String[0]), compilerType),
                          errorExpression
                      );
            } catch (Exception e) {
                t = e;
            } finally {
                synchronized (this) {
                    notifyAll();
                }
            }
        }

        public Throwable getException() {
            return t;
        }
        public CompilerSysProcess getCompilerSysProcess() {
            return csp;
        }
    }
}
... 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.