View Javadoc

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