View Javadoc

1   /*
2   Copyright (c) 2006, University of Tromsø
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without 
6   modification, are permitted provided that the following conditions are met:
7   
8    * Redistributions of source code must retain the above copyright notice, this list 
9      of conditions and the following disclaimer.
10  
11   * Redistributions in binary form must reproduce the above copyright notice, this 
12     list of conditions and the following disclaimer in the documentation and/or other 
13     materials provided with the distribution.
14  
15   * Neither the name of the University of Tromsø nor the names of its contributors may 
16     be used to endorse or promote products derived from this software without specific 
17     prior written permission.
18  
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
20  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
21  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
22  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
23  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
24  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
25  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
26  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
27  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
28  DAMAGE.
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 }