James Moger
2013-10-01 4360b3af73e5e9e575adee9f8d8b462a20445553
commit | author | age
f13c4c 1 /*
JM 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  */
87cc1e 16 package com.gitblit;
JM 17
97a20e 18 import java.io.File;
JM 19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.InputStream;
22 import java.io.OutputStream;
b774de 23 import java.text.MessageFormat;
f339f5 24 import java.util.Enumeration;
97a20e 25 import java.util.Map;
f339f5 26 import java.util.Properties;
87cc1e 27
JM 28 import javax.servlet.ServletContext;
29
85c2e6 30 import com.gitblit.utils.StringUtils;
JM 31
892570 32 /**
JM 33  * Loads Gitblit settings from the context-parameter values of a web.xml file.
699e71 34  *
892570 35  * @author James Moger
699e71 36  *
892570 37  */
f339f5 38 public class WebXmlSettings extends IStoredSettings {
87cc1e 39
f339f5 40     private final Properties properties = new Properties();
85c2e6 41
b774de 42     private File overrideFile;
97a20e 43
87cc1e 44     public WebXmlSettings(ServletContext context) {
f339f5 45         super(WebXmlSettings.class);
JM 46         Enumeration<?> keys = context.getInitParameterNames();
47         while (keys.hasMoreElements()) {
48             String key = keys.nextElement().toString();
49             String value = context.getInitParameter(key);
85c2e6 50             properties.put(key, decodeValue(value));
JM 51             logger.debug(key + "=" + properties.getProperty(key));
97a20e 52         }
b774de 53     }
JM 54
55     public void applyOverrides(File overrideFile) {
56         this.overrideFile = overrideFile;
699e71 57
97a20e 58         // apply any web-configured overrides
b774de 59         if (overrideFile.exists()) {
97a20e 60             try {
b774de 61                 InputStream is = new FileInputStream(overrideFile);
97a20e 62                 properties.load(is);
JM 63                 is.close();
64             } catch (Throwable t) {
b774de 65                 logger.error(
JM 66                         MessageFormat.format("Failed to apply {0} setting overrides",
67                                 overrideFile.getAbsolutePath()), t);
97a20e 68             }
f339f5 69         }
87cc1e 70     }
892570 71
85c2e6 72     private String decodeValue(String value) {
892570 73         // decode escaped backslashes and HTML entities
85c2e6 74         return StringUtils.decodeFromHtml(value).replace("\\\\", "\\");
JM 75     }
76
87cc1e 77     @Override
f339f5 78     protected Properties read() {
JM 79         return properties;
87cc1e 80     }
2a7306 81
87cc1e 82     @Override
97a20e 83     public synchronized boolean saveSettings(Map<String, String> settings) {
JM 84         try {
85             Properties props = new Properties();
86             // load pre-existing web-configuration
b774de 87             if (overrideFile.exists()) {
JM 88                 InputStream is = new FileInputStream(overrideFile);
97a20e 89                 props.load(is);
JM 90                 is.close();
91             }
b774de 92
97a20e 93             // put all new settings and persist
JM 94             props.putAll(settings);
b774de 95             OutputStream os = new FileOutputStream(overrideFile);
97a20e 96             props.store(os, null);
JM 97             os.close();
b774de 98
97a20e 99             // override current runtime settings
JM 100             properties.putAll(settings);
101             return true;
102         } catch (Throwable t) {
103             logger.error("Failed to save settings!", t);
104         }
105         return false;
106     }
107
108     @Override
87cc1e 109     public String toString() {
cf9550 110         return "WEB.XML";
87cc1e 111     }
JM 112 }