Clover Coverage Report - jmxmonitor 1.0.2
Coverage timestamp: Wed Feb 10 2010 07:36:51 GMT
../../../../../img/srcFileCovDistChart9.png 28% of files have more coverage
15   84   4   5
0   45   0.27   3
3     1.33  
1    
 
  DiscriminatingJmxMonitor       Line # 33 15 0% 4 2 88.9% 0.8888889
 
No Tests
 
1    /*
2    * Copyright 2009 Ben Gidley
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   
17    package uk.co.gidley.jmxmonitor.monitoring;
18   
19    import org.slf4j.Logger;
20    import org.slf4j.LoggerFactory;
21   
22    import javax.management.MBeanServerConnection;
23    import javax.management.ObjectInstance;
24    import javax.management.ObjectName;
25    import java.util.HashMap;
26    import java.util.Map;
27    import java.util.Set;
28   
29    /**
30    * A JMX monitor that takes readings based on a discriminator Is that really the best classname I could think of Created
31    * by IntelliJ IDEA. User: ben Date: Jan 8, 2010 Time: 5:11:22 PM
32    */
 
33    public class DiscriminatingJmxMonitor implements Monitor {
34   
35    private static final Logger logger = LoggerFactory.getLogger(DiscriminatingJmxMonitor.class);
36   
37    private String name;
38    private MBeanServerConnection mbeanServerConnection;
39    private String discriminator;
40    private ObjectName objectName;
41   
42    private String attributeName;
43   
 
44  3 toggle public String getName() {
45  3 return name;
46    }
47   
 
48  3 toggle @Override
49    public Object getReading() throws ReadingFailedException {
50  3 try {
51  3 Set<ObjectInstance> beans = mbeanServerConnection.queryMBeans(objectName, null);
52   
53  3 Map<String, Object> results = new HashMap<String, Object>();
54  3 for (ObjectInstance bean : beans) {
55  6 String discriminatorValue = mbeanServerConnection.getAttribute(bean.getObjectName(),
56    this.discriminator).toString();
57  6 results.put(discriminatorValue,
58    mbeanServerConnection.getAttribute(bean.getObjectName(), attributeName));
59    }
60  3 return results;
61    } catch (Exception e) {
62  0 logger.error("Error reading {}. Exception was {}", name, e);
63  0 throw new ReadingFailedException(e);
64    }
65   
66    }
67   
68    /**
69    * @param name The name of this monitor
70    * @param objectName The objectName pattern to resolve 1 or N objects
71    * @param attributeName The attribute to monitor
72    * @param discriminator The attribute to discriminate results by
73    */
 
74  2 toggle public DiscriminatingJmxMonitor(String name, ObjectName objectName, String attributeName, String discriminator,
75    MBeanServerConnection jmxConnection) {
76   
77  2 this.name = name;
78  2 this.mbeanServerConnection = jmxConnection;
79  2 this.objectName = objectName;
80  2 this.attributeName = attributeName;
81  2 this.discriminator = discriminator;
82   
83    }
84    }