KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > usermanager > UserManagerConverter


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;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.log4j.BasicConfigurator;
24 import org.apache.log4j.Logger;
25
26 import net.sf.drftpd.DuplicateElementException;
27 import net.sf.drftpd.event.listeners.Trial;
28
29 /**
30  * Usage: java net.sf.drftpd.master.usermanager.UserManagerConverter net.sf.drftpd.master.usermanager.glftpd.GlftpdUserManager net.sf.drftpd.master.usermanager.JSXUserManager
31  *
32  * @author mog
33  * @version $Id: UserManagerConverter.java,v 1.9 2004/02/10 00:03:09 mog Exp $
34  */

35 public class UserManagerConverter {
36     private static final Logger logger =
37         Logger.getLogger(UserManagerConverter.class);
38
39     public static void main(String JavaDoc[] args)
40         throws
41             InstantiationException JavaDoc,
42             IllegalAccessException JavaDoc,
43             ClassNotFoundException JavaDoc,
44             IOException JavaDoc,
45             UserFileException {
46         BasicConfigurator.configure();
47         if (args.length != 2) {
48             System.out.println(
49                 "arguments: <from usermanager class> <to usermanager class>");
50             return;
51         }
52         UserManager from = (UserManager) Class.forName(args[0]).newInstance();
53         UserManager to = (UserManager) Class.forName(args[1]).newInstance();
54         logger.debug(from.getAllUsers());
55         for (Iterator JavaDoc iter = from.getAllUsers().iterator(); iter.hasNext();) {
56             User user = (User) iter.next();
57             convert(user, to.create(user.getUsername()));
58         }
59     }
60
61     public static void convert(User from, User to) throws UserFileException {
62         logger.debug("Converting " + from.getUsername());
63         for (Iterator JavaDoc iter = from.getGroups().iterator(); iter.hasNext();) {
64             try {
65                 to.addGroup((String JavaDoc) iter.next());
66             } catch (DuplicateElementException e) {
67                 logger.warn("", e);
68             }
69         }
70
71         for (Iterator JavaDoc iter = from.getIpMasks().iterator(); iter.hasNext();) {
72             String JavaDoc ipmask = (String JavaDoc) iter.next();
73             try {
74                 to.addIPMask(ipmask);
75             } catch (DuplicateElementException e) {
76                 logger.warn("", e);
77             }
78         }
79
80         to.setComment(from.getComment());
81
82         to.setCredits(from.getCredits());
83
84         to.setDeleted(from.isDeleted());
85
86         to.setGroup(from.getGroupName());
87
88         to.setGroupLeechSlots(from.getGroupLeechSlots());
89
90         to.setGroupSlots(from.getGroupLeechSlots());
91
92         to.setIdleTime(from.getIdleTime());
93
94         to.setLastAccessTime(from.getLastAccessTime());
95
96         to.setLastNuked(from.getLastNuked());
97
98         to.setLogins(from.getLogins());
99
100         to.setMaxLogins(from.getMaxLogins());
101
102         to.setMaxLoginsPerIP(from.getMaxLoginsPerIP());
103
104         to.setMaxSimDownloads(from.getMaxSimDownloads());
105
106         to.setMaxSimUploads(from.getMaxSimUploads());
107
108         to.setNukedBytes(from.getNukedBytes());
109
110         if (from instanceof PlainTextPasswordUser) {
111             to.setPassword(((PlainTextPasswordUser) from).getPassword());
112         } else if (
113             from instanceof UnixPassword && to instanceof UnixPassword) {
114             ((UnixPassword) to).setUnixPassword(
115                 ((UnixPassword) from).getUnixPassword());
116         } else {
117             logger.warn(
118                 "Don't know how to convert password from "
119                     + from.getUsername());
120         }
121
122         to.setRatio(from.getRatio());
123
124         to.setTagline(from.getTagline());
125
126         to.setTimesNuked(from.getTimesNuked());
127
128         int periods[] =
129             new int[] {
130                 Trial.PERIOD_ALL,
131                 Trial.PERIOD_DAILY,
132                 Trial.PERIOD_MONTHLY,
133                 Trial.PERIOD_WEEKLY };
134
135         for (int i = 0; i < periods.length; i++) {
136             int period = periods[i];
137             to.setUploadedMillisecondsForPeriod(
138                 period,
139                 from.getUploadedMillisecondsForPeriod(period));
140
141             to.setDownloadedMillisecondsForPeriod(
142                 period,
143                 from.getDownloadedMillisecondsForPeriod(period));
144
145             to.setUploadedBytesForPeriod(
146                 period,
147                 from.getUploadedBytesForPeriod(period));
148
149             to.setDownloadedBytesForPeriod(
150                 period,
151                 from.getDownloadedBytesForPeriod(period));
152
153             to.setUploadedFilesForPeriod(
154                 period,
155                 from.getUploadedFilesForPeriod(period));
156
157             to.setDownloadedFilesForPeriod(
158                 period,
159                 from.getDownloadedFilesForPeriod(period));
160         }
161
162         to.commit();
163
164     }
165 }
166
Popular Tags