001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/plain/PlainServicePrototypeAccessor.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.Iterator;
044: import java.util.Collection;
045: import java.util.ArrayList;
046:
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePrototypeAccessor;
048: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePrototype;
049: import es.udc.mypersonalizer.kernel.model.repository.sql.storers.SerializableObject;
050: import es.udc.mypersonalizer.kernel.model.repository.sql.plain.PlainSerializableObjectAccessor;
051: import es.udc.mypersonalizer.kernel.util.exceptions.DuplicateInstanceException;
052: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
053: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
054:
055: /**
056: * This class provides a Plain-based implementation of
057: * <code>ServicePrototypeAccessor</code>. The implementation relies on
058: * <code>PlainSerializableObjectAccessor</code> that implements the logic to
059: * access service prototypes stored as serialized objects in database.
060: *
061: * @author Abel Iago Toral Quiroga
062: * @author Fernando Bellas
063: * @author Abel Muinho
064: * @since 1.0
065: */
066: public class PlainServicePrototypeAccessor implements
067: ServicePrototypeAccessor {
068:
069: private PlainSerializableObjectAccessor accessor;
070:
071: public PlainServicePrototypeAccessor() {
072: accessor = new PlainSerializableObjectAccessor();
073: }
074:
075: public ServicePrototype findServicePrototype(String identifier)
076: throws InternalErrorException, InstanceNotFoundException {
077:
078: String realIdentifier = applyNamingConventionsToIdentifier(identifier);
079: SerializableObject serializableObject = accessor
080: .findSerializableObject(realIdentifier);
081:
082: return ((ServicePrototype) serializableObject.getSerializable());
083: }
084:
085: public void removeServicePrototype(String identifier)
086: throws InternalErrorException, InstanceNotFoundException {
087:
088: String realIdentifier = applyNamingConventionsToIdentifier(identifier);
089: accessor.removeSerializableObject(realIdentifier);
090: }
091:
092: public void updateServicePrototype(ServicePrototype servicePrototype)
093: throws InternalErrorException, InstanceNotFoundException {
094:
095: String realIdentifier = applyNamingConventionsToIdentifier(servicePrototype
096: .getIdentifier());
097:
098: SerializableObject serializableObject = new SerializableObject(
099: realIdentifier, servicePrototype);
100: accessor.updateSerializableObject(serializableObject);
101: }
102:
103: public void addServicePrototype(ServicePrototype servicePrototype)
104: throws InternalErrorException, DuplicateInstanceException {
105:
106: String realIdentifier = applyNamingConventionsToIdentifier(servicePrototype
107: .getIdentifier());
108:
109: SerializableObject serializableObject = new SerializableObject(
110: realIdentifier, servicePrototype);
111: accessor.addSerializableObject(serializableObject);
112: }
113:
114: public Collection findAllServicePrototypes()
115: throws InternalErrorException {
116:
117: Iterator serializableObjectsIterator = accessor
118: .findSerializableObjectsByPattern(
119: "%"
120: + PlainSerializableObjectAccessor.SERVICE_PROTOTYPE_SUFFIX)
121: .iterator();
122:
123: Collection servicePrototypes = new ArrayList();
124: while (serializableObjectsIterator.hasNext()) {
125: SerializableObject serializableObject = (SerializableObject) serializableObjectsIterator
126: .next();
127:
128: ServicePrototype servicePrototype = (ServicePrototype) serializableObject
129: .getSerializable();
130: servicePrototypes.add(servicePrototype);
131:
132: }
133:
134: return servicePrototypes;
135: }
136:
137: /**
138: * Aplies naming conventions to a service identifier to match
139: * its service prototype in the database.
140: * @param identifier The client identifier for the service prototype
141: * (without specific prefixes or sufixes).
142: * @return The identifier with naming conventions aplied.
143: */
144: private String applyNamingConventionsToIdentifier(String identifier) {
145: return (identifier + PlainSerializableObjectAccessor.SERVICE_PROTOTYPE_SUFFIX);
146: }
147:
148: }
|