KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > docoriented > client > stringposervice > StringPOServiceBD


1 /* Copyright 2005 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: StringPOServiceBD.java,v 1.14 2005/08/12 20:45:30 smitha Exp $ */

4
5 package com.sun.j2ee.blueprints.docoriented.client.stringposervice;
6
7 import java.rmi.*;
8 import java.io.*;
9
10 import javax.xml.parsers.*;
11
12 import org.w3c.dom.*;
13 import org.xml.sax.*;
14
15 import com.sun.j2ee.blueprints.docoriented.client.*;
16
17 /**
18  * Implements the Business Delegate pattern for Web services.
19  * Handles making the exchange of messages with the Web service endpoint
20  */

21
22 public class StringPOServiceBD {
23     
24     private ServiceLocator serviceLocator;
25     
26     public StringPOServiceBD(){
27         serviceLocator = new ServiceLocator();
28     }
29     
30     public String JavaDoc submitPO(PurchaseOrder po) throws RequestHandlerException {
31         String JavaDoc ret = null;
32         try {
33             StringPurchaseOrderServiceSEI port = (StringPurchaseOrderServiceSEI)
34             serviceLocator.getServicePort(JNDINames.STRING_SERVICE_REF, StringPurchaseOrderServiceSEI.class);
35             String JavaDoc xmlDoc = po.toXMLString();
36             if(!(validate(xmlDoc))){
37                 throw new RequestHandlerException("Request Handler Exception: Error parsing the purchase order XML document");
38             }
39             ret = port.submitPO(xmlDoc);
40             return ret;
41         } catch(InvalidPOException ipoe){
42             ipoe.printStackTrace(System.err);
43             throw new RequestHandlerException("Request Handler Exception: Service Endpoint Application-Defined Exception "+ipoe.getMessage(), ipoe);
44         } catch(RemoteException re){
45             re.printStackTrace(System.err);
46             throw new RuntimeException JavaDoc("The web service you are trying to access is not available. A possible reason could be that the service has not been deployed yet. "+ re.getMessage(), re);
47         }
48     }
49     
50     /**
51      * Method to validate the XML doc
52      * @param xmlDoc the doc to be validated
53      * @return validation status
54      */

55     private boolean validate(String JavaDoc xmlDoc) {
56         Document doc = null;
57         try {
58             DocumentBuilderFactory dbf = null;
59             DocumentBuilder db = null;
60             try {
61                 dbf = DocumentBuilderFactory.newInstance();
62                 dbf.setValidating(true);
63                 dbf.setNamespaceAware(true);
64                 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
65                         "http://www.w3.org/2001/XMLSchema");
66                 if (dbf != null){
67                     db = dbf.newDocumentBuilder();
68                 }
69                 db.setEntityResolver(new POEntityResolver());
70                 db.setErrorHandler(new POXMLErrorHandler());
71             } catch (ParserConfigurationException pce) {
72                 pce.printStackTrace(System.err);
73                 throw new RuntimeException JavaDoc(pce.getMessage(), pce);
74             }
75             InputSource is = new InputSource(new StringReader(xmlDoc));
76             doc = db.parse(is);
77             return true;
78         } catch (Exception JavaDoc e) {
79             System.err.println("XML Validation Error " + e);
80         }
81         return false;
82     }
83 }
Popular Tags