1 9 package org.jboss.portal.server.invocation.component; 10 11 import org.jboss.portal.common.metadata.MetaData; 12 import org.jboss.portal.common.metadata.MetaDataHolder; 13 import org.jboss.portal.server.Component; 14 import org.jboss.portal.server.Instance; 15 import org.jboss.portal.server.Window; 16 import org.jboss.portal.server.WindowContext; 17 import org.jboss.portal.server.invocation.AttachmentKey; 18 import org.jboss.portal.server.invocation.Interceptor; 19 import org.jboss.portal.server.invocation.Invocation; 20 import org.jboss.portal.server.invocation.InvocationType; 21 import org.jboss.portal.server.metadata.InterceptorMetaData; 22 import org.jboss.portal.server.output.FragmentResult; 23 import org.jboss.portal.server.output.cache.SoftTimedContent; 24 import org.jboss.portal.server.output.cache.StrongTimedContent; 25 import org.jboss.portal.server.output.cache.TimedContent; 26 27 34 public class CacheInterceptor implements 35 Interceptor, 36 MetaDataHolder 37 { 38 39 public static final int REF_STRONG = 0; 40 public static final int REF_SOFT = 1; 41 42 45 private InterceptorMetaData metaData; 46 47 50 private int refType = REF_SOFT; 51 52 public int getRefType() 53 { 54 return refType; 55 } 56 57 public void setRefType(int refType) 58 { 59 this.refType = refType; 60 } 61 62 public Object invoke(Invocation invocation) 63 { 64 InvocationType type = (InvocationType)invocation.getAttachment(AttachmentKey.INVOCATION_TYPE); 65 WindowContext pwc = (WindowContext)invocation.getAttachment(AttachmentKey.WINDOW_CONTEXT); 66 Window window = (Window)invocation.getAttachment(AttachmentKey.WINDOW); 67 Instance instance = window.getInstance(); 68 Component container = instance.getComponent(); 69 Integer expirationCache = container.getExpirationCache(); 70 71 if (expirationCache == null) 72 { 73 return invocation.invokeNext(); 75 } 76 else 77 { 78 if (type == InvocationType.PROCESS_TARGET) 79 { 80 pwc.setCachedResult(null); 82 return invocation.invokeNext(); 83 } 84 else 85 { 86 Object result = null; 88 89 TimedContent cachedResult = pwc.getCachedResult(); 91 if (cachedResult != null && cachedResult.getExpirationTimeMillis() > System.currentTimeMillis()) 92 { 93 result = cachedResult.getContent(); 94 } 95 else 96 { 97 pwc.setCachedResult(null); 98 } 99 100 if (result == null) 102 { 103 result = invocation.invokeNext(); 104 105 if (result instanceof FragmentResult) 107 { 108 long expirationTimeMillis = System.currentTimeMillis() + expirationCache.intValue() * 1000L; 110 if(expirationTimeMillis > System.currentTimeMillis()) 111 { 112 TimedContent newCachedResult = createTimedContent((FragmentResult)result, expirationTimeMillis); 114 pwc.setCachedResult(newCachedResult); 115 } 116 } 117 } 118 return result; 119 } 120 } 121 } 122 123 private TimedContent createTimedContent(FragmentResult result, long expirationTimeMillis) 124 { 125 if (refType == REF_STRONG) 126 { 127 return new StrongTimedContent((FragmentResult)result, expirationTimeMillis); 128 } 129 else 130 { 131 return new SoftTimedContent((FragmentResult)result, expirationTimeMillis); 132 } 133 } 134 135 137 public void setMetaData(MetaData metaData) 138 { 139 this.metaData = (InterceptorMetaData)metaData; 140 configure(); 141 } 142 143 public MetaData getMetaData() 144 { 145 return metaData; 146 } 147 148 150 private void configure() 151 { 152 if ("soft".equals(metaData.getParamValue("RefType"))) 154 { 155 refType = REF_SOFT; 156 } 157 else 158 { 159 refType = REF_STRONG; 160 } 161 } 162 } 163 | Popular Tags |