1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package argos.config;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.PrintStream;
39 import java.util.Properties;
40 import java.util.logging.Level;
41 import java.util.logging.Logger;
42
43 import argos.annotation.Description;
44 import argos.annotation.Impact;
45 import argos.annotation.RemoveInstrumentation;
46
47 @Description("This MBean reads the config.properties file and makes the configuration availible")
48 public class Config {
49 private static final Logger logger = Logger.getLogger(Config.class.getName());
50 private static Config singelton;
51
52 public static final String CONFIG_FILE = "config.properties";
53 public static final String BANG_BANG = "!!";
54 public static final String DEPLOY_DESCRIPTOR = "deploy.xml";
55
56 public static final String DEFAULT_ARGOS_DOMAIN = "ArgosDomain";
57 public static final String DEFAULT_BANGBANG_DOMAIN = "BangBangDomain";
58 public static final String DEFAULT_COMPONENT_DOMAIN = "ComponentsDomain";
59 public static final String DEFAULT_DEPLOY_FOLDER = "deployFolder";
60
61 private Properties properties;
62
63 private Config() {
64 super();
65
66 properties = new Properties();
67
68 load();
69
70 logger.finest("Config file loaded correctly.");
71 }
72
73 @Description("Calling this operation will load the config file again.")
74 @Impact(Impact.ACTION)
75 public void load() {
76 try {
77 FileInputStream in = new FileInputStream(CONFIG_FILE);
78 properties.load(in);
79 in.close();
80 }
81 catch(IOException e) {
82 logger.log(Level.SEVERE, "Unable to load config file", e);
83 }
84 }
85
86 @Description("Calling this operation will return the info (from config file) for the given key.")
87 @Impact(Impact.INFO)
88 public String lookup(String key) {
89 return lookup(key, true);
90 }
91
92 @Description("Calling this operation will return the info (from config file) for the given key.")
93 @Impact(Impact.INFO)
94 public String lookup(String key, boolean verbose) {
95 Object result = properties.get(key);
96 if(verbose && result == null) {
97 logger.severe(key + " key didnt give any information!");
98 return null;
99 }
100 else if(result == null) {
101 return null;
102 }
103 else {
104 return result.toString().trim();
105 }
106 }
107
108 public synchronized static Config getInstance() {
109 if(singelton == null) {
110 singelton = new Config();
111 }
112 return singelton;
113 }
114
115 public static String get(String key) {
116 return getInstance().lookup(key);
117 }
118
119 public Properties getProperties() {
120 return properties;
121 }
122
123 @RemoveInstrumentation
124 public void addProperty(String key, String value) {
125 properties.put(key, value);
126 try {
127 PrintStream out = new PrintStream(new FileOutputStream(new File(CONFIG_FILE), true));
128 out.println(key + "=" + value);
129 out.close();
130 }
131 catch(FileNotFoundException e) {
132 logger.log(Level.SEVERE, "Unable to add property to config file: " + e.getMessage(), e);
133 }
134 }
135 }