1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
package net.sf.infrared.tools.server; |
24 |
|
|
25 |
|
import java.io.File; |
26 |
|
import java.io.IOException; |
27 |
|
import java.net.URL; |
28 |
|
|
29 |
|
import org.apache.commons.io.FileUtils; |
30 |
|
import org.apache.xerces.parsers.DOMParser; |
31 |
|
import org.w3c.dom.DOMException; |
32 |
|
import org.w3c.dom.Document; |
33 |
|
import org.w3c.dom.Node; |
34 |
|
import org.xml.sax.SAXException; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
0 |
public class ServletIntegrator extends AbstractServerIntegrator { |
42 |
|
public void integrateEar(File rootDir) { |
43 |
0 |
throw new UnsupportedOperationException("Servlet containers cannot handle EAR files"); |
44 |
|
} |
45 |
|
|
46 |
|
public void integrateWar(File rootDir) { |
47 |
0 |
File webInfDir = new File(rootDir, "WEB-INF"); |
48 |
0 |
File webDotXmlFile = new File(webInfDir, "web.xml"); |
49 |
|
|
50 |
0 |
addListenerAndFilterToWebDotXml(webDotXmlFile); |
51 |
0 |
} |
52 |
|
|
53 |
|
public String copyIntoWar(File[] toCopy, File warRootDir) { |
54 |
0 |
File web_inf_lib = ensureWebInfLibExists(warRootDir); |
55 |
0 |
File web_inf_classes = ensureWebInfClassesExists(warRootDir); |
56 |
0 |
for (int i = 0; i < toCopy.length; i++) { |
57 |
0 |
File f = toCopy[i]; |
58 |
0 |
if ( (f.isFile()) && (f.getName().endsWith(".jar")) ) { |
59 |
|
try { |
60 |
0 |
FileUtils.copyFileToDirectory(f, web_inf_lib); |
61 |
0 |
} catch (IOException ex) { |
62 |
0 |
throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + |
63 |
|
" to directory " + web_inf_lib.getAbsolutePath(), ex); |
64 |
0 |
} |
65 |
|
} else { |
66 |
|
try { |
67 |
0 |
if (f.isDirectory()) { |
68 |
0 |
FileUtils.copyDirectory(f, web_inf_classes); |
69 |
|
} else { |
70 |
0 |
FileUtils.copyFileToDirectory(f, web_inf_classes); |
71 |
|
} |
72 |
0 |
} catch (IOException ex) { |
73 |
0 |
throw new RuntimeException("Failed to copy file " + f.getAbsolutePath() + |
74 |
|
" to directory " + web_inf_classes.getAbsolutePath(), ex); |
75 |
0 |
} |
76 |
|
} |
77 |
|
} |
78 |
0 |
return ""; |
79 |
|
} |
80 |
|
|
81 |
|
public String copyIntoEar(File[] toCopy, File earRootDir) { |
82 |
0 |
throw new UnsupportedOperationException("Servlet containers cannot handle EAR files"); |
83 |
|
} |
84 |
|
|
85 |
|
void addListenerAndFilterToWebDotXml(File webDotXmlFile) { |
86 |
0 |
if (! webDotXmlFile.exists()) { |
87 |
0 |
createTemplateWebDotXmlFile(webDotXmlFile); |
88 |
0 |
return; |
89 |
|
} |
90 |
|
|
91 |
0 |
DOMParser parser = createParser(); |
92 |
|
try { |
93 |
0 |
parser.parse(webDotXmlFile.getAbsolutePath()); |
94 |
0 |
} catch (Exception ex) { |
95 |
0 |
if (ex instanceof RuntimeException) { |
96 |
0 |
throw (RuntimeException) ex; |
97 |
|
} |
98 |
0 |
throw new RuntimeException("Failed to parse web.xml file at " + |
99 |
|
webDotXmlFile.getAbsolutePath(), ex); |
100 |
0 |
} |
101 |
0 |
Document doc = parser.getDocument(); |
102 |
0 |
Node webAppNode = doc.getElementsByTagName("web-app").item(0); |
103 |
0 |
if (webAppNode == null) { |
104 |
0 |
throw new IllegalArgumentException("WEB-INF/web.xml does not contain <web-app> element"); |
105 |
|
} |
106 |
0 |
addListenerNode(doc, webAppNode); |
107 |
0 |
addFilterNode(doc, webAppNode); |
108 |
0 |
addFilterMappingNode(doc, webAppNode); |
109 |
|
|
110 |
0 |
writeFile(webDotXmlFile, doc); |
111 |
0 |
} |
112 |
|
|
113 |
|
void addFilterMappingNode(final Document doc, class="keyword">final Node webAppNode) throws DOMException { |
114 |
|
|
115 |
0 |
Node filterMappingNode = createFilterMappingNode(doc); |
116 |
0 |
Node refNode = findNodeAfterFilter(doc); |
117 |
|
|
118 |
|
|
119 |
0 |
if (refNode != null) { |
120 |
0 |
webAppNode.insertBefore(filterMappingNode, refNode); |
121 |
|
} else { |
122 |
0 |
webAppNode.appendChild(filterMappingNode); |
123 |
|
} |
124 |
0 |
} |
125 |
|
|
126 |
|
void addFilterNode(final Document doc, class="keyword">final Node webAppNode) throws DOMException { |
127 |
|
|
128 |
0 |
Node filterNode = createFilterNode(doc); |
129 |
0 |
Node refNode = findNodeAfterContextParam(doc); |
130 |
|
|
131 |
|
|
132 |
0 |
if (refNode != null) { |
133 |
0 |
webAppNode.insertBefore(filterNode, refNode); |
134 |
|
} else { |
135 |
0 |
webAppNode.appendChild(filterNode); |
136 |
|
} |
137 |
0 |
} |
138 |
|
|
139 |
|
void addListenerNode(final Document doc, class="keyword">final Node webAppNode) throws DOMException { |
140 |
|
|
141 |
0 |
Node listenerNode = createListenerNode(doc); |
142 |
0 |
Node refNode = findNodeAfterListener(doc); |
143 |
|
|
144 |
|
|
145 |
0 |
if (refNode != null) { |
146 |
0 |
webAppNode.insertBefore(listenerNode, refNode); |
147 |
|
} else { |
148 |
0 |
webAppNode.appendChild(listenerNode); |
149 |
|
} |
150 |
0 |
} |
151 |
|
|
152 |
|
void createTemplateWebDotXmlFile(File webDotXmlFile) { |
153 |
|
try { |
154 |
0 |
FileUtils.forceMkdir(webDotXmlFile.getParentFile()); |
155 |
0 |
} catch (IOException ex) { |
156 |
|
|
157 |
0 |
} |
158 |
0 |
URL template = Thread.currentThread().getContextClassLoader().getResource("template-web.xml"); |
159 |
0 |
if (template != null) { |
160 |
|
try { |
161 |
0 |
FileUtils.copyURLToFile(template, webDotXmlFile); |
162 |
0 |
} catch (IOException ex) { |
163 |
0 |
throw new RuntimeException("Failed to create web.xml file " + |
164 |
|
webDotXmlFile.getAbsolutePath()); |
165 |
0 |
} |
166 |
|
} |
167 |
0 |
} |
168 |
|
|
169 |
|
Node findNodeAfterListener(Document doc) { |
170 |
|
|
171 |
0 |
String[] possibleChildren = new String[] { |
172 |
|
"servlet", "servlet-mapping", "session-config", "mime-mapping", |
173 |
|
"welcome-file-list", "error-page", "taglib", "resource-env-ref", |
174 |
|
"resource-ref", "security-constraint", "login-config", "security-role", |
175 |
|
"env-entry", "ejb-ref", "ejb-local-ref" |
176 |
|
}; |
177 |
0 |
return findFirstMatchingElement(doc, possibleChildren); |
178 |
|
} |
179 |
|
|
180 |
|
Node findNodeAfterContextParam(Document doc) { |
181 |
|
|
182 |
0 |
String[] possibleChildren = new String[] { |
183 |
|
"filter", "filter-mapping", "listener", |
184 |
|
"servlet", "servlet-mapping", "session-config", "mime-mapping", |
185 |
|
"welcome-file-list", "error-page", "taglib", "resource-env-ref", |
186 |
|
"resource-ref", "security-constraint", "login-config", "security-role", |
187 |
|
"env-entry", "ejb-ref", "ejb-local-ref" |
188 |
|
}; |
189 |
0 |
return findFirstMatchingElement(doc, possibleChildren); |
190 |
|
} |
191 |
|
|
192 |
|
Node findNodeAfterFilter(Document doc) { |
193 |
|
|
194 |
0 |
String[] possibleChildren = new String[] { |
195 |
|
"filter-mapping", "listener", |
196 |
|
"servlet", "servlet-mapping", "session-config", "mime-mapping", |
197 |
|
"welcome-file-list", "error-page", "taglib", "resource-env-ref", |
198 |
|
"resource-ref", "security-constraint", "login-config", "security-role", |
199 |
|
"env-entry", "ejb-ref", "ejb-local-ref" |
200 |
|
}; |
201 |
0 |
return findFirstMatchingElement(doc, possibleChildren); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
Node createListenerNode(Document doc) { |
212 |
0 |
Node listenerNode = doc.createElement("listener"); |
213 |
0 |
Node listenerClassNode = doc.createElement("listener-class"); |
214 |
0 |
listenerClassNode.appendChild( |
215 |
|
doc.createTextNode("net.sf.infrared.agent.setup.InfraREDServletContextListener")); |
216 |
0 |
listenerNode.appendChild(listenerClassNode); |
217 |
0 |
return listenerNode; |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
Node createFilterNode(Document doc) { |
230 |
0 |
Node filterNode = doc.createElement("filter"); |
231 |
0 |
Node filterNameNode = doc.createElement("filter-name"); |
232 |
0 |
filterNameNode.appendChild( doc.createTextNode("infrared") ); |
233 |
0 |
Node filterClassNode = doc.createElement("filter-class"); |
234 |
0 |
filterClassNode.appendChild( |
235 |
|
doc.createTextNode("net.sf.infrared.aspects.servlet.InfraREDServletFilter")); |
236 |
0 |
filterNode.appendChild(filterNameNode); |
237 |
0 |
filterNode.appendChild(filterClassNode); |
238 |
0 |
return filterNode; |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
0 |
|
251 |
0 |
|
252 |
0 |
|
253 |
0 |
|
254 |
0 |
|
255 |
0 |
|
256 |
0 |
|
257 |
0 |
|
258 |
|
|
259 |
|
|