[Swift-commit] r7915 - in trunk/src/org/griphyn/vdl: karajan util

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Thu Jun 12 21:05:17 CDT 2014


Author: hategan
Date: 2014-06-12 21:05:17 -0500 (Thu, 12 Jun 2014)
New Revision: 7915

Modified:
   trunk/src/org/griphyn/vdl/karajan/Loader.java
   trunk/src/org/griphyn/vdl/util/ConfigPropertyType.java
   trunk/src/org/griphyn/vdl/util/VDL2Config.java
Log:
better property validation scheme where command line overrides are taken into account and the file in which the problem is found is printed with the error

Modified: trunk/src/org/griphyn/vdl/karajan/Loader.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/Loader.java	2014-06-13 01:38:21 UTC (rev 7914)
+++ trunk/src/org/griphyn/vdl/karajan/Loader.java	2014-06-13 02:05:17 UTC (rev 7915)
@@ -126,9 +126,10 @@
                 source = null;
             }
        
-            setupLogging(ap, projectName, runID);
             VDL2Config config = loadConfig(ap);
             addCommandLineProperties(config, ap);
+            config.validateProperties();
+            setupLogging(ap, projectName, runID);
             logBasicInfo(argv, runID, config);
             debugSitesText(config);
             debugTCText(config);
@@ -537,14 +538,12 @@
 
     private static void addCommandLineProperties(VDL2Config config,
             ArgumentParser ap) {
+        config.setCurrentFile("<command line>");
         Map<String, PropInfo> desc = VDL2ConfigProperties.getPropertyDescriptions();
         for (Map.Entry<String, PropInfo> e : desc.entrySet()) {
             String name = e.getKey();
             if (ap.isPresent(name)) {
             	String value = ap.getStringValue(name);
-            	if (logger.isDebugEnabled()) {
-            	    logger.debug("setting: " + name + " to: " + value);
-            	}
             	config.setProperty(name, value);
             }
         }

Modified: trunk/src/org/griphyn/vdl/util/ConfigPropertyType.java
===================================================================
--- trunk/src/org/griphyn/vdl/util/ConfigPropertyType.java	2014-06-13 01:38:21 UTC (rev 7914)
+++ trunk/src/org/griphyn/vdl/util/ConfigPropertyType.java	2014-06-13 02:05:17 UTC (rev 7915)
@@ -27,7 +27,7 @@
         return new Choices(values);
     }
     
-    public abstract void checkValue(String propName, String value);
+    public abstract void checkValue(String propName, String value, String source);
     
     private static String pp(Collection<String> c) {
         StringBuilder sb = new StringBuilder();
@@ -54,9 +54,9 @@
         }
 
         @Override
