01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.framework;
20:
21: import java.lang.reflect.Constructor;
22: import java.lang.reflect.InvocationTargetException;
23: import org.mactor.brokers.MessageBroker;
24: import org.mactor.framework.spec.MessageBrokersConfig.MessageBrokerConfig;
25:
26: public class ReflectionUtil {
27: public static Object createInstance(String className)
28: throws MactorException {
29: try {
30: Class c = Class.forName(className);
31: return c.newInstance();
32: } catch (ClassNotFoundException cnf) {
33: throw new MactorException("The class '" + className
34: + "' was not found in the classpath", cnf);
35: } catch (InstantiationException ie) {
36: throw new MactorException(
37: "Unable to create objects from '"
38: + className
39: + "'. Check that the class has a default contructor",
40: ie);
41: } catch (IllegalAccessException iae) {
42: throw new MactorException("Unable to create objects from '"
43: + className + "'. Illegal access", iae);
44: }
45: }
46:
47: public static MessageBroker createMessageBroker(
48: MessageBrokerConfig config) throws MactorException {
49: String className = config.getBrokerClass();
50: try {
51: Class c = Class.forName(className);
52: Constructor ct = c
53: .getConstructor(new Class[] { MessageBrokerConfig.class });
54: Object mb = ct.newInstance(new Object[] { config });
55: if (!(mb instanceof MessageBroker))
56: throw new MactorException(
57: "The specified message broker class does not implement '"
58: + MessageBroker.class.getName() + "'");
59: return (MessageBroker) mb;
60: } catch (ClassNotFoundException cnf) {
61: throw new MactorException(
62: "The specified MessageBroker implementation was not found in the class path. Class '"
63: + className + "'", cnf);
64: } catch (NoSuchMethodException nm) {
65: throw new MactorException(
66: "The specified MessageBroker implementation class '"
67: + className
68: + "' does not have the requiered contructor (that takes a single arguemtn of type '"
69: + MessageBrokerConfig.class.getName()
70: + "')", nm);
71: } catch (IllegalAccessException iae) {
72: throw new MactorException(
73: "Unable to instantiate the MessageBroker '"
74: + className + "'. Illegal access", iae);
75: } catch (InvocationTargetException it) {
76: throw new MactorException(
77: "Unable to instantiate the MessageBroker '"
78: + className
79: + "'. InvocationTargetException", it);
80: } catch (InstantiationException ie) {
81: throw new MactorException(
82: "Unable to instantiate the MessageBroker '"
83: + className + "'. InstantiationException",
84: ie);
85: }
86: }
87: }
|