| 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 | 0 | 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 | 0 | super(); |
| 65 | |
|
| 66 | 0 | properties = new Properties(); |
| 67 | |
|
| 68 | 0 | load(); |
| 69 | |
|
| 70 | 0 | logger.finest("Config file loaded correctly."); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
@Description("Calling this operation will load the config file again.") |
| 74 | |
@Impact(Impact.ACTION) |
| 75 | |
public void load() { |
| 76 | |
try { |
| 77 | 0 | FileInputStream in = new FileInputStream(CONFIG_FILE); |
| 78 | 0 | properties.load(in); |
| 79 | 0 | in.close(); |
| 80 | |
} |
| 81 | 0 | catch(IOException e) { |
| 82 | 0 | logger.log(Level.SEVERE, "Unable to load config file", e); |
| 83 | 0 | } |
| 84 | 0 | } |
| 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 | 0 | 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 | 0 | Object result = properties.get(key); |
| 96 | 0 | if(verbose && result == null) { |
| 97 | 0 | logger.severe(key + " key didnt give any information!"); |
| 98 | 0 | return null; |
| 99 | |
} |
| 100 | 0 | else if(result == null) { |
| 101 | 0 | return null; |
| 102 | |
} |
| 103 | |
else { |
| 104 | 0 | return result.toString().trim(); |
| 105 | |
} |
| 106 | |
} |
| 107 | |
|
| 108 | |
public synchronized static Config getInstance() { |
| 109 | 0 | if(singelton == null) { |
| 110 | 0 | singelton = new Config(); |
| 111 | |
} |
| 112 | 0 | return singelton; |
| 113 | |
} |
| 114 | |
|
| 115 | |
public static String get(String key) { |
| 116 | 0 | return getInstance().lookup(key); |
| 117 | |
} |
| 118 | |
|
| 119 | |
public Properties getProperties() { |
| 120 | 0 | return properties; |
| 121 | |
} |
| 122 | |
|
| 123 | |
@RemoveInstrumentation |
| 124 | |
public void addProperty(String key, String value) { |
| 125 | 0 | properties.put(key, value); |
| 126 | |
try { |
| 127 | 0 | PrintStream out = new PrintStream(new FileOutputStream(new File(CONFIG_FILE), true)); |
| 128 | 0 | out.println(key + "=" + value); |
| 129 | 0 | out.close(); |
| 130 | |
} |
| 131 | 0 | catch(FileNotFoundException e) { |
| 132 | 0 | logger.log(Level.SEVERE, "Unable to add property to config file: " + e.getMessage(), e); |
| 133 | 0 | } |
| 134 | 0 | } |
| 135 | |
} |