1 19 package org.lucane.applications.jmailadmin; 20 21 import org.lucane.common.*; 22 import org.lucane.common.crypto.*; 23 import org.lucane.common.net.ObjectConnection; 24 import org.lucane.server.*; 25 import org.lucane.server.database.*; 26 import org.lucane.server.store.Store; 27 28 import java.sql.*; 29 import java.util.*; 30 31 public class JMailAdminService 32 extends Service 33 { 34 private DatabaseAbstractionLayer layer = null; 35 private Store store = null; 36 37 public JMailAdminService() 38 { 39 } 40 41 public void init(Server parent) 42 { 43 layer = parent.getDBLayer(); 44 store = parent.getStore(); 45 } 46 47 public void process(ObjectConnection oc, Message message) 48 { 49 JMailAdminAction jma = (JMailAdminAction)message.getData(); 50 String user; 51 Account account; 52 53 try { 54 switch(jma.action) 55 { 56 case JMailAdminAction.GET_ACCOUNT: 57 user = (String )jma.param; 58 account = this.getAccount(user); 59 oc.write("OK"); 60 oc.write(account); 61 break; 62 63 case JMailAdminAction.STORE_ACCOUNT: 64 account = (Account)jma.param; 65 this.storeAccount(account); 66 oc.write("OK"); 67 break; 68 69 case JMailAdminAction.GET_USERS: 70 ArrayList users = this.getAllUsers(); 71 oc.write("OK"); 72 oc.write(users); 73 } 74 } catch(Exception e) { 75 try { 76 oc.write("FAILED " + e); 77 } catch(Exception e2) {} 78 e.printStackTrace(); 79 } 80 } 81 82 83 private Account getAccount(String user) 84 throws Exception 85 { 86 Account a = null; 87 88 Connection connex = layer.getConnection(); 89 PreparedStatement select = connex.prepareStatement( 90 "SELECT * FROM JMailAccounts WHERE userName=?"); 91 92 select.setString(1, user); 93 94 ResultSet rs = select.executeQuery(); 95 if(rs.next()) 96 { 97 user = rs.getString(1); 98 String address = rs.getString(2); 99 String type = rs.getString(3); 100 String inHost = rs.getString(4); 101 int inPort = rs.getInt(5); 102 String outHost = rs.getString(6); 103 int outPort = rs.getInt(7); 104 String login = rs.getString(8); 105 String password = rs.getString(9); 106 int refreshInterval = rs.getInt(10); 107 108 password = BlowFish.decipher(login, password); 109 a = new Account(user, address, type, inHost, inPort, outHost, outPort, login, password, refreshInterval); 110 } 111 112 rs.close(); 113 select.close(); 114 connex.close(); 115 116 if(a == null) 117 a = new Account(user); 118 119 return a; 120 } 121 122 private void storeAccount(Account a) 123 throws Exception 124 { 125 Connection connex = layer.getConnection(); 126 127 PreparedStatement delete = connex.prepareStatement( 128 "DELETE FROM JMailAccounts WHERE userName = ?"); 129 delete.setString(1, a.user); 130 131 try { 132 delete.execute(); 133 } catch(Exception e) { 134 } 136 delete.close(); 137 138 PreparedStatement insert = connex.prepareStatement( 139 "INSERT INTO JMailAccounts VALUES(?,?,?,?,?,?,?,?,?,?)"); 140 insert.setString(1, a.user); 141 insert.setString(2, a.address); 142 insert.setString(3, a.type); 143 insert.setString(4, a.inHost); 144 insert.setInt(5, a.inPort); 145 insert.setString(6, a.outHost); 146 insert.setInt(7, a.outPort); 147 insert.setString(8, a.login); 148 insert.setString(9, BlowFish.cipher(a.login, a.password)); 149 insert.setInt(10, a.refreshInterval); 150 insert.execute(); 151 insert.close(); 152 153 connex.close(); 154 } 155 156 private ArrayList getAllUsers() 157 { 158 ArrayList users = new ArrayList(); 159 160 try { 161 Iterator i = store.getUserStore().getAllUsers(); 162 while(i.hasNext()) 163 users.add(i.next()); 164 } catch(Exception e) { 165 e.printStackTrace(); 166 } 167 168 return users; 169 } 170 } 171 172 | Popular Tags |