Coverage Report - argos.config.Config
 
Classes in this File Line Coverage Branch Coverage Complexity
Config
0%
0/34
0%
0/8
0
 
 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  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  
 }