1 18 package net.sf.drftpd.master.usermanager; 19 20 import java.io.IOException ; 21 import java.util.Iterator ; 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 35 public class UserManagerConverter { 36 private static final Logger logger = 37 Logger.getLogger(UserManagerConverter.class); 38 39 public static void main(String [] args) 40 throws 41 InstantiationException , 42 IllegalAccessException , 43 ClassNotFoundException , 44 IOException , 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 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 iter = from.getGroups().iterator(); iter.hasNext();) { 64 try { 65 to.addGroup((String ) iter.next()); 66 } catch (DuplicateElementException e) { 67 logger.warn("", e); 68 } 69 } 70 71 for (Iterator iter = from.getIpMasks().iterator(); iter.hasNext();) { 72 String ipmask = (String ) 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 |