📄 jdbcsystemdatabase.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.jdbc;
import java.io.File;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.commons.cache.Cache;
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.maverick.http.HttpAuthenticatorFactory;
import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.HostService;
import com.sslexplorer.boot.PropertyList;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.CoreEvent;
import com.sslexplorer.core.CoreEventConstants;
import com.sslexplorer.core.CoreListener;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.extensions.ApplicationShortcut;
import com.sslexplorer.extensions.DefaultApplicationShortcut;
import com.sslexplorer.navigation.DefaultFavorite;
import com.sslexplorer.navigation.Favorite;
import com.sslexplorer.policyframework.PolicyConstants;
import com.sslexplorer.replacementproxy.DefaultReplacement;
import com.sslexplorer.replacementproxy.Replacement;
import com.sslexplorer.security.AuthenticationModuleManager;
import com.sslexplorer.security.AuthenticationSchemeSequence;
import com.sslexplorer.security.IpRestriction;
import com.sslexplorer.security.SystemDatabase;
import com.sslexplorer.security.User;
import com.sslexplorer.tunnels.DefaultTunnel;
import com.sslexplorer.tunnels.Tunnel;
import com.sslexplorer.vfs.DefaultNetworkPlace;
import com.sslexplorer.vfs.NetworkPlace;
import com.sslexplorer.webforwards.ReplacementProxyWebForward;
import com.sslexplorer.webforwards.ReverseProxyWebForward;
import com.sslexplorer.webforwards.TunneledSiteWebForward;
import com.sslexplorer.webforwards.WebForward;
/**
* Implementation of a {@link com.sslexplorer.security.SystemDatabase} that uses
* a JDBC compliant database to store SSL-Explorer's basic configuration and
* resources.
*
* @author Brett Smith <a href="mailto: brett@3sp.com"><brett@3sp.com></a>
* @author Lee David Painter <a href="mailto: lee@3sp.com"><lee@3sp.com></a>
* @author Peter King <a href="mailto: lee@3sp.com"><peter@3sp.com></a>
* @author James D Robinson <a href="mailto:james@3sp.com"><james@3sp.com></a>
* @version $Revision: 1.127 $
*/
public class JDBCSystemDatabase implements SystemDatabase, CoreListener {
static Log log = LogFactory.getLog(JDBCSystemDatabase.class);
private HashSet excludedIPAddresses = new HashSet();
private HashSet authorizedIPAddresses = new HashSet();
private Cache replacementsCache = null;
private JDBCDatabaseEngine db;
/**
* Constructor
*/
public JDBCSystemDatabase() {
int maxObjects = 1000;
try {
maxObjects = Integer.parseInt(System.getProperty("sslexplorer.jdbcSystemDatabase.replacementsCache", "10000"));
} catch (Exception e) {
}
replacementsCache = new SimpleCache(new MemoryStash(maxObjects));
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.core.Database#open(com.sslexplorer.core.CoreServlet)
*/
public void open(CoreServlet controllingServlet) throws Exception {
String dbName = System.getProperty("sslexplorer.systemDatabase.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("System database is being opened...");
log.info("JDBC vendor class implementation is " + vendorDB);
}
db = (JDBCDatabaseEngine) Class.forName(vendorDB).newInstance();
db.init("systemDatabase", dbName, jdbcUser, jdbcPassword, null);
File upgradeDir = new File("install/upgrade");
DBUpgrader upgrader = new DBUpgrader(ContextHolder.getContext()
.getVersion(), db, ContextHolder.getContext().getDBDirectory(), upgradeDir);
upgrader.upgrade();
CoreServlet.getServlet().addCoreListener(this);
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.boot.Database#close()
*/
public void close() throws Exception {
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.boot.Database#cleanup()
*/
public void cleanup() throws Exception {
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.core.CoreListener#coreEvent(com.sslexplorer.core.CoreEvent)
*/
public void coreEvent(CoreEvent evt) {
if (evt.getId() == CoreEventConstants.USER_REMOVED) {
User user = (User) evt.getParameter();
// LDP - Fix as null user might be passed?
if(user==null)
return;
try {
removeUser(user.getPrincipalName());
} catch (Exception e) {
log.error("Failed to remove user from system database.", e);
}
}
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.security.SystemDatabase#createApplicationShortcut(java.lang.String,
* java.lang.String, java.lang.String, int, java.util.Map)
*/
public int createApplicationShortcut(String application, String name, String description, int parentResourcePermission,
Map settings) throws Exception {
JDBCPreparedStatement ps = db.getStatement("createApplicationShortcut.insert");
try {
ps.setString(1, application);
ps.setString(2, name);
ps.setString(3, description);
ps.setInt(4, parentResourcePermission);
Calendar now = Calendar.getInstance();
ps.setString(5, db.formatTimestamp(now));
ps.setString(6, db.formatTimestamp(now));
ps.execute();
int id = db.getLastInsertId(ps, "createApplicationShortcut.lastInsertId");
JDBCPreparedStatement ps3 = db.getStatement("createApplicationShortcut.insertParameters");
try {
for (Iterator it = settings.keySet().iterator(); it.hasNext();) {
String parameter = (String) it.next();
String value = (String) settings.get(parameter);
ps3.setInt(1, id);
ps3.setString(2, parameter);
ps3.setString(3, value);
ps3.execute();
ps3.reset();
}
} finally {
ps3.releasePreparedStatement();
}
return id;
} finally {
ps.releasePreparedStatement();
}
}
public void updateApplicationShortcut(int id, String name, String description, int parentResourcePermission, Map settings)
throws Exception {
JDBCPreparedStatement ps = db.getStatement("updateApplicationShortcut.update");
// Update the actual shortcut
try {
ps.setString(1, name);
ps.setString(2, description);
ps.setInt(3, parentResourcePermission);
Calendar now = Calendar.getInstance();
ps.setString(4, db.formatTimestamp(now));
ps.setInt(5, id);
ps.execute();
} finally {
ps.releasePreparedStatement();
}
// Delete the current parameters
ps = db.getStatement("updateApplicationShortcut.deleteParameters");
try {
ps.setInt(1, id);
ps.execute();
} finally {
ps.releasePreparedStatement();
}
// Insert the parameters again
ps = db.getStatement("createApplicationShortcut.insertParameters");
try {
for (Iterator it = settings.keySet().iterator(); it.hasNext();) {
String parameter = (String) it.next();
String value = (String) settings.get(parameter);
ps.setInt(1, id);
ps.setString(2, parameter);
ps.setString(3, value);
ps.execute();
ps.reset();
}
} finally {
ps.releasePreparedStatement();
}
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.security.SystemDatabase#removeApplicationShortcuts(java.lang.String)
*/
public void removeApplicationShortcuts(String applicationId) throws Exception {
JDBCPreparedStatement ps = db.getStatement("removeApplicationShortcuts.select");
ps.setString(1, applicationId);
try {
ResultSet rs = ps.executeQuery();
try {
while (rs.next()) {
JDBCPreparedStatement ps2 = db.getStatement("removeApplicationShortcuts.delete.favorites");
ps2.setInt(1, PolicyConstants.APPLICATION_SHORTCUT_RESOURCE_TYPE_ID);
ps2.setInt(2, rs.getInt("resource_id"));
try {
ps2.execute();
} finally {
ps2.releasePreparedStatement();
}
ps2 = db.getStatement("removeApplicationShortcuts.delete.shortcutParameters");
ps2.setString(1, String.valueOf(rs.getInt("resource_id")));
try {
ps2.execute();
} finally {
ps2.releasePreparedStatement();
}
}
} finally {
rs.close();
}
} finally {
ps.releasePreparedStatement();
}
ps = db.getStatement("removeApplicationShortcuts.delete.shortcuts");
ps.setString(1, applicationId);
try {
ps.execute();
} finally {
ps.releasePreparedStatement();
}
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.security.SystemDatabase#getShortcuts(java.lang.String)
*/
public List getShortcuts(String application) throws Exception {
JDBCPreparedStatement ps = db.getStatement(application == null || application.equals("") ? "getShortcuts.selectAll"
: "getShortcuts.select");
if (application != null) {
ps.setString(1, application);
}
Vector v = new Vector();
try {
ResultSet rs = ps.executeQuery();
try {
while (rs.next()) {
v.add(buildShortcut(rs));
}
} finally {
rs.close();
}
} finally {
ps.releasePreparedStatement();
}
return v;
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.security.SystemDatabase#getShortcut(int)
*/
public ApplicationShortcut getShortcut(int shortcutId) throws Exception {
JDBCPreparedStatement ps = db.getStatement("getShortcut.select");
ps.setInt(1, shortcutId);
try {
ResultSet rs = ps.executeQuery();
try {
if (rs.next()) {
return buildShortcut(rs);
} else {
return null;
}
} finally {
rs.close();
}
} finally {
ps.releasePreparedStatement();
}
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.security.SystemDatabase#getShortcutByName(java.lang.String)
*/
public ApplicationShortcut getShortcutByName(String name) throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -