1 package org.antmod.descriptor; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.text.ParseException ; 8 import java.util.ArrayList ; 9 import java.util.Collections ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Properties ; 13 14 import org.antmod.conf.AntmodProperties; 15 import org.antmod.scm.ScmSystem; 16 import org.antmod.scm.ScmSystemFactory; 17 import org.antmod.scm.ScmUrl; 18 import org.apache.commons.io.FileUtils; 19 import org.apache.tools.ant.Project; 20 21 22 28 public class ReleaseDescriptor { 29 private String name; 30 private String versionString; 31 private ArrayList modules = new ArrayList (); 32 private ArrayList javalibs = new ArrayList (); 33 private ArrayList includes = new ArrayList (); 34 private Properties properties = new Properties (); 35 36 private File xmlFile; 38 private String xmlFileRelativePath; 39 40 45 public ReleaseDescriptor(String name, String versionString) { 46 this.name = name; 47 this.versionString = versionString; 48 } 49 50 54 public void verify() throws IllegalStateException { 55 List mainModules = getModulesByType(Module.TYPE_MAIN); 57 if (mainModules.size() > 1) { 58 throw new IllegalStateException ("Multiple 'main' modules in release description " + this.name + "-" + this.versionString + " - make sure there is only one module with type 'main'."); 59 } 60 if (mainModules.isEmpty()) { 61 throw new IllegalStateException ("No 'main' module in release description " + this.name + "-" + this.versionString + " - make sure there is one module with type 'main'."); 62 } 63 64 try { 66 String javalibUrl = AntmodProperties.getProperty("antmod.javalib.url"); 67 if (javalibUrl == null) { 68 throw new IllegalStateException ("\"antmod.javalib.url\" property is not set. Cannot continue."); 69 } 70 if (javalibUrl.startsWith("scm:")) { 71 ScmUrl javalibScmUrl = new ScmUrl(javalibUrl); 72 if (containsModule(javalibScmUrl.getModule())) { 73 throw new IllegalStateException ("'" + javalibScmUrl.getModule() + "' module found in list with modules - this is not allowed! Instead, specify library dependencies using '<javalib>' tag in release descriptor"); 74 } 75 } 76 } catch (ParseException pe) { 77 throw new RuntimeException ("Javalib url in property 'antmod.javalib.repos.url' could not be parsed.", pe); 79 } 80 81 String releaseBuildDir = AntmodProperties.getProperty("antmod.release.dirs.build"); 83 if (containsModule(releaseBuildDir)) { 84 throw new IllegalStateException ("'" + releaseBuildDir + "' module found in list with modules - this is not allowed! Make sure the module is named differently, such that it does not conflict with this release-level build directory."); 85 } 86 } 87 88 94 public void registerReleaseAttributesInto(Project antProject) { 95 antProject.setProperty("antmod.release.mainmodule.name", this.getMainModule().getName()); 96 97 File releaseDir = ((new File (antProject.getBaseDir(), AntmodProperties.getProperty("antmod.release.metadata.file")).exists()) ? antProject.getBaseDir() : antProject.getBaseDir().getParentFile()); 98 antProject.setProperty("antmod.release.mainmodule.dir", new File (releaseDir, this.getMainModule().getName()).getPath()); 99 100 antProject.setProperty("antmod.release.file.relativepath", this.xmlFileRelativePath); 101 antProject.setProperty("antmod.release.file.path", this.xmlFile.getPath()); 102 antProject.setProperty("antmod.release.file.absolutepath", this.xmlFile.getAbsolutePath()); 103 } 104 105 108 public void addModule(Module module) { 109 if (module.getName() == null) { 110 throw new IllegalArgumentException ("Module without name not allowed in release descriptor " + name + "-" + versionString); 111 } 112 modules.add(module); 113 } 114 115 118 public void addInclude(Include include) { 119 includes.add(include); 120 } 121 122 126 public void addJavaLib(JavaLib javalib) { 127 javalibs.add(javalib); 128 } 129 130 134 public Properties getProperties() { 135 return this.properties; 136 } 137 138 143 public void setPropertiesFromText(String propertiesText) { 144 InputStream in = null; 145 try { 146 in = new ByteArrayInputStream (propertiesText.getBytes()); 147 Properties newProps = new Properties (); 148 newProps.load(in); 149 this.properties = newProps; 150 } catch (IOException e) { 151 e.printStackTrace(); 152 } 153 finally { 154 if (in != null) { 155 try { 156 in.close(); 157 } 158 catch (IOException e1) { 159 e1.printStackTrace(); 160 } 161 } 162 } 163 } 164 165 168 public boolean containsModule(String moduleName) { 169 return getModuleByName(moduleName) != null; 170 } 171 172 177 public Module getModuleByName(String moduleName) { 178 Iterator iter = this.modules.iterator(); 179 Module currentMod; 180 while (iter.hasNext()) { 181 currentMod = (Module)iter.next(); 182 if (currentMod.getName().equals(moduleName)) { 183 return currentMod; 184 } 185 } 186 return null; 187 } 188 189 public List getModulesByType(String moduleType) { 190 ArrayList result = new ArrayList (); 191 Iterator iter = modules.iterator(); 192 while (iter.hasNext()) { 193 Module mod = (Module)iter.next(); 194 if (mod.getType().equals(moduleType)) { 195 result.add(mod); 196 } 197 } 198 return result; 199 } 200 201 205 public Module getBuildReleaseModule() { 206 List buildrelease = getModulesByType(Module.TYPE_BUILDRELEASE); 207 if (buildrelease.isEmpty()) { 208 return getMainModule(); 209 } else { 210 return (Module)buildrelease.get(0); 211 } 212 } 213 214 public Module getMainModule() { 215 return (Module)getModulesByType(Module.TYPE_MAIN).get(0); 216 } 217 218 public List getModules() { 219 return Collections.unmodifiableList(this.modules); 220 } 221 222 public List getIncludes() { 223 return Collections.unmodifiableList(this.includes); 224 } 225 226 public List getJavaLibs() { 227 return Collections.unmodifiableList(this.javalibs); 228 } 229 230 public String getName() { 231 return name; 232 } 233 234 public String getVersionString() { 235 return versionString; 236 } 237 238 public String getScmRevision() { 239 try { 241 ScmSystem scm = ScmSystemFactory.getScmSystemByUrl(AntmodProperties.getProperty("antmod.descriptor.xml.repos.url")); 242 return scm.getRevisionNumber(this.xmlFile); 243 } catch (Exception e) { 244 e.printStackTrace(); 245 } 246 return null; 247 } 248 249 public void setXmlFile(File xmlFile, String xmlFileRelativePath) { 250 this.xmlFile = xmlFile; 251 this.xmlFileRelativePath = xmlFileRelativePath; 252 } 253 254 255 256 259 public static class JavaLib { 260 263 private String name; 264 265 public void setName(String name) { 266 this.name = name; 267 } 268 269 public String getName() { 270 return name; 271 } 272 } 273 274 279 public static class Include { 280 private String releaseName; 281 282 public void setReleaseName(String releaseName) { 283 this.releaseName = releaseName; 284 } 285 286 public String getReleaseName() { 287 return releaseName; 288 } 289 } 290 291 294 public static class Module { 295 public static String TYPE_MAIN = "main"; 296 public static String TYPE_BUILDRELEASE = "buildrelease"; 297 public static String TYPE_DIST = "dist"; 298 public static String TYPE_LIBRARY = "library"; 299 300 303 private String name; 304 307 private String version = "trunk"; 308 311 private String type = TYPE_LIBRARY; 312 313 316 private String repos; 317 318 321 public Module() { 322 } 323 324 328 public void setName(String name) { 329 this.name = name; 330 } 331 332 335 public String getName() { 336 return this.name; 337 } 338 339 343 public void setVersion(String version) { 344 this.version = version; 345 } 346 347 350 public String getVersion() { 351 return this.version; 352 } 353 354 358 public void setType(String type) { 359 this.type = type; 360 } 361 362 365 public String getType() { 366 return this.type; 367 } 368 369 public void setRepos(String repos) { 370 this.repos = repos; 371 } 372 373 public String getRepos() { 374 return this.repos; 375 } 376 377 public boolean equals(Object other) { 378 boolean result = false; 379 if (other instanceof Module) { 380 Module otherModule = (Module) other; 381 if (name.equals(otherModule.name) && 382 version.equals(otherModule.version) && 383 type.equals(otherModule.type) 384 ) { 385 result = true; 386 } 387 } 388 return result; 389 } 390 } 391 } 392
| Popular Tags
|