1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 package com.macvu.tiles.xmlDefinition;
62 import com.macvu.tiles.CacheAttribute;
63 import com.macvu.tiles.CacheComponentDefinition;
64 import com.macvu.tiles.CacheInformation;
65 import com.macvu.tiles.cache.CacheObjectWrapper;
66 import org.apache.struts.tiles.ComponentDefinition;
67 import org.apache.struts.tiles.DefinitionsFactoryException;
68 import org.apache.struts.tiles.DefinitionsUtil;
69 import org.jdom.Document;
70 import org.jdom.Element;
71 import org.jdom.JDOMException;
72 import org.jdom.input.SAXBuilder;
73 import org.jdom.output.Format;
74 import org.jdom.output.XMLOutputter;
75 import org.xml.sax.helpers.DefaultHandler;
76
77 import javax.servlet.ServletContext;
78 import javax.servlet.http.HttpServletRequest;
79 import java.io.*;
80 import java.util.ArrayList;
81 import java.util.Iterator;
82 import java.util.List;
83
84 /***
85 * Unfortunate that we have to do the saving in a separate Class from reading but we are hacking existing code in trying
86 * to get the functionality as close to the orginial as possible.
87 */
88 public class CacheDefinitionSaveFactory {
89 boolean initialized = false;
90 List filenames;
91
92 HttpServletRequest request;
93 ServletContext servletContext;
94
95 public CacheDefinitionSaveFactory() {
96 initialized = false;
97 }
98
99 public void initDefintionFiles(HttpServletRequest request, ServletContext servletContext) {
100 this.request = request;
101 this.servletContext = servletContext;
102
103 filenames = CacheObjectWrapper.getTileDefinitionFileList(servletContext);
104 if (filenames == null) {
105 throw new IllegalStateException("Tile definition has not been put into the servlet context yet.");
106 }
107
108 initialized = true;
109 }
110
111 public List getFilenames() {
112 if (!initialized) {
113 throw new IllegalStateException("call init definition files first");
114 }
115
116 return filenames;
117 }
118
119 public void setFilenames(List filenames) {
120 this.filenames = filenames;
121 }
122
123 public void updateFile(String filename) {
124 saveChangeToFile(filename, filename);
125 }
126
127 public void saveChangeToFile(String filename, String destination) {
128 if (!initialized) {
129 throw new IllegalStateException("call init definition files first");
130 }
131
132 try {
133 FileInputStream ifileStream = new FileInputStream(servletContext.getRealPath(filename));
134 FileOutputStream ofileStream = new FileOutputStream(destination);
135
136 saveChange(ifileStream, ofileStream);
137 } catch (FileNotFoundException e) {
138 e.printStackTrace();
139 }
140 }
141
142 public void saveChange(InputStream instream, OutputStream ostream) {
143 if (!initialized) {
144 throw new IllegalStateException("call init definition files first");
145 }
146
147 Document document;
148 SAXBuilder parser = new SAXBuilder();
149 parser.setValidation(false);
150 parser.setDTDHandler(new DefaultHandler());
151
152 try {
153 document = parser.build(instream);
154
155 Element root = document.getRootElement();
156 List definitionList = root.getChildren();
157 Iterator definitionItr = definitionList.iterator();
158 while (definitionItr.hasNext()) {
159 Element definitionElement = (Element) definitionItr.next();
160 updateDefinitionElement(definitionElement);
161 }
162
163 ObjectOutputStream oStream = new ObjectOutputStream(ostream);
164
165 XMLOutputter writer = new XMLOutputter(Format.getPrettyFormat());
166 writer.output(document, oStream);
167 } catch (JDOMException e) {
168 e.printStackTrace();
169 } catch (IOException e) {
170 e.printStackTrace();
171 }
172
173 }
174
175 private void updateDefinitionElement(Element definitionElement) {
176 String tileName = definitionElement.getAttributeValue("name");
177
178 ComponentDefinition definition;
179
180 try {
181 definition = DefinitionsUtil.getDefinition(tileName, request, servletContext);
182 if (definition instanceof CacheComponentDefinition) {
183 CacheInformation cacheInfo = ((CacheComponentDefinition) definition).getCacheInformation();
184 if (cacheInfo == null || cacheInfo.isDefault()) {
185 definitionElement.removeChildren("cacheInformation");
186 } else {
187 Element cacheInformation = definitionElement.getChild("cacheInformation");
188 if (cacheInformation == null) {
189 cacheInformation = new Element("cacheInformation");
190
191 }
192
193 cacheInformation.setAttribute("cacheEnabled", String.valueOf(cacheInfo.getCacheEnabled()));
194 cacheInformation.setAttribute("repositoryName", cacheInfo.getRepositoryName());
195 cacheInformation.setAttribute("repositoryFactory", cacheInfo.getRepositoryFactory());
196 cacheInformation.setAttribute("keyFactory", cacheInfo.getKeyFactory());
197
198 addCacheKeysToCacheInformationElement(cacheInformation, cacheInfo.getCacheAttributes());
199
200 definitionElement.removeChild("cacheInformation");
201 definitionElement.addContent(cacheInformation);
202 }
203 }
204 } catch (DefinitionsFactoryException e) {
205
206 }
207 }
208
209 private void addCacheKeysToCacheInformationElement(Element cacheInformationElement, List cacheAttributes) {
210 List list = new ArrayList();
211
212 Iterator attIter = cacheAttributes.iterator();
213
214 while (attIter.hasNext()) {
215 CacheAttribute attr = (CacheAttribute) attIter.next();
216 Element key = new Element("key");
217 key.setAttribute("name", attr.getName());
218 key.setAttribute("scope", attr.getScope());
219
220 list.add(key);
221 }
222
223 cacheInformationElement.setContent(list);
224 }
225 }
226