001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: // Created on Aug 29, 2006
017: package org.kuali.rice.config;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.Properties;
027:
028: import org.apache.commons.lang.StringUtils;
029: import org.apache.log4j.Logger;
030: import org.kuali.rice.core.Core;
031: import org.springframework.beans.factory.InitializingBean;
032:
033: /**
034: * A simple node settings store that backs the settings with a properties file
035: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
036: */
037: public class SimpleNodeSettingsStore implements NodeSettings,
038: InitializingBean {
039:
040: private static final Logger LOG = Logger
041: .getLogger(SimpleNodeSettingsStore.class);
042:
043: private boolean enabled;
044: private String propertiesPath;
045: private Properties properties;
046:
047: public void afterPropertiesSet() throws Exception {
048: this .enabled = false;
049: if (StringUtils.isEmpty(this .propertiesPath)) {
050: this .propertiesPath = Core.getCurrentContextConfig()
051: .getProperty(Config.NODE_PROPERTIES_PATH);
052: }
053: // if it's still empty, then node settings are not available
054: if (StringUtils.isEmpty(this .propertiesPath)) {
055: LOG
056: .warn("No node-level settings are available, the NodeSettingsStore will be disabled.");
057: this .properties = new Properties();
058: } else {
059: this .properties = load();
060: }
061: }
062:
063: protected Properties load() throws IOException {
064: Properties p = new Properties();
065: File file = new File(this .propertiesPath);
066: try {
067: if (!file.exists()) {
068: LOG.warn("Properties path '" + this .propertiesPath
069: + "' does not exist, attempting to create it.");
070: if (!file.getParentFile().exists()) {
071: file.getParentFile().mkdirs();
072: }
073: file.createNewFile();
074: }
075: FileInputStream fis = new FileInputStream(
076: this .propertiesPath);
077: try {
078: p.load(fis);
079: this .enabled = true;
080: } finally {
081: fis.close();
082: }
083: } catch (IOException e) {
084: LOG
085: .warn(
086: "Properties path '"
087: + this .propertiesPath
088: + "' does not exist despite efforts to create it at: "
089: + file.getAbsolutePath(), e);
090: }
091: return p;
092: }
093:
094: public void setPropertiesPath(String path) {
095: this .propertiesPath = path;
096: }
097:
098: protected synchronized void store(Properties p) {
099: try {
100: FileOutputStream fos = new FileOutputStream(
101: this .propertiesPath);
102: try {
103: p.store(fos, null);
104: } finally {
105: fos.close();
106: }
107: } catch (IOException e) {
108: throw new ConfigurationException(
109: "Failed to persist node-specific settings.", e);
110: }
111: }
112:
113: public synchronized String getSetting(String key) {
114: if (!isEnabled()) {
115: LOG.warn("Node settings are not enabled, getSetting('"
116: + key + "') is returning null.");
117: return null;
118: }
119: return this .properties.getProperty(key);
120: }
121:
122: public synchronized void setSetting(String key, String value) {
123: if (!isEnabled()) {
124: LOG.warn("Node settings are not enabled, setSetting('"
125: + key + "', '" + value + "') will have no effect.");
126: return;
127: }
128: this .properties.put(key, value);
129: store(this .properties);
130: }
131:
132: public synchronized String removeSetting(String key) {
133: if (!isEnabled()) {
134: LOG.warn("Node settings are not enabled, removeSetting('"
135: + key + "') will have no effect.");
136: return null;
137: }
138: String property = (String) this .properties.remove(key);
139: store(this .properties);
140: return property;
141: }
142:
143: public synchronized Map<String, String> getSettings() {
144: if (!isEnabled()) {
145: LOG
146: .warn("Node settings are not enabled, returning empty map for getSettings().");
147: return Collections.emptyMap();
148: }
149: Map<String, String> settings = new HashMap<String, String>();
150: for (Object key : this .properties.keySet()) {
151: settings.put((String) key, (String) this .properties
152: .get(key));
153: }
154: return Collections.unmodifiableMap(settings);
155: }
156:
157: public synchronized boolean isEnabled() {
158: return this.enabled;
159: }
160:
161: }
|