001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/plain/PlainCachedServiceReplyAccessor.java,v 1.1.1.1 2004/03/25 12:08:36 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:36 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.kernel.model.repository.sql.plain;
042:
043: import java.util.Map;
044: import java.util.Collection;
045: import java.sql.Connection;
046:
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.CachedServiceReplyAccessor;
048: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePropertyKey;
049: import es.udc.mypersonalizer.kernel.model.repository.interfaces.CachedServiceReply;
050: import es.udc.mypersonalizer.kernel.model.repository.sql.storers.CachedServiceReplyStorer;
051: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
052:
053: /**
054: * This class provides a Plain-based implementation of
055: * {@link CachedServiceReplyAccessor}. The implementation uses the
056: * {@link TransactionManager} which returns pooled connections,
057: * and needs to rely on the storer to perform the requested
058: * operation.
059: *
060: * @author Fernando Bellas
061: * @since 1.0
062: */
063: public class PlainCachedServiceReplyAccessor implements
064: CachedServiceReplyAccessor {
065:
066: private CachedServiceReplyStorer storer;
067:
068: /**
069: * Creates an instance of this accessor.
070: */
071: public PlainCachedServiceReplyAccessor() {
072: storer = new CachedServiceReplyStorer();
073: }
074:
075: public CachedServiceReply findCachedServiceReply(
076: ServicePropertyKey key) throws InternalErrorException {
077:
078: Transaction transaction = null;
079:
080: try {
081:
082: /* Get the transaction and the associated connection. */
083: transaction = TransactionManager.getInstance()
084: .getTransaction(Transaction.TRANSACTION_SUPPORTS);
085: Connection connection = transaction.getConnection();
086:
087: /* Delegate on the storer. */
088: CachedServiceReply cachedServiceReply = storer
089: .findCachedServiceReply(connection, key);
090:
091: /* Release transaction and return result. */
092: transaction.release();
093:
094: return cachedServiceReply;
095:
096: } catch (InternalErrorException e) {
097: rollbackTransaction(transaction);
098: throw e;
099: } catch (RuntimeException e) {
100: rollbackTransaction(transaction);
101: throw new InternalErrorException(e);
102: }
103:
104: }
105:
106: public Map findCachedServiceReplies(Collection keys)
107: throws InternalErrorException {
108:
109: Transaction transaction = null;
110:
111: try {
112:
113: /* Get the transaction and the associated connection. */
114: transaction = TransactionManager.getInstance()
115: .getTransaction(Transaction.TRANSACTION_SUPPORTS);
116: Connection connection = transaction.getConnection();
117:
118: /* Delegate on the storer. */
119: Map cachedServiceReplies = storer.findCachedServiceReplies(
120: connection, keys);
121:
122: /* Release transaction and return result. */
123: transaction.release();
124:
125: return cachedServiceReplies;
126:
127: } catch (InternalErrorException e) {
128: rollbackTransaction(transaction);
129: throw e;
130: } catch (RuntimeException e) {
131: rollbackTransaction(transaction);
132: throw new InternalErrorException(e);
133: }
134:
135: }
136:
137: public void removeCachedServiceReply(ServicePropertyKey key)
138: throws InternalErrorException {
139:
140: Transaction transaction = null;
141:
142: try {
143:
144: /* Get the transaction and the associated connection. */
145: transaction = TransactionManager.getInstance()
146: .getTransaction(Transaction.TRANSACTION_REQUIRED);
147: Connection connection = transaction.getConnection();
148:
149: /* Delegate on the storer. */
150: storer.removeCachedServiceReply(connection, key);
151:
152: /* Release transaction. */
153: transaction.release();
154:
155: } catch (InternalErrorException e) {
156: rollbackTransaction(transaction);
157: throw e;
158: } catch (RuntimeException e) {
159: rollbackTransaction(transaction);
160: throw new InternalErrorException(e);
161: }
162: }
163:
164: public void updateCachedServiceReply(
165: CachedServiceReply cachedServiceReply)
166: throws InternalErrorException {
167:
168: Transaction transaction = null;
169:
170: try {
171:
172: /* Get the transaction and the associated connection. */
173: transaction = TransactionManager.getInstance()
174: .getTransaction(Transaction.TRANSACTION_REQUIRED);
175: Connection connection = transaction.getConnection();
176:
177: /* Delegate on the storer. */
178: storer.updateCachedServiceReply(connection,
179: cachedServiceReply);
180:
181: /* Release transaction. */
182: transaction.release();
183:
184: } catch (InternalErrorException e) {
185: rollbackTransaction(transaction);
186: throw e;
187: } catch (RuntimeException e) {
188: rollbackTransaction(transaction);
189: throw new InternalErrorException(e);
190: }
191: }
192:
193: public int removeOldCachedServiceReplies(int timePeriod)
194: throws InternalErrorException {
195:
196: Transaction transaction = null;
197:
198: try {
199:
200: /* Get the transaction and the associated connection. */
201: transaction = TransactionManager.getInstance()
202: .getTransaction(Transaction.TRANSACTION_REQUIRED);
203: Connection connection = transaction.getConnection();
204:
205: /* Delegate on the storer. */
206: int removedReplies = storer.removeOldCachedServiceReplies(
207: connection, timePeriod);
208:
209: /* Release transaction and return result. */
210: transaction.release();
211:
212: return removedReplies;
213:
214: } catch (InternalErrorException e) {
215: rollbackTransaction(transaction);
216: throw e;
217: } catch (RuntimeException e) {
218: rollbackTransaction(transaction);
219: throw new InternalErrorException(e);
220: }
221: }
222:
223: public int removeOldCachedServiceReplies(String serviceIdentifier)
224: throws InternalErrorException {
225:
226: Transaction transaction = null;
227:
228: try {
229:
230: /* Get the transaction and the associated connection. */
231: transaction = TransactionManager.getInstance()
232: .getTransaction(Transaction.TRANSACTION_REQUIRED);
233: Connection connection = transaction.getConnection();
234:
235: /* Delegate on the storer. */
236: int removedReplies = storer.removeOldCachedServiceReplies(
237: connection, serviceIdentifier);
238:
239: /* Release transaction and return result. */
240: transaction.release();
241:
242: return removedReplies;
243:
244: } catch (InternalErrorException e) {
245: rollbackTransaction(transaction);
246: throw e;
247: } catch (RuntimeException e) {
248: rollbackTransaction(transaction);
249: throw new InternalErrorException(e);
250: }
251:
252: }
253:
254: private void rollbackTransaction(Transaction transaction)
255: throws InternalErrorException {
256:
257: if (transaction != null) {
258: transaction.rollback();
259: }
260: }
261:
262: }
|