⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultapplicationinitializer.java

📁 java插件系统源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2005 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.boot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.java.plugin.ObjectFactory;
import org.java.plugin.Plugin;
import org.java.plugin.PluginManager;
import org.java.plugin.PluginManager.PluginLocation;
import org.java.plugin.registry.IntegrityCheckReport;
import org.java.plugin.registry.ManifestProcessingException;
import org.java.plugin.registry.PluginRegistry;
import org.java.plugin.registry.PluginRegistry.ManifestInfo;
import org.java.plugin.util.ExtendedProperties;
import org.java.plugin.util.IoUtil;
import org.java.plugin.util.ResourceManager;

/**
 * Default implementation of the application initializer interface.
 * <p>
 * Supported configuration parameters:
 * <dl>
 *   <dt>org.java.plugin.boot.applicationPlugin</dt>
 *   <dd>ID of plug-in to start. There is no default value for this parameter.
 *     In common scenario, this is the only parameter that you must provide.</dd>
 *   <dt>org.java.plugin.boot.integrityCheckMode</dt>
 *   <dd>Regulates how to check plug-ins integrity when running JPF. Possible
 *     values: <code>full</code>, <code>light</code>, <code>off</code>. The
 *     default value is <code>full</code>.</dd>
 *   <dt>org.java.plugin.boot.pluginsCollector</dt>
 *   <dd>Plug-ins location collector class, for details see
 *     {@link org.java.plugin.boot.PluginsCollector}. Default is
 *     {@link org.java.plugin.boot.DefaultPluginsCollector}.</dd>
 *   <dt>org.java.plugin.boot.pluginsWhiteList</dt>
 *   <dd>Location of the file with plug-in identifiers that should be only
 *     accepted by this application initializer. This is optional parameter.</dd>
 *   <dt>org.java.plugin.boot.pluginsBlackList</dt>
 *   <dd>Location of the file with plug-in identifiers that should not be
 *     accepted by this application initializer. This is optional parameter.</dd>
 * </dl>
 * Note that all given configuration parameters are passed to
 * {@link org.java.plugin.ObjectFactory#newInstance(ExtendedProperties)}
 * when running JPF (see bellow). This allows you to configure JPF precisely.
 * </p>
 * <p><i>Black and white lists of plug-ins</i></p>
 * <p>
 * In some situations you may want to temporary exclude some of your plug-ins
 * from the application scope. This may be achieved with help of while and black
 * lists - simple plain text files that contain lists of plug-in identifiers to
 * be included/excluded from the application. Each identifies should be in
 * separate line. You may provide unique plug-in ID also.
 * </p>
 * <p><i>What is application plug-in?</i></p>
 * <p>
 * When application starts, the
 * {@link org.java.plugin.boot.Boot#main(String[])} method executed calling
 * {@link #initApplication(BootErrorHandler, String[])} to get initialized
 * instance of {@link org.java.plugin.boot.Application}
 * (or {@link org.java.plugin.boot.ServiceApplication}) class. The method
 * {@link #initApplication(BootErrorHandler, String[])} in this implementation
 * scans plug-in repositories to collect all available plug-in files and folders
 * (using special class that can be customized),
 * instantiates JPF and publishes all discovered plug-ins. After that it asks
 * {@link org.java.plugin.PluginManager} for an <b>Application Plug-in</b> with
 * ID provided as configuration parameter. Returned class instance is expected
 * to be of type {@link org.java.plugin.boot.ApplicationPlugin} and it's method
 * {@link org.java.plugin.boot.ApplicationPlugin#initApplication(ExtendedProperties, String[])}
 * called.
 * </p>
 * <p>
 * To the mentioned <code>initApplication</code> method passed a subset of
 * configuration properties whose names start with application plug-in ID
 * followed with dot character <code>'.'</code> (see
 * {@link org.java.plugin.util.ExtendedProperties#getSubset(String)} for
 * details).
 * </p>
 * <p>
 * As a result of the described procedure, the <code>Boot</code> get instance of
 * {@link org.java.plugin.boot.Application} interface, so it can start
 * application calling
 * {@link org.java.plugin.boot.Application#startApplication()} method.
 * </p> 
 * 
 * @version $Id: DefaultApplicationInitializer.java,v 1.11 2006/10/10 17:55:50 ddimon Exp $
 */
