1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.jmx;
19
20
21 import java.util.Iterator;
22 import javax.management.DynamicMBean;
23 import javax.management.AttributeList;
24 import javax.management.Attribute;
25 import javax.management.RuntimeOperationsException;
26 import javax.management.MBeanRegistration;
27 import javax.management.MBeanServer;
28 import javax.management.ObjectName;
29
30 import org.apache.log4j.Logger;
31
32 public abstract class AbstractDynamicMBean implements DynamicMBean,
33 MBeanRegistration {
34
35 String dClassName;
36 MBeanServer server;
37
38 /***
39 * Enables the to get the values of several attributes of the Dynamic MBean.
40 */
41 public
42 AttributeList getAttributes(String[] attributeNames) {
43
44
45 if (attributeNames == null) {
46 throw new RuntimeOperationsException(
47 new IllegalArgumentException("attributeNames[] cannot be null"),
48 "Cannot invoke a getter of " + dClassName);
49 }
50
51 AttributeList resultList = new AttributeList();
52
53
54 if (attributeNames.length == 0)
55 return resultList;
56
57
58 for (int i=0 ; i<attributeNames.length ; i++){
59 try {
60 Object value = getAttribute((String) attributeNames[i]);
61 resultList.add(new Attribute(attributeNames[i],value));
62 } catch (Exception e) {
63 e.printStackTrace();
64 }
65 }
66 return(resultList);
67 }
68
69 /***
70 * Sets the values of several attributes of the Dynamic MBean, and returns the
71 * list of attributes that have been set.
72 */
73 public AttributeList setAttributes(AttributeList attributes) {
74
75
76 if (attributes == null) {
77 throw new RuntimeOperationsException(
78 new IllegalArgumentException("AttributeList attributes cannot be null"),
79 "Cannot invoke a setter of " + dClassName);
80 }
81 AttributeList resultList = new AttributeList();
82
83
84 if (attributes.isEmpty())
85 return resultList;
86
87
88 for (Iterator i = attributes.iterator(); i.hasNext();) {
89 Attribute attr = (Attribute) i.next();
90 try {
91 setAttribute(attr);
92 String name = attr.getName();
93 Object value = getAttribute(name);
94 resultList.add(new Attribute(name,value));
95 } catch(Exception e) {
96 e.printStackTrace();
97 }
98 }
99 return(resultList);
100 }
101
102 protected
103 abstract
104 Logger getLogger();
105
106 public
107 void postDeregister() {
108 getLogger().debug("postDeregister is called.");
109 }
110
111 public
112 void postRegister(java.lang.Boolean registrationDone) {
113 }
114
115
116
117 public
118 void preDeregister() {
119 getLogger().debug("preDeregister called.");
120 }
121
122 public
123 ObjectName preRegister(MBeanServer server, ObjectName name) {
124 getLogger().debug("preRegister called. Server="+server+ ", name="+name);
125 this.server = server;
126 return name;
127 }
128
129
130
131 }