KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > invocation > component > CacheInterceptor


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

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 /**
28  * Implements the caching part : per client, per component.
29  * The response properties part is not implemented yet.
30  *
31  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
32  * @version $Revision: 1.1.1.1 $
33  */

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    /**
43     * Our meta data.
44     */

45    private InterceptorMetaData metaData;
46
47    /**
48     * The reference type used for caching content.
49     */

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 JavaDoc 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 JavaDoc expirationCache = container.getExpirationCache();
70
71       if (expirationCache == null)
72       {
73          // No caching
74
return invocation.invokeNext();
75       }
76       else
77       {
78          if (type == InvocationType.PROCESS_TARGET)
79          {
80             // Invalidate cached fragment
81
pwc.setCachedResult(null);
82             return invocation.invokeNext();
83          }
84          else
85          {
86             // The returned result
87
Object JavaDoc result = null;
88
89             // Try to get a valid cached content
90
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 necessary perform invocation
101
if (result == null)
102             {
103                result = invocation.invokeNext();
104
105                // And cache the result if it is a fragment
106
if (result instanceof FragmentResult)
107                {
108                   // And if it has not already expired
109
long expirationTimeMillis = System.currentTimeMillis() + expirationCache.intValue() * 1000L;
110                   if(expirationTimeMillis > System.currentTimeMillis())
111                   {
112                      // Use a strong reference, make that configurable to use soft reference
113
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    // MetaDataHolder implementation ************************************************************************************
136

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    // Private **********************************************************************************************************
149

150    private void configure()
151    {
152       // Configure the interceptor
153
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