001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.jmail;
020:
021: import org.lucane.common.*;
022: import org.lucane.common.crypto.*;
023: import org.lucane.common.net.ObjectConnection;
024: import org.lucane.server.*;
025: import org.lucane.server.database.*;
026:
027: import java.sql.*;
028:
029: public class JMailService extends Service {
030: DatabaseAbstractionLayer layer = null;
031:
032: public JMailService() {
033: }
034:
035: public void init(Server parent) {
036: layer = parent.getDBLayer();
037: }
038:
039: public void install() {
040: try {
041: String dbDescription = getDirectory() + "db-jmail.xml";
042: layer.getTableCreator().createFromXml(dbDescription);
043: } catch (Exception e) {
044: Logging.getLogger().severe(
045: "Unable to install JMailService !");
046: e.printStackTrace();
047: }
048: }
049:
050: public void process(ObjectConnection oc, Message message) {
051: JMailAction jma = (JMailAction) message.getData();
052: String user = message.getSender().getName();
053: Account account;
054:
055: try {
056: switch (jma.action) {
057: case JMailAction.GET_ACCOUNT:
058: account = this .getAccount(user);
059: oc.write("OK");
060: oc.write(account);
061: break;
062: }
063: } catch (Exception e) {
064: try {
065: oc.write("FAILED " + e);
066: } catch (Exception e2) {
067: }
068: e.printStackTrace();
069: }
070: }
071:
072: private Account getAccount(String user) throws Exception {
073: Account a = null;
074:
075: Connection connex = layer.getConnection();
076: PreparedStatement select = connex
077: .prepareStatement("SELECT * FROM JMailAccounts WHERE userName=?");
078: select.setString(1, user);
079:
080: ResultSet rs = select.executeQuery();
081:
082: if (rs.next()) {
083: String address = rs.getString(2);
084: String type = rs.getString(3);
085: String inHost = rs.getString(4);
086: int inPort = rs.getInt(5);
087: String outHost = rs.getString(6);
088: int outPort = rs.getInt(7);
089: String login = rs.getString(8);
090: String password = rs.getString(9);
091: int refreshInterval = rs.getInt(10);
092:
093: password = BlowFish.decipher(login, password);
094: a = new Account(address, type, inHost, inPort, outHost,
095: outPort, login, password, refreshInterval);
096: }
097:
098: rs.close();
099: select.close();
100: connex.close();
101:
102: return a;
103: }
104: }
|