1 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 ; 25 import java.io.StringWriter ; 26 import java.io.StringReader ; 27 28 34 public class PrivateStorage extends BasicModule { 35 36 private static final String LOAD_PRIVATE = 37 "SELECT value FROM jivePrivate WHERE username=? AND namespace=?"; 38 private static final String INSERT_PRIVATE = 39 "INSERT INTO jivePrivate (value,name,username,namespace) VALUES (?,?,?,?)"; 40 private static final String UPDATE_PRIVATE = 41 "UPDATE jivePrivate SET value=?, name=? WHERE username=? AND namespace=?"; 42 43 48 private boolean enabled = JiveGlobals.getBooleanProperty("xmpp.privateStorageEnabled", true); 49 50 SAXReader xmlReader = new SAXReader(); 51 52 55 public PrivateStorage() { 56 super("Private user data storage"); 57 } 58 59 64 public boolean isEnabled() { 65 return enabled; 66 } 67 68 73 public void setEnabled(boolean enabled) { 74 this.enabled = enabled; 75 JiveGlobals.setProperty("xmpp.privateStorageEnabled", Boolean.toString(enabled)); 76 } 77 78 85 public void add(String username, Element data) { 86 if (enabled) { 87 java.sql.Connection con = null; 88 PreparedStatement pstmt = null; 89 try { 90 StringWriter writer = new StringWriter (); 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 e) { 117 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 118 } 119 finally { 120 try { if (pstmt != null) { pstmt.close(); } } 121 catch (Exception e) { Log.error(e); } 122 try { if (con != null) { con.close(); } } 123 catch (Exception e) { Log.error(e); } 124 } 125 } 126 } 127 128 142 public Element get(String 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 result = rs.getString(1).trim(); 155 Document doc = xmlReader.read(new StringReader (result)); 156 data = doc.getRootElement(); 157 } 158 rs.close(); 159 } 160 catch (Exception e) { 161 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 162 } 163 finally { 164 try { if (pstmt != null) { pstmt.close(); } } 165 catch (Exception e) { Log.error(e); } 166 try { if (con != null) { con.close(); } } 167 catch (Exception e) { Log.error(e); } 168 } 169 } 170 return data; 171 } 172 } | Popular Tags |