public class DefaultApplicationInitializer implements ApplicationInitializer {
    protected static final String PARAM_APPLICATION_PLUGIN =
        "org.java.plugin.boot.applicationPlugin"; //$NON-NLS-1$
    protected static final String PARAM_INTEGRITY_CHECK_MODE =
        "org.java.plugin.boot.integrityCheckMode"; //$NON-NLS-1$
    protected static final String PARAM_PLUGINS_COLLECTOR =
        "org.java.plugin.boot.pluginsCollector"; //$NON-NLS-1$
    protected static final String PARAM_PLUGINS_WHITE_LIST =
        "org.java.plugin.boot.pluginsWhiteList"; //$NON-NLS-1$
    protected static final String PARAM_PLUGINS_BLACK_LIST =
        "org.java.plugin.boot.pluginsBlackList"; //$NON-NLS-1$

    private Log log;
    private ExtendedProperties config;
    private String integrityCheckMode;
    private PluginsCollector collector;
    private Set whiteList;
    private Set blackList;

    /**
     * Configures this instance and application environment. The sequence is:
     * <ul>
     *   <li>Configure logging system. There is special code for supporting
     *     <code>Log4j</code> logging system only. All other systems support
     *     come from <code>commons-logging</code> package.</li>
     *   <li>Instantiate and configure {@link PluginsCollector} using
     *     configuration data.</li>
     * </ul>
     * @see org.java.plugin.boot.ApplicationInitializer#configure(
     *      org.java.plugin.util.ExtendedProperties)
     */
    public void configure(final ExtendedProperties configuration)
            throws Exception {
        // Initializing logging system.
        String log4jConfigKey = "log4j.configuration"; //$NON-NLS-1$
        if (System.getProperty(log4jConfigKey) == null) {
            // Trying to find log4j configuration.
            if (configuration.containsKey(log4jConfigKey)) {
                System.setProperty(log4jConfigKey,
                        configuration.getProperty(log4jConfigKey));
            } else {
                File log4jConfig = new File(
                        configuration.getProperty("applicationRoot") //$NON-NLS-1$
                        + File.separator + "log4j.properties"); //$NON-NLS-1$
                if (!log4jConfig.isFile()) {
                    log4jConfig = new File(
                            configuration.getProperty("applicationRoot") //$NON-NLS-1$
                            + File.separator + "log4j.xml"); //$NON-NLS-1$
                }
                if (log4jConfig.isFile()) {
                    try {
                        System.setProperty(log4jConfigKey,
                                IoUtil.file2url(log4jConfig).toExternalForm());
                    } catch (MalformedURLException e) {
                        // ignore
                    }
                }
            }
        }
        log = LogFactory.getLog(getClass());
        log.info("logging system initialized"); //$NON-NLS-1$
        log.info("application root is " //$NON-NLS-1$
                + configuration.getProperty("applicationRoot")); //$NON-NLS-1$
        config = configuration;
        integrityCheckMode =
            configuration.getProperty(PARAM_INTEGRITY_CHECK_MODE, "full"); //$NON-NLS-1$
        collector = getCollectorInstance(
                configuration.getProperty(PARAM_PLUGINS_COLLECTOR));
        collector.configure(configuration);
        log.debug("plug-ins collector is " + collector); //$NON-NLS-1$
        try {
            whiteList = loadList(
                    configuration.getProperty(PARAM_PLUGINS_WHITE_LIST, null));
        } catch (IOException ioe) {
            log.warn("failed loading white list", ioe); //$NON-NLS-1$
        }
        if (whiteList != null) {
            log.debug("white list loaded"); //$NON-NLS-1$
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -