PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/src/main/java/org/apache/log4j/jmx/LayoutDynamicMBean.java

#
Java | 274 lines | 199 code | 54 blank | 21 comment | 33 complexity | 2183637d0ebb531c111927cff3d63664 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.log4j.jmx;
  18. import java.lang.reflect.Constructor;
  19. import org.apache.log4j.Logger;
  20. import org.apache.log4j.Level;
  21. import org.apache.log4j.Layout;
  22. import org.apache.log4j.helpers.OptionConverter;
  23. import org.apache.log4j.spi.OptionHandler;
  24. import java.util.Vector;
  25. import java.util.Hashtable;
  26. import java.lang.reflect.Method;
  27. import java.lang.reflect.InvocationTargetException;
  28. import javax.management.MBeanAttributeInfo;
  29. import javax.management.MBeanConstructorInfo;
  30. import javax.management.MBeanNotificationInfo;
  31. import javax.management.MBeanInfo;
  32. import javax.management.Attribute;
  33. import javax.management.MBeanException;
  34. import javax.management.AttributeNotFoundException;
  35. import javax.management.RuntimeOperationsException;
  36. import javax.management.ReflectionException;
  37. import javax.management.InvalidAttributeValueException;
  38. import javax.management.MBeanOperationInfo;
  39. import javax.management.MBeanParameterInfo;
  40. import java.beans.Introspector;
  41. import java.beans.BeanInfo;
  42. import java.beans.PropertyDescriptor;
  43. import java.beans.IntrospectionException;
  44. import java.io.InterruptedIOException;
  45. public class LayoutDynamicMBean extends AbstractDynamicMBean {
  46. private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
  47. private Vector dAttributes = new Vector();
  48. private String dClassName = this.getClass().getName();
  49. private Hashtable dynamicProps = new Hashtable(5);
  50. private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1];
  51. private String dDescription =
  52. "This MBean acts as a management facade for log4j layouts.";
  53. // This category instance is for logging.
  54. private static Logger cat = Logger.getLogger(LayoutDynamicMBean.class);
  55. // We wrap this layout instance.
  56. private Layout layout;
  57. public LayoutDynamicMBean(Layout layout) throws IntrospectionException {
  58. this.layout = layout;
  59. buildDynamicMBeanInfo();
  60. }
  61. private
  62. void buildDynamicMBeanInfo() throws IntrospectionException {
  63. Constructor[] constructors = this.getClass().getConstructors();
  64. dConstructors[0] = new MBeanConstructorInfo(
  65. "LayoutDynamicMBean(): Constructs a LayoutDynamicMBean instance",
  66. constructors[0]);
  67. BeanInfo bi = Introspector.getBeanInfo(layout.getClass());
  68. PropertyDescriptor[] pd = bi.getPropertyDescriptors();
  69. int size = pd.length;
  70. for(int i = 0; i < size; i++) {
  71. String name = pd[i].getName();
  72. Method readMethod = pd[i].getReadMethod();
  73. Method writeMethod = pd[i].getWriteMethod();
  74. if(readMethod != null) {
  75. Class returnClass = readMethod.getReturnType();
  76. if(isSupportedType(returnClass)) {
  77. String returnClassName;
  78. if(returnClass.isAssignableFrom(Level.class)) {
  79. returnClassName = "java.lang.String";
  80. } else {
  81. returnClassName = returnClass.getName();
  82. }
  83. dAttributes.add(new MBeanAttributeInfo(name,
  84. returnClassName,
  85. "Dynamic",
  86. true,
  87. writeMethod != null,
  88. false));
  89. dynamicProps.put(name, new MethodUnion(readMethod, writeMethod));
  90. }
  91. }
  92. }
  93. MBeanParameterInfo[] params = new MBeanParameterInfo[0];
  94. dOperations[0] = new MBeanOperationInfo("activateOptions",
  95. "activateOptions(): add an layout",
  96. params,
  97. "void",
  98. MBeanOperationInfo.ACTION);
  99. }
  100. private
  101. boolean isSupportedType(Class clazz) {
  102. if(clazz.isPrimitive()) {
  103. return true;
  104. }
  105. if(clazz == String.class) {
  106. return true;
  107. }
  108. if(clazz.isAssignableFrom(Level.class)) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. public
  114. MBeanInfo getMBeanInfo() {
  115. cat.debug("getMBeanInfo called.");
  116. MBeanAttributeInfo[] attribs = new MBeanAttributeInfo[dAttributes.size()];
  117. dAttributes.toArray(attribs);
  118. return new MBeanInfo(dClassName,
  119. dDescription,
  120. attribs,
  121. dConstructors,
  122. dOperations,
  123. new MBeanNotificationInfo[0]);
  124. }
  125. public
  126. Object invoke(String operationName, Object params[], String signature[])
  127. throws MBeanException,
  128. ReflectionException {
  129. if(operationName.equals("activateOptions") &&
  130. layout instanceof OptionHandler) {
  131. OptionHandler oh = (OptionHandler) layout;
  132. oh.activateOptions();
  133. return "Options activated.";
  134. }
  135. return null;
  136. }
  137. protected
  138. Logger getLogger() {
  139. return cat;
  140. }
  141. public
  142. Object getAttribute(String attributeName) throws AttributeNotFoundException,
  143. MBeanException,
  144. ReflectionException {
  145. // Check attributeName is not null to avoid NullPointerException later on
  146. if (attributeName == null) {
  147. throw new RuntimeOperationsException(new IllegalArgumentException(
  148. "Attribute name cannot be null"),
  149. "Cannot invoke a getter of " + dClassName + " with null attribute name");
  150. }
  151. MethodUnion mu = (MethodUnion) dynamicProps.get(attributeName);
  152. cat.debug("----name="+attributeName+", mu="+mu);
  153. if(mu != null && mu.readMethod != null) {
  154. try {
  155. return mu.readMethod.invoke(layout, null);
  156. } catch(InvocationTargetException e) {
  157. if (e.getTargetException() instanceof InterruptedException
  158. || e.getTargetException() instanceof InterruptedIOException) {
  159. Thread.currentThread().interrupt();
  160. }
  161. return null;
  162. } catch(IllegalAccessException e) {
  163. return null;
  164. } catch(RuntimeException e) {
  165. return null;
  166. }
  167. }
  168. // If attributeName has not been recognized throw an AttributeNotFoundException
  169. throw(new AttributeNotFoundException("Cannot find " + attributeName +
  170. " attribute in " + dClassName));
  171. }
  172. public
  173. void setAttribute(Attribute attribute) throws AttributeNotFoundException,
  174. InvalidAttributeValueException,
  175. MBeanException,
  176. ReflectionException {
  177. // Check attribute is not null to avoid NullPointerException later on
  178. if (attribute == null) {
  179. throw new RuntimeOperationsException(
  180. new IllegalArgumentException("Attribute cannot be null"),
  181. "Cannot invoke a setter of " + dClassName +
  182. " with null attribute");
  183. }
  184. String name = attribute.getName();
  185. Object value = attribute.getValue();
  186. if (name == null) {
  187. throw new RuntimeOperationsException(
  188. new IllegalArgumentException("Attribute name cannot be null"),
  189. "Cannot invoke the setter of "+dClassName+
  190. " with null attribute name");
  191. }
  192. MethodUnion mu = (MethodUnion) dynamicProps.get(name);
  193. if(mu != null && mu.writeMethod != null) {
  194. Object[] o = new Object[1];
  195. Class[] params = mu.writeMethod.getParameterTypes();
  196. if(params[0] == org.apache.log4j.Priority.class) {
  197. value = OptionConverter.toLevel((String) value,
  198. (Level) getAttribute(name));
  199. }
  200. o[0] = value;
  201. try {
  202. mu.writeMethod.invoke(layout, o);
  203. } catch(InvocationTargetException e) {
  204. if (e.getTargetException() instanceof InterruptedException
  205. || e.getTargetException() instanceof InterruptedIOException) {
  206. Thread.currentThread().interrupt();
  207. }
  208. cat.error("FIXME", e);
  209. } catch(IllegalAccessException e) {
  210. cat.error("FIXME", e);
  211. } catch(RuntimeException e) {
  212. cat.error("FIXME", e);
  213. }
  214. } else {
  215. throw(new AttributeNotFoundException("Attribute " + name +
  216. " not found in " +
  217. this.getClass().getName()));
  218. }
  219. }
  220. }