001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant;
019:
020: import java.util.EventObject;
021:
022: /**
023: * Class representing an event occurring during a build. An
024: * event is built by specifying either a project, a task or a target.
025: * A project level event will only have a project reference;
026: * a target level event will have project and target references;
027: * a task level event will have project, target and task references.
028: *
029: */
030: public class BuildEvent extends EventObject {
031:
032: /** Project which emitted the event. */
033: private Project project;
034: /** Target which emitted the event, if specified. */
035: private Target target;
036: /** Task which emitted the event, if specified. */
037: private Task task;
038: /**
039: * Message associated with the event. This is only used for
040: * "messageLogged" events.
041: */
042: private String message;
043: /**
044: * The priority of the message, for "messageLogged" events.
045: */
046: private int priority = Project.MSG_VERBOSE;
047: /**
048: * The exception associated with this event, if any.
049: * This is only used for "messageLogged", "taskFinished", "targetFinished",
050: * and "buildFinished" events.
051: */
052: private Throwable exception;
053:
054: /**
055: * Construct a BuildEvent for a project level event.
056: *
057: * @param project the project that emitted the event.
058: * Should not be <code>null</code>.
059: */
060: public BuildEvent(Project project) {
061: super (project);
062: this .project = project;
063: this .target = null;
064: this .task = null;
065: }
066:
067: /**
068: * Construct a BuildEvent for a target level event.
069: * The project associated with the event is derived
070: * from the given target.
071: *
072: * @param target the target that emitted the event.
073: * Must not be <code>null</code>.
074: */
075: public BuildEvent(Target target) {
076: super (target);
077: this .project = target.getProject();
078: this .target = target;
079: this .task = null;
080: }
081:
082: /**
083: * Construct a BuildEvent for a task level event.
084: * The project and target associated with the event
085: * are derived from the given task.
086: *
087: * @param task the task that emitted the event.
088: * Must not be <code>null</code>.
089: */
090: public BuildEvent(Task task) {
091: super (task);
092: this .project = task.getProject();
093: this .target = task.getOwningTarget();
094: this .task = task;
095: }
096:
097: /**
098: * Sets the message and priority associated with this event.
099: * This is used for "messageLogged" events.
100: *
101: * @param message the message to be associated with this event.
102: * Should not be <code>null</code>.
103: * @param priority the priority to be associated with this event,
104: * as defined in the {@link Project Project} class.
105: *
106: * @see BuildListener#messageLogged(BuildEvent)
107: */
108: public void setMessage(String message, int priority) {
109: this .message = message;
110: this .priority = priority;
111: }
112:
113: /**
114: * Sets the exception associated with this event. This is used
115: * for "messageLogged", "taskFinished", "targetFinished", and "buildFinished"
116: * events.
117: *
118: * @param exception The exception to be associated with this event.
119: * May be <code>null</code>.
120: *
121: * @see BuildListener#messageLogged(BuildEvent)
122: * @see BuildListener#taskFinished(BuildEvent)
123: * @see BuildListener#targetFinished(BuildEvent)
124: * @see BuildListener#buildFinished(BuildEvent)
125: */
126: public void setException(Throwable exception) {
127: this .exception = exception;
128: }
129:
130: /**
131: * Returns the project that fired this event.
132: *
133: * @return the project that fired this event
134: */
135: public Project getProject() {
136: return project;
137: }
138:
139: /**
140: * Returns the target that fired this event.
141: *
142: * @return the project that fired this event, or <code>null</code>
143: * if this event is a project level event.
144: */
145: public Target getTarget() {
146: return target;
147: }
148:
149: /**
150: * Returns the task that fired this event.
151: *
152: * @return the task that fired this event, or <code>null</code>
153: * if this event is a project or target level event.
154: */
155: public Task getTask() {
156: return task;
157: }
158:
159: /**
160: * Returns the logging message. This field will only be set
161: * for "messageLogged" events.
162: *
163: * @return the message associated with this event, or <code>null</code>
164: * if no message has been set.
165: *
166: * @see BuildListener#messageLogged(BuildEvent)
167: */
168: public String getMessage() {
169: return message;
170: }
171:
172: /**
173: * Returns the priority of the logging message. This field will only
174: * be set for "messageLogged" events. The meaning of this priority
175: * is as specified by the constants in the {@link Project Project} class.
176: *
177: * @return the priority associated with this event.
178: *
179: * @see BuildListener#messageLogged(BuildEvent)
180: */
181: public int getPriority() {
182: return priority;
183: }
184:
185: /**
186: * Returns the exception that was thrown, if any. This field will only
187: * be set for "messageLogged", "taskFinished", "targetFinished", and "buildFinished"
188: * events.
189: *
190: * @return the exception associated with this exception, or
191: * <code>null</code> if no exception has been set.
192: *
193: * @see BuildListener#messageLogged(BuildEvent)
194: * @see BuildListener#taskFinished(BuildEvent)
195: * @see BuildListener#targetFinished(BuildEvent)
196: * @see BuildListener#buildFinished(BuildEvent)
197: */
198: public Throwable getException() {
199: return exception;
200: }
201: }
|