📄 enginepersistentstore.java
字号:
/** * Copyright (C) 2003-2004 Funambol * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package sync4j.server.store;import java.util.ArrayList;import java.sql.*;import sync4j.server.engine.Sync4jSource;import sync4j.framework.tools.DBTools;import sync4j.framework.server.store.*;import sync4j.framework.server.Sync4jDevice;import sync4j.framework.security.Sync4jPrincipal;/** * This is the store for information regarding the Sync4j engine such as users, * principals, ecc. Currently it persistes the following classes: * * <ul> * <li>sync4j.framework.security.Sync4jPrincipal</li> * </ul> * * * @author Stefano Fornari @ Funambol * * @version $Id: EnginePersistentStore.java,v 1.19 2004/04/13 09:35:29 luigia Exp $ * */public class EnginePersistentStoreextends BasePersistentStoreimplements PersistentStore, java.io.Serializable { // --------------------------------------------------------------- Constants private static final String OPT_INSERT = "INSERT"; public static final int SQL_INSERT_PRINCIPAL = 0; public static final int SQL_GET_PRINCIPAL = 1; public static final int SQL_SELECT_PRINCIPAL = 2; public static final int SQL_UPDATE_PRINCIPAL = 3; public static final int SQL_SELECT_ALL_PRINCIPALS = 4; public static final int SQL_INSERT_SOURCE = 5; public static final int SQL_GET_SOURCE = 6; public static final int SQL_UPDATE_SOURCE = 7; public static final int SQL_SELECT_ALL_SOURCES = 8; public static final int SQL_SELECT_ALL_DEVICES = 9; public static final int SQL_GET_DEVICE = 10; public static final int SQL_INSERT_DEVICE = 11; public static final int SQL_UPDATE_DEVICE = 12; public static final int SQL_DELETE_DEVICE = 13; public static final int SQL_DELETE_DEVICE_PRINCIPAL = 14; public static final int SQL_DELETE_PRINCIPAL = 15; public static final int SQL_DELETE_CLIENT_MAPPING = 16; public static final int SQL_DELETE_LAST_SYNC = 17; public static final int SQL_GET_COUNTER = 18; public static final int SQL_UPDATE_COUNTER = 19; public static final int SQL_DELETE_SOURCE = 20; public static final int SQL_INSERT_SYNCSOURCE = 21; public static final int SQL_UPDATE_SYNCSOURCE = 22; public static final int SQL_DELETE_SOURCE_CLIENT_MAPPING = 23; public static final int SQL_DELETE_SOURCE_LAST_SYNC = 24; public static final String SQL_COUNT_DEVICES = "select count(*) as devices from sync4j_device "; public static final String SQL_COUNT_PRINCIPALS = "select count(*) as principals from sync4j_principal "; // -------------------------------------------------------------- Properties private String[] sql = null; public void setSql(String[] sql) { this.sql = sql; } public String[] getSql() { return this.sql; } // ------------------------------------------------------------ Private data private static final String SEARCH_COUNT_DEVICES = "SCD"; private static final String SEARCH_COUNT_PRINCIPALS = "SCP"; // ------------------------------------------------------------ Constructors // ---------------------------------------------------------- Public methods public boolean delete(Object o) throws PersistentStoreException { if (o instanceof Sync4jDevice) { deleteDevice((Sync4jDevice) o); return true; } else if (o instanceof Sync4jPrincipal) { deletePrincipal((Sync4jPrincipal) o); return true; } else if (o instanceof Sync4jSource) { deleteSource((Sync4jSource) o); return true; } return false; } public boolean store(String id, Object o, String operation) throws PersistentStoreException { if (o instanceof Sync4jSource) { if (operation.equals(OPT_INSERT)) { insertSyncSource(id, (Sync4jSource)o); } else { updateSyncSource(id, (Sync4jSource)o); } return true; } return false; } public boolean store(Object o) throws PersistentStoreException { if (o instanceof Sync4jPrincipal) { storePrincipal((Sync4jPrincipal) o); return true; } else if (o instanceof Sync4jSource) { storeSource((Sync4jSource) o); return true; } else if (o instanceof Sync4jDevice) { storeDevice((Sync4jDevice) o); return true; } return false; } public boolean read(Object o) throws PersistentStoreException { if (o instanceof Sync4jPrincipal) { readPrincipal((Sync4jPrincipal) o); return true; } else if (o instanceof Sync4jSource) { readSource((Sync4jSource) o); return true; } else if (o instanceof Sync4jDevice) { readDevice((Sync4jDevice) o); return true; } return false; } public Object[] read(Class objClass) throws PersistentStoreException { if (objClass.equals(Sync4jSource.class)) { return readAllSources(); } return null; } /** * Read all informations * @param object whichever object Sync * @param clause condition where for select * * @return Object[] array of object */ public Object[] read(Object o, Clause clause) throws PersistentStoreException { if (o instanceof Sync4jPrincipal) { return readAllPrincipals(clause); } else if (o instanceof Sync4jDevice) { return readAllDevices(clause); } else if (o instanceof String) { if (o.equals(SEARCH_COUNT_DEVICES)) { return readCountDevices(clause); } else if (o.equals(SEARCH_COUNT_PRINCIPALS)) { return readCountPrincipals(clause); } } else if (o instanceof Sync4jSource) { return readSource((Sync4jSource)o, clause); } return null; } // --------------------------------------------------------- Private methods private void storePrincipal(Sync4jPrincipal p) throws PersistentStoreException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; int n = 0; try { conn = dataSource.getConnection(); if (p.getId() != null && !p.getId().equals("")) { stmt = conn.prepareStatement(sql[SQL_UPDATE_PRINCIPAL]); stmt.setString(1, p.getUsername()); stmt.setString(2, p.getDeviceId()); stmt.setString(3, p.getId() ); n = stmt.executeUpdate(); stmt.close(); } else { //check if the principal already exist: verify username-deviceid stmt = conn.prepareStatement(sql[SQL_SELECT_PRINCIPAL]); stmt.setString(1, p.getUsername()); stmt.setString(2, p.getDeviceId()); rs = stmt.executeQuery(); if (rs.next() == false) { n = 0; } else { n = 1; p.setId (rs.getString(1)); p.setUsername (rs.getString(2)); p.setDeviceId (rs.getString(3)); p.setEmail (rs.getString(4)); p.setFirstName(rs.getString(5)); p.setLastName (rs.getString(6)); } stmt.close(); rs.close(); } if (n == 0) { // // The first time!!! // int principalId = this.readCounter("principal"); p.setId("" + principalId); stmt = conn.prepareStatement(sql[SQL_INSERT_PRINCIPAL]); stmt.setString(1, p.getId() ); stmt.setString(2, p.getUsername()); stmt.setString(3, p.getDeviceId()); stmt.executeUpdate(); } } catch (SQLException e) { throw new PersistentStoreException("Error storing principal " + p, e); } finally { DBTools.close(conn, stmt, rs); } } /** * Read the principal from the data store. If <i>getId()</i> returns null, * it tries to read the principal from username/device, otherwise throws id * is used for the lookup. * * @throws PersistentException in case of error reading the data store */ private void readPrincipal(Sync4jPrincipal p) throws PersistentStoreException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = dataSource.getConnection(); if (p.getId() == null) { stmt = conn.prepareStatement(sql[SQL_SELECT_PRINCIPAL]); stmt.setString(1, p.getUsername()); stmt.setString(2, p.getDeviceId()); } else { stmt = conn.prepareStatement(sql[SQL_GET_PRINCIPAL]); stmt.setString(1, p.getId()); } rs = stmt.executeQuery(); if (rs.next() == false) { throw new NotFoundException("Principal not found for " + p.toString() ); } p.setId (rs.getString(1)); p.setUsername (rs.getString(2)); p.setDeviceId (rs.getString(3)); p.setEmail (rs.getString(4)); p.setFirstName(rs.getString(5)); p.setLastName (rs.getString(6)); } catch (SQLException e) { throw new PersistentStoreException("Error reading principal " + p, e); } finally { DBTools.close(conn, stmt, rs); } } /** * Read all principals * * @return an array with the principals (empty if no objects are found) * * @throws PersistentException in case of error reading the data store */ private Object[] readAllPrincipals(Clause clause) throws PersistentStoreException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; ArrayList ret = new ArrayList(); try { conn = dataSource.getConnection(); PreparedWhere where = clause.getPreparedWhere(); String query = sql[SQL_SELECT_ALL_PRINCIPALS]; if (where.sql.length() > 0){ query += " where " + where.sql; } stmt = conn.prepareStatement(query); for (int i=0; i<where.parameters.length; ++i) { stmt.setObject(i+1, where.parameters[i]); } rs = stmt.executeQuery(); while (rs.next()) { ret.add( new Sync4jPrincipal( rs.getString(1), rs.getString(2), rs.getString(3) ) ); } return ret.toArray(new Sync4jPrincipal[ret.size()]); } catch (SQLException e) { throw new PersistentStoreException("Error reading principals", e); } finally { DBTools.close(conn, stmt, rs); } } /** * Delete the principal from the data store. * * @param p the principal * * @throws PersistentException in case of error reading the data store */ private void deletePrincipal(Sync4jPrincipal p) throws PersistentStoreException { Connection conn = null; PreparedStatement stmt = null; try { conn = dataSource.getConnection(); stmt = conn.prepareStatement(sql[SQL_DELETE_CLIENT_MAPPING]); stmt.setString(1, p.getId()); stmt.executeUpdate(); stmt.close(); stmt = conn.prepareStatement(sql[SQL_DELETE_LAST_SYNC]); stmt.setString(1, p.getId()); stmt.executeUpdate(); stmt.close(); stmt = conn.prepareStatement(sql[SQL_DELETE_PRINCIPAL]); stmt.setString(1, p.getId()); stmt.executeUpdate(); } catch (SQLException e) { throw new PersistentStoreException("Error deleting the principal " + p, e); } finally { DBTools.close(conn, stmt, null); } } /** * Select the number of principals that satisfy the conditions specified in input * * @return int number of principals */ public Object[] readCountPrincipals(Clause clause) throws PersistentStoreException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; int n = 0; try { conn = dataSource.getConnection(); PreparedWhere where = clause.getPreparedWhere(); String query = SQL_COUNT_PRINCIPALS; if (where.sql.length()>0) { query += " where " + where.sql; } stmt = conn.prepareStatement(query); for (int i=0; i<where.parameters.length; ++i) { stmt.setObject(i+1, where.parameters[i]); } rs = stmt.executeQuery(); while (rs.next()) { n = rs.getInt(1); } String[] s = new String[1]; s[0] = "" + n; return s; } catch (SQLException e) { throw new PersistentStoreException("Error reading count principals ", e); } finally { DBTools.close(conn, stmt, rs); } } private void storeSource(Sync4jSource s) throws PersistentStoreException { Connection conn = null; PreparedStatement stmt = null; try { conn = dataSource.getConnection(); stmt = conn.prepareStatement(sql[SQL_UPDATE_SOURCE]); stmt.setString(1, s.getConfig()); stmt.setString(2, s.getUri() ); int n = stmt.executeUpdate(); if (n == 0) { // // The first time!!! // stmt.close(); stmt = conn.prepareStatement(sql[SQL_INSERT_SOURCE]); stmt.setString(1, s.getUri() ); stmt.setString(2, s.getConfig()); stmt.executeUpdate(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -