DefaultVCardProvider.java 6.28 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile: DefaultVCardProvider.java,v $
 * $Revision: 3062 $
 * $Date: 2005-11-11 13:26:30 -0300 (Fri, 11 Nov 2005) $
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
11 12
 */

13
package org.jivesoftware.openfire.vcard;
14 15 16 17 18

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.AlreadyExistsException;
19
import org.jivesoftware.util.Log;
20 21 22 23 24 25 26 27 28 29 30
import org.jivesoftware.util.NotFoundException;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
31
 * Default implementation of the VCardProvider interface, which reads and writes data
32
 * from the <tt>ofVCard</tt> database table.
33 34 35 36 37 38
 *
 * @author Gaston Dombiak
 */
public class DefaultVCardProvider implements VCardProvider {

    private static final String LOAD_PROPERTIES =
39
        "SELECT vcard FROM ofVCard WHERE username=?";
40
    private static final String DELETE_PROPERTIES =
41
        "DELETE FROM ofVCard WHERE username=?";
42
    private static final String UPDATE_PROPERTIES =
43
        "UPDATE ofVCard SET vcard=? WHERE username=?";
44
    private static final String INSERT_PROPERTY =
45
        "INSERT INTO ofVCard (username, vcard) VALUES (?, ?)";
46

47 48 49
    /**
     * Pool of SAX Readers. SAXReader is not thread safe so we need to have a pool of readers.
     */
50 51 52 53 54 55
    private BlockingQueue<SAXReader> xmlReaders = new LinkedBlockingQueue<SAXReader>();


    public DefaultVCardProvider() {
        super();
        // Initialize the pool of sax readers
56
        for (int i=0; i<10; i++) {
57 58 59
            SAXReader xmlReader = new SAXReader();
            xmlReader.setEncoding("UTF-8");
            xmlReaders.add(xmlReader);
60 61 62 63
        }
    }

    public Element loadVCard(String username) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        synchronized (username.intern()) {
            Element vCardElement = null;
            java.sql.Connection con = null;
            PreparedStatement pstmt = null;
            SAXReader xmlReader = null;
            try {
                // Get a sax reader from the pool
                xmlReader = xmlReaders.take();
                con = DbConnectionManager.getConnection();
                pstmt = con.prepareStatement(LOAD_PROPERTIES);
                pstmt.setString(1, username);
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {
                    vCardElement =
                            xmlReader.read(new StringReader(rs.getString(1))).getRootElement();
                }
80
            }
81 82 83 84 85 86 87 88 89 90 91 92
            catch (Exception e) {
                Log.error("Error loading vCard of username: " + username, e);
            }
            finally {
                // Return the sax reader to the pool
                if (xmlReader != null) {
                    xmlReaders.add(xmlReader);
                }
                try { if (pstmt != null) { pstmt.close(); } }
                catch (Exception e) { Log.error(e); }
                try { if (con != null) { con.close(); } }
                catch (Exception e) { Log.error(e); }
93
            }
94
            return vCardElement;
95 96 97
        }
    }

98
    public Element createVCard(String username, Element vCardElement) throws AlreadyExistsException {
99 100 101
        if (loadVCard(username) != null) {
            // The user already has a vCard
            throw new AlreadyExistsException("Username " + username + " already has a vCard");
102 103 104 105 106 107 108 109 110 111 112 113
        }

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_PROPERTY);
            pstmt.setString(1, username);
            pstmt.setString(2, vCardElement.asXML());
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
114
            Log.error("Error creating vCard for username: " + username, e);
115 116
        }
        finally {
117 118 119 120
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
121
        }
122
        return vCardElement;
123 124
    }

125
    public Element updateVCard(String username, Element vCardElement) throws NotFoundException {
126 127 128
        if (loadVCard(username) == null) {
            // The user already has a vCard
            throw new NotFoundException("Username " + username + " does not have a vCard");
129 130 131 132 133 134 135 136 137 138 139
        }
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_PROPERTIES);
            pstmt.setString(1, vCardElement.asXML());
            pstmt.setString(2, username);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
140
            Log.error("Error updating vCard of username: " + username, e);
141 142
        }
        finally {
143 144 145 146
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
147
        }
148
        return vCardElement;
149 150 151 152 153 154 155 156 157 158 159 160
    }

    public void deleteVCard(String username) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_PROPERTIES);
            pstmt.setString(1, username);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
161
            Log.error("Error deleting vCard of username: " + username, e);
162 163
        }
        finally {
164 165 166 167
            try { if (pstmt != null) { pstmt.close(); } }
            catch (Exception e) { Log.error(e); }
            try { if (con != null) { con.close(); } }
            catch (Exception e) { Log.error(e); }
168 169 170 171 172 173 174
        }
    }

    public boolean isReadOnly() {
        return false;
    }
}