1 package org.jbpm.bpel.data.exe; 2 3 import javax.wsdl.Part; 4 import javax.xml.namespace.QName ; 5 6 import org.w3c.dom.Document ; 7 import org.w3c.dom.Element ; 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 18 public class MessageVariableInstance extends VariableInstance { 19 20 private Element message; 21 22 private static long currentId = 0L; 23 24 public Object getValue() { 25 return getMessage(); 26 } 27 28 public void setValue(Object value) { 29 NodeUtil.setValue(getOrCreateMessage(), value); 30 } 31 32 public Object getOrCreateValue() { 33 return getOrCreateMessage(); 34 } 35 36 public Element getMessage() { 37 if (message == null) { 38 throw new RuntimeException ("Uninitialized variable: " + getDefinition().getName()); 39 } 40 return message; 41 } 42 43 public void setMessage(Element message) { 44 this.message = message; 45 } 46 47 public Element getOrCreateMessage() { 48 if (message == null) { 49 message = createMessageElement(); 50 } 51 return message; 52 } 53 54 public Element getPart(String partName) { 55 VariableDefinition definition = getDefinition(); 57 MessageTypeInfo typeInfo = (MessageTypeInfo) definition.getTypeInfo(); 58 Part wsdlPart = typeInfo.getMessage().getPart(partName); 59 if (wsdlPart == null) { 60 throw new RuntimeException ("Part not found: " + 61 "variable=" + definition.getName() + ", part=" + partName); 62 } 63 Element message = getOrCreateMessage(); 64 Element partWrapper = NodeUtil.getElement(message, partName); 66 if (partWrapper == null) { 67 throw new RuntimeException ("Uninitialized part: " + 68 "variable=" + definition.getName() + ", part=" + partName); 69 } 70 Element part; 71 if (wsdlPart.getTypeName() != null) { 73 part = partWrapper; 74 } 75 else { 76 QName elementName = wsdlPart.getElementName(); 77 String namespaceURI = elementName.getNamespaceURI(); 78 String localName = elementName.getLocalPart(); 79 part = NodeUtil.getElement(partWrapper, namespaceURI, localName); 80 if (part == null) { 81 throw new RuntimeException ("Uninitialized part: " + 82 "variable=" + definition.getName() + ", part=" + partName); 83 } 84 } 85 return part; 86 } 87 88 public Element getOrCreatePart(String partName) { 89 MessageTypeInfo typeInfo = (MessageTypeInfo) getDefinition().getTypeInfo(); 91 Part wsdlPart = typeInfo.getMessage().getPart(partName); 92 if (wsdlPart == null) { 93 throw new RuntimeException ("Part not found: " + partName); 94 } 95 Element message = getOrCreateMessage(); 96 Element partWrapper = NodeUtil.getElement(message, partName); 98 if (partWrapper == null) { 99 partWrapper = message.getOwnerDocument().createElementNS(null, partName); 101 message.appendChild(partWrapper); 102 } 103 Element part; 104 if (wsdlPart.getTypeName() != null) { 106 part = partWrapper; 107 } 108 else { 109 QName elementName = wsdlPart.getElementName(); 110 String namespaceURI = elementName.getNamespaceURI(); 111 String 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 partName, Object value) { 122 Element part = getOrCreatePart(partName); 123 NodeUtil.setValue(part, value); 124 } 125 126 public static Element createMessageElement() { 127 129 String localName = "msg" + Long.toString(generateId(), Character.MAX_RADIX); 130 Document doc = NodeUtil.createDocument(); 131 Element 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 |