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.actions;
20  
21  import com.macvu.tiles.*;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.struts.action.Action;
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.action.ActionForward;
27  import org.apache.struts.action.ActionMapping;
28  import org.apache.struts.tiles.*;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import java.util.*;
33  
34  /***
35   * User: MVu
36   */
37  public class GetCacheTileDefinitionAction extends Action {
38      public static final String CACHE_TILE_NAME = "tileName";
39  
40      protected static Log log = LogFactory.getLog(GetCacheTileDefinitionAction.class);
41  
42  
43      public ActionForward execute(ActionMapping mapping,
44                                   ActionForm form,
45                                   HttpServletRequest request,
46                                   HttpServletResponse response)
47              throws Exception {
48          // Identify the method name to be dispatched to
49          String name = request.getParameter(CACHE_TILE_NAME);
50  
51          if (name == null) {
52              name = (String) request.getAttribute(CACHE_TILE_NAME);
53          }
54  
55          String errorMessage = "";
56          String forwardTo = "success";
57  
58          if (name == null) {
59              errorMessage = "Definition dispatcher action : can't get parameter '"
60                      + CACHE_TILE_NAME + "'.";
61              forwardTo = "failure";
62          } else {
63  
64              // Try to dispatch to requested definition
65              try {
66                  if (log.isDebugEnabled()) {
67                      log.debug("getting tile: " + name);
68                  }
69                  // Read definition from factory, but we can create it here.
70                  ComponentDefinition definition = DefinitionsUtil.getDefinition(name, request, getServlet().getServletContext());
71  
72                  if (definition == null) {
73                      errorMessage = "Cannot find definition: " + name;
74                      forwardTo = "failure";
75                  } else {
76                      if (log.isDebugEnabled()) {
77                          log.debug("Definition Dump: " + definition.toString());
78                      }
79  
80                      if (definition instanceof CacheComponentDefinition) {
81                          CacheComponentDefinition cacheDefinition = (CacheComponentDefinition) definition;
82  
83                          CacheInformationForm infoForm = (CacheInformationForm) form;
84                          infoForm.setTileName(definition.getName());
85                          infoForm.setCacheEnabled(cacheDefinition.getCacheInformation().isCacheEnabled());
86                          infoForm.setRepositoryName(cacheDefinition.getCacheInformation().getRepositoryName());
87                          infoForm.setKeyFactory(cacheDefinition.getCacheInformation().getKeyFactory());
88                          infoForm.setRepositoryFactory(cacheDefinition.getCacheInformation().getRepositoryFactory());
89                          infoForm.setKeyFactory(cacheDefinition.getCacheInformation().getKeyFactory());
90                          infoForm.setCachingAttributeList(getCachingAttributeList((CacheComponentDefinition) definition));
91                      } else {
92                          errorMessage = "Defintion is not a CacheComponentDefinition.";
93                          forwardTo = "failure";
94                      }
95                  }
96              } catch (FactoryNotFoundException ex) {
97                  errorMessage = "Can't get definition factory.";
98                  forwardTo = "failure";
99              } catch (NoSuchDefinitionException ex) {
100                 errorMessage = "Can't get definition '" + name + "'.";
101                 forwardTo = "failure";
102             } catch (DefinitionsFactoryException ex) {
103                 errorMessage = "General Factory error '" + ex.getMessage() + "'.";
104                 forwardTo = "failure";
105             } catch (Exception ex) {
106                 errorMessage = "General error '" + ex.getMessage() + "'.";
107                 forwardTo = "failure";
108             }
109         }
110 
111         if (forwardTo.equals("failure")) {
112             request.setAttribute("errorMessage", errorMessage);
113         }
114         return (mapping.findForward(forwardTo));
115 
116     }
117 
118     private List getCachingAttributeList(CacheComponentDefinition definition) {
119         Set cachingAttributeSet = getCachingAttributeSet(definition);
120 
121         List cachingAttributeList = new ArrayList();
122         Iterator setItr = cachingAttributeSet.iterator();
123         while (setItr.hasNext()) {
124             Object att = setItr.next();
125 
126             cachingAttributeList.add(att);
127         }
128 
129         CacheAttributeComparator comparator = new CacheAttributeComparator();
130         Collections.sort(cachingAttributeList, comparator);
131 
132         return cachingAttributeList;
133     }
134 
135     private Set getCachingAttributeSet(CacheComponentDefinition definition) {
136         Set cachingAttributesSet = new HashSet();
137         CacheInformation info = definition.getCacheInformation();
138 
139         List cacheAttributes = info.getCacheAttributes();
140 
141         if (log.isDebugEnabled()) {
142             log.debug("Definition " + definition.getName() + " contains " + cacheAttributes.size() + " cacheable attributes");
143         }
144         Iterator itr = cacheAttributes.iterator();
145         while (itr.hasNext()) {
146             CacheAttribute att = (CacheAttribute) itr.next();
147             CachingAttribute cachingAttr = new CachingAttribute(att, true);
148 
149             cachingAttributesSet.add(cachingAttr);
150         }
151 
152         List possibleCacheAttribute = definition.getCacheInformation().getPossibleCacheAttributes();
153 
154         if (log.isDebugEnabled()) {
155             log.debug("Definition " + definition.getName() + " contains " + possibleCacheAttribute.size() + " cacheable attributes");
156         }
157         Iterator possibleItr = possibleCacheAttribute.iterator();
158         while (possibleItr.hasNext()) {
159             CacheAttribute att = (CacheAttribute) possibleItr.next();
160 
161             if (!info.isInCacheAttributes(att.getScope(), att.getName())) {
162                 CachingAttribute cachingAttr = new CachingAttribute(att);
163 
164                 cachingAttributesSet.add(cachingAttr);
165             }
166         }
167 
168         return cachingAttributesSet;
169     }
170 
171 }