01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.mx.modelmbean;
23:
24: import java.lang.reflect.Constructor;
25:
26: import javax.management.modelmbean.ModelMBean;
27: import javax.management.modelmbean.ModelMBeanInfo;
28:
29: import org.jboss.mx.server.ServerConstants;
30: import org.jboss.mx.util.PropertyAccess;
31:
32: /**
33: * ModelMBean instantiator. The ModelMBean implementation
34: * can be configured by setting a <tt>jbossmx.required.modelmbean.class</tt>
35: * system property.
36: *
37: * @see javax.management.modelmbean.ModelMBean
38: *
39: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
40: * @author <a href="mailto:Adrian@jboss.org">Adrian Brock</a>.
41: * @version $Revision: 57200 $
42: */
43: public class RequiredModelMBeanInstantiator {
44: public static ModelMBean instantiate() {
45: ClassLoader cl = Thread.currentThread().getContextClassLoader();
46: String className = getClassName();
47: try {
48: Class modelMBean = cl.loadClass(className);
49: return (ModelMBean) modelMBean.newInstance();
50: } catch (ClassNotFoundException e) {
51: throw new Error(
52: "Cannot instantiate model mbean class. Class "
53: + className + " not found.");
54: } catch (ClassCastException e) {
55: throw new Error(
56: "Cannot instantiate model mbean class. The target class is not an instance of ModelMBean interface.");
57: } catch (Exception e) {
58: throw new Error("Cannot instantiate model mbean class "
59: + className + " with default constructor: "
60: + e.getMessage());
61: }
62: }
63:
64: public static ModelMBean instantiate(ModelMBeanInfo info) {
65: ClassLoader cl = Thread.currentThread().getContextClassLoader();
66: String className = getClassName();
67: try {
68: Class modelMBean = cl.loadClass(className);
69: Constructor constructor = modelMBean
70: .getConstructor(new Class[] { ModelMBeanInfo.class });
71: return (ModelMBean) constructor
72: .newInstance(new Object[] { info });
73: } catch (ClassNotFoundException e) {
74: throw new Error(
75: "Cannot instantiate model mbean class. Class "
76: + className + " not found.");
77: } catch (ClassCastException e) {
78: throw new Error(
79: "Cannot instantiate model mbean class. The target class is not an instance of ModelMBean interface.");
80: } catch (Exception e) {
81: throw new Error("Cannot instantiate model mbean class "
82: + className + ": " + e.toString());
83: }
84: }
85:
86: public static String getClassName() {
87: return PropertyAccess.getProperty(
88: ServerConstants.REQUIRED_MODELMBEAN_CLASS_PROPERTY,
89: ServerConstants.DEFAULT_REQUIRED_MODELMBEAN_CLASS);
90: }
91: }
|