/webportal/src/main/java/au/org/emii/portal/factory/PortalDocumentFactoryFileImpl.java
Java | 173 lines | 106 code | 22 blank | 45 comment | 4 complexity | 5f726b2855adb4bda9d8cbe1dad72a31 MD5 | raw file
1/* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 6package au.org.emii.portal.factory; 7 8import au.org.emii.portal.config.ConfigurationFile; 9import au.org.emii.portal.config.xmlbeans.PortalDocument; 10import au.org.emii.portal.settings.Settings; 11import java.io.ByteArrayInputStream; 12import java.io.File; 13import java.io.FileInputStream; 14import java.io.FileNotFoundException; 15import java.io.IOException; 16import java.io.InputStream; 17import org.apache.commons.io.FileUtils; 18import org.apache.log4j.Level; 19import org.apache.log4j.Logger; 20import org.apache.xmlbeans.XmlException; 21import org.springframework.beans.factory.InitializingBean; 22import org.springframework.beans.factory.annotation.Required; 23import org.springframework.util.Assert; 24 25/** 26 * Support for validation, reading and writing of configuration file 27 * 28 * File backed implementation of PortalDocumentFactory 29 * @author geoff 30 */ 31public class PortalDocumentFactoryFileImpl implements PortalDocumentFactory, ConfigurationFile, InitializingBean { 32 33 /** 34 * Logger instance 35 */ 36 private Logger logger = Logger.getLogger(this.getClass()); 37 38 private final static String CONFIG_FILE = "webportal_config.xml"; 39 private Settings settings = null; 40 41 /** 42 * Save the content string inside the config file. Make sure you've validated 43 * the content before attempting to save (use validateConfigFileContents()) 44 * @param content XML string to save as config file contents 45 * @return true on success, otherwise false 46 */ 47 @Override 48 public boolean saveAsConfigFile(String content) { 49 boolean saved = false; 50 try { 51 FileUtils.writeStringToFile(new File(getConfigFilename()), content); 52 saved = true; 53 } catch (IOException ex) { 54 logger.error("Error saving config file. CAUSE: " + ex.getMessage()); 55 } 56 57 return saved; 58 } 59 60 @Override 61 public boolean validateConfigFileContents(String content, boolean quiet) { 62 boolean valid = false; 63 Level level = (quiet) ? Level.DEBUG : Level.INFO; 64 try { 65 InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8")); 66 PortalDocument doc = PortalDocument.Factory.parse(is); 67 if (doc.validate()) { 68 valid = true; 69 } 70 } catch (XmlException ex) { 71 logger.log(level,"Invalid XML validating string for conformity to config file xml schema (not the real config file!) - CAUSE: " + ex.getMessage()); 72 } catch (IOException ex) { 73 logger.error("IO error reading inputstream from byte array(?) should never happen - CAUSE: " + ex.getMessage()); 74 } 75 return valid; 76 77 } 78 79 /** 80 * Lookup the the name of the config file we should be reading from the environement 81 * then validate and parse it returning a pointer to the root element. 82 * 83 * If an error occurs here (null returned) then the system is FUBAR 84 * 85 * @return PortalDocument instance if reading succeeded, null if an error was encountered 86 */ 87 @Override 88 public PortalDocument createPortalDocumentInstance() { 89 PortalDocument portalDocument = null; 90 91 // Have xmlbeans read the file and parse it 92 InputStream is = null; 93 try { 94 is = new FileInputStream(getConfigFilename()); 95 portalDocument = PortalDocument.Factory.parse(is); 96 97 // if the XML is valid, we're good to go... 98 if (portalDocument.validate()) { 99 logger.debug("configuration file is valid xml"); 100 } else { 101 logger.error( 102 "invalid XML in configuration file! - validate manually with " 103 + "xmllint --schema on the command line to determine the problem!"); 104 portalDocument = null; 105 } 106 } catch (FileNotFoundException e) { 107 portalDocument = null; 108 logger.error("Could not load portal configuration file from: " + getConfigFilename()); 109 } catch (XmlException e) { 110 portalDocument = null; 111 logger.error("Unknown error while processing XML in configuration file - check this stack trace", e); 112 } catch (IOException e) { 113 portalDocument = null; 114 logger.error("IOException reading configuration - should never happen, you may have big problems! - check this stack trace", e); 115 } finally { 116 try { 117 is.close(); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 } 122 123 return portalDocument; 124 } 125 126 /** 127 * Validate contents of config file to schema 128 * 129 * File contents must be UTF-8! 130 * @param content 131 * @return 132 */ 133 @Override 134 public boolean validateConfigFileContents(String content) { 135 return validateConfigFileContents(content, false); 136 } 137 138 139 /** 140 * Read and validate the configuration file, then return it as a string 141 * if it validated. 142 * @return Contents of config file as string if valid, otherwise null 143 */ 144 @Override 145 public String configurationFileContents() { 146 PortalDocument doc = createPortalDocumentInstance(); 147 return (doc == null) ? null : doc.toString(); 148 } 149 150 public Settings getSettings() { 151 return settings; 152 } 153 154 @Required 155 public void setSettings(Settings settings) { 156 this.settings = settings; 157 } 158 159 160 /** 161 * Return full path and filename of config file 162 * @return 163 */ 164 public String getConfigFilename() { 165 return settings.getConfigPath() + File.separator + CONFIG_FILE; 166 } 167 168 @Override 169 public void afterPropertiesSet() throws Exception { 170 Assert.notNull(settings); 171 } 172 173}