1 package ejava.projects.esales.ejb;
2
3 import ejava.projects.esales.dto.Account;
4
5 import ejava.projects.esales.dto.Address;
6 import ejava.projects.esales.dto.Auction;
7 import ejava.projects.esales.dto.Bid;
8 import ejava.projects.esales.dto.ESales;
9
10 import java.io.InputStream;
11
12 import static junit.framework.TestCase.*;
13
14 import javax.annotation.PostConstruct;
15 import javax.annotation.Resource;
16 import javax.ejb.EJBException;
17 import javax.ejb.Stateless;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import ejava.projects.esales.xml.ESalesParser;
23
24 @Stateless
25 public class ParserTestEJB implements ParserTestRemote {
26 private static final Logger log = LoggerFactory.getLogger(ParserTestEJB.class);
27
28 @Resource(name="vals/xmlFile")
29 private String xmlFile;
30
31 @PostConstruct
32 public void init() {
33 log.debug("*** ParserTestEJB ***");
34 log.debug("xmlFile=" + xmlFile);
35 }
36
37 public void ingest() throws Exception {
38 log.info("ingest");
39
40 InputStream is = null;
41
42 try {
43 log.trace("getting input file:" + xmlFile);
44 is = this.getClass().getResourceAsStream(xmlFile);
45 if (is == null) {
46 throw new Exception(xmlFile + " was not found");
47 }
48
49 log.trace("creating parser");
50 ESalesParser parser = new ESalesParser(ESales.class, is);
51
52 log.trace("starting parse loop");
53 Object object=null;
54 do {
55 object = parser.getObject(
56 "Address", "Account", "Auction", "Bid");
57 if (object instanceof Address) {
58 log.debug("found address");
59 }
60 else if (object instanceof Account) {
61 log.debug("found Account");
62 }
63 else if (object instanceof Auction) {
64 log.debug("found Auction");
65 }
66 else if (object instanceof Bid) {
67 log.debug("found Bid");
68 }
69 else if (object != null) {
70 fail("object of unknown type:" + object);
71 }
72 } while (object != null);
73 }
74 catch (Throwable ex) {
75 log.error("error parsing doc",ex);
76 throw new EJBException("error parsing doc:" + ex);
77 }
78 finally {
79 if (is != null) is.close();
80 }
81 }
82
83 }