001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.txtimer;
023:
024: // $Id: OracleDatabasePersistencePlugin.java 62306 2007-04-12 18:20:11Z dimitris@jboss.org $
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.InputStream;
028: import java.io.Serializable;
029: import java.sql.Connection;
030: import java.sql.PreparedStatement;
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033: import java.sql.Statement;
034: import java.sql.Timestamp;
035: import java.util.ArrayList;
036: import java.util.Date;
037: import java.util.List;
038:
039: import javax.management.ObjectName;
040:
041: import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
042: import org.jboss.logging.Logger;
043:
044: /**
045: * This DatabasePersistencePlugin uses getBinaryStream/setBinaryStream to persist the
046: * serializable objects associated with the timer.
047: *
048: * @author Thomas.Diesler@jboss.org
049: * @author Dimitris.Andreadis@jboss.org
050: * @version $Revision: 62306 $
051: * @since 23-Sep-2004
052: */
053: public class OracleDatabasePersistencePlugin extends
054: GeneralPurposeDatabasePersistencePlugin {
055: // logging support
056: private static Logger log = Logger
057: .getLogger(OracleDatabasePersistencePlugin.class);
058:
059: /**
060: * Insert a timer object
061: */
062: public void insertTimer(String timerId,
063: TimedObjectId timedObjectId, Date initialExpiration,
064: long intervalDuration, Serializable info)
065: throws SQLException {
066: Connection con = null;
067: PreparedStatement st = null;
068: try {
069: con = ds.getConnection();
070:
071: String sql = "insert into " + getTableName() + " " + "("
072: + getColumnTimerID() + "," + getColumnTargetID()
073: + "," + getColumnInitialDate() + ","
074: + getColumnTimerInterval() + ","
075: + getColumnInstancePK() + "," + getColumnInfo()
076: + ") " + "values (?,?,?,?,?,?)";
077: st = con.prepareStatement(sql);
078:
079: st.setString(1, timerId);
080: st.setString(2, timedObjectId.toString());
081: st.setTimestamp(3, new Timestamp(initialExpiration
082: .getTime()));
083: st.setLong(4, intervalDuration);
084:
085: byte[] pkArr = serialize(timedObjectId.getInstancePk());
086: if (pkArr != null) {
087: InputStream is = new ByteArrayInputStream(pkArr);
088: st.setBinaryStream(5, is, pkArr.length);
089: } else {
090: st.setBytes(5, null);
091: }
092:
093: byte[] infoArr = serialize(info);
094: if (infoArr != null) {
095: InputStream is = new ByteArrayInputStream(infoArr);
096: st.setBinaryStream(6, is, infoArr.length);
097: } else {
098: st.setBytes(6, null);
099: }
100:
101: int rows = st.executeUpdate();
102: if (rows != 1)
103: log.error("Unable to insert timer for: "
104: + timedObjectId);
105: } finally {
106: JDBCUtil.safeClose(st);
107: JDBCUtil.safeClose(con);
108: }
109: }
110:
111: /**
112: * Select a list of currently persisted timer handles
113: *
114: * @return List<TimerHandleImpl>
115: */
116: public List selectTimers(ObjectName containerId)
117: throws SQLException {
118: Connection con = null;
119: Statement st = null;
120: ResultSet rs = null;
121: try {
122: con = ds.getConnection();
123:
124: List list = new ArrayList();
125:
126: st = con.createStatement();
127: rs = st.executeQuery("select * from " + getTableName());
128: while (rs.next()) {
129: String timerId = rs.getString(getColumnTimerID());
130: TimedObjectId targetId = TimedObjectId.parse(rs
131: .getString(getColumnTargetID()));
132:
133: // add this handle to the returned list, if a null containerId was used
134: // or the containerId filter matches
135: if (containerId == null
136: || containerId
137: .equals(targetId.getContainerId())) {
138: Date initialDate = rs
139: .getTimestamp(getColumnInitialDate());
140: long interval = rs
141: .getLong(getColumnTimerInterval());
142:
143: InputStream isPk = rs
144: .getBinaryStream(getColumnInstancePK());
145: Serializable pKey = (Serializable) deserialize(isPk);
146: Serializable info = null;
147: try {
148: InputStream isInfo = rs
149: .getBinaryStream(getColumnInfo());
150: info = (Serializable) deserialize(isInfo);
151: } catch (Exception e) {
152: // may happen if listing all handles (containerId is null)
153: // with a stored custom info object coming from a scoped
154: // deployment.
155: log
156: .warn(
157: "Cannot deserialize custom info object",
158: e);
159: }
160: // is this really needed? targetId encapsulates pKey as well!
161: targetId = new TimedObjectId(targetId
162: .getContainerId(), pKey);
163: TimerHandleImpl handle = new TimerHandleImpl(
164: timerId, targetId, initialDate, interval,
165: info);
166:
167: list.add(handle);
168: }
169: }
170: return list;
171: } finally {
172: JDBCUtil.safeClose(rs);
173: JDBCUtil.safeClose(st);
174: JDBCUtil.safeClose(con);
175: }
176: }
177: }
|