KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > jmx > AppenderDynamicMBean


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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 org.apache.log4j.jmx;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import org.apache.log4j.*;
21 import org.apache.log4j.helpers.OptionConverter;
22 import org.apache.log4j.spi.OptionHandler;
23
24 import java.util.Vector JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import javax.management.MBeanAttributeInfo JavaDoc;
28 import javax.management.MBeanConstructorInfo JavaDoc;
29 import javax.management.MBeanNotificationInfo JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.MBeanInfo JavaDoc;
32 import javax.management.Attribute JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34
35 import javax.management.MBeanException JavaDoc;
36 import javax.management.AttributeNotFoundException JavaDoc;
37 import javax.management.RuntimeOperationsException JavaDoc;
38 import javax.management.ReflectionException JavaDoc;
39 import javax.management.InvalidAttributeValueException JavaDoc;
40 import javax.management.MBeanOperationInfo JavaDoc;
41 import javax.management.MBeanParameterInfo JavaDoc;
42
43 import java.beans.Introspector JavaDoc;
44 import java.beans.BeanInfo JavaDoc;
45 import java.beans.PropertyDescriptor JavaDoc;
46 import java.beans.IntrospectionException JavaDoc;
47
48 public class AppenderDynamicMBean extends AbstractDynamicMBean {
49
50   private MBeanConstructorInfo JavaDoc[] dConstructors = new MBeanConstructorInfo JavaDoc[1];
51   private Vector JavaDoc dAttributes = new Vector JavaDoc();
52   private String JavaDoc dClassName = this.getClass().getName();
53
54   private Hashtable JavaDoc dynamicProps = new Hashtable JavaDoc(5);
55   private MBeanOperationInfo JavaDoc[] dOperations = new MBeanOperationInfo JavaDoc[2];
56   private String JavaDoc dDescription =
57      "This MBean acts as a management facade for log4j appenders.";
58
59   // This category instance is for logging.
60
private static Logger cat = Logger.getLogger(AppenderDynamicMBean.class);
61
62   // We wrap this appender instance.
63
private Appender appender;
64
65   public AppenderDynamicMBean(Appender appender) throws IntrospectionException JavaDoc {
66     this.appender = appender;
67     buildDynamicMBeanInfo();
68   }
69
70   private
71   void buildDynamicMBeanInfo() throws IntrospectionException JavaDoc {
72     Constructor JavaDoc[] constructors = this.getClass().getConstructors();
73     dConstructors[0] = new MBeanConstructorInfo JavaDoc(
74              "AppenderDynamicMBean(): Constructs a AppenderDynamicMBean instance",
75          constructors[0]);
76
77
78     BeanInfo JavaDoc bi = Introspector.getBeanInfo(appender.getClass());
79     PropertyDescriptor JavaDoc[] pd = bi.getPropertyDescriptors();
80
81     int size = pd.length;
82
83     for(int i = 0; i < size; i++) {
84       String JavaDoc name = pd[i].getName();
85       Method JavaDoc readMethod = pd[i].getReadMethod();
86       Method JavaDoc writeMethod = pd[i].getWriteMethod();
87       if(readMethod != null) {
88     Class JavaDoc returnClass = readMethod.getReturnType();
89     if(isSupportedType(returnClass)) {
90       String JavaDoc returnClassName;
91       if(returnClass.isAssignableFrom(Priority.class)) {
92         returnClassName = "java.lang.String";
93       } else {
94         returnClassName = returnClass.getName();
95       }
96
97       dAttributes.add(new MBeanAttributeInfo JavaDoc(name,
98                          returnClassName,
99                          "Dynamic",
100                          true,
101                          writeMethod != null,
102                          false));
103       dynamicProps.put(name, new MethodUnion(readMethod, writeMethod));
104     }
105       }
106     }
107
108     MBeanParameterInfo JavaDoc[] params = new MBeanParameterInfo JavaDoc[0];
109
110     dOperations[0] = new MBeanOperationInfo JavaDoc("activateOptions",
111                         "activateOptions(): add an appender",
112                         params,
113                         "void",
114                         MBeanOperationInfo.ACTION);
115
116     params = new MBeanParameterInfo JavaDoc[1];
117     params[0] = new MBeanParameterInfo JavaDoc("layout class", "java.lang.String",
118                        "layout class");
119
120     dOperations[1] = new MBeanOperationInfo JavaDoc("setLayout",
121                         "setLayout(): add a layout",
122                         params,
123                         "void",
124                         MBeanOperationInfo.ACTION);
125   }
126
127   private
128   boolean isSupportedType(Class JavaDoc clazz) {
129     if(clazz.isPrimitive()) {
130       return true;
131     }
132
133     if(clazz == String JavaDoc.class) {
134       return true;
135     }
136
137
138     if(clazz.isAssignableFrom(Priority.class)) {
139       return true;
140     }
141
142     return false;
143
144
145   }
146
147
148
149   public
150   MBeanInfo JavaDoc getMBeanInfo() {
151     cat.debug("getMBeanInfo called.");
152
153     MBeanAttributeInfo JavaDoc[] attribs = new MBeanAttributeInfo JavaDoc[dAttributes.size()];
154     dAttributes.toArray(attribs);
155
156     return new MBeanInfo JavaDoc(dClassName,
157              dDescription,
158              attribs,
159              dConstructors,
160              dOperations,
161              new MBeanNotificationInfo JavaDoc[0]);
162   }
163
164   public
165   Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc params[], String JavaDoc signature[])
166     throws MBeanException JavaDoc,
167     ReflectionException JavaDoc {
168
169     if(operationName.equals("activateOptions") &&
170                      appender instanceof OptionHandler) {
171       OptionHandler oh = (OptionHandler) appender;
172       oh.activateOptions();
173       return "Options activated.";
174     } else if (operationName.equals("setLayout")) {
175       Layout layout = (Layout) OptionConverter.instantiateByClassName((String JavaDoc)
176                                       params[0],
177                                       Layout.class,
178                                       null);
179       appender.setLayout(layout);
180       registerLayoutMBean(layout);
181     }
182     return null;
183   }
184
185   void registerLayoutMBean(Layout layout) {
186     if(layout == null)
187       return;
188
189     String JavaDoc name = appender.getName()+",layout="+layout.getClass().getName();
190     cat.debug("Adding LayoutMBean:"+name);
191     ObjectName JavaDoc objectName = null;
192     try {
193       LayoutDynamicMBean appenderMBean = new LayoutDynamicMBean(layout);
194       objectName = new ObjectName JavaDoc("log4j:appender="+name);
195       server.registerMBean(appenderMBean, objectName);
196
197       dAttributes.add(new MBeanAttributeInfo JavaDoc("appender="+name,
198                          "javax.management.ObjectName",
199                          "The "+name+" layout.",
200                          true,
201                          true,
202                          false));
203
204     } catch(Exception JavaDoc e) {
205       cat.error("Could not add DynamicLayoutMBean for ["+name+"].", e);
206     }
207   }
208
209   protected
210   Logger getLogger() {
211     return cat;
212   }
213
214
215   public
216   Object JavaDoc getAttribute(String JavaDoc attributeName) throws AttributeNotFoundException JavaDoc,
217                                                    MBeanException JavaDoc,
218                                                    ReflectionException JavaDoc {
219
220        // Check attributeName is not null to avoid NullPointerException later on
221
if (attributeName == null) {
222       throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
223             "Attribute name cannot be null"),
224        "Cannot invoke a getter of " + dClassName + " with null attribute name");
225     }
226
227     cat.debug("getAttribute called with ["+attributeName+"].");
228     if(attributeName.startsWith("appender="+appender.getName()+",layout")) {
229       try {
230     return new ObjectName JavaDoc("log4j:"+attributeName );
231       } catch(Exception JavaDoc e) {
232     cat.error("attributeName", e);
233       }
234     }
235
236     MethodUnion mu = (MethodUnion) dynamicProps.get(attributeName);
237
238     //cat.debug("----name="+attributeName+", b="+b);
239

