001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/services/ServiceReplyManager.java,v 1.1.1.1 2004/03/25 12:08:39 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:39 $
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.portal.controller.services;
042:
043: import es.udc.mypersonalizer.portal.conventions.ServiceConfigurationConventions;
044: import es.udc.mypersonalizer.kernel.log.Log;
045: import es.udc.mypersonalizer.kernel.log.LogManager;
046: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.CachedServiceReply;
048: import es.udc.mypersonalizer.kernel.model.repository.interfaces.CachedServiceReplyAccessor;
049: import es.udc.mypersonalizer.kernel.model.repository.interfaces.RepositoryAccessorFactory;
050: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceButtonsState;
051: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceConfiguration;
052: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceConfigurationRegistrySingleton;
053: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceProperty;
054: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePropertyKey;
055: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
056: import javax.servlet.http.HttpServletRequest;
057: import javax.servlet.http.HttpServletResponse;
058:
059: /**
060: * This class is in charge of calling a service based on the configuration
061: * parameters which are set to it, and returning the reply. It follows
062: * the following steps:
063: * <ol>
064: * <li>Asks the {@link ServiceControllerRegistrySingleton} for the
065: * registered controller for the service.</li>
066: * <li>Asks the service controller to get the reply of the service,
067: * according to the configuration set in its ServiceProperty and
068: * the state of the service buttons.</li>
069: * <li>If the service has been configured as "cacheable" (with
070: * the service configuration parameter <tt>doCache</tt>, puts the
071: * service reply into the cache.</li>
072: * </ol>
073: *
074: * @author Daniel Fernandez
075: * @since 1.0
076: */
077: public class ServiceReplyManager {
078:
079: static {
080:
081: Log mypersonalizerLog = LogManager
082: .getLog(LogNamingConventions.MYPERSONALIZER);
083:
084: try {
085: /*
086: * We need this to make the CacheCleaner thread start
087: */
088: Class.forName("es.udc.mypersonalizer.portal.controller."
089: + "services.CachedServiceReplyCleaner");
090: } catch (Exception e) {
091: mypersonalizerLog.write("Unable to start thread for "
092: + "CachedServiceReplyCleaner", e,
093: ServiceReplyManager.class);
094: }
095: }
096:
097: /**
098: * Avoids this class from being instantiated.
099: */
100: private ServiceReplyManager() {
101: }
102:
103: /**
104: * Performs the action of getting the reply for a service.
105: *
106: * @param request the request object.
107: * @param response the response object.
108: * @param servicePropertyKey the service property key.
109: * @param serviceProperty the service property.
110: * @param buttons the service buttons state object.
111: * @throws ServiceNotAvailableException if the service is not available
112: * (any errors happen during the request).
113: * @throws ServiceTimeOutException if the service exceeds the timeout.
114: * @throws InternalErrorException
115: * @return the service reply.
116: */
117: public static String getReply(HttpServletRequest request,
118: HttpServletResponse response,
119: ServicePropertyKey servicePropertyKey,
120: ServiceProperty serviceProperty, ServiceButtonsState buttons)
121: throws ServiceNotAvailableException,
122: ServiceTimeOutException, InternalErrorException {
123:
124: ServiceControllerRegistrySingleton serviceControllerRegistrySingleton = ServiceControllerRegistrySingleton
125: .getInstance();
126:
127: ServiceController serviceController = serviceControllerRegistrySingleton
128: .getServiceController(servicePropertyKey
129: .getServiceIdentifier());
130:
131: String reply = serviceController.getPersonalizedReply(request,
132: response, servicePropertyKey, serviceProperty, buttons);
133:
134: /*
135: * Now we have to see if the service is cacheable and, if it is,
136: * push the reply into the cache
137: */
138: ServiceConfigurationRegistrySingleton serviceConfigurationRegistrySingleton = ServiceConfigurationRegistrySingleton
139: .getInstance();
140:
141: ServiceConfiguration serviceConfiguration = serviceConfigurationRegistrySingleton
142: .getServiceConfiguration(servicePropertyKey
143: .getServiceIdentifier());
144:
145: String isCacheableStr = (String) serviceConfiguration
146: .getParameters().get(
147: ServiceConfigurationConventions.DO_CACHE);
148:
149: if ((new Boolean(isCacheableStr)).booleanValue()) {
150:
151: RepositoryAccessorFactory repositoryAccessorFactory = RepositoryAccessorFactory
152: .getInstance();
153:
154: CachedServiceReplyAccessor cachedServiceReplyAccessor = repositoryAccessorFactory
155: .createCachedServiceReplyAccessor();
156:
157: CachedServiceReply cachedServiceReply = new CachedServiceReply(
158: servicePropertyKey, reply);
159:
160: cachedServiceReplyAccessor
161: .updateCachedServiceReply(cachedServiceReply);
162:
163: }
164:
165: return reply;
166:
167: }
168:
169: }
|