1 16 package org.jmanage.core.config; 17 18 import org.jdom.input.SAXBuilder; 19 import org.jdom.JDOMException; 20 import org.jdom.Document; 21 import org.jdom.Element; 22 import org.jmanage.core.crypto.Crypto; 23 import org.jmanage.core.util.Loggers; 24 import org.jmanage.core.util.CoreUtils; 25 26 import java.io.File ; 27 import java.util.*; 28 import java.util.logging.Logger ; 29 import java.util.logging.Level ; 30 31 36 public class ConfigReader implements ConfigConstants{ 37 38 private static final Logger logger = Loggers.getLogger(ConfigReader.class); 39 40 41 private static ConfigReader configReader = 42 new ConfigReader(new File (DEFAULT_CONFIG_FILE_NAME)); 43 44 private static long lastModified = -1; 45 46 private static Document config = null; 47 48 49 54 private ConfigReader(File configFile){ 55 try{ 56 lastModified = configFile.lastModified(); 57 config = new SAXBuilder().build(configFile); 58 }catch(JDOMException jdEx){ 59 logger.log(Level.SEVERE, "Error reading config file " + 60 DEFAULT_CONFIG_FILE_NAME); 61 throw new RuntimeException (jdEx); 62 } 63 } 64 65 71 public static ConfigReader getInstance(){ 72 File configFile = new File (DEFAULT_CONFIG_FILE_NAME); 73 if(lastModified < configFile.lastModified()){ 74 75 configReader = new ConfigReader(configFile); 76 } 77 return configReader; 78 } 79 80 83 public List read(){ 84 85 List applications = 86 config.getRootElement().getChild(APPLICATIONS).getChildren(); 87 return getApplicationConfigList(applications, null); 88 } 89 90 91 private List getApplicationConfigList(List applications, 92 ApplicationConfig clusterConfig){ 93 94 final List applicationConfigList = new LinkedList(); 95 for(Iterator appIterator = applications.iterator(); appIterator.hasNext();){ 96 Element application = (Element)appIterator.next(); 97 ApplicationConfig appConfig = null; 98 if(APPLICATION.equalsIgnoreCase(application.getName())){ 99 appConfig = getApplicationConfig(application); 100 appConfig.setClusterConfig(clusterConfig); 101 }else if(APPLICATION_CLUSTER.equals(application.getName())){ 102 assert clusterConfig == null: "found cluster within a cluster"; 103 appConfig = getApplicationClusterConfig(application); 104 }else{ 105 assert false:"Invalid element:" + application.getName(); 106 } 107 applicationConfigList.add(appConfig); 108 } 109 return applicationConfigList; 110 } 111 112 private ApplicationConfig getApplicationConfig(Element application){ 113 114 List params = application.getChildren(PARAMETER); 115 Iterator paramIterator = params.iterator(); 116 Map paramValues = new HashMap(1); 117 while(paramIterator.hasNext()){ 118 Element param = (Element)paramIterator.next(); 119 paramValues.put(param.getChildTextTrim(PARAMETER_NAME), 120 param.getChildTextTrim(PARAMETER_VALUE)); 121 } 122 123 final String encryptedPassword = 124 application.getAttributeValue(PASSWORD); 125 String password = null; 126 if(encryptedPassword != null){ 127 password = Crypto.decrypt(encryptedPassword); 128 } 129 Integer port = null; 130 if(application.getAttributeValue(PORT) != null){ 131 port = new Integer (application.getAttributeValue(PORT)); 132 } 133 ApplicationConfig config = 134 ApplicationConfigFactory.create( 135 application.getAttributeValue(APPLICATION_ID), 136 application.getAttributeValue(APPLICATION_NAME), 137 application.getAttributeValue(APPLICATION_TYPE), 138 application.getAttributeValue(HOST), 139 port, 140 application.getAttributeValue(URL), 141 application.getAttributeValue(USERNAME), 142 password, 143 paramValues); 144 145 config.setMBeans(getMBeanConfigList(application)); 146 config.setAlerts(getAlertsList(application, config)); 147 config.setGraphs(getGraphConfigList(application, config)); 148 return config; 149 } 150 151 private ApplicationConfig getApplicationClusterConfig(Element application){ 152 153 ApplicationClusterConfig config = 154 new ApplicationClusterConfig( 155 application.getAttributeValue(APPLICATION_ID), 156 application.getAttributeValue(APPLICATION_NAME)); 157 if(application.getChild(APPLICATIONS) != null){ 158 List applications = 159 application.getChild(APPLICATIONS).getChildren(); 160 List appConfigList = getApplicationConfigList(applications, config); 161 config.addAllApplications(appConfigList); 162 } 163 164 config.setMBeans(getMBeanConfigList(application)); 165 return config; 169 } 170 171 private List getMBeanConfigList(Element application){ 172 173 List mbeanConfigList = new LinkedList(); 174 if(application.getChild(MBEANS) != null){ 175 List mbeans = application.getChild(MBEANS).getChildren(MBEAN); 176 for(Iterator it=mbeans.iterator(); it.hasNext(); ){ 177 Element mbean = (Element)it.next(); 178 MBeanConfig mbeanConfig = 179 new MBeanConfig(mbean.getAttributeValue(MBEAN_NAME), 180 mbean.getChildTextTrim(MBEAN_OBJECT_NAME)); 181 mbeanConfigList.add(mbeanConfig); 182 } 183 } 184 return mbeanConfigList; 185 } 186 187 private List getGraphConfigList(Element application, ApplicationConfig appConfig){ 188 189 List graphConfigList = new LinkedList(); 190 if(application.getChild(GRAPHS) != null){ 191 List graphs = application.getChild(GRAPHS).getChildren(GRAPH); 192 for(Iterator it=graphs.iterator(); it.hasNext(); ){ 193 Element graph = (Element)it.next(); 194 List attributes = graph.getChildren(GRAPH_ATTRIBUTE); 195 List attributeConfigList = new LinkedList(); 196 for(Iterator attrIt=attributes.iterator(); attrIt.hasNext(); ){ 197 Element attribute = (Element)attrIt.next(); 198 GraphAttributeConfig graphAttrConfig = 199 new GraphAttributeConfig( 200 attribute.getAttributeValue(GRAPH_ATTRIBUTE_MBEAN), 201 attribute.getAttributeValue(GRAPH_ATTRIBUTE_NAME), 202 attribute.getAttributeValue(GRAPH_ATTRIBUTE_DISPLAY_NAME)); 203 attributeConfigList.add(graphAttrConfig); 204 } 205 String graphId = graph.getAttributeValue(GRAPH_ID); 206 String graphName = graph.getAttributeValue(GRAPH_NAME); 207 long pollingInterval = Long.parseLong( 208 graph.getAttributeValue(GRAPH_POLLING_INTERVAL)); 209 210 GraphConfig graphConfig = 211 new GraphConfig(graphId, graphName, pollingInterval, 212 appConfig, attributeConfigList); 213 graphConfig.setYAxisLabel( 214 graph.getAttributeValue(GRAPH_Y_AXIS_LABEL)); 215 String scaleFactor = graph.getAttributeValue(GRAPH_SCALE_FACTOR); 216 if(scaleFactor != null) 217 graphConfig.setScaleFactor(new Double (scaleFactor)); 218 String scaleUp = graph.getAttributeValue(GRAPH_SCALE_UP); 219 if(scaleUp != null) 220 graphConfig.setScaleUp(Boolean.valueOf(scaleUp)); 221 222 graphConfigList.add(graphConfig); 223 } 224 } 225 return graphConfigList; 226 } 227 228 private List getAlertsList(Element application, ApplicationConfig appConfig){ 229 List alertsList = new LinkedList(); 230 if(application.getChild(ALERTS)!=null){ 231 List alerts = application.getChild(ALERTS).getChildren(ALERT); 232 for(Iterator it=alerts.iterator(); it.hasNext();){ 233 AlertConfig alertConfig = new AlertConfig(); 234 Element alert = (Element)it.next(); 235 alertConfig.setAlertId(alert.getAttributeValue(ALERT_ID)); 236 alertConfig.setAlertName(alert.getAttributeValue(ALERT_NAME)); 237 List alertDeliveryList = alert.getChildren(ALERT_DELIVERY); 238 String [] alertDelivery = new String [alertDeliveryList.size()]; 239 for(int i=0;i<alertDeliveryList.size();i++){ 240 Element alertDel = (Element)alertDeliveryList.get(i); 241 alertDelivery[i] = alertDel.getAttributeValue( 242 ALERT_DELIVERY_TYPE); 243 if(alertDelivery[i].equals(AlertDeliveryConstants.EMAIL_ALERT_DELIVERY_TYPE)){ 244 alertConfig.setEmailAddress(alertDel.getAttributeValue( 245 ConfigConstants.ALERT_EMAIL_ADDRESS)); 246 } 247 } 248 alertConfig.setAlertDelivery(alertDelivery); 249 250 251 Element source = alert.getChild(ALERT_SOURCE); 252 String sourceType = source.getAttributeValue(ALERT_SOURCE_TYPE); 253 String mbean = source.getAttributeValue(ALERT_SOURCE_MBEAN); 254 AlertSourceConfig sourceConfig = null; 255 if(sourceType.equals(AlertSourceConfig.SOURCE_TYPE_NOTIFICATION)){ 256 String notificationType = 257 source.getAttributeValue(ALERT_SOURCE_NOTIFICATION_TYPE); 258 sourceConfig = new AlertSourceConfig(mbean,notificationType); 259 }else if(sourceType.equals( 260 AlertSourceConfig.SOURCE_TYPE_GAUGE_MONITOR)){ 261 String attribute = source.getAttributeValue(ALERT_ATTRIBUTE_NAME); 262 String lowThreshold = source.getAttributeValue( 263 ALERT_ATTRIBUTE_LOW_THRESHOLD); 264 String highThreshold = source.getAttributeValue( 265 ALERT_ATTRIBUTE_HIGH_THRESHOLD); 266 String attributeDataType = source.getAttributeValue( 267 ALERT_ATTRIBUTE_DATA_TYPE); 268 sourceConfig = new AlertSourceConfig(mbean, attribute, 269 CoreUtils.valueOf(lowThreshold,attributeDataType), 270 CoreUtils.valueOf(highThreshold,attributeDataType), 271 attributeDataType); 272 }else if(sourceType.equals( 273 AlertSourceConfig.SOURCE_TYPE_STRING_MONITOR)){ 274 String attribute = source.getAttributeValue( 275 ALERT_ATTRIBUTE_NAME); 276 String stringAttributeValue = source.getAttributeValue( 277 ALERT_STRING_ATTRIBUTE_VALUE); 278 sourceConfig = new AlertSourceConfig(mbean, attribute, 279 stringAttributeValue); 280 } 281 sourceConfig.setApplicationConfig(appConfig); 282 alertConfig.setAlertSourceConfig(sourceConfig); 283 alertsList.add(alertConfig); 284 } 285 } 286 return alertsList; 287 } 288 } | Popular Tags |