KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > jmail > JMailService


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.jmail;
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 JMailService
30 extends Service
31 {
32   DatabaseAbstractionLayer layer = null;
33
34   public JMailService()
35   {
36   }
37
38   public void init(Server parent)
39   {
40     layer = parent.getDBLayer();
41   }
42
43   public void install()
44   {
45     try {
46         String JavaDoc dbDescription = getDirectory() + "db-jmail.xml";
47         layer.getTableCreator().createFromXml(dbDescription);
48     } catch (Exception JavaDoc e) {
49         Logging.getLogger().severe("Unable to install JMailService !");
50         e.printStackTrace();
51     }
52   }
53
54   public void process(ObjectConnection oc, Message message)
55   {
56     JMailAction jma = (JMailAction)message.getData();
57     String JavaDoc user = message.getSender().getName();
58     Account account;
59
60     try {
61         switch(jma.action)
62         {
63         case JMailAction.GET_ACCOUNT:
64             account = this.getAccount(user);
65             oc.write("OK");
66             oc.write(account);
67             break;
68         }
69     } catch(Exception JavaDoc e) {
70         try {
71             oc.write("FAILED " + e);
72         } catch(Exception JavaDoc e2) {}
73         e.printStackTrace();
74     }
75   }
76
77   
78   private Account getAccount(String JavaDoc user)
79   throws Exception JavaDoc
80   {
81     Account a = null;
82     
83     Connection connex = layer.getConnection();
84     PreparedStatement select = connex.prepareStatement(
85             "SELECT * FROM JMailAccounts WHERE userName=?");
86     select.setString(1, user);
87     
88     ResultSet rs = select.executeQuery();
89     
90     
91     if(rs.next())
92     {
93         String JavaDoc address = rs.getString(2);
94         String JavaDoc type = rs.getString(3);
95         String JavaDoc inHost = rs.getString(4);
96         int inPort = rs.getInt(5);
97         String JavaDoc outHost = rs.getString(6);
98         int outPort = rs.getInt(7);
99         String JavaDoc login = rs.getString(8);
100         String JavaDoc password = rs.getString(9);
101         int refreshInterval = rs.getInt(10);
102         
103         password = BlowFish.decipher(login, password);
104         a = new Account(address, type, inHost, inPort, outHost, outPort, login, password, refreshInterval);
105     }
106     
107     rs.close();
108     select.close();
109     connex.close();
110     
111     return a;
112   }
113 }
114
Popular Tags