1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
54 try {
55
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 }