1 16 17 package org.apache.jetspeed.services.portletcache; 18 19 import javax.servlet.ServletConfig ; 20 21 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 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 46 public class JetspeedPortletCacheService 47 extends TurbineBaseService 48 implements PortletCacheService 49 { 50 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))); 59 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 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 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 GlobalCache.addObject( handle , cachedObject); 135 } 136 } 137 138 144 public void removeCacheable( String handle ) { 145 146 CachedObject obj = null; 147 148 try { 149 obj = GlobalCache.getObject( handle ); 150 } catch ( ObjectExpiredException e) { 151 } 153 154 if ( obj != null ) { 155 obj.setStale(true); 156 } 157 } 158 159 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 return null; 178 } 181 182 return (Cacheable)obj.getContents(); 183 184 } 185 186 } 187 188 | Popular Tags |