KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > config > ConfigReader


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import java.util.*;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30
31 /**
32  *
33  * Date: Jun 19, 2004
34  * @author Shashank
35  */

36 public class ConfigReader implements ConfigConstants{
37
38     private static final Logger JavaDoc logger = Loggers.getLogger(ConfigReader.class);
39
40     /* Single instance */
41     private static ConfigReader configReader =
42             new ConfigReader(new File JavaDoc(DEFAULT_CONFIG_FILE_NAME));
43     /* Last modified time of the configuration file */
44     private static long lastModified = -1;
45     /* Cache the configuration file */
46     private static Document config = null;
47
48
49     /**
50      * Initilizations done here.
51      *
52      * @param configFile
53      */

54     private ConfigReader(File JavaDoc 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 JavaDoc(jdEx);
62         }
63     }
64
65     /**
66      * Invalidate cached information about configured applciations if the
67      * configuration file got updated after last read.
68      *
69      * @return
70      */

71     public static ConfigReader getInstance(){
72         File JavaDoc configFile = new File JavaDoc(DEFAULT_CONFIG_FILE_NAME);
73         if(lastModified < configFile.lastModified()){
74             /* Refresh the cache */
75             configReader = new ConfigReader(configFile);
76         }
77         return configReader;
78     }
79
80     /**
81      * To retrieve the details of all configured applications.
82      */

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 JavaDoc encryptedPassword =
124                 application.getAttributeValue(PASSWORD);
125         String JavaDoc password = null;
126         if(encryptedPassword != null){
127             password = Crypto.decrypt(encryptedPassword);
128         }
129         Integer JavaDoc port = null;
130         if(application.getAttributeValue(PORT) != null){
131             port = new Integer JavaDoc(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         // todo: graphs are not yet supported at the cluster level
166
// todo: to enable this, we will have to store applicationId at graph attribute level
167
//config.setGraphs(getGraphConfigList(application, config));
168
return config;
169     }
170
171     private List getMBeanConfigList(Element application){
172         /* read mbeans */
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         /* read graphs */
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 JavaDoc graphId = graph.getAttributeValue(GRAPH_ID);
206                 String JavaDoc 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 JavaDoc scaleFactor = graph.getAttributeValue(GRAPH_SCALE_FACTOR);
216                 if(scaleFactor != null)
217                     graphConfig.setScaleFactor(new Double JavaDoc(scaleFactor));
218                 String JavaDoc 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 JavaDoc[] alertDelivery = new String JavaDoc[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                 /* set alert source */
251                 Element source = alert.getChild(ALERT_SOURCE);
252                 String JavaDoc sourceType = source.getAttributeValue(ALERT_SOURCE_TYPE);
253                 String JavaDoc mbean = source.getAttributeValue(ALERT_SOURCE_MBEAN);
254                 AlertSourceConfig sourceConfig = null;
255                 if(sourceType.equals(AlertSourceConfig.SOURCE_TYPE_NOTIFICATION)){
256                     String JavaDoc 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 JavaDoc attribute = source.getAttributeValue(ALERT_ATTRIBUTE_NAME);
262                     String JavaDoc lowThreshold = source.getAttributeValue(
263                             ALERT_ATTRIBUTE_LOW_THRESHOLD);
264                     String JavaDoc highThreshold = source.getAttributeValue(
265                             ALERT_ATTRIBUTE_HIGH_THRESHOLD);
266                     String JavaDoc 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 JavaDoc attribute = source.getAttributeValue(
275                             ALERT_ATTRIBUTE_NAME);
276                     String JavaDoc 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