KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jmx;
30
31 import com.caucho.config.BuilderProgram;
32 import com.caucho.config.ConfigException;
33 import com.caucho.util.L10N;
34
35 import javax.management.Attribute JavaDoc;
36 import javax.management.MBeanAttributeInfo JavaDoc;
37 import javax.management.MBeanInfo JavaDoc;
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.NotificationFilter JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import java.lang.reflect.Constructor JavaDoc;
42 import java.util.ArrayList JavaDoc;
43
44 /**
45  * Configuration for the mbean pattern.
46  */

47 public class MBeanConfig {
48   private static L10N L = new L10N(MBeanConfig.class);
49
50   private Class JavaDoc _type;
51   
52   private String JavaDoc _mbeanName;
53
54   private Class JavaDoc _interface;
55   
56   private String JavaDoc _jndiName;
57
58   private ObjectName JavaDoc _name;
59   private MBeanInfo JavaDoc _mbeanInfo;
60
61   private ArrayList JavaDoc<BuilderProgram> _args = new ArrayList JavaDoc<BuilderProgram>();
62
63   private boolean _isInit;
64
65   /**
66    * Sets the JNDI name
67    */

68   public void setJndiName(String JavaDoc name)
69   {
70     _jndiName = name;
71   }
72
73   /**
74    * Gets the JNDI name
75    */

76   public String JavaDoc getJndiName()
77   {
78     return _jndiName;
79   }
80
81   /**
82    * Sets the mbean name
83    */

84   public void setName(String JavaDoc name)
85   {
86     _mbeanName = name;
87   }
88
89   /**
90    * Gets the mbean name
91    */

92   public String JavaDoc getName()
93   {
94     return _mbeanName;
95   }
96
97   /**
98    * Sets the class
99    */

100   public void setType(Class JavaDoc mbeanClass)
101   {
102     _type = mbeanClass;
103   }
104
105   /**
106    * Gets the type;
107    */

108   public Class JavaDoc getMBeanClass()
109   {
110     return _type;
111   }
112
113   /**
114    * Sets the class
115    */

116   public void setInterface(Class JavaDoc cl)
117   {
118     _interface = cl;
119   }
120
121   /**
122    * Adds an argument.
123    */

124   public void addArg(BuilderProgram builder)
125   {
126     _args.add(builder);
127   }
128
129   /**
130    * Initialize the resource.
131    */

132   public void init()
133     throws Throwable JavaDoc
134   {
135     if (_isInit)
136       return;
137
138     _isInit = true;
139
140     if (_mbeanName == null)
141       throw new ConfigException(L.l("<mbean> configuration needs a 'name' attribute. The 'name' is the MBean ObjectName for the bean."));
142     
143     MBeanServer JavaDoc server = Jmx.getMBeanServer();
144
145     _name = Jmx.getObjectName(_mbeanName);
146
147     if (_type != null) {
148     }
149     else if (server.getMBeanInfo(_name) != null) {
150       return;
151     }
152     else {
153       throw new ConfigException(L.l("<mbean> configuration needs a 'type' attribute. The 'class' is the class name of the resource bean."));
154     }
155
156     Constructor JavaDoc constructor = getConstructor(_args.size());
157
158     Class JavaDoc []params = constructor.getParameterTypes();
159       
160     Object JavaDoc []args = new Object JavaDoc[_args.size()];
161
162     for (int i = 0; i < args.length; i++)
163       args[0] = _args.get(i).configure(params[i]);
164
165     Object JavaDoc obj = constructor.newInstance(args);
166     
167     if (_interface != null) {
168       Object JavaDoc mbean = new IntrospectionMBean(obj, _interface);
169       server.registerMBean(mbean, _name);
170     }
171     else {
172       server.registerMBean(obj, _name);
173     }
174
175     _mbeanInfo = server.getMBeanInfo(_name);
176   }
177
178   ObjectName JavaDoc getObjectName()
179     throws Throwable JavaDoc
180   {
181     if (_name == null)
182       init();
183
184     return _name;
185   }
186
187   private Constructor JavaDoc getConstructor(int len)
188     throws Throwable JavaDoc
189   {
190     Constructor JavaDoc []constructors = _type.getConstructors();
191
192     for (int i = 0; i < constructors.length; i++) {
193       if (constructors[i].getParameterTypes().length == len)
194     return constructors[i];
195     }
196
197     throw new ConfigException(L.l("`{0}' has no matching constructors.",
198                   _type.getName()));
199   }
200
201   MBeanInfo JavaDoc getMBeanInfo()
202     throws Throwable JavaDoc
203   {
204     if (_mbeanInfo == null)
205       init();
206
207     return _mbeanInfo;
208   }
209
210   public Init createInit()
211   {
212     return new Init();
213   }
214
215   public Listener createListener()
216   {
217     return new Listener();
218   }
219
220   public String JavaDoc toString()
221   {
222     return "MBean[" + _mbeanName + "]";
223   }
224
225   public class Init {
226     public void setProperty(String JavaDoc attrName, BuilderProgram program)
227       throws Throwable JavaDoc
228     {
229       MBeanAttributeInfo JavaDoc attr = getAttribute(attrName);
230       if (attr == null)
231     throw new ConfigException(L.l("`{0}' is an unknown attribute for {1}",
232                       attrName, _mbeanName));
233
234       String JavaDoc typeName = attr.getType();
235       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
236       Class JavaDoc type = Class.forName(typeName, false, loader);
237
238       Object JavaDoc value = program.configure(type);
239       
240       MBeanServer JavaDoc server = Jmx.getMBeanServer();
241       
242       server.setAttribute(_name, new Attribute JavaDoc(attrName, value));
243     }
244
245     private MBeanAttributeInfo JavaDoc getAttribute(String JavaDoc key)
246       throws Throwable JavaDoc
247     {
248       MBeanInfo JavaDoc info = getMBeanInfo();
249
250       MBeanAttributeInfo JavaDoc []attrs = info.getAttributes();
251
252       if (attrs == null)
253     return null;
254
255       for (int i = 0; i < attrs.length; i++) {
256     if (attrs[i].getName().equals(key))
257       return attrs[i];
258       }
259
260       return null;
261     }
262   }
263
264   public class Listener {
265     private String JavaDoc _name;
266     private Object JavaDoc _handback;
267     private NotificationFilter JavaDoc _filter;
268
269     public void setName(String JavaDoc name)
270     {
271       _name = name;
272     }
273
274     public String JavaDoc getName()
275     {
276       return _name;
277     }
278
279     public void setHandback(Object JavaDoc handback)
280     {
281       _handback = handback;
282     }
283
284     public Object JavaDoc getHandback()
285     {
286       return _handback;
287     }
288
289     public void init()
290       throws Throwable JavaDoc
291     {
292       ObjectName JavaDoc mbeanName = getObjectName();
293
294       ObjectName JavaDoc listenerName = Jmx.getObjectName(_name);
295
296       MBeanServer JavaDoc server = Jmx.getMBeanServer();
297
298       server.addNotificationListener(mbeanName, listenerName,
299                      _filter, _handback);
300       
301     }
302   }
303 }
304
Popular Tags