KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > attachmentservice > AttachmentPurchaseOrderServiceBean


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: AttachmentPurchaseOrderServiceBean.java,v 1.13 2005/08/12 20:46:25 smitha Exp $ */

4
5 package com.sun.j2ee.blueprints.attachmentservice;
6
7 import java.net.*;
8 import java.rmi.*;
9 import java.util.*;
10
11 import javax.ejb.*;
12 import javax.xml.soap.*;
13 import javax.xml.transform.*;
14 import javax.xml.rpc.handler.*;
15 import javax.xml.transform.dom.*;
16
17 import org.w3c.dom.*;
18 import org.w3c.dom.Node JavaDoc;
19
20 public class AttachmentPurchaseOrderServiceBean implements SessionBean {
21     
22     private SessionContext sc;
23     private TransformerFactory transfactory;
24     
25     public AttachmentPurchaseOrderServiceBean() {}
26     
27     /**
28      * This method implements a web service that processes an XML purchase
29      * order document it receives as an attachment
30      * and returns a WS-I Attachment profile conformat message
31      * using the swaRef data type
32      */

33     public java.net.URI JavaDoc submitPO(Source xmlsrc) throws InvalidPOException, RemoteException{
34         URI uri = null;
35         String JavaDoc id = null;
36         try {
37             Transformer transformer = transfactory.newTransformer();
38             DOMResult result = new DOMResult();
39             transformer.transform(xmlsrc, result);
40             Document doc = (Document)result.getNode();
41             Element root = doc.getDocumentElement();
42             NodeList list = root.getElementsByTagName("poId");
43             for (int loop = 0; loop < list.getLength(); loop++) {
44                 Node JavaDoc node = list.item(loop);
45                 if (node != null) {
46                     Node JavaDoc child = node.getFirstChild();
47                     if ((child != null) && child.getNodeValue() != null) id = child.getNodeValue();
48                 }
49             }
50             MessageContext mc = sc.getMessageContext();
51             AttachmentPart att = MessageFactory.newInstance().createMessage().createAttachmentPart();
52             att.setContentId(" < "+ id + " >");
53             att.setContent("<submitPO response/>","text/plain");
54             ArrayList arrList = new ArrayList();
55             arrList.add(att);
56             // app server implementation specific
57
mc.setProperty("com.sun.xml.rpc.attachment.SetAttachmentContext", arrList);
58             uri = new java.net.URI JavaDoc("cid:" + id);
59         } catch (Exception JavaDoc e) {
60             throw new EJBException("AttachmentPOService Having Problems:"+e.getMessage(), e);
61         }
62         //this is done just to illustrate throwing an application specific exception
63
if(id.equals("100"))
64             throw new InvalidPOException("Invalid ID for the purchase order!!! " +
65                     "For demo purposes, we throw " +
66                     "an application defined exception for the ID value of 100.");
67         return uri;
68     }
69     
70     //life cycle methods
71
public void ejbCreate() throws CreateException {
72         transfactory = TransformerFactory.newInstance();
73     }
74     
75     public void setSessionContext(SessionContext sc) {
76         this.sc = sc;
77     }
78     
79     public void ejbRemove() {
80         transfactory=null;
81     }
82     
83     public void ejbActivate() {}
84     
85     public void ejbPassivate() {}
86 }
87
88
89
90
Popular Tags