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

📄 jdbcpropertydatabase.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  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.jdbc;

import java.io.File;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.prefs.Preferences;

import org.apache.commons.cache.Cache;
import org.apache.commons.cache.CacheStat;
import org.apache.commons.cache.MemoryStash;
import org.apache.commons.cache.SimpleCache;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.DefaultPropertyDefinition;
import com.sslexplorer.boot.PropertyDefinition;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.properties.DefaultPropertyProfile;
import com.sslexplorer.properties.DefinitionComparator;
import com.sslexplorer.properties.PropertyDatabase;
import com.sslexplorer.properties.PropertyDefinitionCategory;
import com.sslexplorer.properties.PropertyProfile;
import com.sslexplorer.security.Constants;

/**
 * Implementation of a {@link com.sslexplorer.properties.PropertyDatabase} that
 * stores profiles, categories and properties in a JDBC compliant database.
 * <p>
 * To improve performance, the values of the properties themselves are 
 * cached in memory.
 * <p>
 * The behaviour of this cache is effected by two Java system properties,
 * <code>sslexplorer.jdbcPropertyDatabase.cacheTTL</code> and
 * <code>sslexplorer.jdbcPropertyDatabase.cacheMaxObjs</code>
 * 
 * @author Brett Smith <a href="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
 * @version $Revision: 1.11 $
 */

public class JDBCPropertyDatabase implements PropertyDatabase {

    static Log log = LogFactory.getLog(JDBCPropertyDatabase.class);


    final static Long CACHE_TTL = new Long(System.getProperty("sslexplorer.jdbcPropertyDatabase.cacheTTL", "180000"));
    final static Integer CACHE_MAXOBJS = new Integer(System.getProperty("sslexplorer.jdbcPropertyDatabase.cacheMaxObjs", "2000"));
    final static Long CACHE_COST = new Long(1);

    // Private instance variables
    private Cache propertyCache;
    private JDBCDatabaseEngine db;
    private HashMap propertyDefinitions;
    private List categories;
    private Map categoryMap;
    
