File Source: GlobalConfig.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.struts2.admin;
20
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.weblogger.WebloggerException;
30 import org.apache.roller.weblogger.business.PropertiesManager;
31 import org.apache.roller.weblogger.business.WebloggerFactory;
32 import org.apache.roller.weblogger.business.plugins.PluginManager;
33 import org.apache.roller.weblogger.business.plugins.comment.WeblogEntryCommentPlugin;
34 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
35 import org.apache.roller.weblogger.config.runtime.ConfigDef;
36 import org.apache.roller.weblogger.config.runtime.RuntimeConfigDefs;
37 import org.apache.roller.weblogger.pojos.RuntimeConfigProperty;
38 import org.apache.roller.weblogger.ui.struts2.util.UIAction;
39 import org.apache.struts2.interceptor.ParameterAware;
40
41
42 /**
43 * Action which handles editing of global configuration.
44 */
45 public class GlobalConfig extends UIAction implements ParameterAware {
46
/*
P/P * Method: org.apache.roller.weblogger.ui.struts2.admin.GlobalConfig__static_init
*
* Postconditions:
* init'ed(log)
*/
47 private static Log log = LogFactory.getLog(GlobalConfig.class);
48
49 // the request parameters as <String, String[]>
50 private Map params = Collections.EMPTY_MAP;
51
52 // map of config properties
53 private Map properties = Collections.EMPTY_MAP;
54
55 // the runtime config def used to populate the display
56 private ConfigDef globalConfigDef = null;
57
58 // list of comment plugins
59 private List<WeblogEntryCommentPlugin> pluginsList = Collections.EMPTY_LIST;
60
61 // comment plugins that are enabled. this is what the html form submits to
62 private String[] commentPlugins = new String[0];
63
64
/*
P/P * Method: void org.apache.roller.weblogger.ui.struts2.admin.GlobalConfig()
*
* Presumptions:
* init'ed(java.util.Collections.EMPTY_LIST)
* init'ed(java.util.Collections.EMPTY_MAP)
*
* Postconditions:
* this.actionName == &"globalConfig"
* this.commentPlugins == &new String[](GlobalConfig#1)
* this.desiredMenu == &"admin"
* this.globalConfigDef == null
* this.pageTitle == &"configForm.title"
* this.params == java.util.Collections.EMPTY_MAP
* (soft) init'ed(this.params)
* this.properties == this.params
* this.pluginsList == java.util.Collections.EMPTY_LIST
* (soft) init'ed(this.pluginsList)
* ...
*/
65 public GlobalConfig() {
66 this.actionName = "globalConfig";
67 this.desiredMenu = "admin";
68 this.pageTitle = "configForm.title";
69 }
70
71
72 @Override
73 public boolean isWeblogRequired() {
/*
P/P * Method: bool isWeblogRequired()
*
* Postconditions:
* return_value == 0
*/
74 return false;
75 }
76
77 @Override
78 public String requiredUserRole() {
/*
P/P * Method: String requiredUserRole()
*
* Postconditions:
* return_value == &"admin"
*/
79 return "admin";
80 }
81
82
83 /**
84 * Prepare action by loading runtime properties map.
85 */
86 @Override
87 public void myPrepare() {
88 try {
89 // just grab our properties map and make it available to the action
/*
P/P * Method: void myPrepare()
*
* Preconditions:
* (soft) log != null
*
* Presumptions:
* java.util.Iterator:next(...)@101 != null
* org.apache.roller.weblogger.business.Weblogger:getPluginManager(...)@108 != null
* org.apache.roller.weblogger.business.Weblogger:getPropertiesManager(...)@90 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@108 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@90 != null
* ...
*
* Postconditions:
* possibly_updated(this.globalConfigDef)
* init'ed(this.pluginsList)
* possibly_updated(this.properties)
*
* Test Vectors:
* java.lang.String:equals(...)@102: {0}, {1}
* java.util.Iterator:hasNext(...)@101: {0}, {1}
*/
90 PropertiesManager mgr = WebloggerFactory.getWeblogger().getPropertiesManager();
91 setProperties(mgr.getProperties());
92 } catch (WebloggerException ex) {
93 log.error("Error getting runtime properties map", ex);
94 // TODO: i18n
95 addError("Unexpected error accessing Roller properties");
96 }
97
98 // set config def used to draw the view
99 RuntimeConfigDefs defs = WebloggerRuntimeConfig.getRuntimeConfigDefs();
100 List<ConfigDef> configDefs = defs.getConfigDefs();
101 for(ConfigDef configDef : configDefs) {
102 if("global-properties".equals(configDef.getName())) {
103 setGlobalConfigDef(configDef);
104 }
105 }
106
107 // load plugins list
108 PluginManager pmgr = WebloggerFactory.getWeblogger().getPluginManager();
109 setPluginsList(pmgr.getCommentPlugins());
110 }
111
112
113 /**
114 * Display global properties editor form.
115 */
116 @Override
117 public String execute() {
118
119 // setup array of configured plugins
/*
P/P * Method: String execute()
*
* Postconditions:
* return_value == &"success"
* possibly_updated(this.commentPlugins)
*
* Test Vectors:
* org.apache.commons.lang.StringUtils:isEmpty(...)@120: {1}, {0}
*/
120 if (!StringUtils.isEmpty(WebloggerRuntimeConfig.getProperty("users.comments.plugins"))) {
121 setCommentPlugins(StringUtils.split(WebloggerRuntimeConfig.getProperty("users.comments.plugins"), ","));
122 }
123
124 return SUCCESS;
125 }
126
127
128 /**
129 * Save global properties.
130 */
131 public String save() {
132
133 // only set values for properties that are already defined
/*
P/P * Method: String save()
*
* Preconditions:
* this.commentPlugins != null
* this.properties != null
* (soft) log != null
* (soft) this.params != null
*
* Presumptions:
* java.util.Map:get(...)@140 != null
* java.util.Map:get(...)@173 != null
* java.util.Map:keySet(...)@137 != null
* log@171 != null
* org.apache.roller.weblogger.business.Weblogger:getPropertiesManager(...)@179 != null
* ...
*
* Postconditions:
* return_value == &"success"
*
* Test Vectors:
* this.commentPlugins.length: {0}, {1..+Inf}
* java.lang.String:equals(...)@150: {1}, {0}
* java.lang.String:equals(...)@150: {0}, {1}
* java.util.Iterator:hasNext(...)@138: {0}, {1}
* org.apache.roller.weblogger.pojos.RuntimeConfigProperty:getValue(...)@150: Addr_Set{null}, Inverse{null}
*/
134 String propName = null;
135 RuntimeConfigProperty updProp = null;
136 String incomingProp = null;
137 Iterator propsIT = getProperties().keySet().iterator();
138 while(propsIT.hasNext()) {
139 propName = (String) propsIT.next();
140 updProp = (RuntimeConfigProperty) getProperties().get(propName);
141 incomingProp = this.getParameter(updProp.getName());
142
143 log.debug("Checking property ["+propName+"]");
144 log.debug("Request value is ["+incomingProp+"]");
145
146 // some special treatment for booleans
147 // this is a bit hacky since we are assuming that any prop
148 // with a value of "true" or "false" is meant to be a boolean
149 // it may not always be the case, but we should be okay for now
150 if( updProp.getValue() != null // null check needed w/Oracle
151 && (updProp.getValue().equals("true") || updProp.getValue().equals("false"))) {
152
+ 153 if(incomingProp == null || !incomingProp.equals("on"))
154 incomingProp = "false";
155 else
156 incomingProp = "true";
157 }
158
159 // only work on props that were submitted with the request
160 if(incomingProp != null) {
161 log.debug("Setting new value for ["+propName+"]");
162
163 // NOTE: the old way had some locale sensitive way to do this??
164 updProp.setValue(incomingProp.trim());
165 }
166 }
167
168 // special handling for comment plugins
169 String enabledPlugins = "";
170 if(getCommentPlugins().length > 0) {
171 enabledPlugins = StringUtils.join(getCommentPlugins(), ",");
172 }
173 RuntimeConfigProperty prop =
174 (RuntimeConfigProperty) getProperties().get("users.comments.plugins");
175 prop.setValue(enabledPlugins);
176
177 try {
178 // save 'em and flush
179 PropertiesManager mgr = WebloggerFactory.getWeblogger().getPropertiesManager();
180 mgr.saveProperties(getProperties());
181 WebloggerFactory.getWeblogger().flush();
182
183 // notify user of our success
184 addMessage("weblogEdit.changesSaved");
185
186 } catch (WebloggerException ex) {
187 log.error("Error saving roller properties", ex);
188 // TODO: i18n
189 addError("error.update.rollerConfig");
190 }
191
192 return SUCCESS;
193 }
194
195
196 public void setParameters(Map parameters) {
/*
P/P * Method: void setParameters(Map)
*
* Preconditions:
* log != null
* (soft) parameters != null
*
* Presumptions:
* java.util.Map:keySet(...)@201 != null
*
* Postconditions:
* this.params == parameters
* (soft) this.params != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@202: {0}, {1}
* org.apache.commons.logging.Log:isDebugEnabled(...)@199: {0}, {1}
*/
197 this.params = parameters;
198
199 if(log.isDebugEnabled()) {
200 log.debug("Parameter map:");
201 Set<String> keys = parameters.keySet();
202 for(String key : keys) {
203 log.debug(key+" = "+parameters.get(key));
204 }
205 }
206 }
207
208 // convenience method for getting a single parameter as a String
209 private String getParameter(String key) {
210
/*
P/P * Method: String getParameter(String)
*
* Preconditions:
* this.params != null
*
* Postconditions:
* return_value == null
*
* Test Vectors:
* java.util.Map:get(...)@211: Addr_Set{null}, Inverse{null}
* p.length@211: {0}, {1..+Inf}
*/
211 String[] p = (String[]) this.params.get(key);
212 if(p != null && p.length > 0) {
213 return p[0];
214 }
215 return null;
216 }
217
218
219 public Map getProperties() {
/*
P/P * Method: Map getProperties()
*
* Preconditions:
* init'ed(this.properties)
*
* Postconditions:
* return_value == this.properties
* init'ed(return_value)
*/
220 return properties;
221 }
222
223 public void setProperties(Map properties) {
/*
P/P * Method: void setProperties(Map)
*
* Postconditions:
* this.properties == properties
* init'ed(this.properties)
*/
224 this.properties = properties;
225 }
226
227 public ConfigDef getGlobalConfigDef() {
/*
P/P * Method: ConfigDef getGlobalConfigDef()
*
* Preconditions:
* init'ed(this.globalConfigDef)
*
* Postconditions:
* return_value == this.globalConfigDef
* init'ed(return_value)
*/
228 return globalConfigDef;
229 }
230
231 public void setGlobalConfigDef(ConfigDef globalConfigDef) {
/*
P/P * Method: void setGlobalConfigDef(ConfigDef)
*
* Postconditions:
* this.globalConfigDef == globalConfigDef
* init'ed(this.globalConfigDef)
*/
232 this.globalConfigDef = globalConfigDef;
233 }
234
235 public List<WeblogEntryCommentPlugin> getPluginsList() {
/*
P/P * Method: List getPluginsList()
*
* Preconditions:
* init'ed(this.pluginsList)
*
* Postconditions:
* return_value == this.pluginsList
* init'ed(return_value)
*/
236 return pluginsList;
237 }
238
239 public void setPluginsList(List<WeblogEntryCommentPlugin> pluginsList) {
/*
P/P * Method: void setPluginsList(List)
*
* Postconditions:
* this.pluginsList == pluginsList
* init'ed(this.pluginsList)
*/
240 this.pluginsList = pluginsList;
241 }
242
243 public String[] getCommentPlugins() {
/*
P/P * Method: String[] getCommentPlugins()
*
* Preconditions:
* init'ed(this.commentPlugins)
*
* Postconditions:
* return_value == this.commentPlugins
* init'ed(return_value)
*/
244 return commentPlugins;
245 }
246
247 public void setCommentPlugins(String[] commentPlugins) {
/*
P/P * Method: void setCommentPlugins(String[])
*
* Postconditions:
* this.commentPlugins == commentPlugins
* init'ed(this.commentPlugins)
*/
248 this.commentPlugins = commentPlugins;
249 }
250
251 }
SofCheck Inspector Build Version : 2.18479
| GlobalConfig.java |
2009-Jan-02 14:25:36 |
| GlobalConfig.class |
2009-Sep-04 03:12:45 |