com.braffdev.server.core.utilities.HandlerClassListInflater.java Source code

Java tutorial

Introduction

Here is the source code for com.braffdev.server.core.utilities.HandlerClassListInflater.java

Source

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *  Copyright (C) 2015, Markus Staudt <info@braffdev.com>                  *
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package com.braffdev.server.core.utilities;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.braffdev.server.Server;
import com.braffdev.server.core.config.handler.ClassConfig;
import com.braffdev.server.core.config.handler.Handler;
import com.braffdev.server.core.io.logger.Logger;

/**
 *
 */
public class HandlerClassListInflater {

    private static final Logger LOGGER = Logger.getLogger(HandlerClassListInflater.class);

    /**
     * Instantiates the list of classConfigs. A class config contains a field named "class" that contains a full qualified class name.<br />
     * This class is instantiated. The class that is instantiated needs to be an instance of <code>{@link Handler}</code>.<br />
     * The handler's <code>initialize</code> method is invoked right after it gets instantiated.
     *
     * @param config the list of configs.
     * @param clazz the class that needs to be instantiated.
     * @return the list.
     */
    public static <T extends Handler> List<T> inflate(List<ClassConfig> config, Class<T> clazz) {
        List<T> instances = new ArrayList<>();

        for (ClassConfig classConfig : config) {
            T instance = initialize(classConfig, clazz);
            if (instance != null) {
                instances.add(instance);
            }
        }

        return instances;
    }

    /**
     * Instantiates and initializes the class the given class config contains.
     *
     * @param classConfig the config.
     * @param clazz the clazz.
     * @return
     */
    private static <T extends Handler> T initialize(ClassConfig classConfig, Class<T> clazz) {
        T instance = createInstance(classConfig, clazz);
        if (instance != null) {
            instance.initialize();
        }

        return instance;
    }

    /**
     * Instantiates the class the given class config contains.<br />
     * This method checks whether the class is assignable from the given target class.
     *
     * @param classConfig the config.
     * @param targetClazz the clazz.
     * @return
     */
    @SuppressWarnings("unchecked")
    private static <T extends Handler> T createInstance(ClassConfig classConfig, Class<T> targetClazz) {

        String clazz = classConfig.getClazz();
        if (StringUtils.isNotBlank(clazz)) {

            try {
                // get class
                Class<?> handlerClass = loadClass(clazz);

                // check if the class is a assignable from the given target class
                if (targetClazz.isAssignableFrom(handlerClass)) {
                    return (T) handlerClass.newInstance();

                } else {
                    LOGGER.error("The class '" + clazz + "' is unrelated to " + targetClazz + ". Skipping..");
                }

            } catch (Exception e) {
                LOGGER.error(e);
            }
        }

        return null;
    }

    /**
     * @param clazz
     * @return
     * @throws ClassNotFoundException
     */
    private static Class<?> loadClass(String clazz) throws ClassNotFoundException {
        ClassLoader classLoader = Server.getInstance().getClassLoader();
        return classLoader.loadClass(clazz);
    }
}