KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.antmod.buildplugin;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.Hashtable JavaDoc;
9
10 import org.antmod.util.AntUtil;
11 import org.apache.commons.io.FileUtils;
12 import org.apache.tools.ant.Project;
13
14 /**
15  * Represents an Antmod build plugin.
16  *
17  * @author Klaas Waslander
18  */

19 public final class BuildPlugin {
20     public final static int BUILDLEVEL_MODULE = 1111;
21     public final static int BUILDLEVEL_RELEASE = 2222;
22
23     private File JavaDoc pluginDir;
24
25     private Project moduleAntProject;
26     private ArrayList JavaDoc moduleAntTargets;
27
28     private Project releaseAntProject;
29     private ArrayList JavaDoc releaseAntTargets;
30
31     /**
32      * Only to be instantiated by this package;
33      * this constructor uses reflection to get properties out of
34      * the given object, assuming the 'otherBuildPlugin' is a
35      * BuildPlugin loaded in another classloader instance.
36      */

37     BuildPlugin(Project invokingAntProject, Object JavaDoc otherBuildPlugin) {
38         this.pluginDir = (File JavaDoc)invokeGetter(otherBuildPlugin, "getPluginHome");
39         this.moduleAntProject = (Project)invokeGetter(otherBuildPlugin, "getModuleAntProject");
40         this.moduleAntTargets = (ArrayList JavaDoc)invokeGetter(otherBuildPlugin, "getModuleAntTargets");
41         this.releaseAntProject = (Project)invokeGetter(otherBuildPlugin, "getReleaseAntProject");
42         this.releaseAntTargets = (ArrayList JavaDoc)invokeGetter(otherBuildPlugin, "getReleaseAntTargets");
43         /*
44         if (moduleAntProject != null) {
45             moduleAntProject.log("[ " + invokingAntProject.getProperty("ant.file") + "] CACHED BUILD PLUGIN: " + pluginDir, Project.MSG_WARN);
46         }
47         else if (releaseAntProject != null) {
48             releaseAntProject.log("[ " + invokingAntProject.getProperty("ant.file") + "] CACHED BUILD PLUGIN: " + pluginDir, Project.MSG_WARN);
49         }
50         */

51     }
52
53     /** Only to be instantiated by this package */
54     BuildPlugin(Project antProject, File JavaDoc pluginDirectory) {
55         //antProject.log("[ " + antProject.getProperty("ant.file") + "] NEW BUILD PLUGIN: " + pluginDirectory, Project.MSG_WARN);
56
this.pluginDir = pluginDirectory;
57
58         // analyze buildfiles
59
//System.err.println("Scanning modulebuild.xml of " + getName() + "...");
60
File JavaDoc modulebuildFile = new File JavaDoc(pluginDirectory, "modulebuild.xml");
61         if (modulebuildFile.exists()) {
62             this.moduleAntProject = AntUtil.getAntProject(antProject, modulebuildFile, true);
63             if (this.moduleAntProject == null) {
64                 throw new RuntimeException JavaDoc("modulebuild.xml for plugin " + getName() + " not found or initialized");
65             }
66             //AntUtil.inheritRefs(antProject, this.moduleAntProject);
67
this.moduleAntProject.setProperty("antmod.plugins." + getName() + ".home", pluginDirectory.getAbsolutePath());
68             this.moduleAntTargets = AntUtil.getAntTargets(this.moduleAntProject);
69         }
70
71         //System.err.println("Scanning releaseebuild.xml of " + getName() + "...");
72
File JavaDoc releasebuildFile = new File JavaDoc(pluginDirectory, "releasebuild.xml");
73         if (releasebuildFile.exists()) {
74             this.releaseAntProject = AntUtil.getAntProject(antProject, releasebuildFile, true);
75             if (this.releaseAntProject == null) {
76                 throw new RuntimeException JavaDoc("releasebuild.xml for plugin " + getName() + " not found or initialized");
77             }
78             //AntUtil.inheritRefs(antProject, this.releaseAntProject);
79
this.releaseAntProject.setProperty("antmod.plugins." + getName() + ".home", pluginDirectory.getAbsolutePath());
80             this.releaseAntTargets = AntUtil.getAntTargets(this.releaseAntProject);
81         }
82         //System.err.println("Scanning done for " + getName() + "!");
83
}
84     
85     
86     private static Object JavaDoc invokeGetter(Object JavaDoc instance, String JavaDoc getMethod) {
87         Method JavaDoc m;
88         try {
89             m = instance.getClass().getMethod(getMethod, null);
90             return m.invoke(instance, null);
91         }
92         catch (Exception JavaDoc e) {
93             e.printStackTrace();
94         }
95         return null;
96     }
97     
98     /**
99      * The plugin home directory.
100      */

101     public File JavaDoc getPluginHome() {
102         return pluginDir;
103     }
104
105     /**
106      * The name of this build plugin.
107      */

108     public String JavaDoc getName() {
109         return pluginDir.getName();
110     }
111     
112     public Project getModuleAntProject() {
113         return moduleAntProject;
114     }
115     public ArrayList JavaDoc getModuleAntTargets() {
116         return moduleAntTargets;
117     }
118     public Project getReleaseAntProject() {
119         return releaseAntProject;
120     }
121     public ArrayList JavaDoc getReleaseAntTargets() {
122         return releaseAntTargets;
123     }
124
125     /**
126      * Check whether this plugin has the given Ant target in the given ant file.
127      * @param buildLevel Either BUILDLEVEL_MODULE or BUILDLEVEL_RELEASE
128      * @param antTarget The name of the Ant target that possibly exists for the given buildlevel
129      */

130     public boolean hasAntTarget(int buildLevel, String JavaDoc antTarget) {
131         if (buildLevel == BUILDLEVEL_MODULE) {
132             return this.moduleAntTargets != null && this.moduleAntTargets.contains(antTarget);
133         } else if (buildLevel == BUILDLEVEL_RELEASE) {
134             return this.releaseAntTargets != null && this.releaseAntTargets.contains(antTarget);
135         }
136         throw new IllegalArgumentException JavaDoc("Invalid build level - should be either BUILDLEVEL_MODULE or BUILDLEVEL_RELEASE");
137     }
138     
139     /**
140      * Invoke the given ant target of the plugin, properties created by the plugin are
141      * pushed back into the parentProject for reuse elsewhere in the Antmod builds.
142      * @param parentProject The project that receives the properties created by the plugin
143      * @param buildLevel Either BUILDLEVEL_MODULE or BUILDLEVEL_RELEASE
144      * @param antTarget The name of the Ant target to invoke for the given buildlevel
145      */

146     public void invokeAntTarget(Project parentProject, int buildLevel, String JavaDoc antTarget) {
147         if (hasAntTarget(buildLevel, antTarget)) {
148             // execute target
149
if (buildLevel == BUILDLEVEL_MODULE) {
150                 /*
151                 this.moduleAntProject.setBaseDir(parentProject.getBaseDir());
152                 this.moduleAntProject.setProperty("basedir",parentProject.getBaseDir().getAbsolutePath());
153                 AntUtil.inheritRefs(parentProject, this.moduleAntProject);
154                 passBackProperties("antmod.", parentProject, this.moduleAntProject);
155
156                 this.moduleAntProject.executeTarget(antTarget);
157                 passBackProperties("antmod." + getName(), this.moduleAntProject, parentProject);
158                 */

159
160                 // REMIND: need to re-create Ant project every time, because basedir is not properly changeable it seems...
161
Project newModuleProject = AntUtil.getAntProject(parentProject, new File JavaDoc(this.pluginDir, "modulebuild.xml"), true);
162                 AntUtil.inheritRefs(parentProject, newModuleProject);
163                 newModuleProject.executeTarget(antTarget);
164                 passBackProperties("antmod." + getName(), newModuleProject, parentProject);
165             }
166             else if (buildLevel == BUILDLEVEL_RELEASE) {
167                 /*
168                 this.releaseAntProject.setBaseDir(parentProject.getBaseDir());
169                 this.releaseAntProject.setProperty("basedir",parentProject.getBaseDir().getAbsolutePath());
170                 AntUtil.inheritRefs(parentProject, this.releaseAntProject);
171                 passBackProperties("antmod.", parentProject, this.releaseAntProject);
172
173                 this.releaseAntProject.executeTarget(antTarget);
174                 passBackProperties("antmod." + getName(), this.releaseAntProject, parentProject);
175                 */

176
177                 // REMIND: need to re-create Ant project every time, because basedir is not properly changeable it seems...
178
Project newReleaseProject = AntUtil.getAntProject(parentProject, new File JavaDoc(this.pluginDir, "releasebuild.xml"), true);
179                 AntUtil.inheritRefs(parentProject, newReleaseProject);
180                 newReleaseProject.executeTarget(antTarget);
181                 passBackProperties("antmod." + getName(), newReleaseProject, parentProject);
182             }
183             else {
184                 throw new IllegalArgumentException JavaDoc("Invalid build level - should be either BUILDLEVEL_MODULE or BUILDLEVEL_RELEASE");
185             }
186         }
187     }
188     
189     /**
190      * Get the contents of the "injectrelease.xml" file of this plugin.
191      * @return Null if no injectrelease file existed
192      */

193     public String JavaDoc getInjectReleaseContents() {
194         File JavaDoc injectRelease = new File JavaDoc(this.pluginDir, "injectrelease.xml");
195         if (injectRelease.exists()) {
196             try {
197                 return FileUtils.readFileToString(injectRelease, System.getProperty("file.encoding"));
198             } catch (IOException JavaDoc ioe) {
199                 throw new IllegalStateException JavaDoc("Cannot read contents of file \"" + injectRelease.getPath() + "\": " + ioe);
200             }
201         }
202         return null;
203     }
204
205     /**
206      * Get the contents of the "injectmodule.xml" file of this plugin.
207      * @return Null if no injectmodule file existed
208      */

209     public String JavaDoc getInjectModuleContents() {
210         File JavaDoc injectModule = new File JavaDoc(this.pluginDir, "injectmodule.xml");
211         if (injectModule.exists()) {
212             try {
213                 return FileUtils.readFileToString(injectModule, System.getProperty("file.encoding"));
214             } catch (IOException JavaDoc ioe) {
215                 throw new IllegalStateException JavaDoc("Cannot read contents of file \"" + injectModule.getPath() + "\": " + ioe);
216             }
217         }
218         return null;
219     }
220     
221     /** 'passes' only Antmod properties (starting with 'antmod.') from this Antmod plugin from 'fromProject' to 'toProject' */
222     private void passBackProperties(String JavaDoc propnameStart, Project fromProject, Project toProject) {
223         Hashtable JavaDoc props = fromProject.getProperties();
224         Enumeration JavaDoc propNames = props.keys();
225         String JavaDoc propName;
226         while (propNames.hasMoreElements()) {
227             propName = (String JavaDoc)propNames.nextElement();
228             if (propName.startsWith(propnameStart)) {
229                 toProject.setProperty(propName, fromProject.getProperty(propName));
230             }
231         }
232     }
233
234 }
235
Popular Tags