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.FileWriter; |
27 |
|
import java.io.IOException; |
28 |
|
import net.sf.infrared.tools.ant.Logger; |
29 |
|
import org.apache.commons.io.FileUtils; |
30 |
|
|
31 |
|
import org.apache.xerces.parsers.DOMParser; |
32 |
|
import org.apache.xml.serialize.OutputFormat; |
33 |
|
import org.apache.xml.serialize.XMLSerializer; |
34 |
|
import org.w3c.dom.Document; |
35 |
|
import org.w3c.dom.Node; |
36 |
|
import org.w3c.dom.NodeList; |
37 |
|
import org.xml.sax.SAXNotRecognizedException; |
38 |
|
import org.xml.sax.SAXNotSupportedException; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
0 |
public abstract class AbstractServerIntegrator implements ServerIntegrator { |
45 |
|
private Logger log; |
46 |
|
|
47 |
|
public void setLogger(Logger log) { |
48 |
0 |
this.log = log; |
49 |
0 |
} |
50 |
|
|
51 |
|
public Logger getLogger() { |
52 |
0 |
return this.log; |
53 |
|
} |
54 |
|
|
55 |
|
protected File ensureWebInfLibExists(File warRootDir) { |
56 |
0 |
File web_inf = new File(warRootDir, "WEB-INF"); |
57 |
0 |
File web_inf_lib = new File(web_inf, "lib"); |
58 |
0 |
if (! web_inf_lib.exists()) { |
59 |
|
try { |
60 |
0 |
FileUtils.forceMkdir(web_inf_lib); |
61 |
0 |
} catch (IOException ex) { |
62 |
0 |
throw new RuntimeException("Failed to create WEB-INF/lib directory in " + |
63 |
|
warRootDir.getAbsolutePath(), ex); |
64 |
0 |
} |
65 |
|
} |
66 |
0 |
return web_inf_lib; |
67 |
|
} |
68 |
|
|
69 |
|
protected File ensureWebInfClassesExists(File warRootDir) { |
70 |
0 |
File web_inf = new File(warRootDir, "WEB-INF"); |
71 |
0 |
File web_inf_classes = new File(web_inf, "classes"); |
72 |
0 |
if (! web_inf_classes.exists()) { |
73 |
|
try { |
74 |
0 |
FileUtils.forceMkdir(web_inf_classes); |
75 |
0 |
} catch (IOException ex) { |
76 |
0 |
throw new RuntimeException("Failed to create WEB-INF/classes directory in " + |
77 |
|
warRootDir.getAbsolutePath(), ex); |
78 |
0 |
} |
79 |
|
} |
80 |
0 |
return web_inf_classes; |
81 |
|
} |
82 |
|
|
83 |
|
protected File ensureAppInfLibExists(File earRootDir) { |
84 |
0 |
File app_inf = new File(earRootDir, "APP-INF"); |
85 |
0 |
File app_inf_lib = new File(app_inf, "lib"); |
86 |
0 |
if (! app_inf_lib.exists()) { |
87 |
|
try { |
88 |
0 |
FileUtils.forceMkdir(app_inf_lib); |
89 |
0 |
} catch (IOException ex) { |
90 |
0 |
throw new RuntimeException("Failed to create APP-INF/lib directory in " + |
91 |
|
earRootDir.getAbsolutePath(), ex); |
92 |
0 |
} |
93 |
|
} |
94 |
0 |
return app_inf_lib; |
95 |
|
} |
96 |
|
|
97 |
|
protected File ensureAppInfClassesExists(File earRootDir) { |
98 |
0 |
File app_inf = new File(earRootDir, "APP-INF"); |
99 |
0 |
File app_inf_classes = new File(app_inf, "classes"); |
100 |
0 |
if (! app_inf_classes.exists()) { |
101 |
|
try { |
102 |
0 |
FileUtils.forceMkdir(app_inf_classes); |
103 |
0 |
} catch (IOException ex) { |
104 |
0 |
throw new RuntimeException("Failed to create APP-INF/classes directory in " + |
105 |
|
earRootDir.getAbsolutePath(), ex); |
106 |
0 |
} |
107 |
|
} |
108 |
0 |
return app_inf_classes; |
109 |
|
} |
110 |
|
|
111 |
|
protected DOMParser createParser() { |
112 |
0 |
DOMParser parser = new DOMParser(); |
113 |
|
try { |
114 |
0 |
parser.setFeature("http://xml.org/sax/features/validation",false); |
115 |
0 |
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false); |
116 |
0 |
} catch (SAXNotRecognizedException ex) { |
117 |
0 |
throw new RuntimeException("Failed to create SAX Parser", ex); |
118 |
0 |
} catch (SAXNotSupportedException ex) { |
119 |
0 |
throw new RuntimeException("Failed to create SAX Parser", ex); |
120 |
0 |
} |
121 |
0 |
return parser; |
122 |
|
} |
123 |
|
|
124 |
|
protected Node findFirstMatchingElement(Document doc, String[] names) { |
125 |
0 |
Node node = null; |
126 |
0 |
for (int i = 0; i < names.length; i++) { |
127 |
0 |
node = getFirstDescendantNodeByName(doc, names[i]); |
128 |
0 |
if (node != null) { |
129 |
0 |
return node; |
130 |
|
} |
131 |
|
} |
132 |
0 |
return null; |
133 |
|
} |
134 |
|
|
135 |
|
protected Node getFirstDescendantNodeByName(Document doc, String name) { |
136 |
0 |
NodeList nodes = doc.getElementsByTagName(name); |
137 |
0 |
if (nodes != null && nodes.getLength() != 0) { |
138 |
0 |
return nodes.item(0); |
139 |
|
} else { |
140 |
0 |
return null; |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
protected void writeFile(File file, Document xmlDocument) { |
145 |
0 |
FileWriter writer = null; |
146 |
|
try { |
147 |
0 |
writer = new FileWriter(file); |
148 |
0 |
OutputFormat of = new OutputFormat(xmlDocument, "UTF-8", false); |
149 |
0 |
of.setIndenting(true); |
150 |
0 |
XMLSerializer xmlser = new XMLSerializer(writer, of); |
151 |
0 |
xmlser.serialize(xmlDocument); |
152 |
0 |
writer.flush(); |
153 |
0 |
} catch (IOException ioex) { |
154 |
0 |
throw new RuntimeException("Failed to write XML document " + xmlDocument + |
155 |
|
" to file " + file.getAbsolutePath()); |
156 |
|
} finally { |
157 |
0 |
try { |
158 |
0 |
if (writer != null) writer.close(); |
159 |
0 |
} catch (IOException ignored) { |
160 |
0 |
} |
161 |
0 |
} |
162 |
0 |
} |
163 |
|
} |