240     if(mu != null && mu.readMethod != null) {
241       try {
242     return mu.readMethod.invoke(appender, null);
243       } catch(Exception JavaDoc e) {
244     return null;
245       }
246     }
247
248
249
250     // If attributeName has not been recognized throw an AttributeNotFoundException
251
throw(new AttributeNotFoundException JavaDoc("Cannot find " + attributeName +
252                      " attribute in " + dClassName));
253
254   }
255
256
257   public
258   void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc,
259                                                 InvalidAttributeValueException JavaDoc,
260                                                 MBeanException JavaDoc,
261                                                 ReflectionException JavaDoc {
262
263     // Check attribute is not null to avoid NullPointerException later on
264
if (attribute == null) {
265       throw new RuntimeOperationsException JavaDoc(
266                   new IllegalArgumentException JavaDoc("Attribute cannot be null"),
267           "Cannot invoke a setter of " + dClassName +
268           " with null attribute");
269     }
270     String JavaDoc name = attribute.getName();
271     Object JavaDoc value = attribute.getValue();
272
273     if (name == null) {
274       throw new RuntimeOperationsException JavaDoc(
275                     new IllegalArgumentException JavaDoc("Attribute name cannot be null"),
276             "Cannot invoke the setter of "+dClassName+
277             " with null attribute name");
278     }
279
280
281
282     MethodUnion mu = (MethodUnion) dynamicProps.get(name);
283
284     if(mu != null && mu.writeMethod != null) {
285       Object JavaDoc[] o = new Object JavaDoc[1];
286
287       Class JavaDoc[] params = mu.writeMethod.getParameterTypes();
288       if(params[0] == org.apache.log4j.Priority.class) {
289     value = OptionConverter.toLevel((String JavaDoc) value,
290                     (Level) getAttribute(name));
291       }
292       o[0] = value;
293
294       try {
295     mu.writeMethod.invoke(appender, o);
296
297       } catch(Exception JavaDoc e) {
298     cat.error("FIXME", e);
299       }
300     } else if(name.endsWith(".layout")) {
301
302     } else {
303       throw(new AttributeNotFoundException JavaDoc("Attribute " + name +
304                        " not found in " +
305                        this.getClass().getName()));
306     }
307   }
308
309   public
310   ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) {
311     cat.debug("preRegister called. Server="+server+ ", name="+name);
312     this.server = server;
313     registerLayoutMBean(appender.getLayout());
314
315     return name;
316   }
317
318
319 }
320
321
Popular Tags