KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > jmailaccount > JMailAccountService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

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 JavaDoc 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 JavaDoc e) {
65         try {
66             oc.write("FAILED " + e);
67         } catch(Exception JavaDoc e2) {}
68         e.printStackTrace();
69     }
70   }
71
72   
73
74   private Account getAccount(String JavaDoc user)
75   throws Exception JavaDoc
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 JavaDoc address = rs.getString(2);
90         String JavaDoc type = rs.getString(3);
91         String JavaDoc inHost = rs.getString(4);
92         int inPort = rs.getInt(5);
93         String JavaDoc outHost = rs.getString(6);
94         int outPort = rs.getInt(7);
95         String JavaDoc login = rs.getString(8);
96         String JavaDoc 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 JavaDoc user, Account a)
110   throws Exception JavaDoc
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 JavaDoc e) {
121         //no such account yet
122
}
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