-        public void checkValue(String propName, String value) {
+        public void checkValue(String propName, String value, String source) {
             if (!choices.contains(value)) {
-                throw new IllegalArgumentException("Invalid value '" + value + "' for property '" + 
+                throw new IllegalArgumentException(source + ":\n\tInvalid value '" + value + "' for property '" + 
                     propName + "'. Valid values are: " + pp(choices));
             }
         }
@@ -64,19 +64,19 @@
     
     private static class CPTString extends ConfigPropertyType {
         @Override
-        public void checkValue(String propName, String value) {
+        public void checkValue(String propName, String value, String source) {
             // all values accepted
         }
     }
     
     private static class Int extends ConfigPropertyType {
         @Override
-        public void checkValue(String propName, String value) {
+        public void checkValue(String propName, String value, String source) {
             try {
                 Integer.parseInt(value);
             }
             catch (NumberFormatException e) {
-                throw new IllegalArgumentException("Invalid value '" + value + "' for property '" + 
+                throw new IllegalArgumentException(source + ":\n\tInvalid value '" + value + "' for property '" + 
                     propName + "'. Must be an integer");
             }
         }
@@ -84,12 +84,12 @@
     
     private static class CPTFloat extends ConfigPropertyType {
         @Override
-        public void checkValue(String propName, String value) {
+        public void checkValue(String propName, String value, String source) {
             try {
                 Double.parseDouble(value);
             }
             catch (NumberFormatException e) {
-                throw new IllegalArgumentException("Invalid value '" + value + "' for property '" + 
+                throw new IllegalArgumentException(source + ":\n\tInvalid value '" + value + "' for property '" + 
                     propName + "'. Must be a floating point number.");
             }
         }
@@ -97,10 +97,10 @@
     
     private static class CPTFile extends ConfigPropertyType {
         @Override
-        public void checkValue(String propName, String value) {
+        public void checkValue(String propName, String value, String source) {
             File f = new File(value);
             if (!f.exists()) {
-                throw new IllegalArgumentException("Invalid value '" + value + "' for property '" + 
+                throw new IllegalArgumentException(source + ":\n\tInvalid value '" + value + "' for property '" + 
                     propName + "'. File does not exist.");
             }
         }

Modified: trunk/src/org/griphyn/vdl/util/VDL2Config.java
===================================================================
--- trunk/src/org/griphyn/vdl/util/VDL2Config.java	2014-06-13 01:38:21 UTC (rev 7914)
+++ trunk/src/org/griphyn/vdl/util/VDL2Config.java	2014-06-13 02:05:17 UTC (rev 7915)
@@ -80,16 +80,18 @@
 		c.load(file);
 		config = c;
 		config.check();
-		config.validateProperties();
 		return config;
 	}
 
 	private List<String> files, tried;
 	private Map<Object, ConfigPropertyType> types;
+	private Map<Object, String> propertySource;
+	private String currentFile;
 
 	private VDL2Config() {
 		files = new LinkedList<String>();
 		tried = new LinkedList<String>();
+		propertySource = new HashMap<Object, String>();
 		types = new HashMap<Object, ConfigPropertyType>();
 		put(VDL2ConfigProperties.POOL_FILE, "${swift.home}/etc/sites.xml", ConfigPropertyType.FILE);
 		put(VDL2ConfigProperties.TC_FILE, "${swift.home}/etc/tc.data", ConfigPropertyType.FILE);
@@ -132,9 +134,11 @@
 	private VDL2Config(VDL2Config other) {
 		this.putAll(other);
 		this.files.addAll(other.files);
+		this.propertySource.putAll(other.propertySource);
 	}
 
 	protected void load(String file) throws IOException {
+	    this.currentFile = file;
 		tried.add(file);
 		File f = new File(file);
 		if (f.exists()) {
@@ -155,7 +159,7 @@
 		}
 	}
 
-	private void validateProperties() {
+	public void validateProperties() {
 	    for (Map.Entry<Object, Object> e : this.entrySet()) {
 	        checkType(e.getKey(), e.getValue());
 	    }
@@ -181,6 +185,7 @@
 	 * occur.
 	 */
 	public synchronized Object put(Object key, Object value) {
+	    propertySource.put(key, currentFile);
 		String svalue = (String) value;
 		if (svalue.indexOf("${") == -1) {
 			return super.put(key, value);
@@ -223,7 +228,7 @@
 	private void checkType(Object key, Object value) {
 	    ConfigPropertyType type = types.get(key);
 	    if (type != null) {
-	        type.checkValue((String) key, (String) value);
+	        type.checkValue((String) key, (String) value, propertySource.get(key));
 	    }
     }
 
@@ -287,6 +292,8 @@
 	public Object clone() {
 		VDL2Config conf = new VDL2Config();
 		conf.putAll(this);
+		conf.files.addAll(files);
+        conf.propertySource.putAll(propertySource);
 		return conf;
 	}
 
@@ -305,4 +312,8 @@
 		}
 		return getProperty(name);
 	}
+
+    public void setCurrentFile(String f) {
+        this.currentFile = f;
+    }
 }




More information about the Swift-commit mailing list