KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > txtimer > OracleDatabasePersistencePlugin


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ejb.txtimer;
23
24 // $Id: OracleDatabasePersistencePlugin.java 37858 2005-11-05 17:06:17Z dimitris $
25

26 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
27 import org.jboss.logging.Logger;
28
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.sql.Connection JavaDoc;
33 import java.sql.PreparedStatement JavaDoc;
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37 import java.sql.Timestamp JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Date JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * This DatabasePersistencePlugin uses getBinaryStream/setBinaryStream to persist the
44  * serializable objects associated with the timer.
45  *
46  * @author Thomas.Diesler@jboss.org
47  * @version $Revision: 37858 $
48  * @since 23-Sep-2004
49  */

50 public class OracleDatabasePersistencePlugin extends GeneralPurposeDatabasePersistencePlugin
51 {
52    // logging support
53
private static Logger log = Logger.getLogger(OracleDatabasePersistencePlugin.class);
54
55    /** Insert a timer object */
56    public void insertTimer(String JavaDoc timerId, TimedObjectId timedObjectId, Date JavaDoc initialExpiration, long intervalDuration, Serializable JavaDoc info)
57            throws SQLException JavaDoc
58    {
59       Connection JavaDoc con = null;
60       PreparedStatement JavaDoc st = null;
61       try
62       {
63          con = ds.getConnection();
64
65          String JavaDoc sql = "insert into " + getTableName() + " " +
66                  "(" + getColumnTimerID() + "," + getColumnTargetID() + "," + getColumnInitialDate() + "," + getColumnTimerInterval() + "," + getColumnInstancePK() + "," + getColumnInfo() + ") " +
67                  "values (?,?,?,?,?,?)";
68          st = con.prepareStatement(sql);
69
70          st.setString(1, timerId);
71          st.setString(2, timedObjectId.toString());
72          st.setTimestamp(3, new Timestamp JavaDoc(initialExpiration.getTime()));
73          st.setLong(4, intervalDuration);
74
75          byte[] pkArr = serialize(timedObjectId.getInstancePk());
76          if (pkArr != null)
77          {
78             InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(pkArr);
79             st.setBinaryStream(5, is, pkArr.length);
80          }
81          else
82          {
83             st.setBytes(5, null);
84          }
85
86          byte[] infoArr = serialize(info);
87          if (infoArr != null)
88          {
89             InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(infoArr);
90             st.setBinaryStream(6, is, infoArr.length);
91          }
92          else
93          {
94             st.setBytes(6, null);
95          }
96
97          int rows = st.executeUpdate();
98          if (rows != 1)
99             log.error("Unable to insert timer for: " + timedObjectId);
100       }
101       finally
102       {
103          JDBCUtil.safeClose(st);
104          JDBCUtil.safeClose(con);
105       }
106    }
107
108    /** Select a list of currently persisted timer handles
109     * @return List<TimerHandleImpl>
110     */

111    public List JavaDoc selectTimers()
112            throws SQLException JavaDoc
113    {
114       Connection JavaDoc con = null;
115       Statement JavaDoc st = null;
116       ResultSet JavaDoc rs = null;
117       try
118       {
119          con = ds.getConnection();
120
121          List JavaDoc list = new ArrayList JavaDoc();
122
123          st = con.createStatement();
124          rs = st.executeQuery("select * from " + getTableName());
125          while (rs.next())
126          {
127             String JavaDoc timerId = rs.getString(getColumnTimerID());
128             TimedObjectId targetId = TimedObjectId.parse(rs.getString(getColumnTargetID()));
129             Date JavaDoc initialDate = rs.getTimestamp(getColumnInitialDate());
130             long interval = rs.getLong(getColumnTimerInterval());
131
132             InputStream JavaDoc isPk = rs.getBinaryStream(getColumnInstancePK());
133             Serializable JavaDoc pKey = (Serializable JavaDoc)deserialize(isPk);
134             InputStream JavaDoc isInfo = rs.getBinaryStream(getColumnInfo());
135             Serializable JavaDoc info = (Serializable JavaDoc)deserialize(isInfo);
136
137             targetId = new TimedObjectId(targetId.getContainerId(), pKey);
138             TimerHandleImpl handle = new TimerHandleImpl(timerId, targetId, initialDate, interval, info);
139             list.add(handle);
140          }
141
142          return list;
143       }
144       finally
145       {
146          JDBCUtil.safeClose(rs);
147          JDBCUtil.safeClose(st);
148          JDBCUtil.safeClose(con);
149       }
150    }
151 }
152
153
Popular Tags