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.CacheAttribute;
22  import com.macvu.tiles.CacheComponentDefinition;
23  import com.macvu.tiles.CacheInformation;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.struts.action.Action;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  import org.apache.struts.tiles.*;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  /***
38   * User: MVu
39   */
40  public class SetCacheTileDefinitionAction extends Action {
41      protected static Log log = LogFactory.getLog(SetCacheTileDefinitionAction.class);
42  
43      public static String SAVE_MESSAGE_KEY = "save_message";
44      public static String SAVE_SUCCESS_MESSAGE = "Cache Tile Information successfully saved.";
45  
46      public ActionForward execute(ActionMapping mapping,
47                                   ActionForm form,
48                                   HttpServletRequest request,
49                                   HttpServletResponse response)
50              throws Exception {
51          CacheInformationForm infoForm = (CacheInformationForm) form;
52  
53          // Try to dispatch to requested definition
54          try {
55              // Read definition from factory, but we can create it here.
56              ComponentDefinition definition = DefinitionsUtil.getDefinition(infoForm.getTileName(), request, getServlet().getServletContext());
57  
58              if (definition instanceof CacheComponentDefinition) {
59                  CacheInformation cacheInfo = ((CacheComponentDefinition) definition).getCacheInformation();
60  
61                  cacheInfo.setCacheEnabled(infoForm.isCacheEnabled());
62                  cacheInfo.setKeyFactory(infoForm.getKeyFactory());
63                  cacheInfo.setRepositoryFactory(infoForm.getRepositoryFactory());
64                  cacheInfo.setRepositoryName(infoForm.getRepositoryName());
65                  String[] cacheFields = request.getParameterValues("cacheFields");
66                  cacheInfo.setCacheAttributes(convertFormCacheFieldsToCacheAttributes(cacheFields));
67              } else {
68                  return (mapping.findForward("error"));
69              }
70          } catch (FactoryNotFoundException ex) {
71              return (mapping.findForward("error"));
72          } catch (NoSuchDefinitionException ex) {
73              return (mapping.findForward("error"));
74          } catch (DefinitionsFactoryException ex) {
75              return (mapping.findForward("error"));
76          } catch (Exception ex) {
77              return (mapping.findForward("error"));
78          }
79  
80          String name = infoForm.getTileName();
81          request.setAttribute(GetCacheTileDefinitionAction.CACHE_TILE_NAME, name);
82          request.setAttribute(SAVE_MESSAGE_KEY, SAVE_SUCCESS_MESSAGE);
83  
84          return (mapping.findForward("success"));
85      }
86  
87      private List convertFormCacheFieldsToCacheAttributes(String[] cacheFields) {
88          if (log.isDebugEnabled()) {
89              log.debug("cacheFields: " + cacheFields);
90          }
91  
92          List cacheAttrSet = new ArrayList();
93  
94          if (cacheFields != null) {
95              for (int i = 0; i < cacheFields.length; i++) {
96                  CacheAttribute cacheAttr = convertTokenToCacheAttribute(cacheFields[i]);
97                  cacheAttrSet.add(cacheAttr);
98              }
99          }
100 
101         return cacheAttrSet;
102     }
103 
104     /***
105      * The attribute string have the format of:  scope.name
106      *
107      * @param attribute the cache attribute in string.
108      * @return the cacheAttribute the input attribute string represents.
109      */
110     private CacheAttribute convertTokenToCacheAttribute(String attribute) {
111         CacheAttribute attr = new CacheAttribute();
112 
113         int findDot = attribute.indexOf('.');
114         String scope = attribute.substring(0, findDot);
115         String name = attribute.substring(findDot + 1);
116 
117         attr.setScope(scope);
118         attr.setName(name);
119 
120         return attr;
121     }
122 }