1 16 package org.jmanage.core.config; 17 18 import org.jdom.Document; 19 import org.jdom.Element; 20 import org.jdom.output.XMLOutputter; 21 import org.jmanage.core.crypto.Crypto; 22 23 import java.io.FileOutputStream ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 33 public class ConfigWriter { 34 35 private static final ConfigWriter configWriter = new ConfigWriter(); 36 37 public static ConfigWriter getInstance(){ 38 return configWriter; 39 } 40 41 private ConfigWriter(){} 42 43 public void write(List applications) { 44 45 try { 46 Document doc = new Document(); 47 Element rootElement = new Element(ConfigConstants.APPLICATION_CONFIG); 48 Element applicationsElement = new Element(ConfigConstants.APPLICATIONS); 49 rootElement.addContent(applicationsElement); 50 for(Iterator it=applications.iterator(); it.hasNext();){ 51 ApplicationConfig application = (ApplicationConfig)it.next(); 52 53 Element applicationElement = null; 54 if(application.isCluster()){ 55 applicationElement = 56 createApplicationClusterElement(application); 57 }else{ 58 applicationElement = createApplicationElement(application); 59 } 60 61 62 applicationsElement.addContent(applicationElement); 63 } 64 doc.setRootElement(rootElement); 65 66 XMLOutputter writer = new XMLOutputter(); 67 writer.output(doc, new FileOutputStream (ConfigConstants.DEFAULT_CONFIG_FILE_NAME)); 68 } catch (Exception e) { 69 throw new RuntimeException (e); 70 } 71 } 72 73 79 private Element createApplicationClusterElement(ApplicationConfig application){ 80 assert application.isCluster(); 81 Element applicationElement = createApplicationElement(application); 82 Element childApplicationsElement = new Element(ConfigConstants.APPLICATIONS); 83 applicationElement.addContent(childApplicationsElement); 84 for(Iterator it=application.getApplications().iterator(); it.hasNext();){ 85 ApplicationConfig appConfig = (ApplicationConfig)it.next(); 86 Element childAppElement = createApplicationElement(appConfig); 87 childApplicationsElement.addContent(childAppElement); 88 } 89 return applicationElement; 90 } 91 92 private Element createApplicationElement(ApplicationConfig application){ 93 Element applicationElement = null; 94 if(application.isCluster()){ 95 applicationElement = new Element(ConfigConstants.APPLICATION_CLUSTER); 96 }else{ 97 applicationElement = new Element(ConfigConstants.APPLICATION); 98 } 99 applicationElement.setAttribute(ConfigConstants.APPLICATION_ID, 101 application.getApplicationId()); 102 applicationElement.setAttribute(ConfigConstants.APPLICATION_NAME, 103 application.getName()); 104 if(application.getHost() != null){ 105 applicationElement.setAttribute(ConfigConstants.HOST, 106 application.getHost()); 107 applicationElement.setAttribute(ConfigConstants.PORT, 108 String.valueOf(application.getPort())); 109 } 110 if(application.getURL() != null){ 111 applicationElement.setAttribute(ConfigConstants.URL, 112 application.getURL()); 113 } 114 if(application.getUsername() != null){ 115 applicationElement.setAttribute(ConfigConstants.USERNAME, 116 application.getUsername()); 117 } 118 if(application.getPassword() != null){ 119 String encryptedPassword = 120 Crypto.encryptToString(application.getPassword()); 121 applicationElement.setAttribute(ConfigConstants.PASSWORD, 122 encryptedPassword); 123 } 124 if(!application.isCluster()){ 125 applicationElement.setAttribute(ConfigConstants.APPLICATION_TYPE, 126 application.getType()); 127 } 128 final Map params = application.getParamValues(); 129 if(params != null){ 130 for(Iterator param=params.keySet().iterator(); param.hasNext(); ){ 131 String paramName = (String )param.next(); 132 Element paramElement = 133 new Element(ConfigConstants.PARAMETER); 134 Element paramNameElement = 135 new Element(ConfigConstants.PARAMETER_NAME); 136 Element paramValueElement = 137 new Element(ConfigConstants.PARAMETER_VALUE); 138 paramNameElement.setText(paramName); 139 paramValueElement.setText((String )params.get(paramName)); 140 paramElement.addContent(paramNameElement); 141 paramElement.addContent(paramValueElement); 142 applicationElement.addContent(paramElement); 143 } 144 } 145 146 147 Element mbeansElement = createMBeansElement(application); 148 applicationElement.addContent(mbeansElement); 149 150 151 Element alertsElement = createAlertsElement(application); 152 applicationElement.addContent(alertsElement); 153 if(!application.isCluster()){ 154 155 Element graphsElement = createGraphsElement(application); 156 applicationElement.addContent(graphsElement); 157 } 158 return applicationElement; 159 } 160 161 private Element createMBeansElement(ApplicationConfig application){ 162 Element mbeansElement = new Element(ConfigConstants.MBEANS); 163 if(application.getMBeans() == null){ 164 return mbeansElement; 165 } 166 for(Iterator mbeans = application.getMBeans().iterator(); 167 mbeans.hasNext();){ 168 MBeanConfig mbeanConfig = (MBeanConfig)mbeans.next(); 169 Element mbeanElement = new Element(ConfigConstants.MBEAN); 170 mbeanElement.setAttribute(ConfigConstants.MBEAN_NAME, 171 mbeanConfig.getName()) ; 172 Element objectNameElement = 173 new Element(ConfigConstants.MBEAN_OBJECT_NAME); 174 objectNameElement.setText(mbeanConfig.getObjectName()); 175 mbeanElement.addContent(objectNameElement); 176 mbeansElement.addContent(mbeanElement); 177 } 178 return mbeansElement; 179 } 180 181 private Element createAlertsElement(ApplicationConfig application){ 182 Element alertsElement = new Element(ConfigConstants.ALERTS); 183 if(application.getAlerts()==null){ 184 return alertsElement; 185 } 186 for(Iterator alerts = application.getAlerts().iterator(); 187 alerts.hasNext();){ 188 AlertConfig alertConfig = (AlertConfig)alerts.next(); 189 Element alertElement = new Element(ConfigConstants.ALERT); 190 alertElement.setAttribute(ConfigConstants.ALERT_ID, 191 alertConfig.getAlertId()); 192 alertElement.setAttribute(ConfigConstants.ALERT_NAME, 193 alertConfig.getAlertName()); 194 AlertSourceConfig sourceConfig = alertConfig.getAlertSourceConfig(); 196 Element source = new Element(ConfigConstants.ALERT_SOURCE); 197 source.setAttribute(ConfigConstants.ALERT_SOURCE_TYPE, 198 sourceConfig.getSourceType()); 199 source.setAttribute(ConfigConstants.ALERT_SOURCE_MBEAN, 200 sourceConfig.getObjectName()); 201 if(sourceConfig.getSourceType().equals( 202 AlertSourceConfig.SOURCE_TYPE_NOTIFICATION)){ 203 source.setAttribute(ConfigConstants.ALERT_SOURCE_NOTIFICATION_TYPE, 204 sourceConfig.getNotificationType()); 205 }else if(sourceConfig.getSourceType().equals( 206 AlertSourceConfig.SOURCE_TYPE_GAUGE_MONITOR)){ 207 source.setAttribute(ConfigConstants.ALERT_ATTRIBUTE_NAME, 208 sourceConfig.getAttributeName()); 209 source.setAttribute(ConfigConstants.ALERT_ATTRIBUTE_DATA_TYPE, 210 sourceConfig.getAttributeDataTYpe()); 211 source.setAttribute(ConfigConstants.ALERT_ATTRIBUTE_LOW_THRESHOLD, 212 sourceConfig.getLowThreshold().toString()); 213 source.setAttribute(ConfigConstants.ALERT_ATTRIBUTE_HIGH_THRESHOLD, 214 sourceConfig.getHighThreshold().toString()); 215 }else if(sourceConfig.getSourceType().equals( 216 AlertSourceConfig.SOURCE_TYPE_STRING_MONITOR)){ 217 source.setAttribute(ConfigConstants.ALERT_ATTRIBUTE_NAME, 218 sourceConfig.getAttributeName()); 219 source.setAttribute(ConfigConstants.ALERT_STRING_ATTRIBUTE_VALUE, 220 sourceConfig.getStringAttributeValue()); 221 } 222 alertElement.addContent(source); 223 224 String [] alertDelivery = alertConfig.getAlertDelivery(); 226 for(int i=0;i<alertDelivery.length;i++){ 227 Element alertDel = new Element(ConfigConstants.ALERT_DELIVERY); 228 alertDel.setAttribute(ConfigConstants.ALERT_DELIVERY_TYPE, 229 alertDelivery[i]); 230 if(alertDelivery[i].equals(AlertDeliveryConstants.EMAIL_ALERT_DELIVERY_TYPE)){ 231 alertDel.setAttribute(ConfigConstants.ALERT_EMAIL_ADDRESS, 232 alertConfig.getEmailAddress()); 233 } 234 alertElement.addContent(alertDel); 235 } 236 237 238 alertsElement.addContent(alertElement); 239 } 240 return alertsElement; 241 } 242 243 private Element createGraphsElement(ApplicationConfig application){ 244 Element graphsElement = new Element(ConfigConstants.GRAPHS); 245 if(application.getGraphs() == null){ 246 return graphsElement; 247 } 248 for(Iterator graphs = application.getGraphs().iterator(); 249 graphs.hasNext();){ 250 GraphConfig graphConfig = (GraphConfig)graphs.next(); 251 Element graphElement = new Element(ConfigConstants.GRAPH); 252 graphElement.setAttribute(ConfigConstants.GRAPH_ID, 253 graphConfig.getId()); 254 graphElement.setAttribute(ConfigConstants.GRAPH_NAME, 255 graphConfig.getName()); 256 graphElement.setAttribute(ConfigConstants.GRAPH_POLLING_INTERVAL, 257 Long.toString(graphConfig.getPollingInterval())); 258 if(graphConfig.getYAxisLabel() != null){ 259 graphElement.setAttribute(ConfigConstants.GRAPH_Y_AXIS_LABEL, 260 graphConfig.getYAxisLabel()); 261 } 262 if(graphConfig.getScaleFactor() != null){ 263 graphElement.setAttribute(ConfigConstants.GRAPH_SCALE_FACTOR, 264 graphConfig.getScaleFactor().toString()); 265 } 266 if(graphConfig.isScaleUp() != null){ 267 graphElement.setAttribute(ConfigConstants.GRAPH_SCALE_UP, 268 graphConfig.isScaleUp().toString()); 269 } 270 271 for(Iterator attributes = graphConfig.getAttributes().iterator(); 272 attributes.hasNext();){ 273 GraphAttributeConfig attrConfig = 274 (GraphAttributeConfig)attributes.next(); 275 Element attributeElement = 276 new Element(ConfigConstants.GRAPH_ATTRIBUTE); 277 attributeElement.setAttribute(ConfigConstants.GRAPH_ATTRIBUTE_MBEAN, 278 attrConfig.getMBean()); 279 attributeElement.setAttribute(ConfigConstants.GRAPH_ATTRIBUTE_NAME, 280 attrConfig.getAttribute()); 281 attributeElement.setAttribute(ConfigConstants.GRAPH_ATTRIBUTE_DISPLAY_NAME, 282 attrConfig.getDisplayName()); 283 graphElement.addContent(attributeElement); 284 } 285 graphsElement.addContent(graphElement); 286 } 287 return graphsElement; 288 } 289 290 } 291 | Popular Tags |