1 19 20 package com.sslexplorer.boot; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.util.Properties ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.jdom.JDOMException; 33 34 35 41 public class ContextConfig extends AbstractXMLDefinedPropertyClass { 42 43 final static Log log = LogFactory.getLog(ContextConfig.class); 44 45 48 public final static String NAME = "contextConfig"; 49 50 52 private Properties contextProperties = new Properties (); 53 54 60 public ContextConfig() throws IOException , JDOMException { 61 super(NAME, false); 62 File contextFile = new File (ContextHolder.getContext().getConfDirectory(), "webserver.properties"); 63 if (contextFile.exists()) { 64 contextProperties.load(new FileInputStream (contextFile)); 65 } 66 } 67 68 73 public String retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException { 74 PropertyDefinition def = getDefinition(key.getName()); 75 try { 76 String val = contextProperties.getProperty(key.getName(), def.getDefaultValue()); 77 if (def.getType() == PropertyDefinition.TYPE_PASSWORD) { 78 try { 79 val = ContextHolder.getContext().deobfuscatePassword(val); 80 } catch (Throwable t) { 81 log.warn("Password property " + def.getName() + " could not be decoded. It has been result to the default.", t); 82 } 83 } 84 return val; 85 } catch (Exception e) { 86 log.error("Failed to retrieve property.", e); 87 } 88 return null; 89 } 90 91 public String storePropertyImpl(AbstractPropertyKey key, String value) throws IllegalArgumentException { 92 PropertyDefinition def = getDefinition(key.getName()); 93 String oldValue = retrieveProperty(key); 94 if (def.getType() == PropertyDefinition.TYPE_PASSWORD) { 95 try { 96 value = ContextHolder.getContext().obfuscatePassword(value); 97 } catch (Throwable t) { 98 log.warn("Password property " + def.getName() + " could not be encoded.", t); 99 } 100 } 101 contextProperties.put(key.getName(), value); 102 try { 103 OutputStream out = new FileOutputStream (new File (ContextHolder.getContext().getConfDirectory(), "webserver.properties")); 104 contextProperties.store(out, "SSL-Explorer webserver properties"); 105 Util.closeStream(out); 106 107 } catch (IOException ex) { 108 log.error("Failed to save web server properties! Your server may not initialize correctly", ex); 109 } 110 return oldValue; 111 } 112 } 113 | Popular Tags |