001 /******************************************************************************
002 * Copyright (C) MActor Developers. All rights reserved. *
003 * ---------------------------------------------------------------------------*
004 * This file is part of MActor. *
005 * *
006 * MActor is free software; you can redistribute it and/or modify *
007 * it under the terms of the GNU General Public License as published by *
008 * the Free Software Foundation; either version 2 of the License, or *
009 * (at your option) any later version. *
010 * *
011 * MActor is distributed in the hope that it will be useful, *
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014 * GNU General Public License for more details. *
015 * *
016 * You should have received a copy of the GNU General Public License *
017 * along with MActor; if not, write to the Free Software *
018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019 ******************************************************************************/
020 package org.mactor.framework;
021
022 import java.lang.reflect.Constructor;
023 import java.lang.reflect.InvocationTargetException;
024
025 import org.mactor.brokers.MessageBroker;
026 import org.mactor.framework.spec.MessageBrokersConfig.MessageBrokerConfig;
027
028 public class ReflectionUtil {
029 public static Object createInstance(String className) throws MactorException {
030 try {
031 Class c = Class.forName(className);
032 return c.newInstance();
033 } catch (ClassNotFoundException cnf) {
034 throw new MactorException("The class '" + className + "' was not found in the classpath", cnf);
035 } catch (InstantiationException ie) {
036 throw new MactorException("Unable to create objects from '" + className + "'. Check that the class has a default contructor", ie);
037 } catch (IllegalAccessException iae) {
038 throw new MactorException("Unable to create objects from '" + className + "'. Illegal access", iae);
039 }
040 }
041 public static MessageBroker createMessageBroker(MessageBrokerConfig config) throws MactorException {
042 String className = config.getBrokerClass();
043 try {
044 Class c = Class.forName(className);
045 Constructor ct = c.getConstructor(new Class[] { MessageBrokerConfig.class });
046 Object mb = ct.newInstance(new Object[] { config });
047 if (!(mb instanceof MessageBroker))
048 throw new MactorException("The specified message broker class does not implement '" + MessageBroker.class.getName() + "'");
049 return (MessageBroker) mb;
050 } catch (ClassNotFoundException cnf) {
051 throw new MactorException("The specified MessageBroker implementation was not found in the class path. Class '" + className + "'", cnf);
052 } catch (NoSuchMethodException nm) {
053 throw new MactorException("The specified MessageBroker implementation class '" + className + "' does not have the requiered contructor (that takes a single arguemtn of type '"
054 + MessageBrokerConfig.class.getName() + "')", nm);
055 } catch (IllegalAccessException iae) {
056 throw new MactorException("Unable to instantiate the MessageBroker '" + className + "'. Illegal access", iae);
057 } catch (InvocationTargetException it) {
058 throw new MactorException("Unable to instantiate the MessageBroker '" + className + "'. InvocationTargetException", it);
059 } catch (InstantiationException ie) {
060 throw new MactorException("Unable to instantiate the MessageBroker '" + className + "'. InstantiationException", ie);
061 }
062 }
063 }