KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > AntMessageImpl


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.ant;
8
9 import org.apache.tools.ant.BuildEvent;
10 import org.apache.tools.ant.BuildListener;
11 import org.apache.tools.ant.Task;
12
13 import fr.jayasoft.ivy.util.Message;
14 import fr.jayasoft.ivy.util.MessageImpl;
15
16 /**
17  * Implementation of the simple message facility for ant.
18  *
19  * @author Xavier Hanin
20  */

21 public class AntMessageImpl implements MessageImpl {
22     private Task _task;
23
24     private static long _lastProgressFlush = 0;
25     private static StringBuffer JavaDoc _buf = new StringBuffer JavaDoc();
26
27     /**
28      * @param task
29      */

30     public AntMessageImpl(Task task) {
31         _task = task;
32         task.getProject().addBuildListener(new BuildListener() {
33             private int stackDepth = 0;
34             public void buildFinished(BuildEvent event) {
35             }
36             public void buildStarted(BuildEvent event) {
37             }
38             public void targetStarted(BuildEvent event) {
39             }
40             public void targetFinished(BuildEvent event) {
41             }
42             public void taskStarted(BuildEvent event) {
43                 stackDepth++;
44             }
45             public void taskFinished(BuildEvent event) {
46                 //NB: There is somtimes task created by an other task
47
//in that case, we should not uninit Message. The log should stay associated
48
//with the initial task, except if it was an antcall, ant or subant target
49
//NB2 : Testing the identity of the task is not enought, event.getTask() return
50
//an instance of UnknownElement is wrapping the concrete instance
51
if (stackDepth==0) {
52                     Message.uninit();
53                     event.getProject().removeBuildListener(this);
54                 }
55                 stackDepth--;
56             }
57             public void messageLogged(BuildEvent event) {
58             }
59         });
60     }
61
62     public void log(String JavaDoc msg, int level) {
63         _task.log(msg, level);
64     }
65     
66     public void rawlog(String JavaDoc msg, int level) {
67         _task.getProject().log(msg, level);
68     }
69
70     public void progress() {
71         _buf.append(".");
72         if (_lastProgressFlush == 0) {
73             _lastProgressFlush = System.currentTimeMillis();
74         }
75         if (_task != null) {
76             // log with ant causes a new line -> we do it only once in a while
77
if (System.currentTimeMillis() - _lastProgressFlush > 1500) {
78                 _task.log(_buf.toString());
79                 _buf.setLength(0);
80                 _lastProgressFlush = System.currentTimeMillis();
81             }
82         }
83     }
84     
85     public void endProgress(String JavaDoc msg) {
86         _task.log(_buf + msg);
87         _buf.setLength(0);
88         _lastProgressFlush = 0;
89     }
90 }
91
Popular Tags