PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 187 lines | 115 code | 27 blank | 45 comment | 14 complexity | 23bd0cb0ddfb15498bbf5bccce381217 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.util.Enumeration;
  19. import java.util.Iterator;
  20. import java.util.Vector;
  21. import javax.management.Attribute;
  22. import javax.management.AttributeList;
  23. import javax.management.DynamicMBean;
  24. import javax.management.InstanceAlreadyExistsException;
  25. import javax.management.InstanceNotFoundException;
  26. import javax.management.JMException;
  27. import javax.management.MBeanRegistration;
  28. import javax.management.MBeanRegistrationException;
  29. import javax.management.MBeanServer;
  30. import javax.management.NotCompliantMBeanException;
  31. import javax.management.ObjectName;
  32. import javax.management.RuntimeOperationsException;
  33. import org.apache.log4j.Logger;
  34. import org.apache.log4j.Appender;
  35. public abstract class AbstractDynamicMBean implements DynamicMBean,
  36. MBeanRegistration {
  37. String dClassName;
  38. MBeanServer server;
  39. private final Vector mbeanList = new Vector();
  40. /**
  41. * Get MBean name.
  42. * @param appender appender, may not be null.
  43. * @return name.
  44. * @since 1.2.16
  45. */
  46. static protected String getAppenderName(final Appender appender){
  47. String name = appender.getName();
  48. if (name == null || name.trim().length() == 0) {
  49. // try to get some form of a name, because null is not allowed (exception), and empty string certainly isn't useful in JMX..
  50. name = appender.toString();
  51. }
  52. return name;
  53. }
  54. /**
  55. * Enables the to get the values of several attributes of the Dynamic MBean.
  56. */
  57. public
  58. AttributeList getAttributes(String[] attributeNames) {
  59. // Check attributeNames is not null to avoid NullPointerException later on
  60. if (attributeNames == null) {
  61. throw new RuntimeOperationsException(
  62. new IllegalArgumentException("attributeNames[] cannot be null"),
  63. "Cannot invoke a getter of " + dClassName);
  64. }
  65. AttributeList resultList = new AttributeList();
  66. // if attributeNames is empty, return an empty result list
  67. if (attributeNames.length == 0)
  68. return resultList;
  69. // build the result attribute list
  70. for (int i=0 ; i<attributeNames.length ; i++){
  71. try {
  72. Object value = getAttribute((String) attributeNames[i]);
  73. resultList.add(new Attribute(attributeNames[i],value));
  74. } catch (JMException e) {
  75. e.printStackTrace();
  76. } catch (RuntimeException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. return(resultList);
  81. }
  82. /**
  83. * Sets the values of several attributes of the Dynamic MBean, and returns the
  84. * list of attributes that have been set.
  85. */
  86. public AttributeList setAttributes(AttributeList attributes) {
  87. // Check attributes is not null to avoid NullPointerException later on
  88. if (attributes == null) {
  89. throw new RuntimeOperationsException(
  90. new IllegalArgumentException("AttributeList attributes cannot be null"),
  91. "Cannot invoke a setter of " + dClassName);
  92. }
  93. AttributeList resultList = new AttributeList();
  94. // if attributeNames is empty, nothing more to do
  95. if (attributes.isEmpty())
  96. return resultList;
  97. // for each attribute, try to set it and add to the result list if successfull
  98. for (Iterator i = attributes.iterator(); i.hasNext();) {
  99. Attribute attr = (Attribute) i.next();
  100. try {
  101. setAttribute(attr);
  102. String name = attr.getName();
  103. Object value = getAttribute(name);
  104. resultList.add(new Attribute(name,value));
  105. } catch(JMException e) {
  106. e.printStackTrace();
  107. } catch(RuntimeException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. return(resultList);
  112. }
  113. protected
  114. abstract
  115. Logger getLogger();
  116. public
  117. void postDeregister() {
  118. getLogger().debug("postDeregister is called.");
  119. }
  120. public
  121. void postRegister(java.lang.Boolean registrationDone) {
  122. }
  123. public
  124. ObjectName preRegister(MBeanServer server, ObjectName name) {
  125. getLogger().debug("preRegister called. Server="+server+ ", name="+name);
  126. this.server = server;
  127. return name;
  128. }
  129. /**
  130. * Registers MBean instance in the attached server. Must <em>NOT</em>
  131. * be called before registration of this instance.
  132. */
  133. protected
  134. void registerMBean(Object mbean, ObjectName objectName)
  135. throws InstanceAlreadyExistsException, MBeanRegistrationException,
  136. NotCompliantMBeanException {
  137. server.registerMBean(mbean, objectName);
  138. mbeanList.add(objectName);
  139. }
  140. /**
  141. * Performs cleanup for deregistering this MBean. Default implementation
  142. * unregisters MBean instances which are registered using
  143. * {@link #registerMBean(Object mbean, ObjectName objectName)}.
  144. */
  145. public
  146. void preDeregister() {
  147. getLogger().debug("preDeregister called.");
  148. Enumeration iterator = mbeanList.elements();
  149. while (iterator.hasMoreElements()) {
  150. ObjectName name = (ObjectName) iterator.nextElement();
  151. try {
  152. server.unregisterMBean(name);
  153. } catch (InstanceNotFoundException e) {
  154. getLogger().warn("Missing MBean " + name.getCanonicalName());
  155. } catch (MBeanRegistrationException e) {
  156. getLogger().warn("Failed unregistering " + name.getCanonicalName());
  157. }
  158. }
  159. }
  160. }