1 19 package org.lucane.applications.jmailaccount; 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 27 import java.sql.*; 28 29 public class JMailAccountService 30 extends Service 31 { 32 DatabaseAbstractionLayer layer = null; 33 34 public JMailAccountService() 35 { 36 } 37 38 public void init(Server parent) 39 { 40 layer = parent.getDBLayer(); 41 } 42 43 public void process(ObjectConnection oc, Message message) 44 { 45 JMailAccountAction jma = (JMailAccountAction)message.getData(); 46 String user = message.getSender().getName(); 47 Account account; 48 49 try { 50 switch(jma.action) 51 { 52 case JMailAccountAction.GET_ACCOUNT: 53 account = this.getAccount(user); 54 oc.write("OK"); 55 oc.write(account); 56 break; 57 58 case JMailAccountAction.STORE_ACCOUNT: 59 account = (Account)jma.param; 60 this.storeAccount(user, account); 61 oc.write("OK"); 62 break; 63 } 64 } catch(Exception e) { 65 try { 66 oc.write("FAILED " + e); 67 } catch(Exception e2) {} 68 e.printStackTrace(); 69 } 70 } 71 72 73 74 private Account getAccount(String user) 75 throws Exception 76 { 77 Account a = null; 78 79 Connection connex = layer.getConnection(); 80 PreparedStatement select = connex.prepareStatement( 81 "SELECT * FROM JMailAccounts WHERE userName=?"); 82 83 select.setString(1, user); 84 85 ResultSet rs = select.executeQuery(); 86 if(rs.next()) 87 { 88 user = rs.getString(1); 89 String address = rs.getString(2); 90 String type = rs.getString(3); 91 String inHost = rs.getString(4); 92 int inPort = rs.getInt(5); 93 String outHost = rs.getString(6); 94 int outPort = rs.getInt(7); 95 String login = rs.getString(8); 96 String password = rs.getString(9); 97 password = BlowFish.decipher(login, password); 98 int refreshInterval = rs.getInt(10); 99 a = new Account(address, type, inHost, inPort, outHost, outPort, login, password, refreshInterval); 100 } 101 102 rs.close(); 103 select.close(); 104 connex.close(); 105 106 return a; 107 } 108 109 private void storeAccount(String user, Account a) 110 throws Exception 111 { 112 Connection connex = layer.getConnection(); 113 114 PreparedStatement delete = connex.prepareStatement( 115 "DELETE FROM JMailAccounts WHERE userName = ?"); 116 delete.setString(1, user); 117 118 try { 119 delete.execute(); 120 } catch(Exception e) { 121 } 123 delete.close(); 124 125 PreparedStatement insert = connex.prepareStatement( 126 "INSERT INTO JMailAccounts VALUES(?,?,?,?,?,?,?,?,?, ?)"); 127 insert.setString(1, user); 128 insert.setString(2, a.address); 129 insert.setString(3, a.type); 130 insert.setString(4, a.inHost); 131 insert.setInt(5, a.inPort); 132 insert.setString(6, a.outHost); 133 insert.setInt(7, a.outPort); 134 insert.setString(8, a.login); 135 insert.setString(9, BlowFish.cipher(a.login, a.password)); 136 insert.setInt(10, a.refreshInterval); 137 insert.execute(); 138 insert.close(); 139 140 connex.close(); 141 } 142 } 143 144 | Popular Tags |