001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/velocity/tags/sakai_2-4-1/tool/src/java/org/sakaiproject/cheftool/PortletConfig.java $
003: * $Id: PortletConfig.java 7519 2006-04-09 13:02:00Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.cheftool;
021:
022: import java.util.Collection;
023: import java.util.Collections;
024: import java.util.Enumeration;
025: import java.util.HashMap;
026: import java.util.Map;
027: import java.util.Properties;
028: import java.util.Set;
029:
030: import javax.servlet.ServletConfig;
031:
032: import org.sakaiproject.tool.api.Placement;
033:
034: /** Provides read access to tool parameters, which are (name, value) pairs.
035: * Write access is not allowed. This provides
036: * an unmodifiable Map that looks first in the tool configuration, then in
037: * the tool registration, then in the servlet configuration. The values
038: * in the tool configuration may have been configured by the end-user,
039: * the values in the tool registration are defaulted in the tool registration
040: * file, and the values in the servlet configuration come from web.xml and
041: * cannot be modified by the user.
042: */
043: public class PortletConfig implements Map {
044: private ServletConfig m_servletCofig;
045: private Properties m_toolConfig;
046: private Properties m_toolReg;
047: private Placement m_placement;
048:
049: /** Lazily instantiated map populated from the servlet config, the tool configuration,
050: * and the tool registration - not modifiable after creation.
051: */
052: private Map m_map = null;
053:
054: PortletConfig(ServletConfig config, Properties toolConfig,
055: Properties reg, Placement p) {
056: m_servletCofig = config;
057: m_toolConfig = toolConfig;
058: m_toolReg = reg;
059: m_placement = p;
060: }
061:
062: public String getInitParameter(String name, String dflt) {
063: String value = getInitParameter(name);
064:
065: if (value == null) {
066: value = dflt;
067: }
068:
069: return value;
070: }
071:
072: public String getInitParameter(String name) {
073: String value = null;
074:
075: // check the tool config if present
076: if (m_toolConfig != null) {
077: value = m_toolConfig.getProperty(name);
078: }
079:
080: // check the registration if present and no value so far
081: if (value == null) {
082: if (m_toolReg != null) {
083: value = m_toolReg.getProperty(name);
084: }
085: }
086:
087: // check the servlet config if no value so far
088: if (value == null) {
089: value = m_servletCofig.getInitParameter(name);
090: }
091:
092: return value;
093: }
094:
095: public Map getInitParameters() {
096: return getMap();
097: }
098:
099: /** Populate the Map, such that (name, value) pairs will be found in this order:
100: * <ol>
101: * <li>ToolConfiguration</li>
102: * <li>ToolRegistration</li>
103: * <li>ServletConfig</li>
104: * </ul>
105: */
106: private synchronized Map getMap() {
107: if (m_map != null)
108: return m_map;
109:
110: Map map = new HashMap();
111:
112: // load up with ServletConfig parameters
113: Enumeration e = m_servletCofig.getInitParameterNames();
114: while (e.hasMoreElements()) {
115: String name = (String) e.nextElement();
116: map.put(name, m_servletCofig.getInitParameter(name));
117: }
118:
119: // add ToolRegistration
120: if (m_toolReg != null) {
121: for (e = m_toolReg.propertyNames(); e.hasMoreElements();) {
122: String name = (String) e.nextElement();
123: map.put(name, m_toolReg.getProperty(name));
124: }
125: }
126:
127: // add ToolConfiguration
128: if (m_toolConfig != null) {
129: for (Enumeration i = m_toolConfig.propertyNames(); i
130: .hasMoreElements();) {
131: String name = (String) i.nextElement();
132: map.put(name, m_toolConfig.getProperty(name));
133: }
134: }
135:
136: // ensure that this Map won't get modified past this point
137: m_map = Collections.unmodifiableMap(map);
138: return m_map;
139: }
140:
141: public String getTitle() {
142: if (m_placement != null) {
143: return m_placement.getTitle();
144: }
145:
146: return "";
147: }
148:
149: /**
150: * Special non-jetspeed imitation: get three possible init parameter values:
151: * [0] from servlet config
152: * [1] from tool registration
153: * [2] from tool config
154: * nulls if not present
155: */
156: public String[] get3InitParameter(String name) {
157: String[] value = new String[3];
158:
159: // check the tool config if present
160: if (m_toolConfig != null) {
161: value[2] = m_toolConfig.getProperty(name);
162: }
163:
164: // check the registration if present
165: if (m_toolReg != null) {
166: value[1] = m_toolReg.getProperty(name);
167: }
168:
169: // check the servlet config
170: value[0] = m_servletCofig.getInitParameter(name);
171:
172: return value;
173: }
174:
175: /** <b>Unsupported operation</b> */
176: public void clear() {
177: throw new UnsupportedOperationException();
178: }
179:
180: public boolean containsKey(Object key) {
181: return getMap().containsKey(key);
182: }
183:
184: public boolean containsValue(Object value) {
185: return getMap().containsValue(value);
186: }
187:
188: public Set entrySet() {
189: return getMap().entrySet();
190: }
191:
192: public Object get(Object key) {
193: return getMap().get(key);
194: }
195:
196: public boolean isEmpty() {
197: return getMap().isEmpty();
198: }
199:
200: public Set keySet() {
201: return getMap().keySet();
202: }
203:
204: /** <b>Unsupported operation</b> */
205: public Object put(Object key, Object value) {
206: throw new UnsupportedOperationException();
207: }
208:
209: /** <b>Unsupported operation</b> */
210: public void putAll(Map t) {
211: throw new UnsupportedOperationException();
212: }
213:
214: /** <b>Unsupported operation</b> */
215: public Object remove(Object key) {
216: throw new UnsupportedOperationException();
217: }
218:
219: public int size() {
220: return getMap().size();
221: }
222:
223: public Collection values() {
224: return getMap().values();
225: }
226: }
|