KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > MBeanWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jmx;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import javax.management.*;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * Wrapper around the dynamic mbean to handle classloader lifecycle.
40  */

41 class MBeanWrapper implements DynamicMBean {
42   private static L10N L = new L10N(MBeanWrapper.class);
43   private static Logger JavaDoc log = Log.open(MBeanWrapper.class);
44   
45   private MBeanContext _context;
46
47   private ObjectName _name;
48   
49   protected Object JavaDoc _object;
50   protected DynamicMBean _mbean;
51
52   private ObjectInstance _instance;
53   
54   protected MBeanWrapper(MBeanContext context,
55              ObjectName name,
56              Object JavaDoc object,
57              DynamicMBean mbean)
58   {
59     _context = context;
60
61     _name = name;
62
63     _object = object;
64     _mbean = mbean;
65   }
66
67   /**
68    * Returns the object instance for the mbean.
69    */

70   public ObjectInstance getObjectInstance()
71   {
72     if (_instance == null)
73       _instance = new ObjectInstance(_name, getMBeanInfo().getClassName());
74     
75     return _instance;
76   }
77
78   /**
79    * Returns the context.
80    */

81   MBeanContext getContext()
82   {
83     return _context;
84   }
85
86   /**
87    * Returns the object.
88    */

89   Object JavaDoc getObject()
90   {
91     return _object;
92   }
93
94   /**
95    * Returns the object as a broadcaster.
96    */

97   private NotificationBroadcaster getBroadcaster()
98   {
99     return (NotificationBroadcaster) _object;
100   }
101
102   /**
103    * Returns the object as an emitter.
104    */

105   private NotificationEmitter getEmitter()
106   {
107     return (NotificationEmitter) _object;
108   }
109
110   /**
111    * Returns the name.
112    */

113   public ObjectName getObjectName()
114   {
115     return _name;
116   }
117
118   /**
119    * Returns the class loader.
120    */

121   public ClassLoader JavaDoc getClassLoader()
122   {
123     return _context.getClassLoader();
124   }
125
126   /**
127    * Returns the MBeanInfo meta-data.
128    */

129   public MBeanInfo getMBeanInfo()
130   {
131     return _mbean.getMBeanInfo();
132   }
133
134   /**
135    * Returns the named attribute.
136    */

137   public Object JavaDoc getAttribute(String JavaDoc name)
138     throws ReflectionException, AttributeNotFoundException, MBeanException
139   {
140     return _mbean.getAttribute(name);
141   }
142   
143   /**
144    * Returns an array of attributes.
145    */

146   public AttributeList getAttributes(String JavaDoc []names)
147   {
148     return _mbean.getAttributes(names);
149   }
150
151   /**
152    * Sets the named attribute.
153    */

154   public void setAttribute(Attribute attr)
155     throws ReflectionException,
156        AttributeNotFoundException,
157        InvalidAttributeValueException,
158        MBeanException
159   {
160     _mbean.setAttribute(attr);
161   }
162   
163   /**
164    * Returns an array of attributes.
165    */

166   public AttributeList setAttributes(AttributeList list)
167   {
168     return _mbean.setAttributes(list);
169   }
170
171   /**
172    * Invokes the operation.
173    */

174   public Object JavaDoc invoke(String JavaDoc operation,
175                Object JavaDoc []params,
176                String JavaDoc[]signature)
177     throws ReflectionException, MBeanException
178   {
179     return _mbean.invoke(operation, params, signature);
180   }
181
182   /**
183    * Returns as listener.
184    */

185   public NotificationListener getListener()
186   {
187     Object JavaDoc obj = getObject();
188     
189     if (obj instanceof NotificationListener)
190       return (NotificationListener) obj;
191     else
192       return null;
193   }
194
195   /**
196    * Adds a notification listener.
197    */

198   public void addNotificationListener(NotificationListener listener,
199                       NotificationFilter filter,
200                       Object JavaDoc handback)
201   {
202     getBroadcaster().addNotificationListener(listener, filter, handback);
203   }
204
205   /**
206    * Removes a notification listener.
207    */

208   public void removeNotificationListener(NotificationListener listener)
209     throws ListenerNotFoundException
210   {
211     getBroadcaster().removeNotificationListener(listener);
212   }
213
214   /**
215    * Removes a notification listener.
216    */

217   public void removeNotificationListener(NotificationListener listener,
218                      NotificationFilter filter,
219                      Object JavaDoc handback)
220     throws ListenerNotFoundException
221   {
222     Object JavaDoc obj = getObject();
223
224     if (obj instanceof NotificationEmitter)
225       getEmitter().removeNotificationListener(listener, filter, handback);
226     else
227       getBroadcaster().removeNotificationListener(listener);
228   }
229 }
230
Popular Tags