KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > buildplugin > BuildPluginEventTask


1 package org.antmod.buildplugin;
2
3 import org.apache.tools.ant.BuildException;
4 import org.apache.tools.ant.Project;
5 import org.apache.tools.ant.Task;
6 import org.apache.tools.ant.types.EnumeratedAttribute;
7
8 /**
9  * An Ant task for handling build plugins inside the buildfiles of Antmod.
10  * This task invokes the buildplugins, passing the current module, release, and target
11  * to the buildplugin. This task is used internally by Antmod, but can also be used
12  * in 'local.build.xml' files if users want that.
13  *
14  * @author Klaas Waslander
15  */

16 public class BuildPluginEventTask extends Task {
17
18     private Level level;
19     private String JavaDoc eventName;
20
21     /**
22      * Public default constructor.
23      */

24     public BuildPluginEventTask() {
25     }
26
27     /** Invoked by Ant */
28     public void setLevel(Level level) {
29         this.level = level;
30     }
31
32     /** Invoked by Ant */
33     public void setEventName(String JavaDoc eventName) {
34         this.eventName = eventName;
35     }
36     
37     private String JavaDoc constructTargetName() {
38         String JavaDoc trimmedName = this.eventName.trim();
39         if (trimmedName.equalsIgnoreCase("-pre") || trimmedName.equalsIgnoreCase("pre")) {
40             return "event-" + getOwningTarget().getName() + "-pre";
41         } else if (trimmedName.equalsIgnoreCase("-post") || trimmedName.equalsIgnoreCase("post")) {
42                 return "event-" + getOwningTarget().getName() + "-post";
43         } else {
44             return "event-" + trimmedName;
45         }
46     }
47
48     /**
49      * Fire an event at on one or more build plugins.
50      * @throws BuildException
51      */

52     public void execute() throws BuildException {
53         if (this.level == null) {
54             throw new BuildException("Level unset: should be set to either 'release' or 'level'.");
55         }
56         if (this.eventName == null) {
57             throw new BuildException("Eventname unset, set it to the event you want to fire to the buildplugins.");
58         }
59
60         String JavaDoc levelValue = this.level.getValue();
61         if (Level.RELEASE.equals(levelValue)) {
62             antOnPlugins(BuildPlugin.BUILDLEVEL_RELEASE, constructTargetName());
63         }
64         else if (Level.MODULE.equals(levelValue)) {
65                 antOnPlugins(BuildPlugin.BUILDLEVEL_MODULE, constructTargetName());
66         }
67         else {
68             throw new BuildException("Invalid 'level' attribute value: should be either 'release' or 'module'.");
69         }
70     }
71
72     
73     private void antOnPlugins(int buildLevel, String JavaDoc antTarget) {
74         //getProject().log("Firing PluginEvent \"" + antTarget + "\" to all plugins...", Project.MSG_WARN);
75
BuildPlugin[] pluginList = BuildPluginFactory.getBuildPlugins(getProject());
76         for (int i = pluginList.length; i-- > 0;) {
77             BuildPlugin plugin = pluginList[i];
78             antOnPlugin(plugin, buildLevel, antTarget);
79         }
80     }
81
82     private void antOnPlugin(BuildPlugin plugin, int buildLevel, String JavaDoc antTarget) {
83         if (plugin.hasAntTarget(buildLevel, antTarget)) {
84             plugin.invokeAntTarget(getProject(), buildLevel, antTarget);
85         }
86     }
87
88
89     public static class Level extends EnumeratedAttribute {
90         public static String JavaDoc RELEASE = "release";
91         public static String JavaDoc MODULE = "module";
92
93         private static String JavaDoc[] values =
94             new String JavaDoc[] {
95                 RELEASE,
96                 MODULE};
97
98         public String JavaDoc[] getValues() {
99             return Level.values;
100         }
101
102     }
103 }
104
Popular Tags