01: /*
02: * This is free software, licensed under the Gnu Public License (GPL)
03: * get a copy from <http://www.gnu.org/licenses/gpl.html>
04: * $Id: PropertyCommand.java,v 1.4 2005/11/27 16:20:28 hzeller Exp $
05: * author: Henner Zeller <H.Zeller@acm.org>
06: */
07: package henplus.commands.properties;
08:
09: import henplus.HenPlus;
10: import henplus.PropertyRegistry;
11: import henplus.io.ConfigurationContainer;
12: import henplus.property.PropertyHolder;
13:
14: import java.util.HashMap;
15: import java.util.Iterator;
16: import java.util.Map;
17:
18: /**
19: * Set global HenPlus properties.
20: */
21: public class PropertyCommand extends AbstractPropertyCommand {
22: private final static String SETTINGS_FILENAME = "properties";
23: private final HenPlus _henplus;
24: private final PropertyRegistry _registry;
25: private final ConfigurationContainer _config;
26:
27: public PropertyCommand(HenPlus henplus, PropertyRegistry registry) {
28: _henplus = henplus;
29: _registry = registry;
30: _config = _henplus
31: .createConfigurationContainer(SETTINGS_FILENAME);
32: }
33:
34: protected String getSetCommand() {
35: return "set-property";
36: }
37:
38: protected String getHelpHeader() {
39: return "global HenPlus";
40: }
41:
42: protected PropertyRegistry getRegistry() {
43: return _registry;
44: }
45:
46: public boolean requiresValidSession(String cmd) {
47: return false;
48: }
49:
50: public void load() {
51: Map props = _config.readProperties();
52:
53: Iterator it = props.entrySet().iterator();
54: while (it.hasNext()) {
55: Map.Entry entry = (Map.Entry) it.next();
56: try {
57: _registry.setProperty((String) entry.getKey(),
58: (String) entry.getValue());
59: } catch (Exception e) {
60: // don't care. silently ignore errornous entries.
61: }
62: }
63: }
64:
65: public void shutdown() {
66: Map writeMap = new HashMap();
67: Iterator propIt = (_registry.getPropertyMap().entrySet()
68: .iterator());
69: while (propIt.hasNext()) {
70: Map.Entry entry = (Map.Entry) propIt.next();
71: PropertyHolder holder = (PropertyHolder) entry.getValue();
72: writeMap.put(entry.getKey(), holder.getValue());
73: }
74: _config.storeProperties(writeMap, true, "user properties");
75: }
76: }
77:
78: /*
79: * Local variables:
80: * c-basic-offset: 4
81: * compile-command: "ant -emacs -find build.xml"
82: * End:
83: */
|