001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jmx;
030:
031: import com.caucho.config.program.ConfigProgram;
032: import com.caucho.config.ConfigException;
033: import com.caucho.util.L10N;
034:
035: import javax.management.Attribute;
036: import javax.management.MBeanAttributeInfo;
037: import javax.management.MBeanInfo;
038: import javax.management.MBeanServer;
039: import javax.management.NotificationFilter;
040: import javax.management.ObjectName;
041: import java.lang.reflect.Constructor;
042: import java.util.ArrayList;
043:
044: /**
045: * Configuration for the mbean pattern.
046: */
047: public class MBeanConfig {
048: private static L10N L = new L10N(MBeanConfig.class);
049:
050: private Class _type;
051:
052: private String _mbeanName;
053:
054: private Class _interface;
055:
056: private String _jndiName;
057:
058: private ObjectName _name;
059: private MBeanInfo _mbeanInfo;
060:
061: private ArrayList<ConfigProgram> _args = new ArrayList<ConfigProgram>();
062:
063: private boolean _isInit;
064:
065: /**
066: * Sets the JNDI name
067: */
068: public void setJndiName(String name) {
069: _jndiName = name;
070: }
071:
072: /**
073: * Gets the JNDI name
074: */
075: public String getJndiName() {
076: return _jndiName;
077: }
078:
079: /**
080: * Sets the mbean name
081: */
082: public void setName(String name) {
083: _mbeanName = name;
084: }
085:
086: /**
087: * Gets the mbean name
088: */
089: public String getName() {
090: return _mbeanName;
091: }
092:
093: /**
094: * Sets the class
095: */
096: public void setType(Class mbeanClass) {
097: _type = mbeanClass;
098: }
099:
100: /**
101: * Gets the type;
102: */
103: public Class getMBeanClass() {
104: return _type;
105: }
106:
107: /**
108: * Sets the class
109: */
110: public void setInterface(Class cl) {
111: _interface = cl;
112: }
113:
114: /**
115: * Adds an argument.
116: */
117: public void addArg(ConfigProgram builder) {
118: _args.add(builder);
119: }
120:
121: /**
122: * Initialize the resource.
123: */
124: public void init() throws Throwable {
125: if (_isInit)
126: return;
127:
128: _isInit = true;
129:
130: if (_mbeanName == null)
131: throw new ConfigException(
132: L
133: .l("<mbean> configuration needs a 'name' attribute. The 'name' is the MBean ObjectName for the bean."));
134:
135: MBeanServer server = Jmx.getMBeanServer();
136:
137: _name = Jmx.getObjectName(_mbeanName);
138:
139: if (_type != null) {
140: } else if (server.getMBeanInfo(_name) != null) {
141: return;
142: } else {
143: throw new ConfigException(
144: L
145: .l("<mbean> configuration needs a 'type' attribute. The 'class' is the class name of the resource bean."));
146: }
147:
148: Constructor constructor = getConstructor(_args.size());
149:
150: Class[] params = constructor.getParameterTypes();
151:
152: Object[] args = new Object[_args.size()];
153:
154: for (int i = 0; i < args.length; i++)
155: args[0] = _args.get(i).configure(params[i]);
156:
157: Object obj = constructor.newInstance(args);
158:
159: if (_interface != null) {
160: Object mbean = new IntrospectionMBean(obj, _interface);
161: server.registerMBean(mbean, _name);
162: } else {
163: server.registerMBean(obj, _name);
164: }
165:
166: _mbeanInfo = server.getMBeanInfo(_name);
167: }
168:
169: ObjectName getObjectName() throws Throwable {
170: if (_name == null)
171: init();
172:
173: return _name;
174: }
175:
176: private Constructor getConstructor(int len) throws Throwable {
177: Constructor[] constructors = _type.getConstructors();
178:
179: for (int i = 0; i < constructors.length; i++) {
180: if (constructors[i].getParameterTypes().length == len)
181: return constructors[i];
182: }
183:
184: throw new ConfigException(L.l(
185: "`{0}' has no matching constructors.", _type.getName()));
186: }
187:
188: MBeanInfo getMBeanInfo() throws Throwable {
189: if (_mbeanInfo == null)
190: init();
191:
192: return _mbeanInfo;
193: }
194:
195: public Init createInit() {
196: return new Init();
197: }
198:
199: public Listener createListener() {
200: return new Listener();
201: }
202:
203: public String toString() {
204: return "MBean[" + _mbeanName + "]";
205: }
206:
207: public class Init {
208: public void setProperty(String attrName, ConfigProgram program)
209: throws Throwable {
210: MBeanAttributeInfo attr = getAttribute(attrName);
211: if (attr == null)
212: throw new ConfigException(L.l(
213: "`{0}' is an unknown attribute for {1}",
214: attrName, _mbeanName));
215:
216: String typeName = attr.getType();
217: ClassLoader loader = Thread.currentThread()
218: .getContextClassLoader();
219: Class type = Class.forName(typeName, false, loader);
220:
221: Object value = program.configure(type);
222:
223: MBeanServer server = Jmx.getMBeanServer();
224:
225: server.setAttribute(_name, new Attribute(attrName, value));
226: }
227:
228: private MBeanAttributeInfo getAttribute(String key)
229: throws Throwable {
230: MBeanInfo info = getMBeanInfo();
231:
232: MBeanAttributeInfo[] attrs = info.getAttributes();
233:
234: if (attrs == null)
235: return null;
236:
237: for (int i = 0; i < attrs.length; i++) {
238: if (attrs[i].getName().equals(key))
239: return attrs[i];
240: }
241:
242: return null;
243: }
244: }
245:
246: public class Listener {
247: private String _name;
248: private Object _handback;
249: private NotificationFilter _filter;
250:
251: public void setName(String name) {
252: _name = name;
253: }
254:
255: public String getName() {
256: return _name;
257: }
258:
259: public void setHandback(Object handback) {
260: _handback = handback;
261: }
262:
263: public Object getHandback() {
264: return _handback;
265: }
266:
267: public void init() throws Throwable {
268: ObjectName mbeanName = getObjectName();
269:
270: ObjectName listenerName = Jmx.getObjectName(_name);
271:
272: MBeanServer server = Jmx.getMBeanServer();
273:
274: server.addNotificationListener(mbeanName, listenerName,
275: _filter, _handback);
276:
277: }
278: }
279: }
|