KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > PrivateStorage


1 /**
2  * $RCSfile: PrivateStorage.java,v $
3  * $Revision: 1.6 $
4  * $Date: 2005/04/11 21:04:00 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger;
13
14 import org.jivesoftware.messenger.container.BasicModule;
15 import org.jivesoftware.database.DbConnectionManager;
16 import org.jivesoftware.util.Log;
17 import org.jivesoftware.util.LocaleUtils;
18 import org.jivesoftware.util.JiveGlobals;
19 import org.dom4j.Element;
20 import org.dom4j.Document;
21 import org.dom4j.io.SAXReader;
22
23 import java.sql.*;
24 import java.sql.Connection JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.io.StringReader JavaDoc;
27
28 /**
29  * Private storage for user accounts (JEP-0049). It is used by some XMPP systems
30  * for saving client settings on the server.
31  *
32  * @author Iain Shigeoka
33  */

34 public class PrivateStorage extends BasicModule {
35
36     private static final String JavaDoc LOAD_PRIVATE =
37         "SELECT value FROM jivePrivate WHERE username=? AND namespace=?";
38     private static final String JavaDoc INSERT_PRIVATE =
39         "INSERT INTO jivePrivate (value,name,username,namespace) VALUES (?,?,?,?)";
40     private static final String JavaDoc UPDATE_PRIVATE =
41         "UPDATE jivePrivate SET value=?, name=? WHERE username=? AND namespace=?";
42
43     // Currently no delete supported, we can detect an add of an empty element and
44
// use that to signal a delete but that optimization doesn't seem necessary.
45
// private static final String DELETE_PRIVATE =
46
// "DELETE FROM jivePrivate WHERE userID=? AND name=? AND namespace=?";
47

48     private boolean enabled = JiveGlobals.getBooleanProperty("xmpp.privateStorageEnabled", true);
49
50     SAXReader xmlReader = new SAXReader();
51
52     /**
53      * Constructs a new PrivateStore instance.
54      */

55     public PrivateStorage() {
56         super("Private user data storage");
57     }
58
59     /**
60      * Returns true if private storage is enabled.
61      *
62      * @return true if private storage is enabled.
63      */

64     public boolean isEnabled() {
65         return enabled;
66     }
67
68     /**
69      * Sets whether private storage is enabled.
70      *
71      * @param enabled true if this private store is enabled.
72      */

73     public void setEnabled(boolean enabled) {
74         this.enabled = enabled;
75         JiveGlobals.setProperty("xmpp.privateStorageEnabled", Boolean.toString(enabled));
76     }
77
78     /**
79      * Stores private data. If the name and namespace of the element matches another
80      * stored private data XML document, then replace it with the new one.
81      *
82      * @param data the data to store (XML element)
83      * @param username the username of the account where private data is being stored
84      */

85     public void add(String JavaDoc username, Element data) {
86         if (enabled) {
87             java.sql.Connection JavaDoc con = null;
88             PreparedStatement pstmt = null;
89             try {
90                 StringWriter JavaDoc writer = new StringWriter JavaDoc();
91                 data.write(writer);
92                 con = DbConnectionManager.getConnection();
93                 pstmt = con.prepareStatement(LOAD_PRIVATE);
94                 pstmt.setString(1, username);
95                 pstmt.setString(2, data.getNamespaceURI());
96                 ResultSet rs = pstmt.executeQuery();
97                 boolean update = false;
98                 if (rs.next()) {
99                     update = true;
100                 }
101                 rs.close();
102                 pstmt.close();
103
104                 if (update) {
105                     pstmt = con.prepareStatement(UPDATE_PRIVATE);
106                 }
107                 else {
108                     pstmt = con.prepareStatement(INSERT_PRIVATE);
109                 }
110                 pstmt.setString(1, writer.toString());
111                 pstmt.setString(2, data.getName());
112                 pstmt.setString(3, username);
113                 pstmt.setString(4, data.getNamespaceURI());
114                 pstmt.executeUpdate();
115             }
116             catch (Exception JavaDoc e) {
117                 Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
118             }
119             finally {
120                 try { if (pstmt != null) { pstmt.close(); } }
121                 catch (Exception JavaDoc e) { Log.error(e); }
122                 try { if (con != null) { con.close(); } }
123                 catch (Exception JavaDoc e) { Log.error(e); }
124             }
125         }
126     }
127
128     /**
129      * Returns the data stored under a key corresponding to the name and namespace
130      * of the given element. The Element must be in the form:<p>
131      *
132      * <code>&lt;name xmlns='namespace'/&gt;</code><p>
133      *
134      * If no data is currently stored under the given key, an empty element will be
135      * returned.
136      *
137      * @param data an XML document who's element name and namespace is used to
138      * match previously stored private data.
139      * @param username the username of the account where private data is being stored.
140      * @return the data stored under the given key or the data element.
141      */

142     public Element get(String JavaDoc username, Element data) {
143         if (enabled) {
144             Connection con = null;
145             PreparedStatement pstmt = null;
146             try {
147                 con = DbConnectionManager.getConnection();
148                 pstmt = con.prepareStatement(LOAD_PRIVATE);
149                 pstmt.setString(1, username);
150                 pstmt.setString(2, data.getNamespaceURI());
151                 ResultSet rs = pstmt.executeQuery();
152                 if (rs.next()) {
153                     data.clearContent();
154                     String JavaDoc result = rs.getString(1).trim();
155                     Document doc = xmlReader.read(new StringReader JavaDoc(result));
156                     data = doc.getRootElement();
157                 }
158                 rs.close();
159             }
160             catch (Exception JavaDoc e) {
161                 Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
162             }
163             finally {
164                 try { if (pstmt != null) { pstmt.close(); } }
165                 catch (Exception JavaDoc e) { Log.error(e); }
166                 try { if (con != null) { con.close(); } }
167                 catch (Exception JavaDoc e) { Log.error(e); }
168             }
169         }
170         return data;
171     }
172 }
Popular Tags