net.ymate.platform.plugin.Plugins.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.plugin.Plugins.java

Source

/*
 * Copyright 2007-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.plugin;

import net.ymate.platform.core.Version;
import net.ymate.platform.core.YMP;
import net.ymate.platform.core.event.Events;
import net.ymate.platform.core.lang.BlurObject;
import net.ymate.platform.core.module.IModule;
import net.ymate.platform.core.module.annotation.Module;
import net.ymate.platform.core.util.ClassUtils;
import net.ymate.platform.core.util.RuntimeUtils;
import net.ymate.platform.plugin.annotation.PluginFactory;
import net.ymate.platform.plugin.impl.DefaultPluginConfig;
import net.ymate.platform.plugin.impl.DefaultPluginEventListener;
import net.ymate.platform.plugin.impl.DefaultPluginFactory;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * ?????
 *
 * @author  (suninformation@163.com) on 2012-11-30 ?6:28:20
 * @version 1.0
 */
@Module
public class Plugins implements IModule, IPlugins {

    public static final Version VERSION = new Version(2, 0, 0,
            Plugins.class.getPackage().getImplementationVersion(), Version.VersionType.GA);

    private final Log _LOG = LogFactory.getLog(Plugins.class);

    private YMP __owner;

    private boolean __inited;

    private static volatile IPlugins __instance;

    private IPluginFactory __pluginFactory;

    /**
     * @return ??
     */
    public static IPlugins get() {
        if (__instance == null) {
            synchronized (VERSION) {
                if (__instance == null) {
                    __instance = YMP.get().getModule(Plugins.class);
                }
            }
        }
        return __instance;
    }

    public String getName() {
        return IPlugins.MODULE_NAME;
    }

    public void init(YMP owner) throws Exception {
        if (!__inited) {
            Map<String, String> _moduleCfgs = owner.getConfig().getModuleConfigs(MODULE_NAME);
            __owner = owner;
            __owner.getEvents().registerEvent(PluginEvent.class);
            //
            _LOG.info("Initializing ymate-platform-plugin-" + VERSION);
            //
            DefaultPluginConfig _config = new DefaultPluginConfig();
            _config.setPluginHome(new File(RuntimeUtils.replaceEnvVariable(
                    StringUtils.defaultIfBlank(_moduleCfgs.get("plugin_home"), "${root}/plugins"))));
            _config.setAutoscanPackages(new ArrayList<String>(Arrays.asList(
                    StringUtils.split(StringUtils.trimToEmpty(_moduleCfgs.get("autoscan_packages")), "|"))));
            //
            for (String _package : __owner.getConfig().getAutoscanPackages()) {
                if (!_config.getAutoscanPackages().contains(_package)) {
                    _config.getAutoscanPackages().add(_package);
                }
            }
            //
            _config.setAutomatic(BlurObject.bind(StringUtils.defaultIfBlank(_moduleCfgs.get("automatic"), "true"))
                    .toBooleanValue());
            _config.setIncludedClassPath(
                    BlurObject.bind(StringUtils.defaultIfBlank(_moduleCfgs.get("included_classpath"), "true"))
                            .toBooleanValue());
            //
            _config.setPluginEventListener(new IPluginEventListener() {
                public void onInited(IPluginContext context, IPlugin plugin) {
                    __owner.getEvents().fireEvent(Events.MODE.NORMAL,
                            new PluginEvent(plugin, PluginEvent.EVENT.PLUGIN_INITED));
                }

                public void onStarted(IPluginContext context, IPlugin plugin) {
                    __owner.getEvents().fireEvent(Events.MODE.NORMAL,
                            new PluginEvent(plugin, PluginEvent.EVENT.PLUGIN_STARTED));
                }

                public void onShutdown(IPluginContext context, IPlugin plugin) {
                    __owner.getEvents().fireEvent(Events.MODE.NORMAL,
                            new PluginEvent(plugin, PluginEvent.EVENT.PLUGIN_SHUTDOWN));
                }

                public void onDestroy(IPluginContext context, IPlugin plugin) {
                    __owner.getEvents().fireEvent(Events.MODE.NORMAL,
                            new PluginEvent(plugin, PluginEvent.EVENT.PLUGIN_DESTROYED));
                }
            });
            //
            __pluginFactory = new DefaultPluginFactory(__owner);
            __pluginFactory.init(_config);
            //
            __inited = true;
        }
    }

