1 package org.antmod.conf; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.text.ParseException ; 6 import java.util.ArrayList ; 7 import java.util.Enumeration ; 8 import java.util.Hashtable ; 9 import java.util.Iterator ; 10 import java.util.Properties ; 11 import java.util.logging.Logger ; 12 13 import org.antmod.buildplugin.BuildPluginFactory; 14 import org.antmod.scm.ScmSystem; 15 import org.antmod.scm.ScmSystemFactory; 16 import org.antmod.scm.ScmUrl; 17 import org.antmod.util.Os; 18 import org.apache.commons.io.FileUtils; 19 import org.apache.commons.lang.BooleanUtils; 20 import org.apache.commons.lang.SystemUtils; 21 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.taskdefs.Property; 23 24 30 public final class AntmodProperties { 31 private final static Logger LOGGER = Logger.getLogger(AntmodProperties.class.getName()); 32 33 private final static Properties PROPS = new Properties (); 34 static { 35 loadAntmodProperties(PROPS); 36 } 37 38 39 private AntmodProperties() { 40 } 41 42 48 public static String getProperty(String propertyName) { 49 return PROPS.getProperty(propertyName); 50 } 51 52 public static boolean getBoolean(String propertyName) { 53 String propVal = getProperty(propertyName); 54 return propVal != null && 55 (propVal.trim().equalsIgnoreCase("true") || propVal.trim().equalsIgnoreCase("yes")); 56 } 57 58 63 public static Iterator getPropertyNamesStartingWith(String startOfPropertyName) { 64 ArrayList result = new ArrayList (); 65 Enumeration propNames = PROPS.propertyNames(); 66 String propName; 67 while (propNames.hasMoreElements()) { 68 propName = (String )propNames.nextElement(); 69 if (propName.startsWith(startOfPropertyName)) { 70 result.add(propName); 71 } 72 } 73 return result.iterator(); 74 } 75 76 80 public static void loadIntoAnt(Project antProject) { 81 Enumeration propNames = PROPS.propertyNames(); 82 String propName; 83 while (propNames.hasMoreElements()) { 84 propName = (String )propNames.nextElement(); 85 antProject.setProperty(propName, PROPS.getProperty(propName)); 86 } 87 } 88 89 90 private static void loadAntmodProperties(Properties antmodProperties) { 91 String antmodHome = Os.getEnvironmentVariable("ANTMOD_HOME"); 92 93 Project localAntProject = new Project(); 94 localAntProject.init(); 95 96 Property antProp = new Property(); 97 antProp.setProject(localAntProject); 98 antProp.setEnvironment("env"); 99 antProp.execute(); 100 101 antProp = new Property(); 103 antProp.setProject(localAntProject); 104 antProp.setFile(new File (SystemUtils.USER_HOME, ".antmodrc")); 105 antProp.execute(); 106 107 antProp = new Property(); 109 antProp.setProject(localAntProject); 110 antProp.setFile(new File (SystemUtils.USER_HOME, ".antmod.properties")); 111 antProp.execute(); 112 113 antProp = new Property(); 115 antProp.setProject(localAntProject); 116 antProp.setFile(new File (SystemUtils.USER_HOME, "local.antmod.properties")); 117 antProp.execute(); 118 119 antProp = new Property(); 121 antProp.setProject(localAntProject); 122 antProp.setFile(new File (antmodHome, "resources" + File.separator + "conf" + File.separator + "antmod.properties")); 123 antProp.execute(); 124 125 PROPS.putAll(localAntProject.getProperties()); 126 127 File [] validPluginHomes = BuildPluginFactory.getValidPluginHomes(); 129 File pluginPropsFile; 130 for (int i = validPluginHomes.length; i-- > 0;) { 131 pluginPropsFile = new File (validPluginHomes[i], "plugin.properties"); 132 if (pluginPropsFile.exists()) { 133 antProp = new Property(); 134 antProp.setProject(localAntProject); 135 antProp.setFile(pluginPropsFile); 136 antProp.execute(); 137 } 138 } 139 PROPS.putAll(localAntProject.getProperties()); 140 141 142 ScmUrl confUrl = null; 146 try { 147 if (BooleanUtils.toBoolean(localAntProject.getProperty("antmod.conf.enabled"))) { 148 confUrl = new ScmUrl(localAntProject.getProperty("antmod.conf.repos.url")); 149 } 150 } 151 catch (IllegalArgumentException e) { 152 } 154 catch (ParseException e) { 155 e.printStackTrace(); 156 } 157 if (confUrl != null) { 158 ScmSystem confScm = ScmSystemFactory.getScmSystemByUrl(confUrl); 160 File localConfDir = new File (localAntProject.getProperty("antmod.conf.localdir")); 161 File localDir = new File (localAntProject.getProperty("antmod.local.dir")); 162 localConfDir.mkdirs(); 163 164 checkoutIfNeeded(confScm, localConfDir, localDir); 166 167 Project confAntProject = new Project(); 169 confAntProject.init(); 170 172 File [] confFiles = localConfDir.listFiles(); 174 if (confFiles != null && confFiles.length > 0) { 175 File confFile; 176 for (int i = 0; i < confFiles.length; i++) { 177 confFile = confFiles[i]; 178 if (!confFile.isDirectory()) { 179 antProp = new Property(); 180 antProp.setProject(confAntProject); 181 antProp.setFile(confFile); 182 antProp.execute(); 183 } 184 } 185 186 Hashtable confProps = confAntProject.getProperties(); 189 Iterator confKeys = confProps.keySet().iterator(); 190 while (confKeys.hasNext()) { 191 String confKey = (String )confKeys.next(); 192 193 String confVal = localAntProject.replaceProperties((String )confProps.get(confKey)); 195 PROPS.put(confKey, confVal); 196 } 197 198 } else { 199 } 201 } 202 } 203 204 private static void checkoutIfNeeded(final ScmSystem confScm, final File localConfDir, File localDir) { 205 localDir.mkdirs(); 207 File lastModifiedFile = new File (localDir, localConfDir.getName() + ".lastcheckout"); 208 long lastModified = 0; 209 if (lastModifiedFile.exists()) { 210 lastModified = lastModifiedFile.lastModified(); 211 } 212 213 if (!confScm.isCheckoutDir(localConfDir) || 214 System.currentTimeMillis() - lastModified > Integer.parseInt(getProperty("antmod.conf.checkoutinterval.minutes")) * 60000) { 215 System.err.println("antmod.conf.enabled=true : retrieving 'antmod-conf' module"); 217 218 if (!confScm.isCheckoutDir(localConfDir)) { 219 confScm.doCheckoutOrUpdate(confScm.getUrl().getModule(), localConfDir, null, true); 222 } else { 223 Thread backgroundCheckout = new Thread () { 224 public void run() { 225 confScm.doCheckoutOrUpdate(confScm.getUrl().getModule(), localConfDir, null, true); 226 } 227 }; 228 backgroundCheckout.setPriority(Thread.MAX_PRIORITY); 229 backgroundCheckout.start(); 230 } 231 232 244 245 if (lastModifiedFile.exists()) { 247 lastModifiedFile.delete(); 248 } 249 try { 250 FileUtils.writeStringToFile(lastModifiedFile, "This file is used for seeing when the centralized antmod-conf module was last checked out / updated.", SystemUtils.FILE_ENCODING); 251 } 252 catch (IOException e) { 253 e.printStackTrace(); 254 } 255 } 256 } 257 } 258 | Popular Tags |