KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > jmailadmin > JMailAdminService


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.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 JavaDoc user;
51     Account account;
52
53     try {
54         switch(jma.action)
55         {
56         case JMailAdminAction.GET_ACCOUNT:
57             user = (String JavaDoc)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 JavaDoc e) {
75         try {
76             oc.write("FAILED " + e);
77         } catch(Exception JavaDoc e2) {}
78         e.printStackTrace();
79     }
80   }
81
82   
83   private Account getAccount(String JavaDoc user)
84   throws Exception JavaDoc
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 JavaDoc address = rs.getString(2);
99         String JavaDoc type = rs.getString(3);
100         String JavaDoc inHost = rs.getString(4);
101         int inPort = rs.getInt(5);
102         String JavaDoc outHost = rs.getString(6);
103         int outPort = rs.getInt(7);
104         String JavaDoc login = rs.getString(8);
105         String JavaDoc 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 JavaDoc
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 JavaDoc e) {
134         //no such account yet
135
}
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 JavaDoc e) {
165         e.printStackTrace();
166     }
167     
168     return users;
169   }
170 }
171
172
Popular Tags