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

📄 connectionservice.java

📁 Ftp服务1.0
💻 JAVA
字号:
package ranab.server.ftp;

import java.io.File;
import java.util.List;
import java.util.Vector;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import ranab.util.Message;
import ranab.server.ftp.usermanager.User;
import ranab.server.ftp.usermanager.UserManager;

/**
 * Ftp connection service class. It tracks all ftp connections.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class ConnectionService {
    
    private FtpConnectionObserver mObserver;
    private FtpConfig mConfig;                 
    private Timer mTimer;                      
    private Vector mConList;               
    
        
    /**
     * Constructor. Start scheduler job.
     */
    public ConnectionService(FtpConfig cfg) throws Exception {     
        mConfig = cfg;
        mConList = new Vector();
        
        // default users creation
        createDefaultUsers();
        
        // set timer to remove inactive users and load data
        mTimer = new Timer();
        TimerTask timerTask = new TimerTask() {
            public void run() {
                timerTask();
            }
        };
        mTimer.schedule(timerTask, 0, mConfig.getSchedulerInterval()*1000);
    }
    
   /**
    * Create default users (admin/anonymous) if necessary
    */
   private void createDefaultUsers() throws Exception {
        UserManager userManager = mConfig.getUserManager();
        
        // create admin user
        String adminName = userManager.getAdminName();
        if(!userManager.doesExist(adminName)) {
            mConfig.getLogger().info("Creating user " + adminName);
            User adminUser = new User();
            adminUser.setName(adminName);
            adminUser.setPassword(adminName);
            adminUser.setEnabled(true);
            adminUser.getVirtualDirectory().setWritePermission(true);
            adminUser.setMaxUploadRate(0);
            adminUser.setMaxDownloadRate(0);
            adminUser.getVirtualDirectory().setRootDirectory(mConfig.getDefaultRoot());
            adminUser.setMaxIdleTime(mConfig.getDefaultIdleTime());
            userManager.save(adminUser);
        }
        
        // create anonymous user
        if(!userManager.doesExist(FtpUser.ANONYMOUS)) {
            mConfig.getLogger().info("Creating user " + FtpUser.ANONYMOUS);
            User anonUser = new User();
            anonUser.setName(FtpUser.ANONYMOUS);
            anonUser.setPassword("");
            anonUser.setEnabled(true);
            anonUser.getVirtualDirectory().setWritePermission(false);
            anonUser.setMaxUploadRate(4800);
            anonUser.setMaxDownloadRate(4800);
            anonUser.getVirtualDirectory().setRootDirectory(mConfig.getDefaultRoot());
            anonUser.setMaxIdleTime(mConfig.getDefaultIdleTime());
            userManager.save(anonUser);
        }
    } 
        
    /**
     * It returns a list of all the currently connected users.
     */
    public List getAllUsers() {
        List userList = new ArrayList();
        synchronized(mConList) {
            for(Iterator conIt=mConList.iterator(); conIt.hasNext(); ) {
                BaseFtpConnection conObj = (BaseFtpConnection)conIt.next();
                if (conObj != null) {
                    userList.add(conObj.getUser());
                }
            }
        }
        return userList;
    }
    
    /**
     * Set user manager observer.
     */
    public void setObserver(FtpConnectionObserver obsr ) {
        mObserver = obsr;
        synchronized(mConList) {
            for(Iterator conIt=mConList.iterator(); conIt.hasNext(); ) {
                BaseFtpConnection conObj = (BaseFtpConnection)conIt.next();
                if (conObj != null) {
                    conObj.setObserver(mObserver);
                }
            }
        }
    }

    /**
     * Get the observer.
     */
    public FtpConnectionObserver getObserver() {
        return mObserver;
    }
     
    /**
     * User login method. If successfull, populates the user object. 
     */
    public boolean login(final FtpUser thisUser) {
       
        // already logged in
        if(thisUser.hasLoggedIn()) {
            return true;
        }
        
        // get name and password
        String user = thisUser.getName();
        String password = thisUser.getPassword();
        if( (user == null) || (password == null) ) {
            return false;
        }               

        // authenticate user
        UserManager userManager = mConfig.getUserManager();
        boolean bAnonymous = thisUser.getIsAnonymous();
        if ( !(bAnonymous || userManager.authenticate(user, password)) ) {
            mConfig.getLogger().warn("Authentication failed - " + user);
            return false;
        }
       
        // populate user properties 
        if (!populateProperties(thisUser, user)){
            return false;
        }
        
        // user enable check
        if(!thisUser.getEnabled()) {
            return false;
        }
        
        // connection limit check
        if (!checkConnection(thisUser)){
            return false;
        }
        
        thisUser.login();
        thisUser.setPassword(null);
        mConfig.getLogger().info("User login - " + thisUser.getClientAddress().getHostAddress() + " - " + thisUser.getName());
        
        // update global statistics
        mConfig.getStatistics().setLogin(thisUser.getIsAnonymous());
        return true;
    }

    /**
     * Close ftp connection for this session id.
     */
    public void closeConnection(final String sessId) {
        BaseFtpConnection con = null;
        synchronized(mConList) { 
            con = getConnection(sessId);
            if (con != null) {
                mConList.remove(con);
            }
        }
        
        // close connection
        if (con != null) {
            
            // logout notification
            final FtpUser thisUser = con.getUser();
            if (thisUser.hasLoggedIn()) {
                mConfig.getStatistics().setLogout(thisUser.getIsAnonymous());
            }
            
            // close socket
            con.stop();
            
            // send message
            Message msg = new Message() {
                public void execute() {
                    FtpConnectionObserver observer = mObserver;
                    if(observer != null) {
                        observer.removeConnection(thisUser); 
                    }
                }
            };    
            mConfig.getMessageQueue().add(msg);
            mConfig.getStatistics().setCloseConnection();
        }
    }
    
     
    /**
     * Close all - close all the connections.
     */
    public void closeAllConnections() {
        List allUsers = getAllUsers();
        for( Iterator userIt = allUsers.iterator(); userIt.hasNext(); ) {
            FtpUser user = (FtpUser)userIt.next();
            closeConnection(user.getSessionId());
        }  
    }
    
    /**
     * Populate user properties
     */
    private boolean populateProperties(FtpUser thisUser, String user) {
        
        // get the existing user
        UserManager userManager = mConfig.getUserManager();
        User existUser = userManager.getUserByName(user);
        if(existUser == null) {
            return false;
        }
        
        // map properties
        thisUser.getVirtualDirectory().setRootDirectory(new File(existUser.getVirtualDirectory().getRootDirectory()));
        thisUser.setEnabled(existUser.getEnabled());
        thisUser.getVirtualDirectory().setWritePermission(existUser.getVirtualDirectory().getWritePermission());
        thisUser.setMaxIdleTime(existUser.getMaxIdleTime());
        thisUser.setMaxUploadRate(existUser.getMaxUploadRate());
        thisUser.setMaxDownloadRate(existUser.getMaxDownloadRate());
        return true;
    }
        
    /**
     * Connection limit check.
     */
    private boolean checkConnection(FtpUser thisUser) {
        int maxLogins = mConfig.getMaxConnections();
        int maxAnonLogins = mConfig.getMaxAnonymousLogins();
        int anonNbr = mConfig.getStatistics().getAnonLoginNbr();
        int totalNbr = mConfig.getStatistics().getLoginNbr();

        // final check
        if(thisUser.getIsAnonymous()) {
            if(!mConfig.isAnonymousLoginAllowed()) {
               return false;
            }            
            if( (anonNbr>=maxAnonLogins) || (totalNbr>=maxLogins) ) {
               return false;
            }
            mConfig.getLogger().info("Anonymous connection - " + thisUser.getClientAddress().getHostAddress() + " - " + thisUser.getPassword());
        }
        else {
            if(totalNbr>=maxLogins) {
                return false;
            }   
        }
        return true;    
    }
    
    /**
     * New connection has been established - not yet logged-in.
     */
    public void newConnection(final BaseFtpConnection newCon) {
         
        // null user - ignore
        if (newCon == null) {
            return;
        }
        
        final FtpUser newUser = newCon.getUser();
        
        mConList.add(newCon);
        newUser.setMaxIdleTime(mConfig.getDefaultIdleTime());
        newUser.getVirtualDirectory().setRootDirectory(mConfig.getDefaultRoot());
        newCon.setObserver(mObserver);
        mConfig.getLogger().info("New connection from " + newUser.getClientAddress().getHostAddress());
        
        // notify observer about a new connection
        final FtpConnectionObserver observer = mObserver;
        if (observer != null) {
            Message msg = new Message() {
                public void execute() {
                    observer.newConnection(newUser);
                }
            };
            mConfig.getMessageQueue().add(msg);                
        }
                
        // update global statistics
        mConfig.getStatistics().setOpenConnection();
    } 
    
    
    /**
     * Set connection spy object
     */
    public void setSpyObject(String sessId, SpyConnectionInterface spy) {
        BaseFtpConnection con = getConnection(sessId);
        if (con != null) {
            con.setSpyObject(spy);
        }
    }
    
    /**
     * Get connection object
     */
    public BaseFtpConnection getConnection(String sessId) {
        BaseFtpConnection con = null;
        synchronized(mConList) {
            for(Iterator conIt=mConList.iterator(); conIt.hasNext(); ) {
                BaseFtpConnection conObj = (BaseFtpConnection)conIt.next();
                if (conObj != null) {
                    if ( conObj.getUser().getSessionId().equals(sessId) ) {
                        con = conObj;
                        break;
                    }
                }
            }
        }
        return con;
    }
    
    /**
     * Reset all spy objects
     */
    public void resetAllSpyObjects() {
        synchronized(mConList) {
            for(Iterator conIt=mConList.iterator(); conIt.hasNext(); ) {
                BaseFtpConnection conObj = (BaseFtpConnection)conIt.next();
                if (conObj != null) {
                    conObj.setSpyObject(null);
                }
            }
        }
    }
   
    /**
     * Timer thread will call this method periodically to
     * close inactice connections and load user information.
     */
    public void timerTask() {
    
        // get inactive user list
        ArrayList inactiveUserList = new ArrayList();
        long currTime = System.currentTimeMillis();
        synchronized(mConList) {
            for( Iterator conIt=mConList.iterator(); conIt.hasNext(); ) {
                BaseFtpConnection con = (BaseFtpConnection)conIt.next();
                if (con != null) {
                    FtpUser user = con.getUser();
                    if (!user.isActive(currTime)) {
                        inactiveUserList.add(user);
                    }
                }
            }
        }
        
        // remove inactive users
        for( Iterator userIt=inactiveUserList.iterator(); userIt.hasNext(); ) {
            FtpUser user = (FtpUser)userIt.next();
            mConfig.getLogger().info("Removing idle user " + user);
            closeConnection(user.getSessionId());
        }
        
        // reload user data
        UserManager userManager = mConfig.getUserManager();
        try {
            userManager.reload();
        }
        catch(Exception ex) {
            mConfig.getLogger().error(ex);
        }
    }
    
    /**
     * Dispose connection service. If logs out all the connected
     * users and stops the cleaner thread.
     */
    public void dispose() {
        
        // close all connections
        if (mConList != null) {
            closeAllConnections();
            mConList = null;
        }
        
        // stop timer
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
    
}    

⌨️ 快捷键说明

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