dbusermanager.java
来自「JAVA FTP 上传下载 的源文件」· Java 代码 · 共 698 行 · 第 1/2 页
JAVA
698 行
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 org.apache.ftpserver.usermanager.impl;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import javax.sql.DataSource;import org.apache.ftpserver.FtpServerConfigurationException;import org.apache.ftpserver.ftplet.Authentication;import org.apache.ftpserver.ftplet.AuthenticationFailedException;import org.apache.ftpserver.ftplet.Authority;import org.apache.ftpserver.ftplet.FtpException;import org.apache.ftpserver.ftplet.User;import org.apache.ftpserver.usermanager.AnonymousAuthentication;import org.apache.ftpserver.usermanager.DbUserManagerFactory;import org.apache.ftpserver.usermanager.PasswordEncryptor;import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;import org.apache.ftpserver.util.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * <strong>Internal class, do not use directly.</strong> * * This is another database based user manager class. It has been tested in * MySQL and Oracle 8i database. The schema file is </code>res/ftp-db.sql</code> * * All the user attributes are replaced during run-time. So we can use your * database schema. Then you need to modify the SQLs in the configuration file. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 737687 $, $Date: 2009-01-26 14:14:10 +0100 (Mon, 26 Jan 2009) $ */public class DbUserManager extends AbstractUserManager { private final Logger LOG = LoggerFactory.getLogger(DbUserManager.class); private String insertUserStmt; private String updateUserStmt; private String deleteUserStmt; private String selectUserStmt; private String selectAllStmt; private String isAdminStmt; private String authenticateStmt; private DataSource dataSource; /** * Internal constructor, do not use directly. Use {@link DbUserManagerFactory} instead. */ public DbUserManager(DataSource dataSource, String selectAllStmt, String selectUserStmt, String insertUserStmt, String updateUserStmt, String deleteUserStmt, String authenticateStmt, String isAdminStmt, PasswordEncryptor passwordEncryptor, String adminName) { super(adminName, passwordEncryptor); this.dataSource = dataSource; this.selectAllStmt = selectAllStmt; this.selectUserStmt = selectUserStmt; this.insertUserStmt = insertUserStmt; this.updateUserStmt = updateUserStmt; this.deleteUserStmt = deleteUserStmt; this.authenticateStmt = authenticateStmt; this.isAdminStmt = isAdminStmt; try { // test the connection createConnection(); LOG.info("Database connection opened."); } catch (SQLException ex) { LOG.error("Failed to open connection to user database", ex); throw new FtpServerConfigurationException( "Failed to open connection to user database", ex); } } /** * Retrive the data source used by the user manager * * @return The current data source */ public DataSource getDataSource() { return dataSource; } /** * Set the data source to be used by the user manager * * @param dataSource * The data source to use */ public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } /** * Get the SQL INSERT statement used to add a new user. * * @return The SQL statement */ public String getSqlUserInsert() { return insertUserStmt; } /** * Set the SQL INSERT statement used to add a new user. All the dynamic * values will be replaced during runtime. * * @param sql * The SQL statement */ public void setSqlUserInsert(String sql) { insertUserStmt = sql; } /** * Get the SQL DELETE statement used to delete an existing user. * * @return The SQL statement */ public String getSqlUserDelete() { return deleteUserStmt; } /** * Set the SQL DELETE statement used to delete an existing user. All the * dynamic values will be replaced during runtime. * * @param sql * The SQL statement */ public void setSqlUserDelete(String sql) { deleteUserStmt = sql; } /** * Get the SQL UPDATE statement used to update an existing user. * * @return The SQL statement */ public String getSqlUserUpdate() { return updateUserStmt; } /** * Set the SQL UPDATE statement used to update an existing user. All the * dynamic values will be replaced during runtime. * * @param sql * The SQL statement */ public void setSqlUserUpdate(String sql) { updateUserStmt = sql; } /** * Get the SQL SELECT statement used to select an existing user. * * @return The SQL statement */ public String getSqlUserSelect() { return selectUserStmt; } /** * Set the SQL SELECT statement used to select an existing user. All the * dynamic values will be replaced during runtime. * * @param sql * The SQL statement */ public void setSqlUserSelect(String sql) { selectUserStmt = sql; } /** * Get the SQL SELECT statement used to select all user ids. * * @return The SQL statement */ public String getSqlUserSelectAll() { return selectAllStmt; } /** * Set the SQL SELECT statement used to select all user ids. All the dynamic * values will be replaced during runtime. * * @param sql * The SQL statement */ public void setSqlUserSelectAll(String sql) { selectAllStmt = sql; } /** * Get the SQL SELECT statement used to authenticate user. * * @return The SQL statement */ public String getSqlUserAuthenticate() { return authenticateStmt; } /** * Set the SQL SELECT statement used to authenticate user. All the dynamic * values will be replaced during runtime. * * @param sql * The SQL statement */ public void setSqlUserAuthenticate(String sql) { authenticateStmt = sql; } /** * Get the SQL SELECT statement used to find whether an user is admin or * not. * * @return The SQL statement */ public String getSqlUserAdmin() { return isAdminStmt; } /** * Set the SQL SELECT statement used to find whether an user is admin or * not. All the dynamic values will be replaced during runtime. * * @param sql * The SQL statement */ public void setSqlUserAdmin(String sql) { isAdminStmt = sql; } /** * @return true if user with this login is administrator */ public boolean isAdmin(String login) throws FtpException { // check input if (login == null) { return false; } Statement stmt = null; ResultSet rs = null; try { // create the sql query HashMap<String, Object> map = new HashMap<String, Object>(); map.put(ATTR_LOGIN, escapeString(login)); String sql = StringUtils.replaceString(isAdminStmt, map); LOG.info(sql); // execute query stmt = createConnection().createStatement(); rs = stmt.executeQuery(sql); return rs.next(); } catch (SQLException ex) { LOG.error("DbUserManager.isAdmin()", ex); throw new FtpException("DbUserManager.isAdmin()", ex); } finally { closeQuitely(rs); closeQuitely(stmt); } } /** * Open connection to database. */ protected Connection createConnection() throws SQLException { Connection connection = dataSource.getConnection(); connection.setAutoCommit(true); return connection; } /** * Delete user. Delete the row from the table. */ public void delete(String name) throws FtpException { // create sql query HashMap<String, Object> map = new HashMap<String, Object>(); map.put(ATTR_LOGIN, escapeString(name)); String sql = StringUtils.replaceString(deleteUserStmt, map); LOG.info(sql); // execute query Statement stmt = null; try { stmt = createConnection().createStatement(); stmt.executeUpdate(sql); } catch (SQLException ex) { LOG.error("DbUserManager.delete()", ex); throw new FtpException("DbUserManager.delete()", ex); } finally { closeQuitely(stmt); } } /** * Save user. If new insert a new row, else update the existing row. */ public void save(User user) throws FtpException { // null value check if (user.getName() == null) { throw new NullPointerException("User name is null."); } Statement stmt = null; try { // create sql query HashMap<String, Object> map = new HashMap<String, Object>(); map.put(ATTR_LOGIN, escapeString(user.getName()));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?