KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > usermanager > xstream > XStreamUser


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.usermanager.xstream;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import net.sf.drftpd.master.usermanager.AbstractUser;
26 import net.sf.drftpd.master.usermanager.PlainTextPasswordUser;
27 import net.sf.drftpd.master.usermanager.UnixPassword;
28 import net.sf.drftpd.master.usermanager.UserExistsException;
29 import net.sf.drftpd.master.usermanager.UserFileException;
30 import net.sf.drftpd.util.Crypt;
31 import net.sf.drftpd.util.SafeFileWriter;
32
33 import org.apache.log4j.Logger;
34
35 import com.thoughtworks.xstream.XStream;
36 import com.thoughtworks.xstream.io.xml.DomDriver;
37
38 /**
39  * @author mog
40  * @version $Id: XStreamUser.java,v 1.4 2004/06/01 15:40:31 mog Exp $
41  */

42 public class XStreamUser
43     extends AbstractUser
44     implements PlainTextPasswordUser, UnixPassword {
45     private String JavaDoc unixPassword;
46     private String JavaDoc password;
47     transient XStreamUserManager usermanager;
48     private transient boolean purged;
49
50     public XStreamUser(XStreamUserManager usermanager, String JavaDoc username) {
51         super(username);
52         created = System.currentTimeMillis();
53         this.usermanager = usermanager;
54     }
55
56     public boolean checkPassword(String JavaDoc password) {
57         if (this.password == null) {
58             if (this.unixPassword == null)
59                 throw new IllegalStateException JavaDoc("no password set");
60             if (this
61                 .unixPassword
62                 .equals(
63                     Crypt.crypt(
64                         this.unixPassword.substring(0, 2),
65                         password))) {
66                 setPassword(password);
67                 return true;
68             }
69             return false;
70         }
71         return this.password.equals(password);
72     }
73
74     public void setPassword(String JavaDoc password) {
75         this.unixPassword = null;
76         this.password = password;
77     }
78
79     public String JavaDoc getPassword() {
80         return this.password;
81     }
82
83     public void rename(String JavaDoc username)
84         throws UserExistsException, UserFileException
85     {
86         usermanager.rename(this, username); // throws ObjectExistsException
87
usermanager.getUserFile(this.getUsername()).delete();
88         this.username = username;
89         commit(); // throws IOException
90
}
91
92     public void commit() throws UserFileException {
93         if (this.purged)
94             return;
95
96         try {
97                         XStream xst = new XStream(new DomDriver());
98             SafeFileWriter out = new SafeFileWriter(
99                             usermanager.getUserFile(this.getUsername())
100                         );
101             try {
102                 out.write(xst.toXML(this));
103             } finally {
104                 out.close();
105             }
106             Logger.getLogger(XStreamUser.class).debug("wrote "+getUsername());
107         } catch (IOException JavaDoc ex) {
108             throw new UserFileException(
109                 "Error writing userfile for "
110                     + this.getUsername()
111                     + ": "
112                     + ex.getMessage(), ex);
113         }
114     }
115
116     public void purge() {
117         this.purged = true;
118         usermanager.remove(this);
119         File JavaDoc userfile = usermanager.getUserFile(this.getUsername());
120         userfile.delete();
121     }
122
123     protected void finalize() throws Throwable JavaDoc {
124         this.commit();
125     }
126
127     public void update() {
128         //an update was made, but commit() should be called from all places so we don't need to do anything.
129
//if we do, make sure it's implemented in all set and update methods in AbstractUser
130
}
131     public String JavaDoc getUnixPassword() {
132         return unixPassword;
133     }
134     public void setUnixPassword(String JavaDoc password) {
135         this.password = null;
136         this.unixPassword = password;
137     }
138     private void readObject(ObjectInputStream JavaDoc stream)
139         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
140         stream.defaultReadObject();
141         if(groups == null) groups = new ArrayList JavaDoc();
142         if(ipMasks == null) ipMasks = new ArrayList JavaDoc();
143     }
144
145 }
146
Popular Tags