1 16 17 package org.apache.log4j.jmx; 18 19 import java.util.Iterator ; 21 import javax.management.DynamicMBean ; 22 import javax.management.AttributeList ; 23 import javax.management.Attribute ; 24 import javax.management.RuntimeOperationsException ; 25 import javax.management.MBeanRegistration ; 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectName ; 28 29 import org.apache.log4j.Logger; 30 31 public abstract class AbstractDynamicMBean implements DynamicMBean , 32 MBeanRegistration { 33 34 String dClassName; 35 MBeanServer server; 36 37 40 public 41 AttributeList getAttributes(String [] attributeNames) { 42 43 if (attributeNames == null) { 45 throw new RuntimeOperationsException ( 46 new IllegalArgumentException ("attributeNames[] cannot be null"), 47 "Cannot invoke a getter of " + dClassName); 48 } 49 50 AttributeList resultList = new AttributeList (); 51 52 if (attributeNames.length == 0) 54 return resultList; 55 56 for (int i=0 ; i<attributeNames.length ; i++){ 58 try { 59 Object value = getAttribute((String ) attributeNames[i]); 60 resultList.add(new Attribute (attributeNames[i],value)); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 } 64 } 65 return(resultList); 66 } 67 68 72 public AttributeList setAttributes(AttributeList attributes) { 73 74 if (attributes == null) { 76 throw new RuntimeOperationsException ( 77 new IllegalArgumentException ("AttributeList attributes cannot be null"), 78 "Cannot invoke a setter of " + dClassName); 79 } 80 AttributeList resultList = new AttributeList (); 81 82 if (attributes.isEmpty()) 84 return resultList; 85 86 for (Iterator i = attributes.iterator(); i.hasNext();) { 88 Attribute attr = (Attribute ) i.next(); 89 try { 90 setAttribute(attr); 91 String name = attr.getName(); 92 Object value = getAttribute(name); 93 resultList.add(new Attribute (name,value)); 94 } catch(Exception e) { 95 e.printStackTrace(); 96 } 97 } 98 return(resultList); 99 } 100 101 protected 102 abstract 103 Logger getLogger(); 104 105 public 106 void postDeregister() { 107 getLogger().debug("postDeregister is called."); 108 } 109 110 public 111 void postRegister(java.lang.Boolean registrationDone) { 112 } 113 114 115 116 public 117 void preDeregister() { 118 getLogger().debug("preDeregister called."); 119 } 120 121 public 122 ObjectName preRegister(MBeanServer server, ObjectName name) { 123 getLogger().debug("preRegister called. Server="+server+ ", name="+name); 124 this.server = server; 125 return name; 126 } 127 128 129 130 } 131 | Popular Tags |