/webportal/src/main/java/au/org/emii/portal/factory/PortalDocumentFactoryFileImpl.java

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