KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > log > MBeanLogger


1 /*
2  * Copyright (C) MX4J.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.log;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13 import javax.management.MBeanException JavaDoc;
14 import javax.management.MBeanInfo JavaDoc;
15 import javax.management.MBeanOperationInfo JavaDoc;
16 import javax.management.MBeanParameterInfo JavaDoc;
17 import javax.management.ServiceNotFoundException JavaDoc;
18
19 /**
20  * This logger forwards log requests to an MBean, that must have an operation with signature
21  * <pre>
22  * public void log(int priority, Object message, Throwable exception);
23  * </pre>
24  * It's used by the ModelMBean implementation. <br>
25  * Since the constructor takes parameters, cannot be used as prototype for logging redirection.
26  *
27  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
28  * @version $Revision: 1.5 $
29  */

30 public class MBeanLogger extends Logger
31 {
32     private MBeanServer JavaDoc m_server;
33     private ObjectName JavaDoc m_name;
34
35     public MBeanLogger(MBeanServer JavaDoc server, ObjectName JavaDoc objectName) throws MBeanException JavaDoc
36     {
37         if (server == null) {throw new MBeanException JavaDoc(new IllegalArgumentException JavaDoc("MBeanServer cannot be null"));}
38         if (objectName == null) {throw new MBeanException JavaDoc(new IllegalArgumentException JavaDoc("ObjectName cannot be null"));}
39
40         m_server = server;
41         m_name = objectName;
42
43         boolean found = false;
44         try
45         {
46             MBeanInfo JavaDoc info = m_server.getMBeanInfo(m_name);
47             MBeanOperationInfo JavaDoc[] opers = info.getOperations();
48             if (opers != null)
49             {
50                 for (int i = 0; i < opers.length; ++i)
51                 {
52                     MBeanOperationInfo JavaDoc oper = opers[i];
53                     if (oper.getName().equals("log"))
54                     {
55                         MBeanParameterInfo JavaDoc[] 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 JavaDoc x)
71         {
72             x.printStackTrace();
73         }
74         if (!found) {throw new MBeanException JavaDoc(new ServiceNotFoundException JavaDoc("MBean does not have an operation log(int,Object,Throwable)"));}
75     }
76
77     protected void log(int priority, Object JavaDoc message, Throwable JavaDoc t)
78     {
79         try
80         {
81             m_server.invoke(m_name, "log", new Object JavaDoc[] {new Integer JavaDoc(priority), message, t}, new String JavaDoc[] {"int", "java.lang.Object", "java.lang.Throwable"});
82         }
83         catch (Exception JavaDoc x)
84         {
85             x.printStackTrace();
86         }
87     }
88 }
89
Popular Tags