01: /**********************************************************************************
02: *
03: * $Id: ConfigurationBean.java 22061 2007-03-01 22:54:37Z ray@media.berkeley.edu $
04: *
05: ***********************************************************************************
06: *
07: * Copyright (c) 2007 The Regents of the University of California
08: *
09: * Licensed under the Educational Community License, Version 1.0 (the "License");
10: * you may not use this file except in compliance with the License.
11: * You may obtain a copy of the License at
12: *
13: * http://www.opensource.org/licenses/ecl1.php
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: *
21: **********************************************************************************/package org.sakaiproject.tool.gradebook.ui;
22:
23: import java.util.Map;
24:
25: import org.apache.commons.lang.StringUtils;
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28: import org.sakaiproject.component.api.ServerConfigurationService;
29: import org.springframework.beans.BeansException;
30: import org.springframework.context.ApplicationContext;
31: import org.springframework.context.ApplicationContextAware;
32:
33: /**
34: * Provides a way to override UI bean definitions from the Sakai component
35: * framework while still visibly injecting them in UI configuration files.
36: *
37: * (If our UI logic was based on Spring beans rather than JSF backing beans,
38: * there might be a more straightforward way to deliver this capability.)
39: */
40: public class ConfigurationBean implements ApplicationContextAware {
41: private static final Log log = LogFactory
42: .getLog(ConfigurationBean.class);
43:
44: private ApplicationContext applicationContext;
45:
46: private Map<String, Object> pluginDefaults;
47: private ServerConfigurationService serverConfigurationService;
48:
49: /**
50: * @param name
51: * @return the bean configured to match the given plug-in key, or null if none were found
52: */
53: public Object getPlugin(String key) {
54: Object target = null;
55: if (log.isDebugEnabled())
56: log.debug("key=" + key + ", serverConfigurationService="
57: + serverConfigurationService + ", default="
58: + pluginDefaults.get(key));
59: if (serverConfigurationService != null) {
60: // As of Sakai 2.4, the framework's configuration service
61: // returns the empty string instead of null if a property isn't found.
62: target = StringUtils.stripToNull(serverConfigurationService
63: .getString(key));
64: }
65: if (target == null) {
66: target = pluginDefaults.get(key);
67: }
68: if (target != null) {
69: if (target instanceof String) {
70: // Assume that we got a bean name instead of the
71: // bean itself.
72: target = applicationContext.getBean((String) target);
73: }
74: }
75: if (log.isDebugEnabled())
76: log.debug("for " + key + " returning " + target);
77: return target;
78: }
79:
80: public void setApplicationContext(
81: ApplicationContext applicationContext)
82: throws BeansException {
83: this .applicationContext = applicationContext;
84: }
85:
86: public void setPluginDefaults(Map<String, Object> pluginDefaults) {
87: this .pluginDefaults = pluginDefaults;
88: }
89:
90: /**
91: * If this method is never called (as in the case of standalone builds), the
92: * default map of property values will be used.
93: * @param serverConfigurationService
94: */
95: public void setServerConfigurationService(
96: ServerConfigurationService serverConfigurationService) {
97: this.serverConfigurationService = serverConfigurationService;
98: }
99: }
|