1. /*
  2. * Copyright 2000,2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant;
  18. import java.util.EventObject;
  19. /**
  20. * Class representing an event occurring during a build. An
  21. * event is built by specifying either a project, a task or a target.
  22. * A project level event will only have a project reference;
  23. * a target level event will have project and target references;
  24. * a task level event will have project, target and task references.
  25. *
  26. */
  27. public class BuildEvent extends EventObject {
  28. /** Project which emitted the event. */
  29. private Project project;
  30. /** Target which emitted the event, if specified. */
  31. private Target target;
  32. /** Task which emitted the event, if specified. */
  33. private Task task;
  34. /**
  35. * Message associated with the event. This is only used for
  36. * "messageLogged" events.
  37. */
  38. private String message;
  39. /**
  40. * The priority of the message, for "messageLogged" events.
  41. */
  42. private int priority = Project.MSG_VERBOSE;
  43. /**
  44. * The exception associated with this event, if any.
  45. * This is only used for "taskFinished", "targetFinished",
  46. * and "buildFinished" events.
  47. */
  48. private Throwable exception;
  49. /**
  50. * Construct a BuildEvent for a project level event.
  51. *
  52. * @param project the project that emitted the event.
  53. * Should not be <code>null</code>.
  54. */
  55. public BuildEvent(Project project) {
  56. super(project);
  57. this.project = project;
  58. this.target = null;
  59. this.task = null;
  60. }
  61. /**
  62. * Construct a BuildEvent for a target level event.
  63. * The project associated with the event is derived
  64. * from the given target.
  65. *
  66. * @param target the target that emitted the event.
  67. * Must not be <code>null</code>.
  68. */
  69. public BuildEvent(Target target) {
  70. super(target);
  71. this.project = target.getProject();
  72. this.target = target;
  73. this.task = null;
  74. }
  75. /**
  76. * Construct a BuildEvent for a task level event.
  77. * The project and target associated with the event
  78. * are derived from the given task.
  79. *
  80. * @param task the task that emitted the event.
  81. * Must not be <code>null</code>.
  82. */
  83. public BuildEvent(Task task) {
  84. super(task);
  85. this.project = task.getProject();
  86. this.target = task.getOwningTarget();
  87. this.task = task;
  88. }
  89. /**
  90. * Sets the message and priority associated with this event.
  91. * This is used for "messageLogged" events.
  92. *
  93. * @param message the message to be associated with this event.
  94. * Should not be <code>null</code>.
  95. * @param priority the priority to be associated with this event,
  96. * as defined in the {@link Project Project} class.
  97. *
  98. * @see BuildListener#messageLogged(BuildEvent)
  99. */
  100. public void setMessage(String message, int priority) {
  101. this.message = message;
  102. this.priority = priority;
  103. }
  104. /**
  105. * Sets the exception associated with this event. This is used
  106. * for "taskFinished", "targetFinished", and "buildFinished"
  107. * events.
  108. *
  109. * @param exception The exception to be associated with this event.
  110. * May be <code>null</code>.
  111. *
  112. * @see BuildListener#taskFinished(BuildEvent)
  113. * @see BuildListener#targetFinished(BuildEvent)
  114. * @see BuildListener#buildFinished(BuildEvent)
  115. */
  116. public void setException(Throwable exception) {
  117. this.exception = exception;
  118. }
  119. /**
  120. * Returns the project that fired this event.
  121. *
  122. * @return the project that fired this event
  123. */
  124. public Project getProject() {
  125. return project;
  126. }
  127. /**
  128. * Returns the target that fired this event.
  129. *
  130. * @return the project that fired this event, or <code>null</code>
  131. * if this event is a project level event.
  132. */
  133. public Target getTarget() {
  134. return target;
  135. }
  136. /**
  137. * Returns the task that fired this event.
  138. *
  139. * @return the task that fired this event, or <code>null</code>
  140. * if this event is a project or target level event.
  141. */
  142. public Task getTask() {
  143. return task;
  144. }
  145. /**
  146. * Returns the logging message. This field will only be set
  147. * for "messageLogged" events.
  148. *
  149. * @return the message associated with this event, or <code>null</code>
  150. * if no message has been set.
  151. *
  152. * @see BuildListener#messageLogged(BuildEvent)
  153. */
  154. public String getMessage() {
  155. return message;
  156. }
  157. /**
  158. * Returns the priority of the logging message. This field will only
  159. * be set for "messageLogged" events. The meaning of this priority
  160. * is as specified by the constants in the {@link Project Project} class.
  161. *
  162. * @return the priority associated with this event.
  163. *
  164. * @see BuildListener#messageLogged(BuildEvent)
  165. */
  166. public int getPriority() {
  167. return priority;
  168. }
  169. /**
  170. * Returns the exception that was thrown, if any. This field will only
  171. * be set for "taskFinished", "targetFinished", and "buildFinished"
  172. * events.
  173. *
  174. * @return the exception associated with this exception, or
  175. * <code>null</code> if no exception has been set.
  176. *
  177. * @see BuildListener#taskFinished(BuildEvent)
  178. * @see BuildListener#targetFinished(BuildEvent)
  179. * @see BuildListener#buildFinished(BuildEvent)
  180. */
  181. public Throwable getException() {
  182. return exception;
  183. }
  184. }