File Source: PlanetConfig.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.planet.ui;
20
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.RollerException;
28 import org.apache.roller.planet.business.PlanetFactory;
29 import org.apache.roller.planet.business.PropertiesManager;
30 import org.apache.roller.planet.config.PlanetRuntimeConfig;
31 import org.apache.roller.planet.config.runtime.ConfigDef;
32 import org.apache.roller.planet.config.runtime.RuntimeConfigDefs;
33 import org.apache.roller.planet.pojos.RuntimeConfigProperty;
34 import org.apache.struts2.interceptor.ParameterAware;
35
36
37 /**
38 * Planet Config Action.
39 *
40 * Handles editing of planet global runtime properties.
41 */
42 public class PlanetConfig extends PlanetUIAction implements ParameterAware {
43
/*
P/P * Method: org.apache.roller.weblogger.planet.ui.PlanetConfig__static_init
*
* Postconditions:
* init'ed(log)
*/
44 private static Log log = LogFactory.getLog(PlanetConfig.class);
45
46 // original request parameters
47 private Map parameters = Collections.EMPTY_MAP;
48
49 // runtime properties data
50 private Map properties = Collections.EMPTY_MAP;
51
52 // the runtime config def used to populate the display
53 private ConfigDef globalConfigDef = null;
54
55
/*
P/P * Method: void org.apache.roller.weblogger.planet.ui.PlanetConfig()
*
* Presumptions:
* init'ed(java.util.Collections.EMPTY_MAP)
*
* Postconditions:
* this.actionName == &"planetConfig"
* this.desiredMenu == &"admin"
* this.globalConfigDef == null
* this.planet == null
* this.pageTitle == &"planetConfig.title"
* this.parameters == java.util.Collections.EMPTY_MAP
* (soft) init'ed(this.parameters)
* this.properties == this.parameters
*/
56 public PlanetConfig() {
57 this.actionName = "planetConfig";
58 this.desiredMenu = "admin";
59 this.pageTitle = "planetConfig.title";
60 }
61
62
63 @Override
64 public String requiredUserRole() {
/*
P/P * Method: String requiredUserRole()
*
* Postconditions:
* return_value == &"admin"
*/
65 return "admin";
66 }
67
68 @Override
69 public boolean isWeblogRequired() {
/*
P/P * Method: bool isWeblogRequired()
*
* Postconditions:
* return_value == 0
*/
70 return false;
71 }
72
73
74 @Override
75 public void myPrepare() {
76 try {
77 // just grab our properties map
/*
P/P * Method: void myPrepare()
*
* Preconditions:
* (soft) log != null
*
* Presumptions:
* java.util.Iterator:next(...)@87 != null
* org.apache.roller.planet.business.Planet:getPropertiesManager(...)@78 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@78 != null
* org.apache.roller.planet.config.PlanetRuntimeConfig:getRuntimeConfigDefs(...)@85 != null
* org.apache.roller.planet.config.runtime.RuntimeConfigDefs:getConfigDefs(...)@86 != null
*
* Postconditions:
* possibly_updated(this.globalConfigDef)
* possibly_updated(this.properties)
*
* Test Vectors:
* java.lang.String:equals(...)@88: {0}, {1}
* java.util.Iterator:hasNext(...)@87: {0}, {1}
*/
78 PropertiesManager pMgr = PlanetFactory.getPlanet().getPropertiesManager();
79 setProperties(pMgr.getProperties());
80 } catch (RollerException ex) {
81 log.error("Error loading planet properties");
82 }
83
84 // set config def used to draw the view
85 RuntimeConfigDefs defs = PlanetRuntimeConfig.getRuntimeConfigDefs();
86 List<ConfigDef> configDefs = defs.getConfigDefs();
87 for(ConfigDef configDef : configDefs) {
88 if("global-properties".equals(configDef.getName())) {
89 setGlobalConfigDef(configDef);
90 }
91 }
92 }
93
94
95 public String execute() {
/*
P/P * Method: String execute()
*
* Postconditions:
* return_value == &"input"
*/
96 return INPUT;
97 }
98
99
100 public String save() {
101
102 try {
103 // only set values for properties that are already defined
/*
P/P * Method: String save()
*
* Preconditions:
* (soft) log != null
* (soft) this.parameters != null
* (soft) this.properties != null
*
* Presumptions:
* java.util.Map:get(...)@113 != null
* java.util.Map:keySet(...)@107 != null
* org.apache.roller.planet.business.Planet:getPropertiesManager(...)@142 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@142 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@144 != null
* ...
*
* Postconditions:
* return_value == &"input"
*
* Test Vectors:
* java.lang.String:equals(...)@124: {1}, {0}
* java.lang.String:equals(...)@124: {0}, {1}
* java.lang.String:equals(...)@127: {1}, {0}
* java.util.Iterator:hasNext(...)@108: {0}, {1}
* java.util.Map:get(...)@114: Addr_Set{null}, Inverse{null}
* org.apache.roller.planet.pojos.RuntimeConfigProperty:getValue(...)@124: Addr_Set{null}, Inverse{null}
* propValues.length@114: {0}, {1..+Inf}
*/
104 String propName = null;
105 RuntimeConfigProperty updProp = null;
106 String incomingProp = null;
107 Iterator propsIT = getProperties().keySet().iterator();
108 while(propsIT.hasNext()) {
109 propName = (String) propsIT.next();
110
111 log.debug("Checking property ["+propName+"]");
112
113 updProp = (RuntimeConfigProperty) getProperties().get(propName);
114 String[] propValues = (String[]) getParameters().get(updProp.getName());
115 if(propValues != null && propValues.length > 0) {
116 // we don't deal with multi-valued props
117 incomingProp = propValues[0];
118 }
119
120 // some special treatment for booleans
121 // this is a bit hacky since we are assuming that any prop
122 // with a value of "true" or "false" is meant to be a boolean
123 // it may not always be the case, but we should be okay for now
124 if( updProp.getValue() != null // null check needed w/Oracle
125 && (updProp.getValue().equals("true") || updProp.getValue().equals("false"))) {
126
127 if(incomingProp == null || !incomingProp.equals("on"))
128 incomingProp = "false";
129 else
130 incomingProp = "true";
131 }
132
133 // only work on props that were submitted with the request
134 if(incomingProp != null) {
135 log.debug("Setting new value for ["+propName+"]");
136
137 updProp.setValue(incomingProp.trim());
138 }
139 }
140
141 // save it
142 PropertiesManager pMgr = PlanetFactory.getPlanet().getPropertiesManager();
143 pMgr.saveProperties(this.properties);
144 PlanetFactory.getPlanet().flush();
145
146 addMessage("ConfigForm.message.saveSucceeded");
147
148 } catch (RollerException e) {
149 log.error(e);
150 addError("ConfigForm.error.saveFailed");
151 }
152
153 return INPUT;
154 }
155
156
157 public Map getParameters() {
/*
P/P * Method: Map getParameters()
*
* Preconditions:
* init'ed(this.parameters)
*
* Postconditions:
* return_value == this.parameters
* init'ed(return_value)
*/
158 return parameters;
159 }
160
161 public void setParameters(Map parameters) {
/*
P/P * Method: void setParameters(Map)
*
* Postconditions:
* this.parameters == parameters
* init'ed(this.parameters)
*/
162 this.parameters = parameters;
163 }
164
165 public Map getProperties() {
/*
P/P * Method: Map getProperties()
*
* Preconditions:
* init'ed(this.properties)
*
* Postconditions:
* return_value == this.properties
* init'ed(return_value)
*/
166 return properties;
167 }
168
169 public void setProperties(Map properties) {
/*
P/P * Method: void setProperties(Map)
*
* Postconditions:
* this.properties == properties
* init'ed(this.properties)
*/
170 this.properties = properties;
171 }
172
173 public ConfigDef getGlobalConfigDef() {
/*
P/P * Method: ConfigDef getGlobalConfigDef()
*
* Preconditions:
* init'ed(this.globalConfigDef)
*
* Postconditions:
* return_value == this.globalConfigDef
* init'ed(return_value)
*/
174 return globalConfigDef;
175 }
176
177 public void setGlobalConfigDef(ConfigDef globalConfigDef) {
/*
P/P * Method: void setGlobalConfigDef(ConfigDef)
*
* Postconditions:
* this.globalConfigDef == globalConfigDef
* init'ed(this.globalConfigDef)
*/
178 this.globalConfigDef = globalConfigDef;
179 }
180
181 }
SofCheck Inspector Build Version : 2.18479
| PlanetConfig.java |
2009-Jan-02 14:24:58 |
| PlanetConfig.class |
2009-Sep-04 03:12:46 |