KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > data > exe > MessageVariableInstance


1 package org.jbpm.bpel.data.exe;
2
3 import javax.wsdl.Part;
4 import javax.xml.namespace.QName JavaDoc;
5
6 import org.w3c.dom.Document JavaDoc;
7 import org.w3c.dom.Element JavaDoc;
8
9 import org.jbpm.bpel.data.def.MessageTypeInfo;
10 import org.jbpm.bpel.data.def.VariableDefinition;
11 import org.jbpm.bpel.xml.BpelConstants;
12 import org.jbpm.bpel.xml.util.NodeUtil;
13
14 /**
15  * @author Alejandro Guízar
16  * @version $Revision: 1.6 $ $Date: 2005/06/23 02:22:46 $
17  */

18 public class MessageVariableInstance extends VariableInstance {
19   
20   private Element JavaDoc message;
21   
22   private static long currentId = 0L;
23
24   public Object JavaDoc getValue() {
25     return getMessage();
26   }
27   
28   public void setValue(Object JavaDoc value) {
29     NodeUtil.setValue(getOrCreateMessage(), value);
30   }
31   
32   public Object JavaDoc getOrCreateValue() {
33     return getOrCreateMessage();
34   }
35   
36   public Element JavaDoc getMessage() {
37     if (message == null) {
38       throw new RuntimeException JavaDoc("Uninitialized variable: " + getDefinition().getName());
39     }
40     return message;
41   }
42   
43   public void setMessage(Element JavaDoc message) {
44     this.message = message;
45   }
46   
47   public Element JavaDoc getOrCreateMessage() {
48     if (message == null) {
49       message = createMessageElement();
50     }
51     return message;
52   }
53
54   public Element JavaDoc getPart(String JavaDoc partName) {
55     // does the wsdl message define such a part?
56
VariableDefinition definition = getDefinition();
57     MessageTypeInfo typeInfo = (MessageTypeInfo) definition.getTypeInfo();
58     Part wsdlPart = typeInfo.getMessage().getPart(partName);
59     if (wsdlPart == null) {
60       throw new RuntimeException JavaDoc("Part not found: " +
61             "variable=" + definition.getName() + ", part=" + partName);
62     }
63     Element JavaDoc message = getOrCreateMessage();
64     // get the part wrapper
65
Element JavaDoc partWrapper = NodeUtil.getElement(message, partName);
66     if (partWrapper == null) {
67       throw new RuntimeException JavaDoc("Uninitialized part: " +
68             "variable=" + definition.getName() + ", part=" + partName);
69     }
70     Element JavaDoc part;
71     // is the part defined as an xml schema type?
72
if (wsdlPart.getTypeName() != null) {
73       part = partWrapper;
74     }
75     else {
76       QName JavaDoc elementName = wsdlPart.getElementName();
77       String JavaDoc namespaceURI = elementName.getNamespaceURI();
78       String JavaDoc localName = elementName.getLocalPart();
79       part = NodeUtil.getElement(partWrapper, namespaceURI, localName);
80       if (part == null) {
81         throw new RuntimeException JavaDoc("Uninitialized part: " +
82                 "variable=" + definition.getName() + ", part=" + partName);
83       }
84     }
85     return part;
86   }
87   
88   public Element JavaDoc getOrCreatePart(String JavaDoc partName) {
89     // does the wsdl message define such a part?
90
MessageTypeInfo typeInfo = (MessageTypeInfo) getDefinition().getTypeInfo();
91     Part wsdlPart = typeInfo.getMessage().getPart(partName);
92     if (wsdlPart == null) {
93       throw new RuntimeException JavaDoc("Part not found: " + partName);
94     }
95     Element JavaDoc message = getOrCreateMessage();
96     // get the part wrapper
97
Element JavaDoc partWrapper = NodeUtil.getElement(message, partName);
98     if (partWrapper == null) {
99       // add part wrapper to message
100
partWrapper = message.getOwnerDocument().createElementNS(null, partName);
101       message.appendChild(partWrapper);
102     }
103     Element JavaDoc part;
104     // is the part defined as an xml schema type?
105
if (wsdlPart.getTypeName() != null) {
106       part = partWrapper;
107     }
108     else {
109       QName JavaDoc elementName = wsdlPart.getElementName();
110       String JavaDoc namespaceURI = elementName.getNamespaceURI();
111       String JavaDoc localName = elementName.getLocalPart();
112       part = NodeUtil.getElement(partWrapper, namespaceURI, localName);
113       if (part == null) {
114         part = partWrapper.getOwnerDocument().createElementNS(namespaceURI, localName);
115         partWrapper.appendChild(part);
116       }
117     }
118     return part;
119   }
120   
121   public void setPart(String JavaDoc partName, Object JavaDoc value) {
122     Element JavaDoc part = getOrCreatePart(partName);
123     NodeUtil.setValue(part, value);
124   }
125   
126   public static Element JavaDoc createMessageElement() {
127     /* create a globally qualified element; the namespace is an uri reserved
128      * by the vendor and the local name is a generated id */

129     String JavaDoc localName = "msg" + Long.toString(generateId(), Character.MAX_RADIX);
130     Document JavaDoc doc = NodeUtil.createDocument();
131     Element JavaDoc message = doc.createElementNS(BpelConstants.NS_VENDOR, localName);
132     doc.appendChild(message);
133     return message;
134   }
135   
136   private static synchronized long generateId() {
137     return currentId++;
138   }
139 }
140
Popular Tags