modulepluginmanager.java~3~

来自「一个自己做的公司网站和办公职员管理系统。」· JAVA~3~ 代码 · 共 167 行

JAVA~3~
167
字号
package ws.woa.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Set;

import org.apache.commons.collections.SequencedHashMap;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 僾儔僌僀儞偝傟偨儌僕儏乕儖偺忣曬傪娗棟偡傞僔儞僌儖僩儞僋儔僗偱偡丅
 *
 * @author Wang
 */
public class ModulePluginManager {

    private static ModulePluginManager instance = null;
    private SequencedHashMap hash = new SequencedHashMap();

    /**
     * 僔儞僌儖僩儞僷僞乕儞偺偨傔偺僾儔僀儀乕僩僐儞僗僩儔僋僞丅
     */
    private ModulePluginManager() throws IOException, SAXException, FileNotFoundException {
        // 偙偙偱XML僼傽僀儖傪SAX僷乕僗
        ModuleSAXHandler handler = new ModuleSAXHandler();
        XMLReader parser = new SAXParser();
        parser.setContentHandler(handler);
        parser.setErrorHandler(handler);

        String path = ControllerServlet.context.getRealPath("/WEB-INF/module.xml");
        parser.parse(new InputSource(new FileInputStream(new File(path))));
    }

    /**
     * ModuleInfo傪捛壛偟傑偡丅
     *
     * @param moduleName 儌僕儏乕儖柤
     * @param moduleInfo 儌僕儏乕儖忣曬
     */
    public void addModuleInfo(String moduleName,ModuleInfo info){
        hash.put(moduleName,info);
    }

    /**
     * ModuleInfo傪庢摼偟傑偡丅
     *
     * @param moduleName 儌僕儏乕儖柤
     * @return ModuleInfo
     */
    public ModuleInfo getModuleInfo(String moduleName){
        return (ModuleInfo)hash.get(moduleName);
    }

    /**
     * 慡偰偺ModuleInfo傪攝楍偱庢摼偟傑偡丅
     *
     * @return ModuleInfo[]
     */
    public ModuleInfo[] getModuleInfoArray(){
        Set set = hash.keySet();
        Object[] keys = set.toArray();
        ModuleInfo[] info = new ModuleInfo[keys.length];
        for(int i=0;i<keys.length;i++){
            info[i] = getModuleInfo((String)keys[i]);
        }
        return info;
    }

    /**
     * ModuleHandler傪庢摼偟傑偡丅
     *
     * @param moduleName 儌僕儏乕儖柤
     * @return ModuleHandler
     */
    public ModuleHandler getModuleHandler(String moduleName)
            throws ClassNotFoundException, IllegalAccessException, InstantiationException{
        ModuleInfo info = getModuleInfo(moduleName);
        ModuleHandler handler = (ModuleHandler)Class.forName(info.getHandlerClassName()).newInstance();
        return handler;
    }

    /**
     * PluginManager偺僀儞僗僞儞僗傪庢摼偟傑偡丅
     *
     * @return PluginManager偺僀儞僗僞儞僗
     */
    public synchronized static ModulePluginManager getInstance()
                  throws FileNotFoundException, IOException, SAXException {
        if(instance==null){
            instance = new ModulePluginManager();
        }
        return instance;
    }

    /////////////////////////////////////////////////////////////////////////////////////////
    /**
     * module.xml傪SAX僷乕僗偡傞撪晹僋儔僗丅
     */
    class ModuleSAXHandler extends DefaultHandler {

        private ModuleInfo info;

    	/**
         * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
         */
        public void startElement(String uri,String local,String raw,
                                 Attributes attrs) throws SAXException {
            if(raw.equals("module")){
                parseModuleTag(attrs);
            }
        }

        /**
         * module僞僌傪夝愅
         */
        private void parseModuleTag(Attributes attrs){
            String name       = attrs.getValue("name");
            String handler    = attrs.getValue("handler");
            String access     = attrs.getValue("access-level");
            String moduleType = attrs.getValue("module-type");
            String menuTitle  = attrs.getValue("menu-title");
            String author     = attrs.getValue("author");
            String ver        = attrs.getValue("version");
            String desc       = attrs.getValue("description");

            info = new ModuleInfo();
            info.setModuleName(name);
            info.setHandlerClassName(handler);
            info.setAuthor(author);
            info.setVersion(ver);
            info.setMenuTitle(menuTitle);

            if(access==null ||
               access.toLowerCase().equals(ModuleAccessLevel.ALL_USERS.getName())){
                info.setAccessLevel(ModuleAccessLevel.ALL_USERS);
            } else if(access.toLowerCase().equals(ModuleAccessLevel.ADMIN_ONLY.getName())){
                info.setAccessLevel(ModuleAccessLevel.ADMIN_ONLY);
            } else if(access.toLowerCase().equals(ModuleAccessLevel.NO_LOGIN.getName())){
                info.setAccessLevel(ModuleAccessLevel.NO_LOGIN);
            } else {
                // 僄儔乕
            }

            if(moduleType==null ||
               moduleType.toLowerCase().equals(ModuleType.FUNCTION.getName())){
                info.setModuleType(ModuleType.FUNCTION);
            } else if(moduleType.toLowerCase().equals(ModuleType.APPLICATION.getName())){
                info.setModuleType(ModuleType.APPLICATION);
            } else if(moduleType.toLowerCase().equals(ModuleType.PORTAL.getName())){
                info.setModuleType(ModuleType.PORTAL);
            } else {
                // 僄儔乕
            }

            addModuleInfo(info.getModuleName(),info);
        }

    }
}

⌨️ 快捷键说明

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