You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebXmlSettings.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileOutputStream;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.text.MessageFormat;
  23. import java.util.Enumeration;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import javax.servlet.ServletContext;
  27. import com.gitblit.utils.StringUtils;
  28. /**
  29. * Loads Gitblit settings from the context-parameter values of a web.xml file.
  30. *
  31. * @author James Moger
  32. *
  33. */
  34. public class WebXmlSettings extends IStoredSettings {
  35. private final Properties properties = new Properties();
  36. private File overrideFile;
  37. public WebXmlSettings(ServletContext context) {
  38. super(WebXmlSettings.class);
  39. Enumeration<?> keys = context.getInitParameterNames();
  40. while (keys.hasMoreElements()) {
  41. String key = keys.nextElement().toString();
  42. String value = context.getInitParameter(key);
  43. properties.put(key, decodeValue(value));
  44. logger.debug(key + "=" + properties.getProperty(key));
  45. }
  46. }
  47. public void applyOverrides(File overrideFile) {
  48. this.overrideFile = overrideFile;
  49. // apply any web-configured overrides
  50. if (overrideFile.exists()) {
  51. try {
  52. InputStream is = new FileInputStream(overrideFile);
  53. properties.load(is);
  54. is.close();
  55. } catch (Throwable t) {
  56. logger.error(
  57. MessageFormat.format("Failed to apply {0} setting overrides",
  58. overrideFile.getAbsolutePath()), t);
  59. }
  60. }
  61. }
  62. private String decodeValue(String value) {
  63. // decode escaped backslashes and HTML entities
  64. return StringUtils.decodeFromHtml(value).replace("\\\\", "\\");
  65. }
  66. @Override
  67. protected Properties read() {
  68. return properties;
  69. }
  70. @Override
  71. public synchronized boolean saveSettings(Map<String, String> settings) {
  72. try {
  73. Properties props = new Properties();
  74. // load pre-existing web-configuration
  75. if (overrideFile.exists()) {
  76. InputStream is = new FileInputStream(overrideFile);
  77. props.load(is);
  78. is.close();
  79. }
  80. // put all new settings and persist
  81. props.putAll(settings);
  82. OutputStream os = new FileOutputStream(overrideFile);
  83. props.store(os, null);
  84. os.close();
  85. // override current runtime settings
  86. properties.putAll(settings);
  87. return true;
  88. } catch (Throwable t) {
  89. logger.error("Failed to save settings!", t);
  90. }
  91. return false;
  92. }
  93. @Override
  94. public String toString() {
  95. return "WEB.XML";
  96. }
  97. }