📄 sessionimpl.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: SessionImpl.java,v 1.45 2005/05/17 16:41:06 jmettraux Exp $ *///// SessionImpl.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.1.00 16.08.2003 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.xdbc;import openwfe.org.Utils;import openwfe.org.ServiceException;import openwfe.org.ApplicationContext;import openwfe.org.xml.XmlUtils;import openwfe.org.sql.SqlUtils;import openwfe.org.sql.ds.OwfeDataSource;import openwfe.org.misc.UniqueIdGenerator;import openwfe.org.query.item.QueryItem;import openwfe.org.xdbc.nquery.IdSet;/** * The implementation of Xdbc Session * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: SessionImpl.java,v 1.45 2005/05/17 16:41:06 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public class SessionImpl implements Session{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(SessionImpl.class.getName()); // // CONSTANTS & co private final static String DOCUMENT = "xdocument"; private final static String ELEMENT = "xelement"; private final static String ATTRIBUTE = "xattribute"; // // FIELDS private java.sql.Connection connection = null; private OwfeDataSource dataSource = null; private UniqueIdGenerator uiGenerator = null; // // CONSTRUCTORS public void init (java.sql.Connection con, int idPrefix) throws XdbcException { this.connection = con; this.uiGenerator = new UniqueIdGenerator(idPrefix); } public void init (OwfeDataSource ds, int idPrefix) throws XdbcException { this.dataSource = ds; this.uiGenerator = new UniqueIdGenerator(idPrefix); log.info("init() with datasource '"+ds.getName()+"'"); } // // METHODS from Session public void close () throws XdbcException { releaseConnection(); } public long insert (String clientId, String xmlDocument) throws XdbcException { //log.debug(">>> incoming document \n\n"+xmlDocument+"\n\n<<<"); try { return insert (clientId, XmlUtils.extractXmlDocument(xmlDocument, false)); } catch (Exception e) { throw new XdbcException ("Failed to parse string to xml", e); } } public long insert (final String clientId, final org.jdom.Document doc) throws XdbcException { return insert(-1, clientId, doc); } public void update (final long xmlDocumentId, final String clientId, final String xmlDocument) throws XdbcException { // // here too transaction would be good... remove(xmlDocumentId); try { insert (xmlDocumentId, clientId, XmlUtils.extractXmlDocument(xmlDocument, false)); } catch (Exception e) { throw new XdbcException ("Failed to re-insert document", e); } } public void update (final long xmlDocumentId, final String clientId, final org.jdom.Document doc) throws XdbcException { // // here too transaction would be good... remove(xmlDocumentId); insert(xmlDocumentId, clientId, doc); } public void remove (final long xmlDocumentId) throws XdbcException { java.sql.Statement st = null; try { st = getConnection().createStatement(); // // BEGIN TRANSACTION would be nice // // delete from document st.executeUpdate ("DELETE FROM "+DOCUMENT+" WHERE id="+xmlDocumentId); // // delete from element st.executeUpdate ("DELETE FROM "+ELEMENT+" WHERE documentId="+xmlDocumentId); // // delete from attribute st.executeUpdate ("DELETE FROM "+ATTRIBUTE+" WHERE documentId="+xmlDocumentId); } catch (java.sql.SQLException se) { throw new XdbcException ("Failed to remove document from db", se); } finally { try { releaseConnection(); st.close(); } catch (Exception e) { // forget it } } } public long remove (final String clientId) throws XdbcException { long id = determineDocumentId(clientId); remove(id); return id; } public org.jdom.Document find (final long xmlDocumentId) throws XdbcException { java.util.List docIds = new java.util.ArrayList(1); docIds.add(new Long(xmlDocumentId)); java.util.List result = find(docIds); if (result == null || result.size() < 1) return null; return (org.jdom.Document)result.get(0); } public String find (final long xmlDocumentId, final String encoding) throws XdbcException { org.jdom.Document doc = find(xmlDocumentId); return XmlUtils.toString(doc, encoding); } public java.util.List find (final java.util.List documentIds, final String encoding) throws XdbcException { java.util.List documents = find(documentIds); if (encoding == null) return documents; java.util.List result = new java.util.ArrayList(documents.size()); java.util.Iterator it = documents.iterator(); while (it.hasNext()) { result.add (XmlUtils.toString((org.jdom.Document)it.next(), encoding)); } return result; } public org.jdom.Document find (final String clientId) throws XdbcException { return find(determineDocumentId(clientId)); } public String find (String clientId, String encoding) throws XdbcException { return find(determineDocumentId(clientId), encoding); } public IdSet query (final String clientIdLike, final QueryItem query) throws XdbcException { try { return (IdSet)query .query(new Object[] { clientIdLike, getConnection() }); } catch (final Exception e) { throw new XdbcException ("Query failure", e); } finally { try { releaseConnection(); } catch (Exception e) { // ignore } } } public IdSet query (final QueryItem query) throws XdbcException { return query(null, query); } public java.util.List determineDocumentIds (String clientIdLike) throws XdbcException { return determineDocumentIds(new String[] { clientIdLike }); } /** * Returns a list of documentIds based on the LIKE sql strings * passed as a parameter. */ public java.util.List determineDocumentIds (String[] clientIdLikes) throws XdbcException { java.sql.Statement st = null; java.sql.ResultSet rs = null; try { st = getConnection().createStatement(); StringBuffer sb = new StringBuffer(); sb.append("SELECT id FROM "); sb.append(DOCUMENT); sb.append(" WHERE "); for (int i=0; i<clientIdLikes.length; i++) { sb.append("clientId LIKE '"); sb.append(clientIdLikes[i]); sb.append("' AND "); } sb.append("isReady = 1"); String query = sb.toString(); //log.debug("determineDocumentIds >\n"+query+"\n<"); rs = st.executeQuery(query); java.util.List result = new java.util.ArrayList(); while(rs.next()) result.add(new Long(rs.getLong(1))); log.debug("found "+result.size()+" documents"); return result; } catch (final java.sql.SQLException se) { final StringBuffer sb = new StringBuffer(); for (int i=0; i<clientIdLikes.length; i++) { if (i > 0) sb.append(", "); sb.append("'"); sb.append(clientIdLikes[i]); sb.append("'"); } throw new XdbcException ("Failed to determine documentIds from clientId LIKE "+ sb.toString(), se); } finally { try { releaseConnection(); rs.close(); st.close(); } catch (Exception e) { // forget it } } } public int count (String clientIdLike) throws XdbcException { java.sql.Statement st = null; java.sql.ResultSet rs = null; try { st = getConnection().createStatement(); StringBuffer sb = new StringBuffer(); sb.append("SELECT COUNT(id) FROM "); sb.append(DOCUMENT); sb.append(" WHERE clientId LIKE '"); sb.append(clientIdLike); sb.append("' AND isReady = 1"); String query = sb.toString(); log.debug("count >\n"+query+"\n<"); rs = st.executeQuery(query); int result = 0; if (rs.next()) result = rs.getInt(1); log.debug("counted "+result+" documents"); return result; } catch (java.sql.SQLException se) { throw new XdbcException ("Failed to count documents from clientId LIKE '"+ clientIdLike+"'", se); } finally { try { releaseConnection(); rs.close(); st.close(); } catch (Exception e) { // forget it } } } public java.util.List listClientIds (String clientIdLike) throws XdbcException { java.sql.Statement st = null; java.sql.ResultSet rs = null; try { st = getConnection().createStatement(); StringBuffer sb = new StringBuffer(); sb.append("SELECT clientId FROM "); sb.append(DOCUMENT); sb.append(" WHERE clientId LIKE '"); sb.append(clientIdLike); sb.append("' AND isReady = 1"); String query = sb.toString(); log.debug("listClientIds >\n"+query+"\n<"); rs = st.executeQuery(query); java.util.List result = new java.util.ArrayList(); while (rs.next()) result.add(rs.getString(1)); return result; } catch (java.sql.SQLException se) { throw new XdbcException ("Failed to list clientIds LIKE '"+ clientIdLike+"'", se); } finally { try { releaseConnection(); rs.close(); st.close(); } catch (Exception e) { // forget it } } } public long determineDocumentId (final String clientId) throws XdbcException { java.sql.Statement st = null; java.sql.ResultSet rs = null; try { st = getConnection().createStatement(); StringBuffer sb = new StringBuffer(); sb.append("SELECT id FROM "); sb.append(DOCUMENT); sb.append(" WHERE clientId = '"); sb.append(clientId); sb.append("' AND isReady = 1"); String query = sb.toString(); rs = st.executeQuery(query); if (rs.next()) return rs.getLong(1); return -1; } catch (java.sql.SQLException se) { throw new XdbcException ("Failed to determine documentId from clientId '"+clientId+"'", se); } finally { try { releaseConnection(); rs.close(); st.close(); } catch (Exception e) { // forget it } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -