client/ftpd/src/se/lu/thep/coreftpd/ftp_server/UserDatabase.java

Code
Comments
Other
Rev Date Author Line
741 10 Oct 06 olle 1 /*
1652 22 May 07 gregory 2  $Id$
741 10 Oct 06 olle 3
1916 31 Aug 07 jari 4  Copyright (C) 2006 Olle Mansson
1916 31 Aug 07 jari 5  Copyright (C) 2007 Gregory Vincic
741 10 Oct 06 olle 6
1652 22 May 07 gregory 7  This file is part of Proteios.
1652 22 May 07 gregory 8  Available at http://www.proteios.org/
741 10 Oct 06 olle 9
1652 22 May 07 gregory 10  Proteios is free software; you can redistribute it and/or modify it
1652 22 May 07 gregory 11  under the terms of the GNU General Public License as published by
1652 22 May 07 gregory 12  the Free Software Foundation; either version 2 of the License, or
1652 22 May 07 gregory 13  (at your option) any later version.
741 10 Oct 06 olle 14
1652 22 May 07 gregory 15  Proteios is distributed in the hope that it will be useful, but
1652 22 May 07 gregory 16  WITHOUT ANY WARRANTY; without even the implied warranty of
1652 22 May 07 gregory 17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1652 22 May 07 gregory 18  General Public License for more details.
741 10 Oct 06 olle 19
1652 22 May 07 gregory 20  You should have received a copy of the GNU General Public License
1652 22 May 07 gregory 21  along with this program; if not, write to the Free Software
1652 22 May 07 gregory 22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1652 22 May 07 gregory 23  02111-1307, USA.
1652 22 May 07 gregory 24  */
741 10 Oct 06 olle 25 //  Xerver Free Web Server
741 10 Oct 06 olle 26 //  Copyright (C) 2002-2005 Omid Rouhani
741 10 Oct 06 olle 27 //
741 10 Oct 06 olle 28 //
741 10 Oct 06 olle 29 //  This program is free software; you can redistribute it and/or
741 10 Oct 06 olle 30 //  modify it under the terms of the GNU General Public License
741 10 Oct 06 olle 31 //  as published by the Free Software Foundation; either version 2
741 10 Oct 06 olle 32 //  of the License, or (at your option) any later version.
741 10 Oct 06 olle 33 //
741 10 Oct 06 olle 34 //  This program is distributed in the hope that it will be useful,
741 10 Oct 06 olle 35 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
741 10 Oct 06 olle 36 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
741 10 Oct 06 olle 37 //  GNU General Public License for more details.
741 10 Oct 06 olle 38 //
741 10 Oct 06 olle 39 //  You should have received a copy of the GNU General Public License
741 10 Oct 06 olle 40 //  along with this program; if not, write to the Free Software
741 10 Oct 06 olle 41 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
741 10 Oct 06 olle 42 //
741 10 Oct 06 olle 43 //
741 10 Oct 06 olle 44 //  #############################################################
741 10 Oct 06 olle 45 //  ##  YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT:          ##
741 10 Oct 06 olle 46 //  ##  HTTP://WWW.JAVASCRIPT.NU/XERVER/                       ##
741 10 Oct 06 olle 47 //  ##                                                         ##
741 10 Oct 06 olle 48 //  ##  IF YOUR SOFTWARE IS NOT RELEASED UNDER THE             ##
741 10 Oct 06 olle 49 //  ##  GNU GENERAL PUBLIC LICENSE (GPL),                      ##
741 10 Oct 06 olle 50 //  ##  PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!!   ##
741 10 Oct 06 olle 51 //  ##                                                         ##
741 10 Oct 06 olle 52 //  ##  FOR FULL LICENSE, PLEASE READ "XERVER LICENSE".        ##
741 10 Oct 06 olle 53 //  #############################################################
1652 22 May 07 gregory 54 package se.lu.thep.coreftpd.ftp_server;
741 10 Oct 06 olle 55
741 10 Oct 06 olle 56 import java.util.HashMap;
741 10 Oct 06 olle 57
741 10 Oct 06 olle 58 /**
1652 22 May 07 gregory 59  * <B>About this class:</B> <BR>
741 10 Oct 06 olle 60  * This class represents a database with all user accounts.
1652 22 May 07 gregory 61  * 
741 10 Oct 06 olle 62  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 63  * @version 1.0
741 10 Oct 06 olle 64  */
2142 16 Oct 07 gregory 65 final public class UserDatabase
2142 16 Oct 07 gregory 66 {
1652 22 May 07 gregory 67   final static private boolean b_showErrors = false;
1652 22 May 07 gregory 68   // private final static String
1652 22 May 07 gregory 69   // s_userFolder=FTPServerController.s_userFolder;
1652 22 May 07 gregory 70   // private final static String
1652 22 May 07 gregory 71   // s_userDataExtension=FTPServerController.s_userDataExtension;
1652 22 May 07 gregory 72   private String[] as_allUsers;
741 10 Oct 06 olle 73   private int i_numberOfUsers;
2142 16 Oct 07 gregory 74   private HashMap<String, UserData> hm_dataBase;
790 23 Oct 06 olle 75   private String userLogin = new String("");
790 23 Oct 06 olle 76   /**
790 23 Oct 06 olle 77    * Logger used. Used to log specific events.
790 23 Oct 06 olle 78    */
790 23 Oct 06 olle 79   private static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
2142 16 Oct 07 gregory 80     .getLogger("se.lu.thep.coreftpd.ftp_server");
790 23 Oct 06 olle 81
2142 16 Oct 07 gregory 82
790 23 Oct 06 olle 83   /**
1652 22 May 07 gregory 84    * Get the user login name. Not part of the original Xerver code.
790 23 Oct 06 olle 85    * 
790 23 Oct 06 olle 86    * @return String the user login name.
790 23 Oct 06 olle 87    */
2142 16 Oct 07 gregory 88   public String getUserLogin()
2142 16 Oct 07 gregory 89   {
790 23 Oct 06 olle 90     return this.userLogin;
790 23 Oct 06 olle 91   }
1652 22 May 07 gregory 92
2142 16 Oct 07 gregory 93
790 23 Oct 06 olle 94   /**
1652 22 May 07 gregory 95    * Set the user login name. Not part of the original Xerver code.
790 23 Oct 06 olle 96    * 
2142 16 Oct 07 gregory 97    * @param userLogin String the user login name to set.
790 23 Oct 06 olle 98    */
2142 16 Oct 07 gregory 99   public void setUserLogin(String userLogin)
2142 16 Oct 07 gregory 100   {
790 23 Oct 06 olle 101     this.userLogin = userLogin;
790 23 Oct 06 olle 102   }
1652 22 May 07 gregory 103
2142 16 Oct 07 gregory 104
2142 16 Oct 07 gregory 105   public UserDatabase()
2142 16 Oct 07 gregory 106   {
790 23 Oct 06 olle 107     // *** Debug Info
790 23 Oct 06 olle 108     log.info("UserDatabase::Userdatabase(): Start");
741 10 Oct 06 olle 109     updateDatabase();
741 10 Oct 06 olle 110   }
741 10 Oct 06 olle 111
2142 16 Oct 07 gregory 112
741 10 Oct 06 olle 113   /**
1652 22 May 07 gregory 114    * Alternative constructor that sets the user login name. Not part of the
1652 22 May 07 gregory 115    * original Xerver code.
790 23 Oct 06 olle 116    * 
2142 16 Oct 07 gregory 117    * @param userLogin String the user login name to add to the FTP database.
790 23 Oct 06 olle 118    */
2142 16 Oct 07 gregory 119   public UserDatabase(String userLogin)
2142 16 Oct 07 gregory 120   {
790 23 Oct 06 olle 121     // *** Debug Info
1652 22 May 07 gregory 122     log
2142 16 Oct 07 gregory 123       .info("UserDatabase::Userdatabase(String userLogin): Start - userLogin = \"" + userLogin + "\"");
790 23 Oct 06 olle 124     setUserLogin(userLogin);
790 23 Oct 06 olle 125     updateDatabase();
790 23 Oct 06 olle 126   }
790 23 Oct 06 olle 127
2142 16 Oct 07 gregory 128
790 23 Oct 06 olle 129   /**
1652 22 May 07 gregory 130    * Update the information stored in this class from file (in case an user
1652 22 May 07 gregory 131    * data file has been changed).
1652 22 May 07 gregory 132    */
2142 16 Oct 07 gregory 133   public void updateDatabase()
2142 16 Oct 07 gregory 134   {
790 23 Oct 06 olle 135     // *** Debug Info
790 23 Oct 06 olle 136     log.info("UserDatabase::updateDatabase(): Start");
2142 16 Oct 07 gregory 137     try
2142 16 Oct 07 gregory 138     {
790 23 Oct 06 olle 139       /*
1652 22 May 07 gregory 140        * Get logged-in user data from core database instead of getting
2142 16 Oct 07 gregory 141        * user information from user configuration files. As the log-in
2142 16 Oct 07 gregory 142        * control now is performed outside the FTP server, we only need to
2142 16 Oct 07 gregory 143        * add the current user to the FTP user database.
790 23 Oct 06 olle 144        */
1652 22 May 07 gregory 145       // File f_userFolder=new File(s_userFolder);
1652 22 May 07 gregory 146       // as_allUsers=f_userFolder.list(new DatabaseFileFilter()); //Only
1652 22 May 07 gregory 147       // get files with ".dat"
790 23 Oct 06 olle 148       /*
790 23 Oct 06 olle 149        * Add the logged-in core user to the FTP user database.
790 23 Oct 06 olle 150        */
2142 16 Oct 07 gregory 151       log
2142 16 Oct 07 gregory 152         .info("UserDatabase::updateDatabase(): getUserLogin() = \"" + getUserLogin() + "\"");
790 23 Oct 06 olle 153       as_allUsers = new String[1];
790 23 Oct 06 olle 154       as_allUsers[0] = getUserLogin();
790 23 Oct 06 olle 155       /*
790 23 Oct 06 olle 156        * End of changed code.
790 23 Oct 06 olle 157        */
1652 22 May 07 gregory 158       i_numberOfUsers = as_allUsers.length;
1652 22 May 07 gregory 159       // "If the initial capacity is greater than the maximum number of
1652 22 May 07 gregory 160       // entries divided by the load factor, no rehash operations will
1652 22 May 07 gregory 161       // ever occur."
1652 22 May 07 gregory 162       // The line above says that if "argument below" > maxEntries/0.75
1652 22 May 07 gregory 163       // ==> No (expensive) rehash() will occur, so we set [ "argument
1652 22 May 07 gregory 164       // below"=initial capacity >> as_allUsers.length() ]
2142 16 Oct 07 gregory 165       hm_dataBase = new HashMap<String, UserData>(i_numberOfUsers * 2 + 2);// new
2142 16 Oct 07 gregory 166       // TreeMap();
1652 22 May 07 gregory 167       // Note HashMap is NOT synchronized, for this reason...
1652 22 May 07 gregory 168       // If we want add a new entry at any moment to the database,
1652 22 May 07 gregory 169       // create a completely new UserDatabase (and a new HashMap),
1652 22 May 07 gregory 170       // instead of make just an "put()" to add a new value.
1652 22 May 07 gregory 171       // Because if we make an "put()" and a "get()" at the same time
1652 22 May 07 gregory 172       // we don't know what we end up with.
1652 22 May 07 gregory 173       // Createing a new UserDatabase is indeed costly but it will only
1652 22 May 07 gregory 174       // occur when admin change settings to the server and it's
1652 22 May 07 gregory 175       // a safe way to make changes to this user-database.
2142 16 Oct 07 gregory 176       // String tmpUsername;// , tmpFilename;
2142 16 Oct 07 gregory 177       for (int i = 0; i < as_allUsers.length; i++)
2142 16 Oct 07 gregory 178       {
1652 22 May 07 gregory 179         // tmpFilename=as_allUsers[i];
1652 22 May 07 gregory 180         // if (tmpFilename.endsWith(s_userDataExtension))
1652 22 May 07 gregory 181         // {
1652 22 May 07 gregory 182         // as_allUsers[i]=as_allUsers[i].substring(0,as_allUsers[i].length()-4);
1652 22 May 07 gregory 183         hm_dataBase.put(as_allUsers[i], new UserData(as_allUsers[i]));
1652 22 May 07 gregory 184         // }
741 10 Oct 06 olle 185       }
2142 16 Oct 07 gregory 186     }
2142 16 Oct 07 gregory 187     catch (Exception e)
2142 16 Oct 07 gregory 188     {
2142 16 Oct 07 gregory 189       if (b_showErrors)
2142 16 Oct 07 gregory 190       {
1652 22 May 07 gregory 191         System.out.println("An error occured @ updateDatabase: " + e);
741 10 Oct 06 olle 192         e.printStackTrace();
741 10 Oct 06 olle 193       }
741 10 Oct 06 olle 194     }
741 10 Oct 06 olle 195   }
741 10 Oct 06 olle 196
2142 16 Oct 07 gregory 197
741 10 Oct 06 olle 198   /**
1652 22 May 07 gregory 199    * Get the user data for the user <CODE>s_username</CODE>, represented as
1652 22 May 07 gregory 200    * a <CODE>UserData</CODE>.
1652 22 May 07 gregory 201    */
2142 16 Oct 07 gregory 202   public UserData getUserData(String s_username)
2142 16 Oct 07 gregory 203   {
2386 14 Nov 07 gregory 204     return hm_dataBase.get(s_username);
741 10 Oct 06 olle 205   }
741 10 Oct 06 olle 206
2142 16 Oct 07 gregory 207
741 10 Oct 06 olle 208   /**
1652 22 May 07 gregory 209    * Get all account names stored in this database.
1652 22 May 07 gregory 210    */
2142 16 Oct 07 gregory 211   public String[] getAllUsers()
2142 16 Oct 07 gregory 212   {
741 10 Oct 06 olle 213     return as_allUsers;
741 10 Oct 06 olle 214   }
741 10 Oct 06 olle 215 }