| 1 package net.sf.invicta.process; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileNotFoundException ; 6 import java.io.FileOutputStream ; 7 import java.io.IOException ; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 import java.util.Properties ; 11 12 import net.sf.invicta.InvictaConstants; 13 import net.sf.invicta.InvictaException; 14 import net.sf.invicta.Logger; 15 16 29 public class ConfigurationManager { 30 private boolean forceRun = false; 31 private Properties props; 32 private Properties previousProps = new Properties (); 33 private Properties outputProps = new Properties (); 34 private Map requestedProperties = new HashMap (); 35 private String outputPropsFileName; 36 37 40 public ConfigurationManager() throws InvictaException { 41 super(); 42 readProperties(); 43 } 44 50 public void setOutputProperty(String propertyName, String propertyValue) throws InvictaProcessException { 51 this.outputProps.setProperty(propertyName, propertyValue); 52 checkPropertyChange(propertyName); 53 } 54 55 63 public void setOutputTimestampProperty(String propertyPrefix, File file) throws InvictaProcessException { 64 String propertyName = 65 propertyPrefix 66 + InvictaConstants.PROPERTY_SEPARATOR 67 + getPropertyPath(file.getAbsolutePath()); 68 69 String timestamp = String.valueOf(file.lastModified()); 70 71 setOutputProperty(propertyName, timestamp); 72 } 73 74 75 83 public String getRequiredProperty(String propertyName) throws InvictaProcessException { 84 String value = getProperty(propertyName); 85 86 if (value == null) 88 throw InvictaProcessException.propertyUndefined(propertyName); 89 90 return value; 91 } 92 93 100 public String getProperty(String propertyName) throws InvictaProcessException { 101 if (this.requestedProperties.containsKey(propertyName)) 103 throw InvictaProcessException.propertyVariableCycle(propertyName); 104 105 this.requestedProperties.put(propertyName, propertyName); 107 108 String value = System.getProperty(propertyName); 110 111 if ((value == null) && (this.props != null)) 114 value = this.props.getProperty(propertyName); 115 116 if (value != null) { 119 120 value = parseProperty(value); 121 this.outputProps.setProperty(propertyName, value); 123 } 124 125 checkPropertyChange(propertyName); 126 127 this.requestedProperties.remove(propertyName); 129 return value; 130 } 131 132 138 public String getPreviousProperty(String propertyName) throws InvictaException { 139 return this.previousProps.getProperty(propertyName); 140 } 141 142 149 public String getOutputProperty(String propertyName) throws InvictaException { 150 return this.outputProps.getProperty(propertyName); 151 } 152 153 159 public boolean forceRun() { 160 String forceRunProperty = 161 this.props.getProperty(InvictaConstants.PROPERTY_FORCE_RUN); 162 if ((forceRunProperty != null) && 163 (Boolean.valueOf(forceRunProperty).booleanValue())) 164 return true; 165 166 return this.forceRun; 167 } 168 169 173 public void writeOutputProperties() throws InvictaProcessException { 174 try { 175 this.outputProps.store(new FileOutputStream (this.outputPropsFileName), 176 "Invicta temporary output properties for checking whether Invicta should run."); 177 } catch (Exception e) { 178 throw InvictaProcessException.fileIOError(this.outputPropsFileName, e); 179 } 180 } 181 182 187 private String getPropertyPath(String path) { 188 path = path.replace('/', '.'); 189 path = path.replace('\\', '.'); 190 return path; 191 } 192 193 199 private void readProperties() throws InvictaProcessException { 200 201 String propsFile = InvictaConstants.DEFAULT_PROPERTIES_FILE; 202 203 if (System.getProperty(InvictaConstants.PROPERTIES_FILE_NAME_PROPERTY) != null) { 205 propsFile = 206 System.getProperty(InvictaConstants.PROPERTIES_FILE_NAME_PROPERTY); 207 } 208 209 this.props = new Properties (); 211 try { 212 props.load(new FileInputStream (new File (propsFile))); 213 } catch (FileNotFoundException e) { 214 Logger.warn( 215 "Properties file not found (" + propsFile + ")"); 216 props = null; 217 } catch (IOException e) { 218 throw InvictaProcessException.fileIOError(propsFile, e); 219 } 220 221 String previousPropsFile = 223 InvictaConstants.DEFAULT_OUTPUT_PROPERTIES_FILE; 224 if (System.getProperty(InvictaConstants.OUTPUT_PROPERTIES_FILE_NAME_PROPERTY) != null) 225 previousPropsFile = 226 System.getProperty(InvictaConstants.OUTPUT_PROPERTIES_FILE_NAME_PROPERTY); 227 228 this.outputPropsFileName = previousPropsFile; 229 230 try { 232 previousProps.load( 233 new FileInputStream (new File (previousPropsFile))); 234 } catch (FileNotFoundException e) { 235 Logger.info( 236 "Note: Previous output properties file not found. " + 237 "Forcing Invicta execution."); 238 this.forceRun = true; 239 } catch (IOException e) { 240 throw InvictaProcessException.fileIOError(previousPropsFile, e); 241 } 242 } 243 244 252 private String parseProperty(String value) throws InvictaProcessException { 253 String resolvedValue = ""; 254 int varStartPos; 255 int currentStartPos = 0; 256 while ((varStartPos = value.indexOf(InvictaConstants.PROPERTY_VARIABLE_PREFIX, currentStartPos)) 259 != -1) { 260 if (varStartPos > currentStartPos) 262 resolvedValue += value.substring(currentStartPos, varStartPos); 263 264 int varNameStartPos = varStartPos + InvictaConstants.PROPERTY_VARIABLE_PREFIX.length(); 266 267 int varEndPos = value.indexOf(InvictaConstants.PROPERTY_VARIABLE_SUFFIX, varNameStartPos); 269 270 if (varEndPos == -1) 272 throw InvictaProcessException.propertyVariableSyntaxError(); 273 274 String varName = value.substring(varNameStartPos, varEndPos); 276 277 String varValue = getRequiredProperty(varName); 279 280 resolvedValue += varValue; 282 283 currentStartPos = varEndPos + 1; 285 } 286 287 if (currentStartPos >= 0) 289 resolvedValue += value.substring(currentStartPos); 290 return resolvedValue; 291 } 292 293 299 private void checkPropertyChange(String propertyName) { 300 if (this.forceRun) 301 return; 302 303 String oldValue = this.previousProps.getProperty(propertyName); 304 String newValue = this.outputProps.getProperty(propertyName); 305 if (!equals(oldValue, newValue)) { 306 Logger.info( 307 "Note: Property '" 308 + propertyName 309 + "' was changed. Forcing Invicta execution."); 310 this.forceRun = true; 311 } 312 } 313 314 321 private boolean equals(Object o1, Object o2) { 322 if ((o1 == null) && (o2 == null)) 323 return true; 324 if ((o1 == null) || (o2 == null)) 325 return false; 326 return o1.equals(o2); 327 } 328 329 330 } 331 | Popular Tags |