001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.utils;
019
020import io.stallion.plugins.StallionJavaPlugin;
021import io.stallion.plugins.PluginRegistry;
022
023public class StallionClassLoader {
024
025    /**
026    * Loads a class based on the name, looking in the Stallion jar classpath, and then
027    * also looking up via the class loader for all plugins
028    */
029    public static Class loadClass(String className) {
030        Class cls = null;
031        try {
032            cls = StallionClassLoader.class.getClassLoader().loadClass(className);
033            return cls;
034        } catch (ClassNotFoundException e) {
035
036        }
037        for (StallionJavaPlugin booter: PluginRegistry.instance().getJavaPluginByName().values()) {
038            try {
039                cls = StallionClassLoader.class.getClassLoader().loadClass(className);
040                return cls;
041            } catch (ClassNotFoundException e) {
042
043            }
044        }
045        return null;
046    }
047
048    /**
049     * Load a class using the class loader of the given plugin
050     *
051     * @param pluginName
052     * @param className
053     * @return
054     */
055    public static Class loadClass(String pluginName, String className) {
056        try {
057            Class cls = PluginRegistry.instance().getJavaPluginByName().get(pluginName).getClass().getClassLoader().loadClass(className);
058            return cls;
059        } catch (ClassNotFoundException e) {
060            throw new RuntimeException(e);
061        }
062    }
063}