KickJava   Java API By Example, From Geeks To Geeks.

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


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.Logger;
21 import org.apache.log4j.Level;
22 import org.apache.log4j.Layout;
23 import org.apache.log4j.helpers.OptionConverter;
24 import org.apache.log4j.spi.OptionHandler;
25
26 import java.util.Vector JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import javax.management.MBeanAttributeInfo JavaDoc;
30 import javax.management.MBeanConstructorInfo JavaDoc;
31 import javax.management.MBeanNotificationInfo JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.Attribute 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 LayoutDynamicMBean 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[1];
56   private String JavaDoc dDescription =
57      "This MBean acts as a management facade for log4j layouts.";
58
59   // This category instance is for logging.
60
private static Logger cat = Logger.getLogger(LayoutDynamicMBean.class);
61
62   // We wrap this layout instance.
63
private Layout layout;
64
65   public LayoutDynamicMBean(Layout layout) throws IntrospectionException JavaDoc {
66     this.layout = layout;
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              "LayoutDynamicMBean(): Constructs a LayoutDynamicMBean instance",
75          constructors[0]);
76
77
78     BeanInfo JavaDoc bi = Introspector.getBeanInfo(layout.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(Level.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 layout",
112                         params,
113                         "void",
114                         MBeanOperationInfo.ACTION);
115   }
116
117   private
118   boolean isSupportedType(Class JavaDoc clazz) {
119     if(clazz.isPrimitive()) {
120       return true;
121     }
122
123     if(clazz == String JavaDoc.class) {
124       return true;
125     }
126     if(clazz.isAssignableFrom(Level.class)) {
127       return true;
128     }
129
130     return false;
131   }
132
133
134
135   public
136   MBeanInfo JavaDoc getMBeanInfo() {
137     cat.debug("getMBeanInfo called.");
138
139     MBeanAttributeInfo JavaDoc[] attribs = new MBeanAttributeInfo JavaDoc[dAttributes.size()];
140     dAttributes.toArray(attribs);
141
142     return new MBeanInfo JavaDoc(dClassName,
143              dDescription,
144              attribs,
145              dConstructors,
146              dOperations,
147              new MBeanNotificationInfo JavaDoc[0]);
148   }
149
150   public
151   Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc params[], String JavaDoc signature[])
152     throws MBeanException JavaDoc,
153     ReflectionException JavaDoc {
154
155     if(operationName.equals("activateOptions") &&
156                      layout instanceof OptionHandler) {
157       OptionHandler oh = (OptionHandler) layout;
158       oh.activateOptions();
159       return "Options activated.";
160     }
161     return null;
162   }
163
164   protected
165   Logger getLogger() {
166     return cat;
167   }
168
169
170   public
171   Object JavaDoc getAttribute(String JavaDoc attributeName) throws AttributeNotFoundException JavaDoc,
172                                                    MBeanException JavaDoc,
173                                                    ReflectionException JavaDoc {
174
175        // Check attributeName is not null to avoid NullPointerException later on
176
if (attributeName == null) {
177       throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
178             "Attribute name cannot be null"),
179        "Cannot invoke a getter of " + dClassName + " with null attribute name");
180     }
181
182
183     MethodUnion mu = (MethodUnion) dynamicProps.get(attributeName);
184
185     cat.debug("----name="+attributeName+", mu="+mu);
186
187     if(mu != null && mu.readMethod != null) {
188       try {
189     return mu.readMethod.invoke(layout, null);
190       } catch(Exception JavaDoc e) {
191     return null;
192       }
193     }
194
195
196
197     // If attributeName has not been recognized throw an AttributeNotFoundException
198
throw(new AttributeNotFoundException JavaDoc("Cannot find " + attributeName +
199                      " attribute in " + dClassName));
200
201   }
202
203
204   public
205   void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc,
206                                                 InvalidAttributeValueException JavaDoc,
207                                                 MBeanException JavaDoc,
208                                                 ReflectionException JavaDoc {
209
210     // Check attribute is not null to avoid NullPointerException later on
211
if (attribute == null) {
212       throw new RuntimeOperationsException JavaDoc(
213                   new IllegalArgumentException JavaDoc("Attribute cannot be null"),
214           "Cannot invoke a setter of " + dClassName +
215           " with null attribute");
216     }
217     String JavaDoc name = attribute.getName();
218     Object JavaDoc value = attribute.getValue();
219
220     if (name == null) {
221       throw new RuntimeOperationsException JavaDoc(
222                     new IllegalArgumentException JavaDoc("Attribute name cannot be null"),
223             "Cannot invoke the setter of "+dClassName+
224             " with null attribute name");
225     }
226
227
228
229     MethodUnion mu = (MethodUnion) dynamicProps.get(name);
230
231     if(mu != null && mu.writeMethod != null) {
232       Object JavaDoc[] o = new Object JavaDoc[1];
233
234       Class JavaDoc[] params = mu.writeMethod.getParameterTypes();
235       if(params[0] == org.apache.log4j.Priority.class) {
236     value = OptionConverter.toLevel((String JavaDoc) value,
237                     (Level) getAttribute(name));
238       }
239       o[0] = value;
240
241       try {
242     mu.writeMethod.invoke(layout, o);
243
244       } catch(Exception JavaDoc e) {
245     cat.error("FIXME", e);
246       }
247     } else {
248       throw(new AttributeNotFoundException JavaDoc("Attribute " + name +
249                        " not found in " +
250                        this.getClass().getName()));
251     }
252   }
253 }
254
255
256
Popular Tags