001: package org.objectweb.celtix.js.rhino;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import javax.xml.namespace.QName;
009: import javax.xml.transform.Source;
010: import javax.xml.transform.dom.DOMSource;
011: import javax.xml.transform.stream.StreamSource;
012: import javax.xml.ws.Endpoint;
013:
014: import org.w3c.dom.Node;
015:
016: import org.apache.xmlbeans.XmlObject;
017:
018: import org.mozilla.javascript.Context;
019: import org.mozilla.javascript.Function;
020: import org.mozilla.javascript.Scriptable;
021: import org.mozilla.javascript.ScriptableObject;
022: import org.mozilla.javascript.Wrapper;
023:
024: public abstract class AbstractDOMProvider {
025: public static class JSDOMProviderException extends Exception {
026: public JSDOMProviderException(String reason) {
027: super (reason);
028: }
029: }
030:
031: public static final String NO_WSDL_LOCATION = "no wsdlLocation property found";
032: public static final String NO_SERVICE_NAME = "no serviceName property found";
033: public static final String NO_PORT_NAME = "no portName property found";
034: public static final String NO_TGT_NAMESPACE = "no targetNamespace property found";
035: public static final String NO_INVOKE = "no invoke property found";
036: public static final String ILLEGAL_INVOKE_TYPE = "invoke property is not a Function type";
037: public static final String NO_EP_ADDR = "no endpoint address specified";
038:
039: private Scriptable scriptScope;
040: private Scriptable webSvcProviderVar;
041: private String epAddress;
042: private boolean isBaseAddr;
043: private boolean isE4X;
044: private Function invokeFunc;
045:
046: protected AbstractDOMProvider(Scriptable scope, Scriptable wspVar,
047: String epAddr, boolean isBase, boolean e4x) {
048: scriptScope = scope;
049: webSvcProviderVar = wspVar;
050: epAddress = epAddr;
051: isBaseAddr = isBase;
052: isE4X = e4x;
053: }
054:
055: public void publish() throws Exception {
056: String addr = epAddress;
057: String wsdlLoc = null;
058: String svcNm = null;
059: String portNm = null;
060: String tgtNmspc = null;
061: String binding = null;
062: Object obj = webSvcProviderVar.get("wsdlLocation",
063: webSvcProviderVar);
064: if (obj == Scriptable.NOT_FOUND) {
065: throw new JSDOMProviderException(NO_WSDL_LOCATION);
066: }
067: if (obj instanceof String) {
068: wsdlLoc = (String) obj;
069: }
070: obj = webSvcProviderVar.get("serviceName", webSvcProviderVar);
071: if (obj == Scriptable.NOT_FOUND) {
072: throw new JSDOMProviderException(NO_SERVICE_NAME);
073: }
074: if (obj instanceof String) {
075: svcNm = (String) obj;
076: }
077: obj = webSvcProviderVar.get("portName", webSvcProviderVar);
078: if (obj == Scriptable.NOT_FOUND) {
079: throw new JSDOMProviderException(NO_PORT_NAME);
080: }
081: if (obj instanceof String) {
082: portNm = (String) obj;
083: }
084: obj = webSvcProviderVar.get("targetNamespace",
085: webSvcProviderVar);
086: if (obj == Scriptable.NOT_FOUND) {
087: throw new JSDOMProviderException(NO_TGT_NAMESPACE);
088: }
089: if (obj instanceof String) {
090: tgtNmspc = (String) obj;
091: }
092: if (addr == null) {
093: obj = webSvcProviderVar.get("EndpointAddress", scriptScope);
094: if (obj != Scriptable.NOT_FOUND && obj instanceof String) {
095: addr = (String) obj;
096: isBaseAddr = false;
097: }
098: }
099: if (addr == null) {
100: throw new JSDOMProviderException(NO_EP_ADDR);
101: }
102: if (isBaseAddr) {
103: if (addr.endsWith("/")) {
104: addr += portNm;
105: } else {
106: addr = addr + "/" + portNm;
107: }
108: }
109: obj = webSvcProviderVar.get("BindingType", scriptScope);
110: if (obj != Scriptable.NOT_FOUND && obj instanceof String) {
111: binding = (String) obj;
112: }
113: obj = webSvcProviderVar.get("invoke", webSvcProviderVar);
114: if (obj == Scriptable.NOT_FOUND) {
115: throw new JSDOMProviderException(NO_INVOKE);
116: }
117: if (obj instanceof Function) {
118: invokeFunc = (Function) obj;
119: } else {
120: throw new JSDOMProviderException(ILLEGAL_INVOKE_TYPE);
121: }
122: Endpoint ep = Endpoint.create(binding, this );
123: List<Source> metadata = new ArrayList<Source>();
124: metadata.add(new StreamSource(wsdlLoc));
125: ep.setMetadata(metadata);
126: Map<String, Object> props = new HashMap<String, Object>();
127: props.put(Endpoint.WSDL_SERVICE, new QName(tgtNmspc, svcNm));
128: props.put(Endpoint.WSDL_PORT, new QName(tgtNmspc, portNm));
129: ep.setProperties(props);
130: ep.publish(addr);
131: }
132:
133: public DOMSource invoke(DOMSource request) {
134: DOMSource response = new DOMSource();
135: Context cx = Context.enter();
136: try {
137: Scriptable scope = cx.newObject(scriptScope);
138: scope.setPrototype(scriptScope);
139: scope.setParentScope(null);
140: Node node = request.getNode();
141: Object inDoc = null;
142: if (isE4X) {
143: try {
144: Object xo = XmlObject.Factory.parse(node);
145: inDoc = Context.toObject(xo, scope);
146: Object[] args = { inDoc };
147: inDoc = cx.newObject(scriptScope, "XML", args);
148: } catch (Exception ex) {
149: ex.printStackTrace();
150: }
151: } else {
152: inDoc = Context.toObject(node, scope);
153: }
154: Object[] args = { inDoc };
155: Object jsResp = invokeFunc.call(cx, scope, scope, args);
156: if (isE4X) {
157: // need to check return type and throw exception
158: // if wrong type
159: Scriptable s = (Scriptable) jsResp;
160: Object out = ScriptableObject.callMethod(s,
161: "getXmlObject", Context.emptyArgs);
162: Wrapper wrapped = (Wrapper) out;
163: XmlObject xml = (XmlObject) wrapped.unwrap();
164: node = xml.getDomNode();
165: response.setNode(node.getOwnerDocument());
166: } else {
167: if (jsResp instanceof Wrapper) {
168: jsResp = ((Wrapper) jsResp).unwrap();
169: }
170: if (jsResp instanceof Node) {
171: node = (Node) jsResp;
172: response.setNode(node);
173: }
174: }
175: } catch (Exception ex) {
176: ex.printStackTrace();
177: } finally {
178: Context.exit();
179: }
180: return response;
181: }
182: }
|