1
2
3
4
5
6
7
8 package it.imolinfo.jbi4ejb.configuration;
9
10 import it.imolinfo.jbi4ejb.Logger;
11 import it.imolinfo.jbi4ejb.LoggerFactory;
12 import it.imolinfo.jbi4ejb.exception.EJBWSDLGenerationException;
13 import it.imolinfo.jbi4ejb.jbi.Messages;
14 import it.imolinfo.jbi4ejb.webservice.generator.JarUtil;
15 import it.imolinfo.jbi4ejb.webservice.generator.Util;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25
26 import org.w3c.dom.CharacterData;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31 import org.xml.sax.SAXException;
32
33
34
35
36 public final class InterfaceExtractorUtil {
37
38
39 private static final String EAR_TEMP_DIR = "EAR_";
40
41
42 private static final String REMOTE = "remote";
43
44
45 private static final String STATELESS = "Stateless";
46
47
48 private static final String SESSION_TYPE = "session-type";
49
50
51 private static final String SESSION = "session";
52
53
54 private static final String EJB = "ejb";
55
56
57 private static final String APPLICATION_XML = "application.xml";
58
59
60 private static final Logger LOG = LoggerFactory.getLogger(InterfaceExtractorUtil.class);
61
62 private static final Messages MESSAGES=Messages.getMessages(InterfaceExtractorUtil.class);
63
64
65
66
67 private InterfaceExtractorUtil(){}
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public static List<String> extractRemoteInterfacesFromEar(String earPath) throws EJBWSDLGenerationException {
84
85
86 File tempDir;
87 try {
88 tempDir = File.createTempFile(EAR_TEMP_DIR, null);
89 } catch (IOException e) {
90 String msg=MESSAGES.getString("EJB000001_Could_not_create_directory", new Object[]{EAR_TEMP_DIR});
91 LOG.error(msg,e);
92 throw new EJBWSDLGenerationException(msg,e);
93 }
94 tempDir.delete();
95 tempDir.mkdir();
96
97 try {
98 JarUtil.unjar(new File(earPath), tempDir);
99 } catch (IOException e) {
100 String msg=MESSAGES.getString("EJB000002_Failure_in_opening_Jarfile", new Object[] {earPath});
101 LOG.error(msg,e);
102 throw new EJBWSDLGenerationException(msg,e);
103 }
104 File applicationXml = findSingleFile(tempDir.getAbsolutePath(), APPLICATION_XML);
105
106 if (applicationXml == null) {
107 String msg=MESSAGES.getString("EJB000003_No_application.xml_found_in_earPath", new Object[] {earPath});
108 LOG.error(msg);
109 throw new EJBWSDLGenerationException(msg);
110 }
111
112
113 List<String> jarList = getEjbJarFromApplicationXML(applicationXml);
114
115 List<String> remoteInterfaces = new ArrayList<String>();
116
117
118 for (String jarName:jarList) {
119 File jarFile = findSingleFile(tempDir.getAbsolutePath(), jarName);
120 remoteInterfaces.addAll(extractRemoteInterfaceFromJar(jarFile.getAbsolutePath()));
121 }
122
123
124 tempDir.delete();
125
126 return remoteInterfaces;
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public static List<String> extractRemoteInterfaceFromJar(String jarPath) throws EJBWSDLGenerationException {
142
143 List<String> remoteInterfaces = new ArrayList<String>();
144
145
146 File tempDir;
147 try {
148 tempDir = File.createTempFile("JAR_", null);
149 } catch (IOException e) {
150 String msg=MESSAGES.getString("EJB000001_Could_not_create_directory", new Object[]{"JAR_"});
151 LOG.error(msg,e);
152 throw new EJBWSDLGenerationException(msg,e);
153
154 }
155 tempDir.delete();
156 tempDir.mkdir();
157
158 try {
159 JarUtil.unjar(new File(jarPath), tempDir);
160 } catch (IOException e) {
161 String msg=MESSAGES.getString("EJB000002_Failure_in_opening_Jarfile", new Object[] {jarPath});
162 LOG.error(msg,e);
163 throw new EJBWSDLGenerationException(msg,e);
164 }
165
166 File ejbJarXml = findSingleFile(tempDir.getAbsolutePath(), "ejb-jar.xml");
167 List<String> interfacesListFromEjbJar = new ArrayList<String>();
168 if (ejbJarXml != null) {
169
170 interfacesListFromEjbJar = getInterfacesFromEjbJarXml(ejbJarXml);
171 }
172 remoteInterfaces.addAll(interfacesListFromEjbJar);
173
174
175
176
177
178
179
180 tempDir.delete();
181
182 return remoteInterfaces;
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 private static File findSingleFile(String sourceDir, String fileName) throws EJBWSDLGenerationException {
205
206 File applicationXml = null;
207 List<File> files = Util.findFilesFromSourceDirectoryFromExactFileName(sourceDir, fileName);
208 if (files.size() > 1) {
209 String msg=MESSAGES.getString("EJB000004_Found_file_size_exceeded", new Object[] {files.size()}, new Object[] {fileName});
210 LOG.error(msg);
211 throw new EJBWSDLGenerationException(msg);
212 } else if (files.size() != 0) {
213 applicationXml = files.get(0);
214 }
215 return applicationXml;
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229 private static List<String>getInterfacesFromEjbJarXml(File ejbJarXml) throws EJBWSDLGenerationException {
230 List<String> interfacesList = new ArrayList<String>();
231
232 Document doc = getDocument(ejbJarXml);
233
234
235 NodeList nodes = doc.getElementsByTagName(SESSION);
236
237 for (int i = 0; i < nodes.getLength(); i++) {
238 Element element = (Element) nodes.item(i);
239
240 NodeList sessionType = element.getElementsByTagName(SESSION_TYPE);
241 Element type = (Element) sessionType.item(0);
242
243 if (getCharacterDataFromElement(type).equalsIgnoreCase(STATELESS)) {
244 LOG.debug("Found a stateless EJB");
245 NodeList remoteElement = element.getElementsByTagName(REMOTE);
246 Element remote = (Element) remoteElement.item(0);
247 String remoteInterfaceName = getCharacterDataFromElement(remote);
248 LOG.debug("Found a stateless EJB with interface: " + remoteInterfaceName);
249 interfacesList.add(remoteInterfaceName);
250 }
251 }
252
253 return interfacesList;
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 private static List<String> getEjbJarFromApplicationXML(File applicationXml) throws EJBWSDLGenerationException {
307
308 List<String> jarList = new ArrayList<String>();
309
310 Document doc = getDocument(applicationXml);
311
312 NodeList nodes = doc.getElementsByTagName(EJB);
313
314 for (int i = 0; i < nodes.getLength(); i++) {
315 Element element = (Element) nodes.item(i);
316 String ejbJarName = getCharacterDataFromElement(element);
317 jarList.add(ejbJarName);
318 }
319
320 return jarList;
321 }
322
323
324
325
326
327
328
329
330
331
332
333 private static String getCharacterDataFromElement(Element e) {
334 Node child = e.getFirstChild();
335 if (child instanceof CharacterData) {
336 CharacterData cd = (CharacterData) child;
337 return cd.getData();
338 }
339 return "";
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353 private static Document getDocument(File file) throws EJBWSDLGenerationException {
354 Document doc = null;
355 try {
356
357 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
358 factory.setValidating(false);
359 factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", Boolean.FALSE);
360 DocumentBuilder builder = factory.newDocumentBuilder();
361 doc = builder.parse(file);
362
363 }catch (ParserConfigurationException e) {
364 String msg=MESSAGES.getString("EJB000005_Error_in_getting_document", new Object[]{file});
365 LOG.error(msg,e);
366 throw new EJBWSDLGenerationException(msg,e);
367 } catch (IOException e) {
368 String msg=MESSAGES.getString("EJB000005_Error_in_getting_document", new Object[]{file});
369 LOG.error(msg,e);
370 throw new EJBWSDLGenerationException(msg,e);
371 } catch (SAXException e) {
372 String msg=MESSAGES.getString("EJB000005_Error_in_getting_document", new Object[]{file});
373 LOG.error(msg,e);
374 throw new EJBWSDLGenerationException(msg,e);
375 }
376 return doc;
377 }
378
379
380
381
382
383
384
385
386
387
388
389
390
391 public static String extractEarClassesInTempDirectory(String earPath) throws EJBWSDLGenerationException {
392
393
394 File tempDir;
395 try {
396 tempDir = File.createTempFile(EAR_TEMP_DIR, null);
397 } catch (IOException e) {
398 String msg=MESSAGES.getString("EJB000001_Could_not_create_directory", new Object[]{EAR_TEMP_DIR});
399 LOG.error(msg,e);
400 throw new EJBWSDLGenerationException(msg,e);
401 }
402 tempDir.delete();
403 tempDir.mkdir();
404
405 try {
406 JarUtil.unjar(new File(earPath), tempDir);
407 } catch (IOException e) {
408 String msg=MESSAGES.getString("EJB000001_Could_not_create_directory", new Object[]{EAR_TEMP_DIR});
409 LOG.error(msg,e);
410 throw new EJBWSDLGenerationException(msg,e);
411 }
412 File applicationXml = findSingleFile(tempDir.getAbsolutePath(), APPLICATION_XML);
413
414 if (applicationXml == null) {
415 String msg=MESSAGES.getString("EJB000003_No_application.xml_found_in_earPath", new Object[] {earPath});
416 LOG.error(msg);
417 throw new EJBWSDLGenerationException(msg);
418 }
419
420
421 List<String> jarList = getEjbJarFromApplicationXML(applicationXml);
422
423
424 for (String jarName:jarList) {
425 LOG.info("EJB000006_Found_ejb-jar", new Object[]{jarName});
426 File jarFile = findSingleFile(tempDir.getAbsolutePath(), jarName);
427 try {
428 JarUtil.unjar(jarFile, tempDir);
429 } catch (IOException e) {
430 String msg=MESSAGES.getString("EJB000002_Failure_in_opening_Jarfile", new Object[] {jarFile});
431 LOG.error(msg,e);
432 throw new EJBWSDLGenerationException(msg,e);
433 }
434 }
435
436 return tempDir.getAbsolutePath();
437 }
438
439
440 }