KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.antmod.buildplugin;
2
3 import java.io.BufferedWriter JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.StringWriter JavaDoc;
7 import java.util.ArrayList JavaDoc;
8
9 import org.antmod.conf.AntmodProperties;
10 import org.apache.commons.io.HexDump;
11 import org.apache.tools.ant.Project;
12
13 /**
14  * Entry point for getting a handle on the available build plugins
15  * that are plugged into the current Antmod runtime.
16  * This is the case if the plugin has been added to the
17  * 'antmod.plugins.home' directory (default ${ANTMOD_HOME}/plugins).
18  *
19  * @author Klaas Waslander
20  */

21 public final class BuildPluginFactory {
22
23     /** cached build plugins*/
24     private static BuildPlugin[] cachedPlugins;
25     
26     /** cached build plugins within system classloader */
27     //private static BuildPlugin[] systemClassLoaderCachedPlugins;
28

29     /** never to be instantiated */
30     private BuildPluginFactory() {
31     }
32
33     /**
34      * Returns all currently available build plugins.
35      * @return
36      */

37     public static BuildPlugin[] getBuildPlugins(Project antProject) {
38         if (cachedPlugins == null) {
39             //cachedPlugins = createPlugins(antProject);
40
Object JavaDoc[] o = (Object JavaDoc[])antProject.getReferences().get("antmod.cachedbuildplugins");
41             if (o != null) {
42                 //antProject.log("CACHED PLUGINS FOUND IN ANT PROJECT: " + antProject.getProperty("ant.file"), Project.MSG_WARN);
43
cachedPlugins = new BuildPlugin[o.length];
44                 for (int i = 0; i < o.length; i++) {
45                     // Problem: classcast does not work here
46
//cachedPlugins[i] = (BuildPlugin)o[i];
47

48                     // use reflection to get properties out of cached build plugin
49
cachedPlugins[i] = new BuildPlugin(antProject, o[i]);
50                 }
51             }
52
53             if (cachedPlugins == null) {
54                 // TODO: check for ant property which gets set once, and gets inherited even if inheritrefs=false
55
if (AntmodProperties.getBoolean("antmod.verbose") &&
56                         antProject.getName() != null && !antProject.getName().trim().equalsIgnoreCase("antmod-releasebuild")) {
57                     antProject.log(HexDump.EOL + "[antmod.buildpluginfactory] WARNING: *NO* Antmod plugin cache found in Ant buildfile: " + antProject.getProperty("ant.file") + ". This message should appear only once... if not, probably the buildfile has been invoked using '<ant' or '<antcall' without inheritrefs='true'." + HexDump.EOL + HexDump.EOL, Project.MSG_WARN);
58                 }
59                 cachedPlugins = createPlugins(antProject);
60                 antProject.addReference("antmod.cachedbuildplugins", cachedPlugins);
61             }
62         }
63         return cachedPlugins;
64     }
65
66     /**
67      * Sets the ant properties for the various plugin home directories
68      * in the given Ant project.
69      */

70     public static void loadPluginHomesIntoAnt(Project antProject) {
71         // this is invoked *every time* an Ant buildfile is loaded, because
72
// antmodpropertiestask uses this method
73
// --> this is why we do not try loading all buildplugins here, which would be slower
74
File JavaDoc[] validPluginHomes = getValidPluginHomes();
75         File JavaDoc pluginHome;
76         for (int i = validPluginHomes.length; i-- > 0;) {
77             pluginHome = validPluginHomes[i];
78             antProject.setProperty("antmod.plugins." + pluginHome.getName() + ".home", pluginHome.getAbsolutePath());
79         }
80     }
81     
82     /**
83      * Returns the plugin home directories of all valid plugins
84      * found inside the currently running Antmod JVM instance.
85      */

86     public static File JavaDoc[] getValidPluginHomes() {
87         ArrayList JavaDoc result = new ArrayList JavaDoc();
88         File JavaDoc[] pluginDirs = getPluginDirs();
89         File JavaDoc pluginDir;
90         for (int i = pluginDirs.length; i-- > 0;) {
91             pluginDir = pluginDirs[i];
92             if (isValidPluginDirectory(pluginDir)) {
93                 result.add(pluginDir);
94             }
95         }
96         return (File JavaDoc[])result.toArray(new File JavaDoc[result.size()]);
97     }
98
99     /**
100      * Returns the to-be-injected release-level injection contents for
101      * the buildfile in each release checkout directory.
102      */

103     public static String JavaDoc getInjectReleaseContents(Project antProject) {
104         StringWriter JavaDoc writer = new StringWriter JavaDoc();
105         BufferedWriter JavaDoc buffer = new BufferedWriter JavaDoc(writer);
106         try {
107             BuildPlugin[] plugins = getBuildPlugins(antProject);
108             for (int i = plugins.length; i-- > 0; ) {
109 // String injectRelease = antProject.replaceProperties(plugins[i].getInjectReleaseContents());
110
String JavaDoc injectRelease = plugins[i].getInjectReleaseContents();
111                 buffer.newLine();
112                 buffer.newLine();
113
114                 buffer.write(" <!-- (start Antmod plugin \"" + plugins[i].getName() + "\") -->");
115                 buffer.newLine();
116                 if (injectRelease != null) {
117                     buffer.write(injectRelease);
118                 } else {
119                     buffer.write(" <!-- No inject release contents for buildplugin \"" + plugins[i].getName() + "\" -->");
120                 }
121                 buffer.newLine();
122                 buffer.write(" <!-- (end Antmod plugin \"" + plugins[i].getName() + "\") -->");
123                 buffer.newLine();
124             }
125         } catch (IOException JavaDoc ioe) {
126             throw new IllegalStateException JavaDoc("Could not read inject release contents: " + ioe);
127         } finally {
128             try {
129                 buffer.flush();
130             }
131             catch (IOException JavaDoc e) {
132             }
133         }
134         return writer.getBuffer().toString();
135     }
136
137     /**
138      * Returns the to-be-injected module-level injection contents for
139      * the buildfile in each module directory.
140      */

141     public static String JavaDoc getInjectModuleContents(Project antProject) {
142         StringWriter JavaDoc writer = new StringWriter JavaDoc();
143         BufferedWriter JavaDoc buffer = new BufferedWriter JavaDoc(writer);
144         try {
145             BuildPlugin[] plugins = getBuildPlugins(antProject);
146             for (int i = plugins.length; i-- > 0; ) {
147 // String injectModule = antProject.replaceProperties(plugins[i].getInjectModuleContents());
148
String JavaDoc injectModule = plugins[i].getInjectModuleContents();
149                 buffer.newLine();
150                 buffer.newLine();
151
152                 buffer.write(" <!-- (start Antmod plugin \"" + plugins[i].getName() + "\") -->");
153                 buffer.newLine();
154                 if (injectModule != null) {
155                     buffer.write(injectModule);
156                 } else {
157                     buffer.write(" <!-- No inject module contents for buildplugin \"" + plugins[i].getName() + "\" -->");
158                 }
159                 buffer.newLine();
160                 buffer.write(" <!-- (end Antmod plugin \"" + plugins[i].getName() + "\") -->");
161                 buffer.newLine();
162             }
163         } catch (IOException JavaDoc ioe) {
164             throw new IllegalStateException JavaDoc("Could not read inject module contents: " + ioe);
165         } finally {
166             try {
167                 buffer.flush();
168             }
169             catch (IOException JavaDoc e) {
170             }
171         }
172         return writer.getBuffer().toString();
173     }
174
175     /** retrieve plugins currently available in Antmod and return them initialized */
176     private static BuildPlugin[] createPlugins(Project antProject) {
177         File JavaDoc[] pluginDirs = getPluginDirs();
178         ArrayList JavaDoc resultList = new ArrayList JavaDoc(pluginDirs.length);
179         File JavaDoc pluginDir;
180         for (int i = pluginDirs.length; i-- > 0;) {
181             pluginDir = pluginDirs[i];
182             if (isPossiblePluginDirectory(pluginDir)) {
183                 if (isValidPluginDirectory(pluginDir)) {
184                     BuildPlugin newPlugin = new BuildPlugin(antProject, pluginDir);
185                     resultList.add(newPlugin);
186                 } else {
187                     antProject.log("Ignoring buildplugin directory \"" + pluginDir.getName() + "\" because it is not valid.", Project.MSG_WARN);
188                 }
189             }
190         }
191         BuildPlugin[] result = new BuildPlugin[resultList.size()];
192         return (BuildPlugin[]) resultList.toArray(result);
193     }
194     
195     private static File JavaDoc[] getPluginDirs() {
196         File JavaDoc pluginHome = new File JavaDoc(AntmodProperties.getProperty("antmod.plugins.home"));
197         if (!pluginHome.exists()) {
198             throw new IllegalStateException JavaDoc(
199                 "Build plugins home directory does not exist (configuration property 'antmod.plugins.home' = "
200                     + pluginHome.getPath()
201                     + ")");
202         }
203         File JavaDoc[] pluginDirs = pluginHome.listFiles();
204         if (pluginDirs == null) {
205             throw new IllegalStateException JavaDoc("Cannot list plugin directories under plugin home dir '" + pluginHome.getPath() + "'");
206         }
207         return pluginDirs;
208     }
209
210     private static boolean isPossiblePluginDirectory(File JavaDoc pluginDir) {
211         return pluginDir.isDirectory() && !pluginDir.getName().equals("CVS") && !pluginDir.getName().equals(".svn");
212     }
213
214     private static boolean isValidPluginDirectory(File JavaDoc pluginDir) {
215         // module is only valid if it injects something into either release or module buildfile
216
return isPossiblePluginDirectory(pluginDir) &&
217                     new File JavaDoc(pluginDir, "injectrelease.xml").exists() || new File JavaDoc(pluginDir, "injectmodule.xml").exists() ||
218                     new File JavaDoc(pluginDir, "releasebuild.xml").exists() || new File JavaDoc(pluginDir, "modulebuild.xml").exists();
219     }
220
221 }
222
Popular Tags