741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
27 |
// |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
33 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
38 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// 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 |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
47 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
51 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## 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 |
* <B>About this class:</B> <BR> |
741 |
10 Oct 06 |
olle |
* This class represents a database with all user accounts. |
1652 |
22 May 07 |
gregory |
61 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @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 |
// private final static String |
1652 |
22 May 07 |
gregory |
// s_userFolder=FTPServerController.s_userFolder; |
1652 |
22 May 07 |
gregory |
// private final static String |
1652 |
22 May 07 |
gregory |
// 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 |
* 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 |
* Get the user login name. Not part of the original Xerver code. |
790 |
23 Oct 06 |
olle |
85 |
* |
790 |
23 Oct 06 |
olle |
* @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 |
* Set the user login name. Not part of the original Xerver code. |
790 |
23 Oct 06 |
olle |
96 |
* |
2142 |
16 Oct 07 |
gregory |
* @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 |
// *** 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 |
* Alternative constructor that sets the user login name. Not part of the |
1652 |
22 May 07 |
gregory |
* original Xerver code. |
790 |
23 Oct 06 |
olle |
116 |
* |
2142 |
16 Oct 07 |
gregory |
* @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 |
// *** 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 |
* Update the information stored in this class from file (in case an user |
1652 |
22 May 07 |
gregory |
* 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 |
// *** 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 |
* Get logged-in user data from core database instead of getting |
2142 |
16 Oct 07 |
gregory |
* user information from user configuration files. As the log-in |
2142 |
16 Oct 07 |
gregory |
* control now is performed outside the FTP server, we only need to |
2142 |
16 Oct 07 |
gregory |
* add the current user to the FTP user database. |
790 |
23 Oct 06 |
olle |
144 |
*/ |
1652 |
22 May 07 |
gregory |
// File f_userFolder=new File(s_userFolder); |
1652 |
22 May 07 |
gregory |
// as_allUsers=f_userFolder.list(new DatabaseFileFilter()); //Only |
1652 |
22 May 07 |
gregory |
// get files with ".dat" |
790 |
23 Oct 06 |
olle |
148 |
/* |
790 |
23 Oct 06 |
olle |
* 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 |
* 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 |
// "If the initial capacity is greater than the maximum number of |
1652 |
22 May 07 |
gregory |
// entries divided by the load factor, no rehash operations will |
1652 |
22 May 07 |
gregory |
// ever occur." |
1652 |
22 May 07 |
gregory |
// The line above says that if "argument below" > maxEntries/0.75 |
1652 |
22 May 07 |
gregory |
// ==> No (expensive) rehash() will occur, so we set [ "argument |
1652 |
22 May 07 |
gregory |
// 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 |
// TreeMap(); |
1652 |
22 May 07 |
gregory |
// Note HashMap is NOT synchronized, for this reason... |
1652 |
22 May 07 |
gregory |
// If we want add a new entry at any moment to the database, |
1652 |
22 May 07 |
gregory |
// create a completely new UserDatabase (and a new HashMap), |
1652 |
22 May 07 |
gregory |
// instead of make just an "put()" to add a new value. |
1652 |
22 May 07 |
gregory |
// Because if we make an "put()" and a "get()" at the same time |
1652 |
22 May 07 |
gregory |
// we don't know what we end up with. |
1652 |
22 May 07 |
gregory |
// Createing a new UserDatabase is indeed costly but it will only |
1652 |
22 May 07 |
gregory |
// occur when admin change settings to the server and it's |
1652 |
22 May 07 |
gregory |
// a safe way to make changes to this user-database. |
2142 |
16 Oct 07 |
gregory |
// 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 |
// tmpFilename=as_allUsers[i]; |
1652 |
22 May 07 |
gregory |
// if (tmpFilename.endsWith(s_userDataExtension)) |
1652 |
22 May 07 |
gregory |
181 |
// { |
1652 |
22 May 07 |
gregory |
// 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 |
* Get the user data for the user <CODE>s_username</CODE>, represented as |
1652 |
22 May 07 |
gregory |
* 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 |
* 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 |
} |