Source code: com/arranger/jarl/shell/ShellConfig.java
1 package com.arranger.jarl.shell;
2
3 import com.arranger.jarl.util.IOUtil;
4 import com.arranger.jarl.util.StringTools;
5
6 import java.io.File;
7 import java.io.OutputStream;
8 import java.util.Properties;
9
10 /**
11 * ShellConfig
12 * maintains a property bag for the jarl shell app.
13 * Usage:
14 * <code>
15 * ShellConfig config = new ShellConfig();
16 * config.getProperties().setProperty("foo", "bar");
17 * config.save();
18 * </code>
19 */
20 public class ShellConfig {
21
22 protected Properties m_properties = new Properties();
23
24 public ShellConfig() throws Exception {
25 File prefFile = getPrefFile();
26 if (prefFile != null && prefFile.exists()) {
27 m_properties = IOUtil.loadProperties(prefFile.getAbsolutePath());
28 }
29 }
30
31 public void save() throws Exception {
32 File prefFile = getPrefFile();
33 if (prefFile != null) {
34 OutputStream outputStream = IOUtil.getOutputStream(prefFile.getAbsolutePath());
35 m_properties.store(outputStream, "jarl shell properties");
36 outputStream.close();
37 }
38 }
39
40 public Properties getProperties() {
41 return m_properties;
42 }
43
44 public String getProperty(String name) {
45 return m_properties.getProperty(name);
46 }
47
48 public void setProperty(String name, String value) {
49 if (name == null || value == null) {
50 return;
51 }
52 m_properties.setProperty(name, value);
53 }
54
55 protected File getPrefFile() {
56 String userDir = System.getProperty("user.home");
57 File file = new File(userDir);
58 if (!file.exists()) {
59 return null;
60 }
61 String prefFile = StringTools.appendPath(userDir, ".jarlshell.properties");
62 return new File(prefFile);
63 }
64 }