    public boolean isInited() {
        return __inited;
    }

    public YMP getOwner() {
        return __owner;
    }

    public void destroy() throws Exception {
        if (__inited) {
            __inited = false;
            //
            __pluginFactory.destroy();
            __pluginFactory = null;
            //
            __owner = null;
        }
    }

    public IPluginFactory getPluginFactory() {
        return __pluginFactory;
    }

    //---------------------=================------------------------

    /**
     * @param pluginHome   ?
     * @param autoPackages ??
     * @return ???
     * @throws Exception ??
     */
    private static DefaultPluginConfig loadConfig(String pluginHome, String[] autoPackages) throws Exception {
        DefaultPluginConfig _config = new DefaultPluginConfig();
        _config.setPluginHome(new File(pluginHome));
        _config.setAutoscanPackages(Arrays.asList(autoPackages));
        return _config;
    }

    /**
     * @param clazz ?
     * @return ????
     * @throws Exception ??
     */
    private static DefaultPluginConfig loadConfig(Class<? extends IPluginFactory> clazz) throws Exception {
        DefaultPluginConfig _config = null;
        if (clazz != null && clazz.isAnnotationPresent(PluginFactory.class)) {
            _config = new DefaultPluginConfig();
            //
            PluginFactory _factoryAnno = clazz.getAnnotation(PluginFactory.class);
            if (StringUtils.isNotBlank(_factoryAnno.pluginHome())) {
                _config.setPluginHome(new File(_factoryAnno.pluginHome()));
            }
            String[] _packages = _factoryAnno.autoscanPackages();
            if (ArrayUtils.isEmpty(_packages)) {
                _packages = new String[] { clazz.getPackage().getName() };
            }
            _config.setAutoscanPackages(Arrays.asList(_packages));
            _config.setIncludedClassPath(_factoryAnno.includedClassPath());
            _config.setAutomatic(_factoryAnno.automatic());
            //
            IPluginEventListener _listener = ClassUtils.impl(_factoryAnno.listenerClass(),
                    IPluginEventListener.class);
            if (_listener != null) {
                _config.setPluginEventListener(_listener);
            } else {
                _config.setPluginEventListener(new DefaultPluginEventListener());
            }
        }
        return _config;
    }

    /**
     * @param pluginHome   ?
     * @param autoPackages ??
     * @return ?
     * @throws Exception ??
     */
    public static IPluginFactory createFactory(String pluginHome, String[] autoPackages) throws Exception {
        IPluginFactory _factory = new DefaultPluginFactory();
        _factory.init(loadConfig(pluginHome, autoPackages));
        return _factory;
    }

    /**
     * @param clazz ?
     * @return clazz?
     * @throws Exception ??
     */
    public static IPluginFactory createFactory(Class<? extends IPluginFactory> clazz) throws Exception {
        IPluginFactory _factory = ClassUtils.impl(clazz, IPluginFactory.class);
        if (_factory != null) {
            if (clazz.isAnnotationPresent(PluginFactory.class)) {
                _factory.init(loadConfig(clazz));
            }
        }
        return _factory;
    }

    /**
     * @param clazz  ?
     * @param config ???
     * @return ??clazz?
     * @throws Exception ??
     */
    public static IPluginFactory createFactory(Class<? extends IPluginFactory> clazz, IPluginConfig config)
            throws Exception {
        IPluginFactory _factory = ClassUtils.impl(clazz, IPluginFactory.class);
        if (_factory != null) {
            if (config != null) {
                _factory.init(config);
            } else if (clazz.isAnnotationPresent(PluginFactory.class)) {
                _factory.init(loadConfig(clazz));
            }
        }
        return _factory;
    }
}