1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.core.config;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.log4j.Logger;
30 import org.springframework.beans.factory.InitializingBean;
31
32
33
34
35
36 public class SimpleNodeSettingsStore implements NodeSettings, InitializingBean {
37
38 private static final Logger LOG = Logger.getLogger(SimpleNodeSettingsStore.class);
39
40 private boolean enabled;
41 private String propertiesPath;
42 private Properties properties;
43
44 public void afterPropertiesSet() throws Exception {
45 this.enabled = false;
46 if (StringUtils.isEmpty(this.propertiesPath)) {
47 this.propertiesPath = ConfigContext.getCurrentContextConfig().getProperty(Config.NODE_PROPERTIES_PATH);
48 }
49
50 if (StringUtils.isEmpty(this.propertiesPath)) {
51 LOG.warn("No node-level settings are available, the NodeSettingsStore will be disabled.");
52 this.properties = new Properties();
53 } else {
54 this.properties = load();
55 }
56 }
57
58 protected Properties load() throws IOException {
59 Properties p = new Properties();
60 File file = new File(this.propertiesPath);
61 try {
62 if (!file.exists()) {
63 LOG.warn("Properties path '" + this.propertiesPath + "' does not exist, attempting to create it.");
64 if (!file.getParentFile().exists()) {
65 file.getParentFile().mkdirs();
66 }
67 file.createNewFile();
68 }
69 FileInputStream fis = new FileInputStream(this.propertiesPath);
70 try {
71 p.load(fis);
72 this.enabled = true;
73 } finally {
74 fis.close();
75 }
76 } catch (IOException e) {
77 LOG.warn("Properties path '" + this.propertiesPath + "' does not exist despite efforts to create it at: " + file.getAbsolutePath(), e);
78 }
79 return p;
80 }
81
82 public void setPropertiesPath(String path) {
83 this.propertiesPath = path;
84 }
85
86 protected synchronized void store(Properties p) {
87 try {
88 FileOutputStream fos = new FileOutputStream(this.propertiesPath);
89 try {
90 p.store(fos, null);
91 } finally {
92 fos.close();
93 }
94 } catch (IOException e) {
95 throw new ConfigurationException("Failed to persist node-specific settings.", e);
96 }
97 }
98
99 public synchronized String getSetting(String key) {
100 if (!isEnabled()) {
101 LOG.warn("Node settings are not enabled, getSetting('"+key+"') is returning null.");
102 return null;
103 }
104 return this.properties.getProperty(key);
105 }
106
107 public synchronized void setSetting(String key, String value) {
108 if (!isEnabled()) {
109 LOG.warn("Node settings are not enabled, setSetting('"+key+"', '"+value+"') will have no effect.");
110 return;
111 }
112 this.properties.put(key, value);
113 store(this.properties);
114 }
115
116 public synchronized String removeSetting(String key) {
117 if (!isEnabled()) {
118 LOG.warn("Node settings are not enabled, removeSetting('"+key+"') will have no effect.");
119 return null;
120 }
121 String property = (String)this.properties.remove(key);
122 store(this.properties);
123 return property;
124 }
125
126 public synchronized Map<String, String> getSettings() {
127 if (!isEnabled()) {
128 LOG.warn("Node settings are not enabled, returning empty map for getSettings().");
129 return Collections.emptyMap();
130 }
131 Map<String, String> settings = new HashMap<String, String>();
132 for (Object key : this.properties.keySet()) {
133 settings.put((String)key, (String)this.properties.get(key));
134 }
135 return Collections.unmodifiableMap(settings);
136 }
137
138 public synchronized boolean isEnabled() {
139 return this.enabled;
140 }
141
142 }