001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.usermanager.javabeans;
019:
020: import java.beans.DefaultPersistenceDelegate;
021: import java.beans.ExceptionListener;
022: import java.beans.XMLDecoder;
023: import java.beans.XMLEncoder;
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.OutputStream;
028:
029: import org.apache.log4j.Logger;
030: import org.drftpd.dynamicdata.Key;
031: import org.drftpd.usermanager.AbstractUserManager;
032: import org.drftpd.usermanager.HostMask;
033: import org.drftpd.usermanager.NoSuchUserException;
034: import org.drftpd.usermanager.User;
035: import org.drftpd.usermanager.UserFileException;
036:
037: /**
038: * This is a new usermanager that after recommendation from captain- serializes
039: * user data using javabeans XMLSerialize.
040: *
041: * @author mog
042: * @version $Id: BeanUserManager.java 1513 2006-10-13 22:41:08Z tdsoul $
043: */
044: public class BeanUserManager extends AbstractUserManager {
045:
046: private String _userpath = "users/javabeans/";
047:
048: private File _userpathFile = new File(_userpath);
049:
050: protected static final Logger logger = Logger
051: .getLogger(BeanUserManager.class);
052:
053: public BeanUserManager() throws UserFileException {
054: this (true);
055: }
056:
057: public BeanUserManager(boolean createIfNoUser)
058: throws UserFileException {
059: super ();
060: init(createIfNoUser);
061: }
062:
063: protected User createUser(String username) {
064: return new BeanUser(this , username);
065: }
066:
067: public User getUserByNameUnchecked(String username)
068: throws NoSuchUserException, UserFileException {
069: XMLDecoder xd = null;
070: try {
071: BeanUser user = (BeanUser) _users.get(username);
072:
073: if (user != null) {
074: user.reset(getGlobalContext());
075: return user;
076: }
077:
078: xd = new XMLDecoder(new FileInputStream(
079: getUserFile(username)));
080: user = (BeanUser) xd.readObject();
081:
082: user.setUserManager(this );
083: _users.put(user.getName(), user);
084: user.reset(getGlobalContext());
085: return user;
086: } catch (FileNotFoundException ex) {
087: throw new NoSuchUserException("No such user", ex);
088: } catch (Exception ex) {
089: if (ex instanceof NoSuchUserException) {
090: throw (NoSuchUserException) ex;
091: }
092: throw new UserFileException("Error loading " + username, ex);
093: } finally {
094: if (xd != null) {
095: xd.close();
096: }
097: }
098: }
099:
100: public static void main(String args[]) throws UserFileException {
101: BeanUserManager bu = new BeanUserManager();
102: User u = bu.createUser("drftpd");
103: u.commit();
104: }
105:
106: protected File getUserFile(String username) {
107: return new File(_userpath + username + ".xml");
108: }
109:
110: public XMLEncoder getXMLEncoder(OutputStream out) {
111: XMLEncoder e = new XMLEncoder(out);
112: e.setExceptionListener(new ExceptionListener() {
113: public void exceptionThrown(Exception e1) {
114: logger.error("", e1);
115: }
116: });
117: e
118: .setPersistenceDelegate(BeanUser.class,
119: new DefaultPersistenceDelegate(
120: new String[] { "name" }));
121: e.setPersistenceDelegate(Key.class,
122: new DefaultPersistenceDelegate(new String[] { "owner",
123: "key", "type" }));
124: e
125: .setPersistenceDelegate(HostMask.class,
126: new DefaultPersistenceDelegate(
127: new String[] { "mask" }));
128: return e;
129: }
130:
131: protected File getUserpathFile() {
132: return _userpathFile;
133: }
134: }
|