net.ymate.platform.base.impl.DefaultModuleLoader.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.base.impl.DefaultModuleLoader.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.base.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import net.ymate.platform.base.IModule;
import net.ymate.platform.base.IModuleLoader;
import net.ymate.platform.module.ConfigModule;
import net.ymate.platform.module.JdbcModule;
import net.ymate.platform.module.LogModule;
import net.ymate.platform.module.MongoDBModule;
import net.ymate.platform.module.WebMvcModule;

import org.apache.commons.lang.StringUtils;

/**
 * <p>
 * DefaultModuleLoader
 * </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-12-23?6:08:03</td>
 *          </tr>
 *          </table>
 */
public class DefaultModuleLoader implements IModuleLoader {

    protected static Map<String, String> __DEFAULT_MODULE_LOADER_NAMES = new HashMap<String, String>();

    static {
        // configuration|logger|jdbc|mongodb|webmvc
        __DEFAULT_MODULE_LOADER_NAMES.put("configuration", ConfigModule.class.getName());
        __DEFAULT_MODULE_LOADER_NAMES.put("logger", LogModule.class.getName());
        __DEFAULT_MODULE_LOADER_NAMES.put("jdbc", JdbcModule.class.getName());
        __DEFAULT_MODULE_LOADER_NAMES.put("mongodb", MongoDBModule.class.getName());
        __DEFAULT_MODULE_LOADER_NAMES.put("webmvc", WebMvcModule.class.getName());
    }

    private List<IModule> __loadedModules = new ArrayList<IModule>();

    /* (non-Javadoc)
     * @see net.ymate.platform.module.base.IModuleLoader#initialize(java.util.Properties)
     */
    public void initialize(Properties configs) throws Exception {
        String[] _moduleList = StringUtils.split(configs.getProperty("ymp.module_list"), '|');
        if (_moduleList != null && _moduleList.length > 0) {
            for (String _moduleName : _moduleList) {
                String _moduleClassName = configs.getProperty("ymp.modules." + _moduleName);
                if (StringUtils.isBlank(_moduleClassName)) {
                    if (__DEFAULT_MODULE_LOADER_NAMES.containsKey(_moduleName)) {
                        _moduleClassName = __DEFAULT_MODULE_LOADER_NAMES.get(_moduleName);
                    }
                }
                if (StringUtils.isNotBlank(_moduleClassName)) {
                    IModule _module = (IModule) Class.forName(_moduleClassName).newInstance();
                    _module.initialize(__parseModuleCfg(_moduleName, configs));
                    __loadedModules.add(_module);
                }
            }
        }
    }

    /* (non-Javadoc)
     * @see net.ymate.platform.module.base.IModuleLoader#destroy()
     */
    public void destroy() throws Exception {
        for (IModule _module : __loadedModules) {
            _module.destroy();
        }
        __loadedModules.clear();
    }

    /**
     * @param moduleName ???
     * @param configs ???
     * @return ??????
     */
    private Map<String, String> __parseModuleCfg(String moduleName, Properties configs) {
        Map<String, String> _returnValue = new HashMap<String, String>();
        // ?????
        for (Object _key : configs.keySet()) {
            String _prefix = "ymp.configs." + moduleName + ".";
            if (StringUtils.startsWith((String) _key, _prefix)) {
                String _cfgKey = StringUtils.substring((String) _key, _prefix.length());
                String _cfgValue = configs.getProperty((String) _key);
                _returnValue.put(_cfgKey, _cfgValue);
            }
        }
        return _returnValue;
    }

}