    /**
     * Constructor
     */
    public JDBCPropertyDatabase() {
        categories = new ArrayList();
        categoryMap = new HashMap();
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.core.Database#open(com.sslexplorer.core.CoreServlet)
     */
    public void open(CoreServlet controllingServlet) throws Exception {
        String dbName = System.getProperty("sslexplorer.propertyDatabase.jdbc.dbName", "explorer_configuration"); 
        controllingServlet.addDatabase(dbName);
        String jdbcUser = System.getProperty("sslexplorer.jdbc.username", "sa");
        String jdbcPassword = System.getProperty("sslexplorer.jdbc.password", "");
        String vendorDB = System.getProperty("sslexplorer.jdbc.vendorClass", "com.sslexplorer.jdbc.hsqldb.HSQLDBDatabaseEngine");

        if (log.isInfoEnabled()) {
        	log.info("Property database is being opened...");
        	log.info("JDBC vendor class implementation is " + vendorDB);
        }

        db = (JDBCDatabaseEngine) Class.forName(vendorDB).newInstance();
        db.init("propertyDatabase", dbName, jdbcUser, jdbcPassword, null);

        File upgradeDir = new File("install/upgrade");
        DBUpgrader upgrader = new DBUpgrader(ContextHolder.getContext()
            .getVersion(), db, ContextHolder.getContext().getDBDirectory(), upgradeDir);
        upgrader.upgrade();
        
        int maxObjs = CACHE_MAXOBJS.intValue();
        propertyCache = new SimpleCache(new MemoryStash(maxObjs));

        // Load all of the property definitions
        loadPropertyDefinitions();
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.boot.Database#close()
     */
    public void close() throws Exception {
        propertyCache.clear();
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.boot.PropertyDatabase#getProperty(int, java.lang.String, java.lang.String)
     */
    public String getProperty(int profile, String username, String name) throws Exception {
        String cacheKey = profile + "-" + ( username == null ? "" : username) + "-" + name;
        String val = (String)propertyCache.retrieve(cacheKey);
        if(val == null) {        
            PropertyDefinition def = getPropertyDefinition(name);
            if (log.isDebugEnabled())
            	log.debug("Getting property " + name + " for user '" + username + "' and profile " + profile);
            if (def == null) {
                throw new Exception("No property definition for " + name);
            }
            if (profile != 0 && def.getVisibility() != PropertyDefinition.PROFILE) {
                throw new Exception("Property " + name + " cannot be retrieved by profile as it is not user overidable.");
            }
            
            // If the definitions shows a visibilitiy of CONTEXT_CONFIGURATION then get the property value from the context
            if(def.getVisibility() == PropertyDefinition.CONTEXT_CONFIGURATION) {
                val = ContextHolder.getContext().getContextProperty(name);
            }
            else {    
                JDBCPreparedStatement ps = db.getStatement("select.property");
                ps.setString(1, username == null || username.equals("") ? "" : username);
                ps.setString(2, "");
                ps.setString(3, name);
                ps.setInt(4, profile);
        
                ResultSet rs = ps.executeQuery();
        
                try {
                    boolean found = rs.next();
                    val = found ? rs.getString("value") : def.getDefaultValue();
                    if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
                        try {
                            val = ContextHolder.getContext().deobfuscatePassword(val);
                        } catch (Throwable t) {
                            log.warn("Password property " + def.getName() + " could not be decoded. It has been result to the default.", t);
                        }
                    }
                    if (log.isDebugEnabled())
                    	log.debug("Value = '" + val + "'");
                } finally {
                    rs.close();
                    ps.releasePreparedStatement();
                }
            }
            storeToCache(cacheKey, val);
        }
        
        return CoreUtil.doStandardReplacements(username, val);

    }

    /* (non-Javadoc)
     * @see com.sslexplorer.boot.PropertyDatabase#getPropertyDefinitions(java.lang.String)
     */
    public List getPropertyDefinitions(String scope) throws Exception {
        List v = new ArrayList();
        for (Iterator i = propertyDefinitions.entrySet().iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry) i.next();
            PropertyDefinition def = (PropertyDefinition) entry.getValue();
            if ( ( Constants.SCOPE_PERSONAL.equals(scope) || Constants.SCOPE_GLOBAL.equals(scope) ) && def.getVisibility() == PropertyDefinition.PROFILE) {
                v.add(def);
            } else if (Constants.SCOPE_SETUP.equals(scope) && (def.getVisibility()  == PropertyDefinition.SYSTEM_CONFIGURATION || def.getVisibility() == PropertyDefinition.CONTEXT_CONFIGURATION)) {
                v.add(def);
            }
        }
        Collections.sort(v, new DefinitionComparator());
        return v;
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.boot.PropertyDatabase#getPropertyDefinition(java.lang.String)
     */
    public PropertyDefinition getPropertyDefinition(String name) throws Exception {
        return (PropertyDefinition) propertyDefinitions.get(name);
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.boot.PropertyDatabase#registerPropertyDefinition(com.sslexplorer.boot.PropertyDefinition)
     */
    public void registerPropertyDefinition(PropertyDefinition def) {
    	if (log.isDebugEnabled())
    		log.debug("Register property definition " + def.getName());
        propertyDefinitions.put(def.getName(), def);
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.boot.PropertyDatabase#deregisterPropertyDefinition(java.lang.String)
     */
    public void deregisterPropertyDefinition(String name) {
    	if (log.isDebugEnabled())
    		log.debug("Deregister property definition " + name);
        propertyDefinitions.remove(name);
    }

    /* (non-Javadoc)
     * @see com.sslexplorer.boot.PropertyDatabase#setProperty(int, java.lang.String, java.lang.String, java.lang.String)
     */
    public String setProperty(int profile, String username, String name, String value) throws Exception {
        String oldVal = getProperty(profile, username, name);
        propertyCache.clear();
        PropertyDefinition def = getPropertyDefinition(name);
        if (def == null) {
            throw new Exception("No property definition for " + name);
        }
        if (profile != 0 && def.getVisibility() != PropertyDefinition.PROFILE) {
            throw new Exception("Property " + name + " cannot be set by profile as it is not user overidable.");
        }

        // If the definitions shows a visibilitiy of CONTEXT_CONFIGURATION then set the property value in the context
        if(def.getVisibility() == PropertyDefinition.CONTEXT_CONFIGURATION) {
            ContextHolder.getContext().setContextProperty(name, value);
        }
        else {
            JDBCPreparedStatement ps = db.getStatement("select.property");
            ps.setString(1, username == null || username.equals("") ? "" : username);
            ps.setString(2, "");
            ps.setString(3, name);
            ps.setInt(4, profile);
    
            ResultSet rs = ps.executeQuery();
    
            JDBCPreparedStatement ps2;
            try {
                if (!rs.next()) {
                	if (log.isDebugEnabled())
                		log.debug("Property doesnt currently exist, inserting value '" + value + "'");
    
                    ps2 = db.getStatement("insert.property");
    
                    ps2.setInt(1, profile);
                    ps2.setString(2, (username == null ? "" : username));
                    ps2.setString(3, name);
    
                    // Insert new property
                    if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
                        ps2.setString(4, ContextHolder.getContext().obfuscatePassword(value));
                        ;
                    } else {
                        ps2.setString(4, value);
                    }
    
                    try {
                        ps2.execute();
                    } finally {
                        ps2.releasePreparedStatement();
                    }
    
                } else {
                	if (log.isDebugEnabled())
                		log.debug("Property exists, updating value '" + value + "'");
    
                    ps2 = db.getStatement("update.property");
    
                    // Insert new property
                    if (def.getType() == PropertyDefinition.TYPE_PASSWORD) {
                        ps2.setString(1, ContextHolder.getContext().obfuscatePassword(value));
                        ;
                    } else {
                        ps2.setString(1, value);
                    }
                    ps2.setString(2, name);
    
                    ps2.setString(3, (username == null ? "" : username));
                    ps2.setInt(4, profile);
    
                    try {

⌨️ 快捷键说明

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