1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.portletcache;
18
19 import javax.servlet.ServletConfig;
20
21
22 import org.apache.jetspeed.portal.portlets.AbstractPortlet;
23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25 import org.apache.jetspeed.services.resources.JetspeedResources;
26 import org.apache.jetspeed.services.portletcache.Cacheable;
27
28
29 import org.apache.turbine.services.TurbineBaseService;
30 import org.apache.turbine.services.TurbineServices;
31 import org.apache.turbine.services.cache.CachedObject;
32 import org.apache.turbine.services.cache.Refreshable;
33 import org.apache.turbine.services.cache.RefreshableCachedObject;
34 import org.apache.turbine.services.cache.ObjectExpiredException;
35
36
37 /***
38 * <P>This implementation of the PortletCache service is a simple adapter to
39 * the Turbine GlobalCacheService</p>
40 *
41 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
42 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
43 * @author <a href="mailto:paulsp@apache.org">Paul Spencer</a>
44 * @version $Id: JetspeedPortletCacheService.java,v 1.10 2004/02/23 03:34:54 jford Exp $
45 */
46 public class JetspeedPortletCacheService
47 extends TurbineBaseService
48 implements PortletCacheService
49 {
50 /***
51 * Static initialization of the logger for this class
52 */
53 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortletCacheService.class.getName());
54
55 private static int DefaultTimeToLiveMillis = (JetspeedResources.getInt(TurbineServices.SERVICE_PREFIX
56 + PortletCacheService.SERVICE_NAME
57 + ".TimeToLive.default", (30 * 60 * 1000)));
58
59 /***
60 * Called during Turbine.init()
61 *
62 * @param config A ServletConfig.
63 */
64 public void init( ServletConfig config ) {
65 try {
66 logger.info( "JetspeedPortletCacheService early init()....starting!");
67 if (DefaultTimeToLiveMillis < 0) {
68 logger.info( "JetspeedPortletCacheService - By default refreshable objects will live for ever");
69 } else {
70 logger.info( "JetspeedPortletCacheService - By default refreshable objects will be removed after "
71 + DefaultTimeToLiveMillis + " Millis ( "
72 + (DefaultTimeToLiveMillis/(1000*60)) + " minutes "
73 + ((DefaultTimeToLiveMillis%(1000*60))/1000.00)
74 + " Seconds "+")" );
75 }
76
77 logger.info( "JetspeedPortletCacheService early init()....finished!");
78 }
79 catch (Exception e) {
80 logger.error( "Cannot initialize JetspeedPortletCacheService!", e );
81 }
82 setInit(true);
83 }
84
85 /***
86 * Add a Cacheable object to the cache.
87 *
88 * @param item the object to store in the Cache
89 */
90 public void addCacheable( Cacheable item ) {
91
92 String handle = item.getHandle();
93
94 if ( handle.length() == 0 ) {
95 throw new RuntimeException("You must specify a handle for the item you want to cache.");
96 }
97
98 if ( item.isCacheable() ) {
99 CachedObject cachedObject = null;
100 Long expirationMillis = item.getExpirationMillis();
101 if (expirationMillis != null) {
102 if (System.currentTimeMillis() < expirationMillis.longValue()) {
103 cachedObject.setExpires(expirationMillis.longValue() - cachedObject.getCreated());
104 }
105 }
106 if (item instanceof Refreshable) {
107 RefreshableCachedObject rco = new RefreshableCachedObject( (Refreshable) item);
108 if (item instanceof AbstractPortlet) {
109 AbstractPortlet portlet = (AbstractPortlet)item;
110 String tempString = portlet.getPortletConfig().getInitParameter(JetspeedResources.TIME_TO_LIVE);
111 if (tempString != null) {
112 rco.setTTL(Integer.parseInt(tempString));
113 if (logger.isWarnEnabled())
114 {
115 logger.warn("PortletCache: portlet "
116 + item.getHandle()
117 + " overrides default time to live with "
118 + tempString);
119 }
120 } else {
121 rco.setTTL(DefaultTimeToLiveMillis);
122 }
123 } else {
124 rco.setTTL(DefaultTimeToLiveMillis);
125 }
126 cachedObject = rco;
127
128 } else {
129 cachedObject = new CachedObject(item);
130 }
131 item.setCachedObject(cachedObject);
132
133
134 GlobalCache.addObject( handle , cachedObject);
135 }
136 }
137
138 /***
139 * Removes an object from the cache based on its handle
140 *
141 * @see PortletCacheService#removeCacheable
142 * @param handle the identifier of the object we wish to retrieve
143 */
144 public void removeCacheable( String handle ) {
145
146 CachedObject obj = null;
147
148 try {
149 obj = GlobalCache.getObject( handle );
150 } catch ( ObjectExpiredException e) {
151
152 }
153
154 if ( obj != null ) {
155 obj.setStale(true);
156 }
157 }
158
159 /***
160 * Retrieves a Cacheable object from the cache.
161 *
162 * @param handle the identifier of the object we wish to retrieve
163 * @return the cacehd object or null if not found
164 */
165 public Cacheable getCacheable( String handle ) {
166
167 CachedObject obj = null;
168
169 try {
170 obj = GlobalCache.getObject( handle );
171 } catch (ObjectExpiredException e) {
172 logger.info( "cache miss, object expired: " + handle );
173 }
174
175 if ( obj == null ) {
176
177 return null;
178 }
179
180
181
182 return (Cacheable)obj.getContents();
183
184 }
185
186 }
187