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.*;
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
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
65 try {
66 if (log.isDebugEnabled()) {
67 log.debug("getting tile: " + name);
68 }
69
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 }