KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > modelmbean > RequiredModelMBeanInstantiator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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 JavaDoc;
25
26 import javax.management.modelmbean.ModelMBean JavaDoc;
27 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
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: 37459 $
42  */

43 public class RequiredModelMBeanInstantiator
44 {
45    public static ModelMBean JavaDoc instantiate()
46    {
47       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
48       String JavaDoc className = getClassName();
49       try
50       {
51          Class JavaDoc modelMBean = cl.loadClass(className);
52          return (ModelMBean JavaDoc) modelMBean.newInstance();
53       }
54       catch (ClassNotFoundException JavaDoc e)
55       {
56          throw new Error JavaDoc("Cannot instantiate model mbean class. Class " + className + " not found.");
57       }
58       catch (ClassCastException JavaDoc e)
59       {
60          throw new Error JavaDoc("Cannot instantiate model mbean class. The target class is not an instance of ModelMBean interface.");
61       }
62       catch (Exception JavaDoc e)
63       {
64          throw new Error JavaDoc("Cannot instantiate model mbean class " + className + " with default constructor: " + e.getMessage());
65       }
66    }
67
68    public static ModelMBean JavaDoc instantiate(ModelMBeanInfo JavaDoc info)
69    {
70       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
71       String JavaDoc className = getClassName();
72       try
73       {
74          Class JavaDoc modelMBean = cl.loadClass(className);
75          Constructor JavaDoc constructor = modelMBean.getConstructor(new Class JavaDoc[] { ModelMBeanInfo JavaDoc.class });
76          return (ModelMBean JavaDoc) constructor.newInstance(new Object JavaDoc[] { info });
77       }
78       catch (ClassNotFoundException JavaDoc e)
79       {
80          throw new Error JavaDoc("Cannot instantiate model mbean class. Class " + className + " not found.");
81       }
82       catch (ClassCastException JavaDoc e)
83       {
84          throw new Error JavaDoc("Cannot instantiate model mbean class. The target class is not an instance of ModelMBean interface.");
85       }
86       catch (Exception JavaDoc e)
87       {
88          throw new Error JavaDoc("Cannot instantiate model mbean class " + className + ": " + e.toString());
89       }
90    }
91
92    public static String JavaDoc getClassName()
93    {
94       return PropertyAccess.getProperty
95       (
96             ServerConstants.REQUIRED_MODELMBEAN_CLASS_PROPERTY,
97             ServerConstants.DEFAULT_REQUIRED_MODELMBEAN_CLASS
98       );
99    }
100 }
101
Popular Tags