Coverage Report - org.naftulin.configmgr.ConfigurationManagerConsole
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationManagerConsole
0%
0/51
0%
0/18
0
 
 1  
 package org.naftulin.configmgr;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import org.apache.commons.lang.StringUtils;
 6  
 import org.apache.commons.lang.exception.ExceptionUtils;
 7  
 import org.apache.commons.lang.time.DateFormatUtils;
 8  
 import org.apache.log4j.Logger;
 9  
 
 10  0
 public class ConfigurationManagerConsole implements ConfigurationManagerConsoleMBean {
 11  0
         private static final Logger log = Logger.getLogger(ConfigurationManagerConsole.class);
 12  
         private ConfigurationManagement manager;
 13  0
         private String errorMessage = null;
 14  0
         private String jmxConsoleDateFormat = "MM/dd/yyyy HH:mm:ss";
 15  0
         private int jmxConsoleAbbreviateLength = 20;
 16  0
         private int jmxConsoleAbbreviateContentLength=1000;
 17  
         
 18  
         
 19  
         
 20  0
         public String getJmxConsoleDateFormat() { return jmxConsoleDateFormat; }        
 21  0
         public void setJmxConsoleDateFormat(final String dateFormat) { this.jmxConsoleDateFormat = dateFormat; }        
 22  0
         public int getJmxConsoleAbbreviateFileLength() { return jmxConsoleAbbreviateLength;        }
 23  0
         public void setJmxConsoleAbbreviateFileLength(final int len) { jmxConsoleAbbreviateLength = len; }
 24  0
         public int getJmxConsoleAbbreviateContentLength() { return jmxConsoleAbbreviateContentLength; }
 25  0
         public void setJmxConsoleAbbreviateContentLength(final int len) { jmxConsoleAbbreviateContentLength = len; }
 26  
         
 27  0
         public ConfigurationManagerConsole() {
 28  
                 try {
 29  0
                         manager = ConfigurationManagerFactory.getConfigurationManagement();
 30  0
                 } catch(ConfigurationManagerException e) {
 31  0
                         log.fatal("Could not start Configuration Manager Console. Error: " + e.getMessage(), e);
 32  0
                         errorMessage = "Could not start Configuration Manager Console. Error: " + e.getMessage() + "\n";
 33  0
                         errorMessage += ExceptionUtils.getStackTrace(e);
 34  
                 }
 35  0
         }
 36  
         
 37  
         /* (non-Javadoc)
 38  
          * @see org.naftulin.configmgr.ConfigurationManagerConsoleMBean#reload()
 39  
          */
 40  
         public String reload() {
 41  0
                 if (manager != null) {
 42  
                         try {
 43  0
                                 manager.reload();
 44  0
                         } catch (ConfigurationManagerException e) {
 45  0
                                 log.error("Error had occured while reloading " + e.getMessage(), e);
 46  0
                                 return "Error had occured while reloading " + e.getMessage();
 47  
                         }                
 48  
                 }
 49  0
                 return manager == null ? errorMessage : "Successfully reloaded configuration manager";
 50  
         }
 51  
         
 52  
         /* (non-Javadoc)
 53  
          * @see org.naftulin.configmgr.ConfigurationManagerConsoleMBean#getConfigurationEntriesCount()
 54  
          */
 55  
         public int getConfigurationEntriesCount() {
 56  0
                 return manager == null ? -1 : manager.getConfigurationManagmentEntries().size();
 57  
         }
 58  
         
 59  
         /* (non-Javadoc)
 60  
          * @see org.naftulin.configmgr.ConfigurationManagerConsoleMBean#getConfigurationEntries()
 61  
          */
 62  
         public String[] getConfigurationEntries() {
 63  
                 String[] conifurationEntries;
 64  0
                 if (manager != null) {
 65  0
                         final List<ConfigurationManagementEntry> list = manager.getConfigurationManagmentEntries();
 66  0
                         conifurationEntries = new String[list.size()];
 67  0
                         for(int i =0; i < conifurationEntries.length; ++i) {
 68  0
                                 ConfigurationManagementEntry entry = list.get(i);
 69  0
                                 conifurationEntries[i] = "key: " + entry.getKey() 
 70  0
                                                                                 + ", modified: " + DateFormatUtils.format(entry.getModifiedDate(), jmxConsoleDateFormat) 
 71  0
                                                                                 + ", file name: " + abbreviate(entry.getFileName());
 72  
                         }
 73  
                 } else {
 74  0
                         conifurationEntries = new String[1];
 75  0
                         conifurationEntries[0] = errorMessage;
 76  
                 }
 77  0
                 return conifurationEntries;
 78  
         }
 79  
         
 80  
         public String getContent(final String key) {
 81  0
                 String content = errorMessage;
 82  0
                 if (manager != null) {                        
 83  
                         try {
 84  0
                                 final ConfigurationManagementEntry entry = manager.getConfigurationManagmentEntry(key);                                
 85  0
                                 content = entry.getContent() == null ? "empty content" : entry.getContent().toString();
 86  0
                                 content = StringUtils.abbreviate(content, jmxConsoleAbbreviateContentLength);
 87  0
                         } catch (ConfigurationManagerException e) {
 88  0
                                 content = "Key provided " + key + " was not a valid configuration manager key. Please check confiuration entries for possible keys.";
 89  
                         }                                
 90  
                 }
 91  
                 
 92  0
                 return content;
 93  
         }
 94  
         
 95  
         private String abbreviate(final String fileName) {
 96  0
                 if (fileName == null) {
 97  0
                         return "";                        
 98  
                 }
 99  
                 
 100  0
                 final int len = fileName.length();
 101  0
                 if (len <= jmxConsoleAbbreviateLength) {
 102  0
                         return fileName;
 103  
                 }
 104  0
                 return "..." + StringUtils.right(fileName, jmxConsoleAbbreviateLength);
 105  
         }
 106  
 }