avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver ConnectionService.java FtpConfig.java FtpServerImpl.java FtpServerImpl.xinfo FtpStatistics.java
Date Wed, 06 Mar 2002 13:55:17 GMT
rana_b      02/03/06 05:55:17

  Added:       ftpserver/src/java/org/apache/avalon/ftpserver
                        ConnectionService.java FtpConfig.java
                        FtpServerImpl.java FtpServerImpl.xinfo
                        FtpStatistics.java
  Log:
  second stage of refactoring
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/ConnectionService.java
  
  Index: ConnectionService.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  
  package org.apache.avalon.ftpserver;
  
  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 org.apache.avalon.ftpserver.util.Message;
  import org.apache.avalon.ftpserver.interfaces.FtpConnectionObserver;
  import org.apache.avalon.ftpserver.interfaces.SpyConnectionInterface;
  import org.apache.avalon.ftpserver.usermanager.User;
  import org.apache.avalon.ftpserver.usermanager.UserManagerInterface;
  
  /**
   * Ftp user 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 {
          UserManagerInterface userManager = mConfig.getUserManager();
          
          // create admin user
          if(!userManager.doesExist(FtpUser.ADMIN)) {
              mConfig.getLogger().info("Creating user " + FtpUser.ADMIN);
              User adminUser = new User();
              adminUser.setName(FtpUser.ADMIN);
              adminUser.setPassword(FtpUser.ADMIN);
              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(); ) {
                  FtpConnection conObj = (FtpConnection)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(); ) {
                  FtpConnection conObj = (FtpConnection)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
          UserManagerInterface 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) {
          FtpConnection 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
          UserManagerInterface 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 FtpConnection 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) {
          FtpConnection con = getConnection(sessId);
          if (con != null) {
              con.setSpyObject(spy);
          }
      }
      
      /**
       * Get connection object
       */
      public FtpConnection getConnection(String sessId) {
          FtpConnection con = null;
          synchronized(mConList) {
              for(Iterator conIt=mConList.iterator(); conIt.hasNext(); ) {
                  FtpConnection conObj = (FtpConnection)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(); ) {
                  FtpConnection conObj = (FtpConnection)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(); ) {
                  FtpConnection con = (FtpConnection)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
          UserManagerInterface userManager = mConfig.getUserManager();
          try {
              userManager.reload();
          }
          catch(Exception ex) {
              mConfig.getLogger().error("ConnectionService.timerTask()", ex);
          }
      }
      
      /**
       * Close this user manager. If logs out all the connected
       * users and stops the cleaner thread.
       */
      public void close() {
          
          // close all connections
          if (mConList != null) {
              closeAllConnections();
              mConList = null;
          }
          
          // stop timer
          if (mTimer != null) {
              mTimer.cancel();
              mTimer = null;
          }
      }
      
      
      /**
       * Last defense to logout all the connected users and stop 
       * the timer.
       */
      protected void finalize() throws Throwable {
          close();
          super.finalize();
      }      
  }
  
  
  
  1.10      +193 -216  jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpConfig.java
  
  
  
  
  1.10      +83 -108   jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpServerImpl.java
  
  
  
  
  1.6       +11 -7     jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpServerImpl.xinfo
  
  
  
  
  1.5       +97 -88    jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpStatistics.java
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message