net.ymate.platform.plugin.util.PluginUtils.java Source code

Java tutorial

Introduction

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

Source

/*
 * Copyright 2007-2107 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.util;

import net.ymate.platform.commons.util.FileUtils;
import net.ymate.platform.commons.util.ResourceUtils;
import net.ymate.platform.configuration.Cfgs;
import net.ymate.platform.configuration.IConfiguration;
import net.ymate.platform.configuration.annotation.Configuration;
import net.ymate.platform.configuration.annotation.ConfigurationProvider;
import net.ymate.platform.plugin.IPlugin;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.net.URL;

/**
 * <p>
 * PluginUtils
 * </p>
 * <p>
 * ????
 * </p>
 * 
 * @author (suninformation@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">?</th><th width="100px"></th><th
 *          width="100px"></th><th width="100px"></th>
 *          </tr>
 *          <!--  Table ?? -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td></td>
 *          <td></td>
 *          <td>2012-5-23?4:12:19</td>
 *          </tr>
 *          </table>
 */
public class PluginUtils {

    /**
     * @param cfgFile
     * @param plugin
     * @return ????null
     */
    public static String getResourcePath(String cfgFile, IPlugin plugin) {
        File _resFile = getResourceFile(cfgFile, plugin);
        if (_resFile != null) {
            return _resFile.getPath();
        }
        return null;
    }

    /**
     * @param cfgFile
     * @param plugin
     * @return ????
     */
    public static File getResourceFile(String cfgFile, IPlugin plugin) {
        if (plugin != null) {
            // ?
            String _path = StringUtils.defaultIfEmpty(plugin.getPluginMeta().getPath(),
                    plugin.getPluginFactory().getPluginConfig().getPluginHomePath());
            if (StringUtils.isNotBlank(_path)) {
                File _result = new File(new File(_path, StringUtils
                        .defaultIfEmpty(plugin.getPluginMeta().getAlias(), plugin.getPluginMeta().getId())),
                        cfgFile);
                if (_result.exists() && _result.isAbsolute() && _result.canRead()) {
                    return _result;
                }
            }
            // ??
            URL _targetFileURL = ResourceUtils.getResource(cfgFile, plugin.getClass());
            if (_targetFileURL != null) {
                return FileUtils.toFile(_targetFileURL);
            }
        }
        return null;
    }

    /**
     * ??????
     *
     * @param config ???
     * @param cfgFileName  ???????
     * @param plugin ?????
     * @return
     */
    public static boolean fillCfg(IConfiguration config, String cfgFileName, IPlugin plugin) {
        String _cfgFileName = getResourcePath(cfgFileName, plugin);
        return Cfgs.fillCfg(config, _cfgFileName, _cfgFileName == null);
    }

    /**
     * ?? Configuration ????SimpleName????SimpleName.CfgTagName.xml
     *
     * @param config ???
     * @param plugin ?????
     * @return ???
     */
    public static boolean fillCfg(IConfiguration config, IPlugin plugin) {
        if (config != null) {
            String _cfgFileName = null;
            Configuration _configuration = config.getClass().getAnnotation(Configuration.class);
            if (_configuration != null) {
                _cfgFileName = config.getClass().getAnnotation(Configuration.class).value();
            }
            if (StringUtils.isBlank(_cfgFileName)) {
                _cfgFileName = "cfgs/" + config.getClass().getSimpleName().toLowerCase() + config.getCfgTagName()
                        + ".xml";
            }
            _cfgFileName = getResourcePath(_cfgFileName, plugin);
            ConfigurationProvider _providerClass = config.getClass().getAnnotation(ConfigurationProvider.class);
            return Cfgs.fillCfg((_providerClass != null ? _providerClass.value() : null), config, _cfgFileName,
                    _cfgFileName == null);
        }
        return false;
    }

}