1 package org.antmod.buildplugin; 2 3 import java.io.BufferedWriter ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.StringWriter ; 7 import java.util.ArrayList ; 8 9 import org.antmod.conf.AntmodProperties; 10 import org.apache.commons.io.HexDump; 11 import org.apache.tools.ant.Project; 12 13 21 public final class BuildPluginFactory { 22 23 24 private static BuildPlugin[] cachedPlugins; 25 26 27 29 30 private BuildPluginFactory() { 31 } 32 33 37 public static BuildPlugin[] getBuildPlugins(Project antProject) { 38 if (cachedPlugins == null) { 39 Object [] o = (Object [])antProject.getReferences().get("antmod.cachedbuildplugins"); 41 if (o != null) { 42 cachedPlugins = new BuildPlugin[o.length]; 44 for (int i = 0; i < o.length; i++) { 45 48 cachedPlugins[i] = new BuildPlugin(antProject, o[i]); 50 } 51 } 52 53 if (cachedPlugins == null) { 54 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 70 public static void loadPluginHomesIntoAnt(Project antProject) { 71 File [] validPluginHomes = getValidPluginHomes(); 75 File 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 86 public static File [] getValidPluginHomes() { 87 ArrayList result = new ArrayList (); 88 File [] pluginDirs = getPluginDirs(); 89 File 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 [])result.toArray(new File [result.size()]); 97 } 98 99 103 public static String getInjectReleaseContents(Project antProject) { 104 StringWriter writer = new StringWriter (); 105 BufferedWriter buffer = new BufferedWriter (writer); 106 try { 107 BuildPlugin[] plugins = getBuildPlugins(antProject); 108 for (int i = plugins.length; i-- > 0; ) { 109 String 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 ioe) { 126 throw new IllegalStateException ("Could not read inject release contents: " + ioe); 127 } finally { 128 try { 129 buffer.flush(); 130 } 131 catch (IOException e) { 132 } 133 } 134 return writer.getBuffer().toString(); 135 } 136 137 141 public static String getInjectModuleContents(Project antProject) { 142 StringWriter writer = new StringWriter (); 143 BufferedWriter buffer = new BufferedWriter (writer); 144 try { 145 BuildPlugin[] plugins = getBuildPlugins(antProject); 146 for (int i = plugins.length; i-- > 0; ) { 147 String 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 ioe) { 164 throw new IllegalStateException ("Could not read inject module contents: " + ioe); 165 } finally { 166 try { 167 buffer.flush(); 168 } 169 catch (IOException e) { 170 } 171 } 172 return writer.getBuffer().toString(); 173 } 174 175 176 private static BuildPlugin[] createPlugins(Project antProject) { 177 File [] pluginDirs = getPluginDirs(); 178 ArrayList resultList = new ArrayList (pluginDirs.length); 179 File 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 [] getPluginDirs() { 196 File pluginHome = new File (AntmodProperties.getProperty("antmod.plugins.home")); 197 if (!pluginHome.exists()) { 198 throw new IllegalStateException ( 199 "Build plugins home directory does not exist (configuration property 'antmod.plugins.home' = " 200 + pluginHome.getPath() 201 + ")"); 202 } 203 File [] pluginDirs = pluginHome.listFiles(); 204 if (pluginDirs == null) { 205 throw new IllegalStateException ("Cannot list plugin directories under plugin home dir '" + pluginHome.getPath() + "'"); 206 } 207 return pluginDirs; 208 } 209 210 private static boolean isPossiblePluginDirectory(File pluginDir) { 211 return pluginDir.isDirectory() && !pluginDir.getName().equals("CVS") && !pluginDir.getName().equals(".svn"); 212 } 213 214 private static boolean isValidPluginDirectory(File pluginDir) { 215 return isPossiblePluginDirectory(pluginDir) && 217 new File (pluginDir, "injectrelease.xml").exists() || new File (pluginDir, "injectmodule.xml").exists() || 218 new File (pluginDir, "releasebuild.xml").exists() || new File (pluginDir, "modulebuild.xml").exists(); 219 } 220 221 } 222
| Popular Tags
|