KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > antsupport > logger > AntProcessBuildLogger


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Matt Conway - Patch for Bug 28052
11  *******************************************************************************/

12
13 package org.eclipse.ant.internal.ui.antsupport.logger;
14
15 import java.io.BufferedReader JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.StringReader JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.tools.ant.BuildEvent;
23 import org.apache.tools.ant.Location;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Target;
26 import org.apache.tools.ant.util.FileUtils;
27 import org.eclipse.ant.internal.core.AbstractEclipseBuildLogger;
28 import org.eclipse.ant.internal.ui.AntUtil;
29 import org.eclipse.ant.internal.ui.ExternalHyperlink;
30 import org.eclipse.ant.internal.ui.IAntUIConstants;
31 import org.eclipse.ant.internal.ui.antsupport.AntSupportMessages;
32 import org.eclipse.ant.internal.ui.antsupport.logger.util.AntDebugState;
33 import org.eclipse.ant.internal.ui.launchConfigurations.AntProcess;
34 import org.eclipse.ant.internal.ui.launchConfigurations.AntStreamMonitor;
35 import org.eclipse.ant.internal.ui.launchConfigurations.AntStreamsProxy;
36 import org.eclipse.ant.internal.ui.launchConfigurations.TaskLinkManager;
37 import org.eclipse.core.resources.IFile;
38 import org.eclipse.core.runtime.OperationCanceledException;
39 import org.eclipse.debug.core.DebugPlugin;
40 import org.eclipse.debug.core.model.IProcess;
41 import org.eclipse.debug.ui.console.FileLink;
42 import org.eclipse.jface.text.IRegion;
43 import org.eclipse.jface.text.Region;
44 import org.eclipse.ui.console.IHyperlink;
45     
46 public class AntProcessBuildLogger extends NullBuildLogger {
47     
48     private File JavaDoc fBuildFileParent= null;
49     private long fStartTime;
50     private Map JavaDoc fFileNameToIFile= new HashMap JavaDoc();
51
52     /**
53      * Associated process - discovered as needed to log messages
54      */

55     private AntProcess fProcess = null;
56     
57     protected void logMessage(String JavaDoc message, BuildEvent event, int overridePriority) {
58         int priority= overridePriority;
59         if (priority == -1) {
60             priority= event.getPriority();
61         }
62         
63         if (priority > getMessageOutputLevel()) {
64             return;
65         }
66         AntProcess antProcess = getAntProcess(fProcessId);
67         if (antProcess == null) {
68             return;
69         }
70         
71         StringBuffer JavaDoc fullMessage= new StringBuffer JavaDoc();
72          if (!loggingToLogFile()) {
73             fullMessage.append(System.getProperty("line.separator")); //$NON-NLS-1$
74
}
75         if (event.getException() == null && event.getTask() != null && !fEmacsMode) {
76             adornMessage(event, fullMessage);
77         } else {
78             fullMessage.append(message);
79         }
80         message= fullMessage.toString();
81         
82         if (loggingToLogFile()) {
83             logMessageToLogFile(message, priority);
84         } else {
85             AntStreamMonitor monitor = getMonitor(priority);
86             monitor.append(message);
87         }
88     }
89
90     /**
91      * Builds a right justified task prefix for the given build event, placing it
92      * in the given string buffer. Creates the hyperlinks for the task prefix.
93      *
94      * @param event build event
95      * @param fullMessage buffer to place task prefix in
96      */

97     private void adornMessage(BuildEvent event, StringBuffer JavaDoc fullMessage) {
98         String JavaDoc name = event.getTask().getTaskName();
99         if (name == null) {
100             name = "null"; //$NON-NLS-1$
101
}
102         Location location = event.getTask().getLocation();
103         StringBuffer JavaDoc column= new StringBuffer JavaDoc();
104         int size = IAntUIConstants.LEFT_COLUMN_SIZE - (name.length() + 3);
105         for (int i = 0; i < size; i++) {
106             column.append(' ');
107         }
108         StringBuffer JavaDoc labelBuff= new StringBuffer JavaDoc();
109         labelBuff.append('[');
110         labelBuff.append(name);
111         labelBuff.append("] "); //$NON-NLS-1$
112

113         int offset = Math.max(size, 0) + 1;
114         String JavaDoc label= labelBuff.toString();
115         if (event.getMessage() == null) {
116             return;
117         }
118         try {
119             BufferedReader JavaDoc r = new BufferedReader JavaDoc(new StringReader JavaDoc(event.getMessage()));
120             String JavaDoc line = r.readLine();
121             fullMessage.append(column);
122             appendAndLink(fullMessage, location, label, offset, line);
123             line = r.readLine();
124             while (line != null) {
125                 fullMessage.append(System.getProperty("line.separator")); //$NON-NLS-1$
126
fullMessage.append(column);
127                 appendAndLink(fullMessage, location, label, offset, line);
128                 line = r.readLine();
129             }
130         } catch (IOException JavaDoc e) {
131             if (event.getMessage() != null) {
132                 fullMessage.append(label).append(event.getMessage());
133             }
134         }
135     }
136     
137     private void appendAndLink(StringBuffer JavaDoc fullMessage, Location location, String JavaDoc label, int offset, String JavaDoc line) {
138         fullMessage.append(label);
139         fullMessage.append(line);
140         if (location != null) {
141             String JavaDoc newLine= (label + line).trim();
142             IRegion region= new Region(offset, label.length() - 3); // only want the name length "[name] "
143
IHyperlink link= getLocationLink(location);
144             if (link != null) {
145                 TaskLinkManager.addTaskHyperlink(getAntProcess(fProcessId), link, region, newLine);
146             }
147         }
148     }
149
150     private AntStreamMonitor getMonitor(int priority) {
151         AntStreamsProxy proxy = (AntStreamsProxy)fProcess.getStreamsProxy();
152         AntStreamMonitor monitor = null;
153         switch (priority) {
154             case Project.MSG_INFO:
155                 monitor = (AntStreamMonitor)proxy.getOutputStreamMonitor();
156                 break;
157             case Project.MSG_ERR:
158                 monitor = (AntStreamMonitor)proxy.getErrorStreamMonitor();
159                 break;
160             case Project.MSG_DEBUG:
161                 monitor = (AntStreamMonitor)proxy.getDebugStreamMonitor();
162                 break;
163             case Project.MSG_WARN:
164                 monitor = (AntStreamMonitor)proxy.getWarningStreamMonitor();
165                 break;
166             case Project.MSG_VERBOSE:
167                 monitor = (AntStreamMonitor)proxy.getVerboseStreamMonitor();
168                 break;
169         }
170         return monitor;
171     }
172
173     private void logMessageToLogFile(String JavaDoc message, int priority) {
174         if (priority == Project.MSG_ERR) {
175             getErrorPrintStream().println(message);
176         } else {
177             getOutputPrintStream().println(message);
178         }
179     }
180     
181     /**
182      * Returns a hyperlink for the given task, or <code>null</code> if unable to
183      * parse a valid location for the task. The link is set to exist at the specified
184      * offset and length.
185      *
186      * @return hyper link, or <code>null</code>
187      */

188     private IHyperlink getLocationLink(Location location) {
189         if (location != null && !location.equals(Location.UNKNOWN_LOCATION)) {
190             try {
191                 String JavaDoc fileName= location.getFileName();
192                 IFile file= (IFile) fFileNameToIFile.get(fileName);
193                 int lineNumber= location.getLineNumber();
194                 if (file != null) {
195                     return new FileLink(file, null, -1, -1, lineNumber);
196                 }
197                 file= AntUtil.getFileForLocation(fileName, fBuildFileParent);
198                 if (file != null) {
199                     fFileNameToIFile.put(fileName, file);
200                     return new FileLink(file, null, -1, -1, lineNumber);
201                 }
202                 //maintain backwards compatibility
203
File JavaDoc javaIOFile= FileUtils.newFileUtils().resolveFile(fBuildFileParent, fileName);
204                 if (javaIOFile.exists()) {
205                     return new ExternalHyperlink(javaIOFile, lineNumber);
206                 }
207             } catch (NoSuchMethodError JavaDoc e) {
208                 //support for Ant older than 1.6
209
return AntUtil.getLocationLink(location.toString(), fBuildFileParent);
210             }
211         }
212         return null;
213     }
214     
215     /**
216      * Returns the associated Ant process, finding it if necessary, if not
217      * already found.
218      */

219     protected AntProcess getAntProcess(String JavaDoc processId) {
220         if (fProcess == null && processId != null) {
221             IProcess[] all = DebugPlugin.getDefault().getLaunchManager().getProcesses();
222             for (int i = 0; i < all.length; i++) {
223                 IProcess process = all[i];
224                 if (process instanceof AntProcess && processId.equals(process.getAttribute(AbstractEclipseBuildLogger.ANT_PROCESS_ID))) {
225                     fProcess = (AntProcess)process;
226                     break;
227                 }
228             }
229         }
230         return fProcess;
231     }
232
233     /* (non-Javadoc)
234      * Set the start time.
235      *
236      * @see org.apache.tools.ant.BuildListener#buildStarted(org.apache.tools.ant.BuildEvent)
237      */

238     public void buildStarted(BuildEvent event) {
239         fStartTime= System.currentTimeMillis();
240     }
241     
242     /* (non-Javadoc)
243      * @see org.apache.tools.ant.BuildListener#buildFinished(org.apache.tools.ant.BuildEvent)
244      */

245     public void buildFinished(BuildEvent event) {
246         String JavaDoc message= handleException(event);
247         if (message != null) {
248             try {
249                 BufferedReader JavaDoc r = new BufferedReader JavaDoc(new StringReader JavaDoc(message));
250                 String JavaDoc line = r.readLine();
251                 logMessage(line, event, Project.MSG_ERR);
252                 line = r.readLine();
253                 AntProcess antProcess = getAntProcess(fProcessId);
254                 while (line != null) {
255                     logMessage(line, event, Project.MSG_ERR);
256                     if (!message.startsWith("Total time:")) { //$NON-NLS-1$
257
AntUtil.linkBuildFailedMessage(line, antProcess);
258                     }
259                     line = r.readLine();
260                 }
261                 logMessage("", event, Project.MSG_ERR); //$NON-NLS-1$
262
} catch (IOException JavaDoc e) {
263             }
264         }
265         fHandledException= null;
266         fBuildFileParent= null;
267         if (!(event.getException() instanceof OperationCanceledException)) {
268             logMessage(getTimeString(System.currentTimeMillis() - fStartTime), event, fMessageOutputLevel);
269         }
270         fProcess= null;
271         event.getProject().removeBuildListener(this);
272         fFileNameToIFile= null;
273     }
274     
275     private String JavaDoc getTimeString(long milliseconds) {
276             long seconds = milliseconds / 1000;
277             long minutes = seconds / 60;
278             seconds= seconds % 60;
279         
280             StringBuffer JavaDoc result= new StringBuffer JavaDoc(AntSupportMessages.AntProcessBuildLogger_Total_time);
281             if (minutes > 0) {
282                 result.append(minutes);
283                 if (minutes > 1) {
284                     result.append(AntSupportMessages.AntProcessBuildLogger__minutes_2);
285                 } else {
286                     result.append(AntSupportMessages.AntProcessBuildLogger__minute_3);
287                 }
288             }
289             if (seconds > 0) {
290                 if (minutes > 0) {
291                     result.append(' ');
292                 }
293                 result.append(seconds);
294             
295                 if (seconds > 1) {
296                     result.append(AntSupportMessages.AntProcessBuildLogger__seconds_4);
297                 } else {
298                     result.append(AntSupportMessages.AntProcessBuildLogger__second_5);
299                 }
300             }
301             if (seconds == 0 && minutes == 0) {
302                 result.append(milliseconds);
303                 result.append(AntSupportMessages.AntProcessBuildLogger__milliseconds_6);
304             }
305             
306             result.append(System.getProperty("line.separator")); //$NON-NLS-1$
307
return result.toString();
308         }
309     
310     /* (non-Javadoc)
311      * @see org.apache.tools.ant.BuildListener#messageLogged(org.apache.tools.ant.BuildEvent)
312      */

313     public void messageLogged(BuildEvent event) {
314         if (event.getPriority() > getMessageOutputLevel()) {
315             return;
316         }
317         if (event.getMessage() != null && event.getMessage().length() > 0) {
318             logMessage(event.getMessage(), event, -1);
319         }
320     }
321
322     /* (non-Javadoc)
323      * @see org.apache.tools.ant.BuildListener#targetStarted(org.apache.tools.ant.BuildEvent)
324      */

325     public void targetStarted(BuildEvent event) {
326         if (Project.MSG_INFO > getMessageOutputLevel()) {
327             return;
328         }
329         Target target= event.getTarget();
330         StringBuffer JavaDoc msg= new StringBuffer JavaDoc(System.getProperty("line.separator")); //$NON-NLS-1$
331
String JavaDoc targetName= target.getName();
332         msg.append(targetName);
333         msg.append(':');
334         String JavaDoc message= msg.toString();
335         Location location= AntDebugState.getLocation(target);
336         if (location != null && location != Location.UNKNOWN_LOCATION) {
337             IRegion region= new Region(0, targetName.length());
338             IHyperlink link= getLocationLink(location);
339             if (link != null) {
340                 TaskLinkManager.addTaskHyperlink(getAntProcess(fProcessId), link, region, message.trim());
341             }
342         }
343         logMessage(message, event, Project.MSG_INFO);
344     }
345     
346     private boolean loggingToLogFile() {
347         //check if user has designated to log to a log file
348
return getErrorPrintStream() != null && getErrorPrintStream() != System.err;
349     }
350 }
351
Popular Tags