001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/services/CachedServiceReplyCleaner.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.kernel.log.Log;
044: import es.udc.mypersonalizer.kernel.log.LogManager;
045: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
046: import es.udc.mypersonalizer.kernel.model.repository.interfaces.CachedServiceReplyAccessor;
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.RepositoryAccessorFactory;
048: import es.udc.mypersonalizer.portal.config.CachedServiceReplyCleanerConfig;
049: import es.udc.mypersonalizer.portal.config.PortalConfig;
050: import es.udc.mypersonalizer.portal.config.PortalConfigManager;
051:
052: /**
053: * This thread is responsible for cleaning the service reply cache. It
054: * reads its configuration from {@link CachedServiceReplyCleanerConfig},
055: * and, if it is configured to be <i>active</i>, performs regularly
056: * (its period is a configuration parameter) a removal of the
057: * replies older than certain age (from configuration) from the cache.
058: *
059: * @author Daniel Fernandez
060: * @since 1.0
061: */
062: public class CachedServiceReplyCleaner extends Thread {
063:
064: /**
065: * The period for the thread activation.
066: */
067: private int cleaningPeriodSeconds;
068:
069: /**
070: * The maximum age a service reply can live in the cache before being
071: * removed.
072: */
073: private int entryLifeTimeSeconds;
074:
075: /**
076: * The log in which the thread will write any messages.
077: */
078: private Log log;
079:
080: /**
081: * The error log of MyPersonalizer.
082: */
083: private static Log mypersonalizerLog = null;
084:
085: /*
086: * We get the configuration and, if the cleaner is set to be active,
087: * initialize the thread with the configuration parameters read.
088: */
089: static {
090:
091: try {
092:
093: PortalConfig portalConfig = PortalConfigManager.getConfig();
094:
095: CachedServiceReplyCleanerConfig cachedServiceReplyCleanerConfig = portalConfig
096: .getPortalControllerConfig()
097: .getCachedServiceReplyCleanerConfig();
098:
099: String cleanerActiveStr = cachedServiceReplyCleanerConfig
100: .getCleanerActive();
101:
102: Log mypersonalizerLog = LogManager
103: .getLog(LogNamingConventions.MYPERSONALIZER);
104:
105: boolean cleanerActive = (new Boolean(cleanerActiveStr))
106: .booleanValue();
107:
108: if (cleanerActive) {
109:
110: String cleaningPeriodSecondsStr = cachedServiceReplyCleanerConfig
111: .getCleaningPeriodSeconds();
112: String serviceReplyMaximumLifeTimeSecondsStr = cachedServiceReplyCleanerConfig
113: .getServiceReplyMaximumLifeTimeSeconds();
114:
115: int cleaningPeriodSeconds = (new Integer(
116: cleaningPeriodSecondsStr)).intValue();
117: int serviceReplyMaximumLifeTimeSeconds = (new Integer(
118: serviceReplyMaximumLifeTimeSecondsStr))
119: .intValue();
120:
121: /* We create the thread */
122: CachedServiceReplyCleaner cachedServiceReplyCleaner = new CachedServiceReplyCleaner(
123: cleaningPeriodSeconds,
124: serviceReplyMaximumLifeTimeSeconds,
125: mypersonalizerLog);
126:
127: /* ...and we start it */
128: cachedServiceReplyCleaner.start();
129:
130: mypersonalizerLog.write("Cache cleaner active. "
131: + "Cleaning time period seconds: "
132: + cleaningPeriodSeconds
133: + ". Entries life time seconds: "
134: + serviceReplyMaximumLifeTimeSeconds, null, // no exception
135: CachedServiceReplyCleaner.class);
136:
137: } else {
138: mypersonalizerLog.write("Cache cleaner inactive.",
139: null, // no exception
140: CachedServiceReplyCleaner.class);
141: }
142:
143: } catch (Exception e) {
144:
145: Log mypersonalizerLog = LogManager
146: .getLog(LogNamingConventions.MYPERSONALIZER);
147: mypersonalizerLog.write(
148: "Could not initialize configuration for "
149: + "CachedServiceReplyCleaner", e,
150: CachedServiceReplyCleaner.class);
151:
152: }
153:
154: }
155:
156: /**
157: * Creates the thread and initializes its parameters.
158: *
159: * @param cleaningPeriodSeconds the activation period
160: * @param entryLifeTimeSeconds maximum age allowed for an entry.
161: * @param log the thread's log.
162: */
163: public CachedServiceReplyCleaner(int cleaningPeriodSeconds,
164: int entryLifeTimeSeconds, Log log) {
165:
166: this .cleaningPeriodSeconds = cleaningPeriodSeconds;
167: this .entryLifeTimeSeconds = entryLifeTimeSeconds;
168: this .log = log;
169: }
170:
171: /**
172: * Performs a cyclic removal of old entries from the cache. It's activation
173: * period is indicated by the <tt>cleaningTimePeriodSeconds</tt> field.
174: */
175: public void run() {
176: int removed;
177: RepositoryAccessorFactory factory = RepositoryAccessorFactory
178: .getInstance();
179: CachedServiceReplyAccessor accessor = null;
180:
181: while (true) {
182: try {
183: /* We wait the activation period. */
184: sleep(cleaningPeriodSeconds * 1000);
185:
186: /* We get the accessor */
187: accessor = factory.createCachedServiceReplyAccessor();
188:
189: /* ...and remove the old entries */
190: removed = accessor
191: .removeOldCachedServiceReplies(entryLifeTimeSeconds);
192:
193: if (removed > 0) {
194: log.write("Rows removed by cache cleaner: "
195: + removed, null, // no exception
196: CachedServiceReplyCleaner.class);
197: }
198:
199: } catch (Exception e) {
200: log.write("The cache cleaner got an exception "
201: + "while trying to clean the cache.", e,
202: CachedServiceReplyCleaner.class);
203:
204: }
205: }
206: }
207:
208: }
|