001 /** 002 * 003 */ 004 package org.wdssii.core; 005 006 import java.io.File; 007 import java.io.FileInputStream; 008 import java.io.IOException; 009 import java.text.DecimalFormat; 010 import java.util.List; 011 import java.util.Properties; 012 013 import org.apache.commons.logging.Log; 014 import org.apache.commons.logging.LogFactory; 015 016 /** 017 * Base class of algorithms and tools 018 * 019 * @author lakshman 020 * 021 */ 022 public abstract class WDSSIIProgram { 023 private static Log log = LogFactory.getLog(WDSSIIProgram.class); 024 025 private String outputDir = "/tmp/output"; 026 027 private boolean realTime = false; 028 029 public String getOutputDir() { 030 return outputDir; 031 } 032 033 public void setOutputDir(String outputDir) { 034 this.outputDir = outputDir; 035 } 036 037 public boolean isRealTime() { 038 return realTime; 039 } 040 041 public void setRealTime(boolean realTime) { 042 this.realTime = realTime; 043 } 044 045 public void printUsage(Properties defaults, boolean exit) { 046 try { 047 String className = this.getClass().getCanonicalName(); 048 System.err.println("Usage:"); 049 System.err.println(" java " + className + " properties.xml"); 050 System.err.println("A complete properties.xml is shown below. Edit as needed."); 051 defaults.storeToXML(System.err, className + " options"); 052 } catch (IOException e) { 053 // ignore 054 } finally { 055 if (exit) { 056 System.exit(-1); 057 } 058 } 059 } 060 061 /** 062 * Gets properties from the file specified on command-line backed by 063 * defaults. To autogenerate defaults, consider using: 064 * 065 * <pre> 066 * Properties defaults = PropertyGetterSetter.getProperties(this); 067 * </pre> 068 * 069 * Prints usage and exits on error. 070 */ 071 public Properties getPropertiesFromCommandLine(String[] args, 072 Properties defaults) { 073 // load up user-provided properties 074 try { 075 String filename = args[0]; 076 Properties props = new Properties(defaults); 077 props.loadFromXML(new FileInputStream(filename)); 078 if (log.isInfoEnabled()) { 079 log.info("From " + filename + ":" + props.toString()); 080 } 081 return props; 082 } catch (Exception e) { 083 if (args.length > 0) { 084 System.err.println(e); 085 } 086 printUsage(defaults, true); 087 } 088 return null; 089 } 090 091 /** 092 * Will use reflection to set up all the properties on the algorithm object. 093 */ 094 public void setup(Properties props) { 095 // use reflection to call setter methods on algorithm class 096 PropertyGetterSetter.setProperties(this, props); 097 098 // create output directory 099 new File(outputDir).mkdirs(); 100 } 101 102 103 /** 104 * convenience function 105 */ 106 public void setup(String[] args) { 107 Properties defaults = PropertyGetterSetter.getProperties(this); 108 Properties props = getPropertiesFromCommandLine(args, defaults); 109 setup(props); 110 } 111 112 /** 113 * convenience function 114 */ 115 public void setupAndExecute(String[] args) { 116 setup(args); 117 execute(); 118 } 119 120 /** Start doing main task. */ 121 abstract public void execute(); 122 123 /** uses 00.00 as the format. Override if necessary */ 124 public String getSubTypeForElevation(float elevation) { 125 DecimalFormat df = new DecimalFormat("00.00"); 126 return df.format(elevation); 127 } 128 129 /** initializes wdssii */ 130 protected WDSSIIProgram(){ 131 WDSSII.getInstance(); 132 } 133 }