KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > store > PrincipalPersistentStore


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.server.store;
20
21 import java.util.ArrayList JavaDoc;
22 import java.sql.*;
23
24 import sync4j.framework.tools.Base64;
25 import sync4j.framework.tools.DBTools;
26 import sync4j.framework.server.store.*;
27 import sync4j.framework.security.Sync4jPrincipal;
28
29 import sync4j.server.tools.IdSpaceGenerator;
30
31 /**
32  * This is the store for principal information of the Sync4j engine.
33  * Currently it persistes the following classes:
34  * <ul>
35  * <li>sync4j.framework.security.Sync4jPrincipal</li>
36  * </ul>
37  *
38  * @author Stefano Fornari @ Funambol
39  *
40  * @version $Id: PrincipalPersistentStore.java,v 1.9 2005/07/25 15:26:56 nichele Exp $
41  *
42  */

43 public class PrincipalPersistentStore
44 extends BasePersistentStore
45 implements PersistentStore, java.io.Serializable JavaDoc {
46
47     // --------------------------------------------------------------- Constants
48

49     private static final String JavaDoc OPT_INSERT = "INSERT";
50
51     public static final int SQL_INSERT_PRINCIPAL = 0;
52     public static final int SQL_GET_PRINCIPAL = 1;
53     public static final int SQL_SELECT_PRINCIPAL = 2;
54     public static final int SQL_UPDATE_PRINCIPAL = 3;
55     public static final int SQL_SELECT_ALL_PRINCIPALS = 4;
56     public static final int SQL_DELETE_PRINCIPAL = 5;
57     public static final int SQL_DELETE_CLIENT_MAPPING = 6;
58     public static final int SQL_DELETE_LAST_SYNC = 7;
59     public static final int SQL_COUNT_PRINCIPALS = 8;
60
61     public static final String JavaDoc NS_PRINCIPAL = "principal";
62
63     // -------------------------------------------------------------- Properties
64

65     protected String JavaDoc[] sql = null;
66
67     public void setSql(String JavaDoc[] sql) {
68         this.sql = sql;
69     }
70
71     public String JavaDoc[] getSql() {
72         return this.sql;
73     }
74
75     // ------------------------------------------------------------ Private data
76
private static final String JavaDoc SEARCH_COUNT_PRINCIPALS = "SCP";
77
78     // ------------------------------------------------------------ Constructors
79
// ---------------------------------------------------------- Public methods
80

81     /**
82      * Delete the principal from the data store.
83      *
84      * @param o The object that must be a Sync4jPrincipal for this method
85      *
86      * @throws PersistentException in case of error reading the data store
87      */

88     public boolean delete(Object JavaDoc o) throws PersistentStoreException {
89         if (o instanceof Sync4jPrincipal) {
90             Sync4jPrincipal p = (Sync4jPrincipal) o;
91
92             Connection conn = null;
93             PreparedStatement stmt = null;
94
95             try {
96                 conn = dataSource.getConnection();
97
98                 long pid = Long.parseLong(p.getId());
99
100                 stmt = conn.prepareStatement(sql[SQL_DELETE_CLIENT_MAPPING]);
101                 stmt.setLong(1, pid);
102                 stmt.executeUpdate();
103                 stmt.close();
104
105                 stmt = conn.prepareStatement(sql[SQL_DELETE_LAST_SYNC]);
106                 stmt.setLong(1, pid);
107                 stmt.executeUpdate();
108                 stmt.close();
109
110                 stmt = conn.prepareStatement(sql[SQL_DELETE_PRINCIPAL]);
111                 stmt.setLong(1, pid);
112                 stmt.executeUpdate();
113
114             } catch (SQLException e) {
115                 throw new PersistentStoreException("Error deleting the principal " + p, e);
116             } finally {
117                 DBTools.close(conn, stmt, null);
118             }
119             return true;
120         }
121         return false;
122     }
123
124     public boolean store(Object JavaDoc o, String JavaDoc operation) throws PersistentStoreException {
125         return false;
126     }
127
128     public boolean store(Object JavaDoc o) throws PersistentStoreException {
129         if (o instanceof Sync4jPrincipal) {
130             Sync4jPrincipal p = (Sync4jPrincipal) o;
131
132             Connection conn = null;
133             PreparedStatement stmt = null;
134             ResultSet rs = null;
135             int n = 0;
136
137             try {
138                 conn = dataSource.getConnection();
139
140                 if (p.getId() != null && !p.getId().equals("")) {
141                     stmt = conn.prepareStatement(sql[SQL_UPDATE_PRINCIPAL]);
142                     stmt.setString(1, p.getUsername());
143                     stmt.setString(2, p.getDeviceId());
144                     stmt.setLong(3, Long.parseLong(p.getId()));
145                     n = stmt.executeUpdate();
146                     stmt.close(); stmt = null;
147                 } else {
148                     //check if the principal already exist: verify username-deviceid
149
stmt = conn.prepareStatement(sql[SQL_SELECT_PRINCIPAL]);
150                     stmt.setString(1, p.getUsername());
151                     stmt.setString(2, p.getDeviceId());
152                     rs = stmt.executeQuery();
153
154                     if (rs.next() == false) {
155                         n = 0;
156                     } else {
157                         n = 1;
158                         p.setId (rs.getString(1));
159                         p.setUsername (rs.getString(2));
160                         p.setDeviceId (rs.getString(3));
161                     }
162                     rs.close(); rs = null;
163                     stmt.close(); stmt = null;
164                 }
165
166                 if (n == 0) {
167                     // The first time!!!
168
int principalId = getPrincipalId();
169                     p.setId(String.valueOf(principalId));
170
171                     stmt = conn.prepareStatement(sql[SQL_INSERT_PRINCIPAL]);
172                     stmt.setLong(1, principalId);
173                     stmt.setString(2, p.getUsername());
174                     stmt.setString(3, p.getDeviceId());
175                     stmt.executeUpdate();
176                 }
177                 return true;
178             } catch (SQLException e) {
179                 throw new PersistentStoreException("Error storing principal " + p, e);
180             } finally {
181                 DBTools.close(conn, stmt, rs);
182             }
183         }
184         return false;
185     }
186
187     /**
188      * Read the principal from the data store. If <i>getId()</i> returns null,
189      * it tries to read the principal from username/device, otherwise throws id
190      * is used for the lookup.
191      * @param o The object that must be a Sync4jPrincipal for this method
192      * @throws PersistentException in case of error reading the data store
193      */

194     public boolean read(Object JavaDoc o) throws PersistentStoreException {
195         if (o instanceof Sync4jPrincipal) {
196             Sync4jPrincipal p = (Sync4jPrincipal) o;
197
198             Connection conn = null;
199             PreparedStatement stmt = null;
200             ResultSet rs = null;
201
202             try {
203                 conn = dataSource.getConnection();
204
205                 if (p.getId() == null) {
206                     stmt = conn.prepareStatement(sql[SQL_SELECT_PRINCIPAL]);
207                     stmt.setString(1, p.getUsername());
208                     stmt.setString(2, p.getDeviceId());
209                 } else {
210                     stmt = conn.prepareStatement(sql[SQL_GET_PRINCIPAL]);
211                     stmt.setLong(1, Long.parseLong(p.getId()));
212                 }
213                 rs = stmt.executeQuery();
214
215                 if (rs.next() == false) {
216                     throw new NotFoundException("Principal not found for "
217                     + p.toString());
218                 }
219                 p.setId (String.valueOf(rs.getLong(1)));
220                 p.setUsername (rs.getString(2));
221                 p.setDeviceId (rs.getString(3));
222
223             } catch (SQLException e) {
224                 throw new PersistentStoreException("Error reading principal " + p, e);
225             } finally {
226                 DBTools.close(conn, stmt, rs);
227             }
228             return true;
229         }
230         return false;
231     }
232
233     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
234         return null;
235     }
236
237     /**
238      * Read all for the Sync4jPrincipal.
239      * @param o The object that must be a Sync4jPrincipal for this method
240      * @param clause condition where for select
241      *
242      * @return Object[] array of Sync4jPrincipals
243      */

