KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > stringposervice > StringPurchaseOrderServiceBean


1 /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2  http://developer.sun.com/berkeley_license.html
3  $Id: StringPurchaseOrderServiceBean.java,v 1.10 2005/08/12 20:46:39 smitha Exp $ */

4
5 package com.sun.j2ee.blueprints.stringposervice;
6
7 import java.rmi.*;
8 import java.io.*;
9
10 import javax.ejb.*;
11 import javax.xml.parsers.*;
12
13 import org.w3c.dom.*;
14 import org.xml.sax.*;
15
16 /**
17  * A simple endpoint that receives XML PO documents
18  * and returns the po ID
19  */

20 public class StringPurchaseOrderServiceBean implements SessionBean {
21     
22     private SessionContext sc;
23     private DocumentBuilderFactory docBuilderFactory;
24     
25     public StringPurchaseOrderServiceBean(){}
26     
27     public String JavaDoc submitPO(String JavaDoc xmlPO) throws InvalidPOException, RemoteException {
28         String JavaDoc id =null;
29         if (!(validate(xmlPO))) {
30             throw new InvalidPOException("Error parsing the purchase order XML document!!!");
31         }
32         try {
33             //extract the PO ID from the document and return to the client
34
id = getID(xmlPO);
35         } catch (Exception JavaDoc e) {
36             throw new EJBException("StringPurchaseOrderService implementation had trouble parsing PO.xml, some system or configuration problem " + e.getMessage(), e);
37         }
38         //this is done just to illustrate throwing an application specific exception
39
if(id.equals("100"))
40             throw new InvalidPOException("Invalid ID for the purchase order!!! " +
41                     "For demo purposes, we throw " +
42                     "an application defined exception for the ID value of 100.");
43         return id;
44     }
45     
46     /**
47      * Method to extract the PO ID from the XML doc
48      * @param xmlDoc the xml doc
49      * @return the PO ID
50      */

51     private String JavaDoc getID(String JavaDoc xmlDoc) throws org.xml.sax.SAXException JavaDoc, javax.xml.parsers.ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
52         String JavaDoc ret = "";
53         DocumentBuilder db = null;
54         if (docBuilderFactory != null)
55             db = docBuilderFactory.newDocumentBuilder();
56         InputSource is = new InputSource(new StringReader(xmlDoc));
57         Document doc = db.parse(is);
58         Element root = doc.getDocumentElement();
59         NodeList list = root.getElementsByTagName("poId");
60         for (int loop = 0; loop < list.getLength(); loop++) {
61             Node node = list.item(loop);
62             if (node != null) {
63                 Node child = node.getFirstChild();
64                 if ((child != null) && child.getNodeValue() != null) return child.getNodeValue();
65             }
66         }
67         return ret;
68     }
69     
70     /**
71      * Method to validate the XML doc
72      * @param xmlDoc the doc to be validated
73      * @return validation status
74      */

75     private boolean validate(String JavaDoc xmlDoc) {
76         Document doc = null;
77         try {
78             DocumentBuilderFactory dbf = null;
79             DocumentBuilder db = null;
80             try {
81                 dbf = DocumentBuilderFactory.newInstance();
82                 dbf.setValidating(true);
83                 dbf.setNamespaceAware(true);
84                 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
85                         "http://www.w3.org/2001/XMLSchema");
86                 if (dbf != null){
87                     db = dbf.newDocumentBuilder();
88                 }
89                 db.setEntityResolver(new POEntityResolver());
90                 db.setErrorHandler(new POXMLErrorHandler());
91             } catch ( javax.xml.parsers.ParserConfigurationException JavaDoc pce) {
92                 System.err.println(pce);
93             }
94             InputSource is = new InputSource(new StringReader(xmlDoc));
95             doc = db.parse(is);
96             return true;
97         } catch (Exception JavaDoc e) {
98             System.err.println("XML Validation Error " + e);
99         }
100         return false;
101     }
102     
103     //life cycle methods
104
public void ejbCreate() throws CreateException {
105         docBuilderFactory = DocumentBuilderFactory.newInstance();
106     }
107     
108     public void setSessionContext(SessionContext sc) {
109         this.sc = sc;
110     }
111     
112     public void ejbRemove() {
113         docBuilderFactory = null;
114     }
115     
116     public void ejbActivate() {}
117     
118     public void ejbPassivate() {}
119 }
120
Popular Tags