Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/eclipse/builder/AbstractBuilder.java


1   package com.port80.eclipse.builder;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.Map;
6   
7   import org.eclipse.core.internal.resources.Project;
8   import org.eclipse.core.resources.IProject;
9   import org.eclipse.core.resources.IResource;
10  import org.eclipse.core.resources.IncrementalProjectBuilder;
11  import org.eclipse.core.runtime.CoreException;
12  import org.eclipse.core.runtime.IProgressMonitor;
13  import org.eclipse.core.runtime.IStatus;
14  import org.eclipse.core.runtime.Status;
15  import org.eclipse.core.runtime.SubProgressMonitor;
16  import org.eclipse.jdt.core.IJavaProject;
17  import org.eclipse.jdt.internal.core.JavaModelManager;
18  import org.eclipse.jdt.internal.ui.JavaPlugin;
19  import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
20  
21  import com.port80.eclipse.util.IProcessMonitorListener;
22  import com.port80.eclipse.util.ProcessMonitor;
23  
24  /**
25   * @author chrisl Jun 2, 2002
26   */
27  public abstract class AbstractBuilder extends IncrementalProjectBuilder {
28  
29    public abstract String getBuilderName();
30    public abstract String[] getCommand(Map args);
31  
32    private static final String NAME = "AbstractBuilder";
33    private static final boolean DEBUG = true;
34    private static final boolean VERBOSE = false;
35  
36    public AbstractBuilder() {
37      super();
38    }
39  
40    /**
41     * @param kind the kind of build being requested (defined in IncrementalProjectBuilder). Valid values are
42     * <ul>
43     * <li> <code>FULL_BUILD</code> - indicates a full build.</li>
44     * <li> <code>INCREMENTAL_BUILD</code> - indicates an incremental build.</li>
45     * <li> <code>AUTO_BUILD</code> - indicates an automatically triggered
46     *   incremental build (auto-building on).</li>
47     * </ul>
48     * @param args a table of builder-specific arguments keyed by argument name
49     *    (key type: <code>String</code>, value type: <code>String</code>);
50     *    <code>null</code> is equivalent to an empty map
51     * @param monitor a progress monitor, or <code>null</code> if progress
52     *    reporting and cancellation are not desired
53     * @return the list of projects for which this builder would like deltas the next
54     *              time it is run or <code>null</code> if none
55     * @exception CoreException if this build fails.
56     * @see IProject#build
57     * @see InternalBuilder#build(int, Map, IProgressMonitor)
58     */
59    protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
60      IProject project = getProject();
61      String projectname = project.getName();
62      monitor.beginTask(projectname, 100);
63      String work_dir = project.getLocation().toString();
64      if (VERBOSE) {
65        IJavaProject javaproject =
66          JavaModelManager.getJavaModelManager().getJavaModel().findJavaProject(project);
67        String outdir = javaproject.getOutputLocation().toString();
68        System.err.println(
69          this +": project=" + projectname + ", dir=" + work_dir + ", outdir=" + outdir);
70        System.err.println("\t" + project.getName() + ": dir=" + work_dir + ": out=" + outdir);
71      }
72      return build(project, work_dir, args, monitor);
73    }
74  
75    public IProject[] build(IProject project, String work_dir, Map args, IProgressMonitor monitor)
76      throws CoreException {
77      String builder_name = getBuilderName();
78      if (DEBUG) {
79        StringBuffer buf = new StringBuffer(NAME);
80        buf.append(".build(): builder=");
81        buf.append(builder_name);
82        buf.append(", command=");
83        String[] a = getCommand(args);
84        for (int i = 0; i < a.length; ++i) {
85          if (i != 0)
86            buf.append(' ');
87          buf.append(a[i]);
88        }
89        System.err.println(buf.toString());
90      }
91      int ret = -1;
92      try {
93        Process proc = Runtime.getRuntime().exec(getCommand(args), null, new File(work_dir));
94        ProcessMonitor m = new ProcessMonitor(proc, builder_name, new IProcessMonitorListener() {
95          public void terminated(int code) {
96          }
97          public void outputAppended(String text) {
98            System.out.print(text);
99          }
100         public void errorAppended(String text) {
101           System.err.print(text);
102         }
103       });
104       // Wait for everything to finish
105       m.waitFor();
106       ret = m.exitValue();
107       //FIXME: This don't work, need to generate a proper launch configuration on the fly. 
108       //        console.setViewerInput(
109       //          new RuntimeProcess(
110       //            new Launch(null, ILaunchManager.RUN_MODE, null),
111       //            proc,
112       //            builder_name,
113       //            null));
114       //        proc.waitFor();
115       //        ret = proc.exitValue();
116     } catch (IOException e) {
117       if (VERBOSE)
118         System.err.println(e.toString());
119       ret = 1;
120     }
121     if (ret != 0) {
122       if (VERBOSE)
123         System.err.println(builder_name + ".build(): FAIL: " + ret);
124       throw new CoreException(
125         new Status(
126           Status.ERROR,
127           builder_name,
128           ret,
129           "build(): failed: project=" + project.getName(),
130           null));
131     } else {
132       if (VERBOSE)
133         System.err.println(new StatusInfo(IStatus.INFO, builder_name + ".build(): OK"));
134     }
135     monitor.worked(50);
136     // Refresh from local
137     try {
138       ((Project) project).refreshLocal(
139         IResource.DEPTH_INFINITE,
140         new SubProgressMonitor(monitor, 50, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
141     } catch (Exception e) {
142       JavaPlugin.log(e);
143     } finally {
144       //Thread.sleep(1000);
145     }
146     monitor.done();
147     return null;
148   }
149 
150   /**
151    * Build without progress monitor and refresh, use for building in background.
152    */
153   public IProject[] build(IProject project, String work_dir, Map args) throws CoreException {
154     String builder_name = getBuilderName();
155     if (DEBUG) {
156       StringBuffer buf = new StringBuffer(NAME);
157       buf.append(".build(): builder=");
158       buf.append(builder_name);
159       buf.append(", command=");
160       String[] a = getCommand(args);
161       for (int i = 0; i < a.length; ++i) {
162         if (i != 0)
163           buf.append(' ');
164         buf.append(a[i]);
165       }
166       System.err.println(buf.toString());
167     }
168     int ret = -1;
169     try {
170       Process proc = Runtime.getRuntime().exec(getCommand(args), null, new File(work_dir));
171       ProcessMonitor m = new ProcessMonitor(proc, builder_name, new IProcessMonitorListener() {
172         public void terminated(int code) {
173         }
174         public void outputAppended(String text) {
175           System.out.print(text);
176         }
177         public void errorAppended(String text) {
178           System.err.print(text);
179         }
180       });
181       // Wait for everything to finish
182       m.waitFor();
183       ret = m.exitValue();
184     } catch (IOException e) {
185       if (VERBOSE)
186         System.err.println(e.toString());
187       ret = 1;
188     }
189     if (ret != 0) {
190       if (VERBOSE)
191         System.err.println(builder_name + ".build(): FAIL: " + ret);
192       throw new CoreException(
193         new Status(
194           Status.ERROR,
195           builder_name,
196           ret,
197           "build(): failed: project=" + project.getName(),
198           null));
199     } else {
200       if (VERBOSE)
201         System.err.println(new StatusInfo(IStatus.INFO, builder_name + ".build(): OK"));
202     }
203     return null;
204   }
205 
206   public String toString() {
207     return getBuilderName();
208   }
209 }