244     public Object JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException {
245         if (!(o instanceof Sync4jPrincipal)) {
246             return null; // Not me!!
247
}
248         Connection conn = null;
249         PreparedStatement stmt = null;
250         ResultSet rs = null;
251
252         ArrayList JavaDoc ret = new ArrayList JavaDoc();
253
254         try {
255             conn = dataSource.getConnection();
256
257             PreparedWhere where = clause.getPreparedWhere();
258             String JavaDoc query = sql[SQL_SELECT_ALL_PRINCIPALS];
259             if (where.sql.length() > 0){
260                 query += " where " + where.sql;
261             }
262             stmt = conn.prepareStatement(query);
263             for (int i=0; i<where.parameters.length; ++i) {
264                 stmt.setObject(i+1, where.parameters[i]);
265             }
266             rs = stmt.executeQuery();
267
268             while (rs.next()) {
269                 ret.add(
270                     new Sync4jPrincipal(
271                         String.valueOf(rs.getLong(1)),
272                         rs.getString(2),
273                         rs.getString(3)));
274             }
275
276             return ret.toArray(new Sync4jPrincipal[ret.size()]);
277         } catch (SQLException e) {
278             throw new PersistentStoreException("Error reading principals", e);
279         } finally {
280             DBTools.close(conn, stmt, rs);
281         }
282     }
283
284
285
286     /**
287      * Count the number of principals that satisfy the conditions specified in input
288      * @param o The object of the Sync4jPrincipal type.
289      * @param clause The additional where clause.
290      * @return int number of principals
291      */

292     public int count(Object JavaDoc o, Clause clause) throws PersistentStoreException {
293         if (!(o instanceof Sync4jPrincipal)) {
294             return -1; // Not me!
295
}
296         Connection conn = null;
297         PreparedStatement stmt = null;
298         ResultSet rs = null;
299         int n = 0;
300
301         try {
302             conn = dataSource.getConnection();
303
304             PreparedWhere where = clause.getPreparedWhere();
305             String JavaDoc query = sql[SQL_COUNT_PRINCIPALS];
306             if (where.sql.length()>0) {
307                 query += " where " + where.sql;
308             }
309             stmt = conn.prepareStatement(query);
310             for (int i=0; i<where.parameters.length; ++i) {
311                 stmt.setObject(i+1, where.parameters[i]);
312             }
313             rs = stmt.executeQuery();
314
315             while (rs.next()) {
316                 n = rs.getInt(1);
317             }
318             return n;
319         } catch (SQLException e) {
320             throw new PersistentStoreException("Error reading count principals ", e);
321         } finally {
322             DBTools.close(conn, stmt, rs);
323         }
324     }
325
326     //---------------------------------------------------------- Private Methods
327

328     /**
329      * Return the next principal id reading it by IdSpaceGenerator
330      * @return int
331      */

332     private int getPrincipalId() {
333         IdSpaceGenerator idGenerator = new IdSpaceGenerator(NS_PRINCIPAL);
334         return Integer.parseInt(idGenerator.next());
335     }
336 }
337
338
Popular Tags