001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/plain/PlainServicePropertyAccessor.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.sql.Connection;
044:
045: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePropertyAccessor;
046: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceProperty;
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePropertyKey;
048: import es.udc.mypersonalizer.kernel.model.repository.sql.storers.ServicePropertyStorer;
049: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
050: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
051: import es.udc.mypersonalizer.kernel.model.properties.Property;
052:
053: /**
054: * This class provides a Plain-based implementation of
055: * {@link ServicePropertyAccessor}. 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 PlainServicePropertyAccessor implements
064: ServicePropertyAccessor {
065:
066: private ServicePropertyStorer storer;
067:
068: /**
069: * Creates an instance of this accessor.
070: */
071: public PlainServicePropertyAccessor() {
072: storer = new ServicePropertyStorer();
073: }
074:
075: public ServiceProperty findServiceProperty(ServicePropertyKey key)
076: throws InternalErrorException, InstanceNotFoundException {
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: ServiceProperty serviceProperty = storer
089: .findServiceProperty(connection, key);
090:
091: /* Release transaction and return result. */
092: transaction.release();
093:
094: return serviceProperty;
095:
096: } catch (InternalErrorException e) {
097: rollbackTransaction(transaction);
098: throw e;
099: } catch (InstanceNotFoundException e) {
100: transaction.release();
101: throw e;
102: } catch (RuntimeException e) {
103: rollbackTransaction(transaction);
104: throw new InternalErrorException(e);
105: }
106: }
107:
108: public void removeServiceProperty(ServicePropertyKey key)
109: throws InternalErrorException, InstanceNotFoundException {
110:
111: Transaction transaction = null;
112:
113: try {
114:
115: /* Get the transaction and the associated connection. */
116: transaction = TransactionManager.getInstance()
117: .getTransaction(Transaction.TRANSACTION_REQUIRED);
118: Connection connection = transaction.getConnection();
119:
120: /* Delegate on the storer. */
121: storer.removeServiceProperty(connection, key);
122:
123: /* Release transaction. */
124: transaction.release();
125:
126: } catch (InternalErrorException e) {
127: rollbackTransaction(transaction);
128: throw e;
129: } catch (InstanceNotFoundException e) {
130: transaction.release();
131: throw e;
132: } catch (RuntimeException e) {
133: rollbackTransaction(transaction);
134: throw new InternalErrorException(e);
135: }
136: }
137:
138: public void updateServiceProperty(ServiceProperty serviceProperty)
139: throws InternalErrorException, InstanceNotFoundException {
140:
141: Transaction transaction = null;
142:
143: try {
144:
145: /* Get the transaction and the associated connection. */
146: transaction = TransactionManager.getInstance()
147: .getTransaction(Transaction.TRANSACTION_REQUIRED);
148: Connection connection = transaction.getConnection();
149:
150: /* Delegate on the storer. */
151: storer.updateServiceProperty(connection, serviceProperty);
152:
153: /* Release transaction. */
154: transaction.release();
155:
156: } catch (InternalErrorException e) {
157: rollbackTransaction(transaction);
158: throw e;
159: } catch (InstanceNotFoundException e) {
160: transaction.release();
161: throw e;
162: } catch (RuntimeException e) {
163: rollbackTransaction(transaction);
164: throw new InternalErrorException(e);
165: }
166: }
167:
168: public ServiceProperty addServiceProperty(String serviceIdentifier,
169: Property property) throws InternalErrorException {
170:
171: Transaction transaction = null;
172:
173: try {
174:
175: /* Get the transaction and the associated connection. */
176: transaction = TransactionManager.getInstance()
177: .getTransaction(Transaction.TRANSACTION_REQUIRED);
178: Connection connection = transaction.getConnection();
179:
180: /* Delegate on the storer. */
181: Long propertyIdentifier = storer.addServiceProperty(
182: connection, serviceIdentifier, property);
183: ServicePropertyKey key = new ServicePropertyKey(
184: serviceIdentifier, propertyIdentifier);
185: ServiceProperty serviceProperty = new ServiceProperty(key,
186: property);
187:
188: /* Release transaction and return result. */
189: transaction.release();
190:
191: return serviceProperty;
192:
193: } catch (InternalErrorException e) {
194: rollbackTransaction(transaction);
195: throw e;
196: } catch (RuntimeException e) {
197: rollbackTransaction(transaction);
198: throw new InternalErrorException(e);
199: }
200: }
201:
202: private void rollbackTransaction(Transaction transaction)
203: throws InternalErrorException {
204:
205: if (transaction != null) {
206: transaction.rollback();
207: }
208: }
209:
210: }
|