001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.services;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.jasig.portal.EntityIdentifier;
011: import org.jasig.portal.IBasicEntity;
012: import org.jasig.portal.concurrency.CachingException;
013: import org.jasig.portal.concurrency.IEntityCachingService;
014: import org.jasig.portal.concurrency.IEntityCachingServiceFactory;
015: import org.jasig.portal.properties.PropertiesManager;
016:
017: /**
018: * This class presents a facade for the IEntityCachingService implementation
019: * that lets clients cache and retrieve <code>IBasicEntities</code>. It
020: * hides such details as the physical structure of the cache and whether
021: * it is running in a multi- or single-JVM environment.
022: * <p>
023: * An <code>IBasicEntity</code> can answer its type and key. (See
024: * org.jasig.portal.groups.EntityTypes).
025: * <p>
026: * Caching consists of asking the service to add, retrieve, update and
027: * remove elements from the cache, e.g.,
028: * <p>
029: * <code>
030: * // Retrieve the entity from its store:<br>
031: * Class type = getEntityClass();<br>
032: * String key = getEntityKey();<br>
033: * IBasicEntity ent = findEntity(key); <br>
034: * ... <br>
035: * // Cache the entity:<br>
036: * EntityCachingService.add(ent);<br>
037: * ... <br>
038: * // Retrieve the entity from the cache:<br>
039: * IEntity aCopy = EntityCachingService.get(type, key);<br>
040: * ...<br>
041: * // Update the entity and then:<br>
042: * EntityCachingService..update(aCopy); // notifies peer caches.<br>
043: * ...<br>
044: * // Or delete the entity and:<br>
045: * EntityCachingService.remove(type, key); // notifies peer caches.<br>
046: * </code>
047: * <p>
048: * @author Dan Ellentuck
049: * @version $Revision: 35418 $
050: */
051:
052: public class EntityCachingService {
053:
054: private static final Log log = LogFactory
055: .getLog(EntityCachingService.class);
056: // Singleton instance of the bootstrap class:
057: private static EntityCachingService instance = null;
058: // The caching service:
059: private IEntityCachingService cache = null;
060:
061: /** Creates new EntityLockService */
062: private EntityCachingService() throws CachingException {
063: super ();
064: initialize();
065: }
066:
067: /**
068: * Adds the entity to the cache.
069: * @param ent org.jasig.portal.IBasicEntity
070: * @exception org.jasig.portal.concurrency.CachingException
071: */
072: public void add(IBasicEntity ent) throws CachingException {
073: cache.add(ent);
074: }
075:
076: /**
077: * Returns the cached entity identified by type and key.
078: * @param type Class
079: * @param key String
080: * @return IBasicEntity entity
081: * @exception org.jasig.portal.concurrency.CachingException
082: */
083: public IBasicEntity get(Class type, String key)
084: throws CachingException {
085: return cache.get(type, key);
086: }
087:
088: /**
089: * Returns the cached entity referred to by entityID.
090: * @param entityID entity identifier
091: * @return IBasicEntity entity
092: * @exception org.jasig.portal.concurrency.CachingException
093: */
094: public IBasicEntity get(EntityIdentifier entityID)
095: throws CachingException {
096: return cache.get(entityID.getType(), entityID.getKey());
097: }
098:
099: /**
100: * @exception org.jasig.portal.concurrency.CachingException
101: */
102: private void initialize() throws CachingException {
103: String eMsg = null;
104: String factoryName = PropertiesManager
105: .getProperty("org.jasig.portal.concurrency.IEntityCachingServiceFactory");
106:
107: if (factoryName == null) {
108: eMsg = "EntityCachingService.initialize(): No entry for org.jasig.portal.concurrency.caching.IEntityCachingServiceFactory in portal.properties.";
109: log.error(eMsg);
110: throw new CachingException(eMsg);
111: }
112:
113: try {
114: IEntityCachingServiceFactory cachingServiceFactory = (IEntityCachingServiceFactory) Class
115: .forName(factoryName).newInstance();
116: cache = cachingServiceFactory.newCachingService();
117: } catch (Exception e) {
118: eMsg = "EntityCachingService.initialize(): Problem creating entity caching service...";
119: log.error(eMsg, e);
120: throw new CachingException(eMsg, e);
121: }
122: }
123:
124: public static synchronized EntityCachingService instance()
125: throws CachingException {
126: if (instance == null) {
127: instance = new EntityCachingService();
128: }
129: return instance;
130: }
131:
132: /**
133: * Removes the entity identified by type and key from the cache and notifies
134: * peer caches.
135: * @param type Class
136: * @param key String
137: * @exception org.jasig.portal.concurrency.CachingException
138: */
139: public void remove(Class type, String key) throws CachingException {
140: cache.remove(type, key);
141: }
142:
143: /**
144: * Removes the entity referred to by entityID from the cache and notifies peer
145: * caches.
146: * @param entityID
147: * @exception org.jasig.portal.concurrency.CachingException
148: */
149: public void remove(EntityIdentifier entityID)
150: throws CachingException {
151: remove(entityID.getType(), entityID.getKey());
152: }
153:
154: /**
155: * Removes the <code>IBasicEntity</code> from the cache and notifies peer
156: * caches.
157: * @param ent org.jasig.portal.IBasicEntity
158: * @exception org.jasig.portal.concurrency.CachingException
159: */
160: public void remove(IBasicEntity ent) throws CachingException {
161: remove(ent.getEntityIdentifier());
162: }
163:
164: public static synchronized EntityCachingService start()
165: throws CachingException {
166: return instance();
167: }
168:
169: /**
170: * Updates the entity in the cache and notifies peer caches.
171: * @param ent org.jasig.portal.concurrency.IBasicEntity
172: * @exception org.jasig.portal.concurrency.CachingException
173: */
174: public void update(IBasicEntity ent) throws CachingException {
175: cache.update(ent);
176: }
177: }
|