View Javadoc

1   /*
2   MadCaching is a Tile Caching solution
3   Copyright (C) 2005  Mac Vu
4   
5   This program is free software; you can redistribute it and/or
6   modify it under the terms of the GNU General Public License
7   as published by the Free Software Foundation; either version 2
8   of the License, or (at your option) any later version.
9   
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package com.macvu.tiles.capture;
20  
21  import com.macvu.tiles.CacheComponentDefinition;
22  import com.macvu.tiles.CacheAttribute;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.jsp.PageContext;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpSession;
28  import java.util.Enumeration;
29  import java.util.Set;
30  
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.commons.logging.Log;
33  
34  public class CacheablesCaptureWrapper {
35      protected static Log log = LogFactory.getLog(CacheablesCaptureWrapper.class);
36      public static final String TILES_CACHEABLE_CAPTURE = "TILES_CACHEABLE_CAPTURE";
37      TilesCacheableCapture tilesCacheableCapture;
38      public CacheablesCaptureWrapper(ServletContext servletContext) {
39          init(servletContext);
40      }
41  
42      synchronized protected void init(ServletContext servletContext) {
43          tilesCacheableCapture = (TilesCacheableCapture) servletContext.getAttribute(TILES_CACHEABLE_CAPTURE);
44  
45          if (tilesCacheableCapture == null) {
46              tilesCacheableCapture = new TilesCacheableCapture();
47  
48              servletContext.setAttribute(TILES_CACHEABLE_CAPTURE, tilesCacheableCapture);
49          }
50      }
51  
52      public boolean isEnableCapture() {
53          return tilesCacheableCapture.isEnableCapture();
54      }
55  
56      public void enableCapture() {
57          tilesCacheableCapture.setEnableCapture(true);
58      }
59  
60      public void disableCapture() {
61          tilesCacheableCapture.setEnableCapture(false);
62      }
63  
64      public void clearCapturedTilesSet() {
65          tilesCacheableCapture.clearCapturedTilesSet();
66      }
67  
68      public void captureCacheablesForTile(CacheComponentDefinition definition, HttpServletRequest request) {
69          addCacheablesForTile(definition, request);
70  
71          HttpSession session = request.getSession();
72          if (session != null) {
73              addCacheablesForTile(definition, session);
74          }
75  
76          // Let's not do the Application Scope for now until we find a reason to do it.
77  
78          if (log.isDebugEnabled()) {
79              log.debug("Captured Tile: " + definition.getName());
80          }
81          tilesCacheableCapture.putCapturedTile(definition.getName());
82      }
83  
84      public void captureCacheablesForTile(CacheComponentDefinition definition, PageContext pageContext) {
85          addCacheablesForTile(definition, pageContext);
86  
87  
88          HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
89  
90          captureCacheablesForTile(definition, request);
91      }
92  
93  
94      public Set getCapturedTiles() {
95          return tilesCacheableCapture.getCapturedTiles();
96      }
97  
98      protected void addCacheablesForTile(CacheComponentDefinition definition, PageContext pageContext) {
99          Enumeration pageEnum = pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
100         while (pageEnum.hasMoreElements()) {
101             String pageName = (String) pageEnum.nextElement();
102             CacheAttribute newAttribute = new CacheAttribute(pageName, CacheAttribute.PAGE_SCOPE);
103             if (log.isDebugEnabled()) {
104                 log.debug("Add to tile: " + definition.getName() + " cacheable attribute: " + newAttribute.toString());
105             }
106             definition.getCacheInformation().addPossibleCacheAttribute(newAttribute);
107         }
108     }
109 
110     protected void addCacheablesForTile(CacheComponentDefinition definition, HttpServletRequest request) {
111         Enumeration paramEnum = request.getParameterNames();
112         while (paramEnum.hasMoreElements()) {
113             String paramName = (String) paramEnum.nextElement();
114             CacheAttribute newAttribute = new CacheAttribute(paramName, CacheAttribute.PARAM_SCOPE);
115 
116             if (log.isDebugEnabled()) {
117                 log.debug("Add to tile: " + definition.getName() + " cacheable attribute: " + newAttribute.toString());
118             }
119             definition.getCacheInformation().addPossibleCacheAttribute(newAttribute);
120         }
121 
122         Enumeration requestEnum = request.getAttributeNames();
123         while (requestEnum.hasMoreElements()) {
124             String requestName = (String) requestEnum.nextElement();
125             CacheAttribute newAttribute = new CacheAttribute(requestName, CacheAttribute.REQUEST_SCOPE);
126             if (log.isDebugEnabled()) {
127                 log.debug("Add to tile: " + definition.getName() + " cacheable attribute: " + newAttribute.toString());
128             }
129             definition.getCacheInformation().addPossibleCacheAttribute(newAttribute);
130         }
131     }
132 
133     protected void addCacheablesForTile(CacheComponentDefinition definition, HttpSession session) {
134         Enumeration sessionEnum = session.getAttributeNames();
135         while (sessionEnum.hasMoreElements()) {
136             String sessionName = (String) sessionEnum.nextElement();
137             CacheAttribute newAttribute = new CacheAttribute(sessionName, CacheAttribute.SESSION_SCOPE);
138             if (log.isDebugEnabled()) {
139                 log.debug("Add to tile: " + definition.getName() + " cacheable attribute: " + newAttribute.toString());
140             }
141             definition.getCacheInformation().addPossibleCacheAttribute(newAttribute);
142         }
143     }
144 }