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

📄 userdatabasemanager.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.core;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sslexplorer.boot.PropertyDefinition;
import com.sslexplorer.boot.TypeMetaListItem;
import com.sslexplorer.properties.PropertyDatabase;
import com.sslexplorer.security.UserDatabase;

/**
 * @author Brett Smith <brett@3sp.com>
 */
public class UserDatabaseManager {
    
    final static Log log = LogFactory.getLog(UserDatabaseManager.class);
    
    private static UserDatabaseManager instance;
    private TreeMap userDatabases;
    private HashMap instances;

    /**
     * 
     */
    private UserDatabaseManager() {
        super();
        userDatabases = new TreeMap();
        instances = new HashMap();
    }
    
    public void registerDatabase(String name, Class userDatabaseClass, String messageResourcesKey) {
    	if (log.isInfoEnabled())
    		log.info("Registering user database " + name + " with class " + userDatabaseClass.getName());
        userDatabases.put(name, userDatabaseClass);   
        try {
            getAuthSchemes().add(new TypeMetaListItem(name, messageResourcesKey));
        }
        catch(Exception e) {
            log.error("Failed to add scheme to property definition.");
        }
    }
    
    public Class getUserDatabaseClass(String name) {
        return (Class)userDatabases.get(name);
    }
    
    public void closeAll() {
    	if (log.isInfoEnabled())
    		log.info("Closing all user databases");
        for(Iterator i = instances.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry entry = (Map.Entry)i.next();
            try {
                ((UserDatabase)entry.getValue()).close();
            } catch (Exception e) {
                log.error("Failed to close userdatabase.", e);
            }
        }
    }
    
    public void close(String name) throws Exception {
        UserDatabase udbInstance = (UserDatabase)instances.get(name);
        if(udbInstance != null) {
            udbInstance.close();
        }
        else {
            throw new Exception("No user database with name " + name + ".");
        }
    }
    
    public UserDatabase getUserDatabase(String name) throws Exception {
        UserDatabase udbInstance = null;
        if(instances.containsKey(name)) {
            udbInstance = (UserDatabase)instances.get(name);
        }
        else {
            Class clazz = getUserDatabaseClass(name);
            if(clazz == null) {
                throw new IllegalArgumentException("No user database named " + name);
            }
            else {
                udbInstance = (UserDatabase)clazz.newInstance();
                instances.put(name, udbInstance);
            }
        }
        return udbInstance;
    }
    
    public static UserDatabaseManager getInstance() {
        if(instance == null) {
            instance = new UserDatabaseManager();
        }
        return instance;
    }

    public List getAuthSchemes() throws Exception {
        PropertyDatabase pdb = CoreServlet.getServlet().getPropertyDatabase();
        PropertyDefinition def1 = pdb.getPropertyDefinition("security.userDatabase");
        List authSchemes = (List)def1.getTypeMetaObject();
        return authSchemes;
    }

    public TypeMetaListItem getAuthScheme(String name) throws Exception {
        List l = getAuthSchemes();
        for(Iterator i = l.iterator(); i.hasNext(); ) {
            TypeMetaListItem item = (TypeMetaListItem)i.next();
            if(item.getValue().equals(name)) {
                return item;
            }
        }
        return null;
    }

}

⌨️ 快捷键说明

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