KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > conf > AntmodProperties


1 package org.antmod.conf;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.text.ParseException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.Hashtable JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.logging.Logger JavaDoc;
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 /**
25  * Accesses the proper set of Antmod properties, reading
26  * all default and user specific config files in the proper order.
27  *
28  * @author Klaas Waslander
29  */

30 public final class AntmodProperties {
31     private final static Logger JavaDoc LOGGER = Logger.getLogger(AntmodProperties.class.getName());
32
33     private final static Properties JavaDoc PROPS = new Properties JavaDoc();
34     static {
35         loadAntmodProperties(PROPS);
36     }
37     
38     /** never to be instantiated */
39     private AntmodProperties() {
40     }
41
42     /**
43      * Get the property with the given name, first checking user specific properties,
44      * and then the globally defined default property values.
45      * @param propertyName The name of the antmod property to retrieve.
46      * @return The value of the property if set.
47      */

48     public static String JavaDoc getProperty(String JavaDoc propertyName) {
49         return PROPS.getProperty(propertyName);
50     }
51     
52     public static boolean getBoolean(String JavaDoc propertyName) {
53         String JavaDoc propVal = getProperty(propertyName);
54         return propVal != null &&
55         (propVal.trim().equalsIgnoreCase("true") || propVal.trim().equalsIgnoreCase("yes"));
56     }
57     
58     /**
59      * Get all property names starting with the given string.
60      * @param startOfPropertyName The string that each property name should start with, case sensitive!
61      * @return An iterator with String objects, being the property names that are valid.
62      */

63     public static Iterator JavaDoc getPropertyNamesStartingWith(String JavaDoc startOfPropertyName) {
64         ArrayList JavaDoc result = new ArrayList JavaDoc();
65         Enumeration JavaDoc propNames = PROPS.propertyNames();
66         String JavaDoc propName;
67         while (propNames.hasMoreElements()) {
68             propName = (String JavaDoc)propNames.nextElement();
69             if (propName.startsWith(startOfPropertyName)) {
70                 result.add(propName);
71             }
72         }
73         return result.iterator();
74     }
75
76     /**
77      * Load the antmod configuration properties into the given ant project.
78      * @param antProject The Ant project into which the config is to be loaded
79      */

80     public static void loadIntoAnt(Project antProject) {
81         Enumeration JavaDoc propNames = PROPS.propertyNames();
82         String JavaDoc propName;
83         while (propNames.hasMoreElements()) {
84             propName = (String JavaDoc)propNames.nextElement();
85             antProject.setProperty(propName, PROPS.getProperty(propName));
86         }
87     }
88
89     /** Internal utility for loading properties from both global and user specific props files */
90     private static void loadAntmodProperties(Properties JavaDoc antmodProperties) {
91         String JavaDoc 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         // user configuration properties
102
antProp = new Property();
103         antProp.setProject(localAntProject);
104         antProp.setFile(new File JavaDoc(SystemUtils.USER_HOME, ".antmodrc"));
105         antProp.execute();
106
107         // backwards compatibility
108
antProp = new Property();
109         antProp.setProject(localAntProject);
110         antProp.setFile(new File JavaDoc(SystemUtils.USER_HOME, ".antmod.properties"));
111         antProp.execute();
112
113         // backwards compatibility
114
antProp = new Property();
115         antProp.setProject(localAntProject);
116         antProp.setFile(new File JavaDoc(SystemUtils.USER_HOME, "local.antmod.properties"));
117         antProp.execute();
118
119         // default Antmod configuration properties
120
antProp = new Property();
121         antProp.setProject(localAntProject);
122         antProp.setFile(new File JavaDoc(antmodHome, "resources" + File.separator + "conf" + File.separator + "antmod.properties"));
123         antProp.execute();
124
125         PROPS.putAll(localAntProject.getProperties());
126
127         // plugin property files
128
File JavaDoc[] validPluginHomes = BuildPluginFactory.getValidPluginHomes();
129         File JavaDoc pluginPropsFile;
130         for (int i = validPluginHomes.length; i-- > 0;) {
131             pluginPropsFile = new File JavaDoc(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         //
143
// Add centralized configuration from scm
144
//
145
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 JavaDoc e) {
152             //e.printStackTrace();
153
}
154         catch (ParseException JavaDoc e) {
155             e.printStackTrace();
156         }
157         if (confUrl != null) {
158             // update locally cached config
159
ScmSystem confScm = ScmSystemFactory.getScmSystemByUrl(confUrl);
160             File JavaDoc localConfDir = new File JavaDoc(localAntProject.getProperty("antmod.conf.localdir"));
161             File JavaDoc localDir = new File JavaDoc(localAntProject.getProperty("antmod.local.dir"));
162             localConfDir.mkdirs();
163
164             // only checkout or update periodically
165
checkoutIfNeeded(confScm, localConfDir, localDir);
166
167             // load properties into empty ant project
168
Project confAntProject = new Project();
169             confAntProject.init();
170             //int initialPropertyCount = confAntProject.getProperties().size();
171

172             // iterate across all files in the conf directory, and load into ant project
173
File JavaDoc[] confFiles = localConfDir.listFiles();
174             if (confFiles != null && confFiles.length > 0) {
175                 File JavaDoc 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                 // override already loaded properties
187
// iterate across properties, and replace variable references with value of variables already loaded
188
Hashtable JavaDoc confProps = confAntProject.getProperties();
189                 Iterator JavaDoc confKeys = confProps.keySet().iterator();
190                 while (confKeys.hasNext()) {
191                     String JavaDoc confKey = (String JavaDoc)confKeys.next();
192
193                     // replace "${}" constructs left in the property value with values in local config
194
String JavaDoc confVal = localAntProject.replaceProperties((String JavaDoc)confProps.get(confKey));
195                     PROPS.put(confKey, confVal);
196                 }
197
198             } else {
199                 //System.err.println("NO centralized antmod config found in SCM");
200
}
201         }
202     }
203     
204     private static void checkoutIfNeeded(final ScmSystem confScm, final File JavaDoc localConfDir, File JavaDoc localDir) {
205         // check last modified of temp file
206
localDir.mkdirs();
207         File JavaDoc lastModifiedFile = new File JavaDoc(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             // more than one hour passed!
216
System.err.println("antmod.conf.enabled=true : retrieving 'antmod-conf' module");
217             
218             if (!confScm.isCheckoutDir(localConfDir)) {
219                 // the first time a checkout is attempted, always block until it completes
220
// such that the invocating code can immediately load the centralized config parameters
221
confScm.doCheckoutOrUpdate(confScm.getUrl().getModule(), localConfDir, null, true);
222             } else {
223                 Thread JavaDoc backgroundCheckout = new Thread JavaDoc() {
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             /* PROBLEM: if main thread sleeps, the backgroundCheckout thread cannot start subprocess for svn/cvs...
233             try {
234                 int secondsWait = Integer.parseInt(getProperty("antmod.conf.checkoutwait.seconds"));
235                 Thread.sleep(secondsWait * 1000);
236                 if (backgroundCheckout.isAlive()) {
237                     System.err.println("- check NOT completed, you are probably working offline -");
238                 }
239             }
240             catch (InterruptedException e1) {
241                 e1.printStackTrace();
242             }
243             */

244
245             // (re-)create lastmodified file
246
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 JavaDoc e) {
253                 e.printStackTrace();
254             }
255         }
256     }
257 }
258
Popular Tags