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 import java.util.Enumeration;
21 import java.util.Iterator;
22 import java.util.Vector;
23
24 import javax.management.Attribute;
25 import javax.management.AttributeList;
26 import javax.management.DynamicMBean;
27 import javax.management.InstanceAlreadyExistsException;
28 import javax.management.InstanceNotFoundException;
29 import javax.management.JMException;
30 import javax.management.MBeanRegistration;
31 import javax.management.MBeanRegistrationException;
32 import javax.management.MBeanServer;
33 import javax.management.NotCompliantMBeanException;
34 import javax.management.ObjectName;
35 import javax.management.RuntimeOperationsException;
36
37 import org.apache.log4j.Logger;
38 import org.apache.log4j.Appender;
39
40 public abstract class AbstractDynamicMBean implements DynamicMBean,
41 MBeanRegistration {
42
43 String dClassName;
44 MBeanServer server;
45 private final Vector mbeanList = new Vector();
46
47
48
49
50
51
52
53 static protected String getAppenderName(final Appender appender){
54 String name = appender.getName();
55 if (name == null || name.trim().length() == 0) {
56
57 name = appender.toString();
58 }
59 return name;
60 }
61
62
63
64
65
66 public
67 AttributeList getAttributes(String[] attributeNames) {
68
69
70 if (attributeNames == null) {
71 throw new RuntimeOperationsException(
72 new IllegalArgumentException("attributeNames[] cannot be null"),
73 "Cannot invoke a getter of " + dClassName);
74 }
75
76 AttributeList resultList = new AttributeList();
77
78
79 if (attributeNames.length == 0)
80 return resultList;
81
82
83 for (int i=0 ; i<attributeNames.length ; i++){
84 try {
85 Object value = getAttribute((String) attributeNames[i]);
86 resultList.add(new Attribute(attributeNames[i],value));
87 } catch (JMException e) {
88 e.printStackTrace();
89 } catch (RuntimeException e) {
90 e.printStackTrace();
91 }
92 }
93 return(resultList);
94 }
95
96
97
98
99
100 public AttributeList setAttributes(AttributeList attributes) {
101
102
103 if (attributes == null) {
104 throw new RuntimeOperationsException(
105 new IllegalArgumentException("AttributeList attributes cannot be null"),
106 "Cannot invoke a setter of " + dClassName);
107 }
108 AttributeList resultList = new AttributeList();
109
110
111 if (attributes.isEmpty())
112 return resultList;
113
114
115 for (Iterator i = attributes.iterator(); i.hasNext();) {
116 Attribute attr = (Attribute) i.next();
117 try {
118 setAttribute(attr);
119 String name = attr.getName();
120 Object value = getAttribute(name);
121 resultList.add(new Attribute(name,value));
122 } catch(JMException e) {
123 e.printStackTrace();
124 } catch(RuntimeException e) {
125 e.printStackTrace();
126 }
127 }
128 return(resultList);
129 }
130
131 protected
132 abstract
133 Logger getLogger();
134
135 public
136 void postDeregister() {
137 getLogger().debug("postDeregister is called.");
138 }
139
140 public
141 void postRegister(java.lang.Boolean registrationDone) {
142 }
143
144
145
146 public
147 ObjectName preRegister(MBeanServer server, ObjectName name) {
148 getLogger().debug("preRegister called. Server="+server+ ", name="+name);
149 this.server = server;
150 return name;
151 }
152
153
154
155
156 protected
157 void registerMBean(Object mbean, ObjectName objectName)
158 throws InstanceAlreadyExistsException, MBeanRegistrationException,
159 NotCompliantMBeanException {
160 server.registerMBean(mbean, objectName);
161 mbeanList.add(objectName);
162 }
163
164
165
166
167
168
169 public
170 void preDeregister() {
171 getLogger().debug("preDeregister called.");
172
173 Enumeration iterator = mbeanList.elements();
174 while (iterator.hasMoreElements()) {
175 ObjectName name = (ObjectName) iterator.nextElement();
176 try {
177 server.unregisterMBean(name);
178 } catch (InstanceNotFoundException e) {
179 getLogger().warn("Missing MBean " + name.getCanonicalName());
180 } catch (MBeanRegistrationException e) {
181 getLogger().warn("Failed unregistering " + name.getCanonicalName());
182 }
183 }
184 }
185
186
187 }