001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/interfaces/ServiceConfigurationRegistrySingleton.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.interfaces;
042:
043: import java.util.Map;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.Timer;
047: import java.util.TimerTask;
048:
049: import es.udc.mypersonalizer.kernel.log.Log;
050: import es.udc.mypersonalizer.kernel.log.LogManager;
051: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
052: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceConfiguration;
053: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceConfigurationAccessor;
054: import es.udc.mypersonalizer.kernel.model.repository.interfaces.RepositoryAccessorFactory;
055: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
056:
057: /**
058: * This class is a cache that stores the registered service configurations.
059: * Uses a static block to create its unique instance and for filling the
060: * cache. Uses a refreshing daemon that updates the cache periodically, the
061: * period time is readed from the configuration.
062: *
063: * @author Abel Iago Toral Quiroga
064: * @author Fernando Bellas
065: * @since 1.0
066: */
067: public class ServiceConfigurationRegistrySingleton extends TimerTask {
068:
069: /** The singleton instance of this class */
070: private static ServiceConfigurationRegistrySingleton singleton = null;
071:
072: /** All workspace definitions cache */
073: private static Map serviceConfigurations = null;
074:
075: /** Cache refresh time period */
076: private static long refreshTimeInSeconds = 300;
077:
078: static {
079: try {
080:
081: singleton = new ServiceConfigurationRegistrySingleton();
082:
083: } catch (Exception e) {
084: Log mypersonalizerLog = LogManager
085: .getLog(LogNamingConventions.MYPERSONALIZER);
086:
087: mypersonalizerLog.write(
088: "An error occurred while initializing " + "cache",
089: e, ServiceConfigurationRegistrySingleton.class);
090: }
091: }
092:
093: /**
094: * Can't create instances of this class, call <code>getInstance()</code>
095: * instead. Fills the cache and inits the thread for refreshing.
096: */
097: private ServiceConfigurationRegistrySingleton()
098: throws InternalErrorException {
099:
100: try {
101:
102: /* Fill the cache */
103: fillCache();
104:
105: /* Init refreshing thread */
106: Timer timer = new Timer(true);
107: timer.scheduleAtFixedRate(this , 0,
108: refreshTimeInSeconds * 1000L);
109:
110: } catch (InternalErrorException e) {
111: throw e;
112: } catch (Exception e) {
113: throw new InternalErrorException(e);
114: }
115: }
116:
117: /** Returns the singleton instance of this class. */
118: public static ServiceConfigurationRegistrySingleton getInstance() {
119: return singleton;
120: }
121:
122: /**
123: * Returns the <code>ServiceConfiguration</code> object that represents
124: * the definition for the given workspace type.
125: * @param serviceIdentifier The service identifer.
126: * @return the configuration for the given service or null
127: * if it doesn't exist.
128: */
129: public ServiceConfiguration getServiceConfiguration(
130: String serviceIdentifier) {
131:
132: return ((ServiceConfiguration) serviceConfigurations
133: .get(serviceIdentifier));
134: }
135:
136: /**
137: * The refreshing thread calls this method each
138: * <code>refreshTimeInSeconds</code> seconds to refresh the cache.
139: */
140: public void run() {
141:
142: try {
143: fillCache();
144: } catch (Exception e) {
145: Log mypersonalizerLog = LogManager
146: .getLog(LogNamingConventions.MYPERSONALIZER);
147:
148: mypersonalizerLog.write(
149: "Cache refreshing thread could not "
150: + "refresh cache", e,
151: ServiceConfigurationRegistrySingleton.class);
152: }
153: }
154:
155: /**
156: * Fills the cache using <code>ServiceConfigurationAccessor</code> in a
157: * thread-safe way.
158: * @throws InternalErrorException if an error occured.
159: */
160: private void fillCache() throws InternalErrorException {
161:
162: ServiceConfigurationAccessor serviceConfigurationAccessor = RepositoryAccessorFactory
163: .getInstance().createServiceConfigurationAccessor();
164:
165: Iterator allServiceConfigurations = serviceConfigurationAccessor
166: .findAllServiceConfigurations().iterator();
167:
168: Map temporalServiceConfigurations = new HashMap();
169: while (allServiceConfigurations.hasNext()) {
170:
171: ServiceConfiguration this ServiceConfiguration = (ServiceConfiguration) allServiceConfigurations
172: .next();
173:
174: temporalServiceConfigurations.put(this ServiceConfiguration
175: .getServiceIdentifier(), this ServiceConfiguration);
176: }
177:
178: /* Do an atomic assignment */
179: serviceConfigurations = temporalServiceConfigurations;
180:
181: }
182: }
|