1 8 9 package mx4j.log; 10 11 import javax.management.MBeanServer ; 12 import javax.management.ObjectName ; 13 import javax.management.MBeanException ; 14 import javax.management.MBeanInfo ; 15 import javax.management.MBeanOperationInfo ; 16 import javax.management.MBeanParameterInfo ; 17 import javax.management.ServiceNotFoundException ; 18 19 30 public class MBeanLogger extends Logger 31 { 32 private MBeanServer m_server; 33 private ObjectName m_name; 34 35 public MBeanLogger(MBeanServer server, ObjectName objectName) throws MBeanException 36 { 37 if (server == null) {throw new MBeanException (new IllegalArgumentException ("MBeanServer cannot be null"));} 38 if (objectName == null) {throw new MBeanException (new IllegalArgumentException ("ObjectName cannot be null"));} 39 40 m_server = server; 41 m_name = objectName; 42 43 boolean found = false; 44 try 45 { 46 MBeanInfo info = m_server.getMBeanInfo(m_name); 47 MBeanOperationInfo [] opers = info.getOperations(); 48 if (opers != null) 49 { 50 for (int i = 0; i < opers.length; ++i) 51 { 52 MBeanOperationInfo oper = opers[i]; 53 if (oper.getName().equals("log")) 54 { 55 MBeanParameterInfo [] params = oper.getSignature(); 56 if (params.length == 3) 57 { 58 if (params[0].getType().equals("int") && 59 params[1].getType().equals("java.lang.Object") && 60 params[2].getType().equals("java.lang.Throwable")) 61 { 62 found = true; 63 break; 64 } 65 } 66 } 67 } 68 } 69 } 70 catch (Exception x) 71 { 72 x.printStackTrace(); 73 } 74 if (!found) {throw new MBeanException (new ServiceNotFoundException ("MBean does not have an operation log(int,Object,Throwable)"));} 75 } 76 77 protected void log(int priority, Object message, Throwable t) 78 { 79 try 80 { 81 m_server.invoke(m_name, "log", new Object [] {new Integer (priority), message, t}, new String [] {"int", "java.lang.Object", "java.lang.Throwable"}); 82 } 83 catch (Exception x) 84 { 85 x.printStackTrace(); 86 } 87 } 88 } 89 | Popular Tags |