001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: ProviderProcessor.java 1206 2006-09-23 03:51:32Z elu $
023: */
024: package com.bostechcorp.cbesb.runtime.component.script.processors;
025:
026: import javax.jbi.JBIException;
027: import javax.jbi.messaging.DeliveryChannel;
028: import javax.jbi.messaging.MessageExchange;
029: import javax.jbi.messaging.MessageExchangeFactory;
030: import javax.jbi.messaging.NormalizedMessage;
031: import javax.jbi.messaging.MessageExchange.Role;
032: import javax.xml.namespace.QName;
033: import javax.xml.transform.Source;
034: import javax.xml.transform.Transformer;
035: import javax.xml.transform.TransformerFactory;
036: import javax.xml.transform.dom.DOMResult;
037: import javax.xml.transform.dom.DOMSource;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043: import org.w3c.dom.Node;
044:
045: import com.bostechcorp.cbesb.common.runtime.CbesbException;
046: import com.bostechcorp.cbesb.common.runtime.DataContentException;
047: import com.bostechcorp.cbesb.common.util.ErrorUtil;
048: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbProviderProcessor;
049: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.ServiceDescriptionHandler;
050: import com.bostechcorp.cbesb.runtime.ccsl.lib.NormalizedMessageUtil;
051: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.JbiWrapperHandler;
052: import com.bostechcorp.cbesb.runtime.component.script.ScriptEndpoint;
053:
054: public class ScriptProviderProcessor extends CbProviderProcessor {
055:
056: protected final transient Log logger = LogFactory
057: .getLog(getClass());
058:
059: private ScriptEndpoint endpoint;
060: private MessageExchangeFactory messageExchangeFactory;
061: private DeliveryChannel channel;
062:
063: public ScriptProviderProcessor(ScriptEndpoint endpoint) {
064: super (endpoint);
065: this .endpoint = endpoint;
066: }
067:
068: public MessageExchangeFactory getMessageExchangeFactory() {
069: return messageExchangeFactory;
070: }
071:
072: public void setMessageExchangeFactory(
073: MessageExchangeFactory messageExchangeFactory) {
074: this .messageExchangeFactory = messageExchangeFactory;
075: }
076:
077: public DeliveryChannel getChannel() {
078: return channel;
079: }
080:
081: public void setChannel(DeliveryChannel channel) {
082: this .channel = channel;
083: }
084:
085: @Override
086: protected void doStart() throws CbesbException {
087: //Only execute if the endpoint is configured as a provider.
088: if (endpoint.getRole().equals(Role.PROVIDER)) {
089: if (logger.isDebugEnabled())
090: logger.debug("ScriptProviderProcessr::doStart role="
091: + endpoint.getRole());
092:
093: ScriptUtility.getInstance().processScript("start", logger,
094: endpoint, channel, null);
095: }
096: }
097:
098: @Override
099: protected void doStop() throws CbesbException {
100: //Only execute if the endpoint is configured as a provider.
101: if (endpoint.getRole().equals(Role.PROVIDER)) {
102: if (logger.isDebugEnabled())
103: logger.debug("ScriptProviderProcessr::doStop role="
104: + endpoint.getRole());
105:
106: ScriptUtility.getInstance().processScript("stop", logger,
107: endpoint, channel, null);
108: }
109: }
110:
111: @Override
112: public void processInMessage(QName service, QName operation,
113: NormalizedMessage in, MessageExchange exchange)
114: throws CbesbException, JBIException {
115:
116: in.setContent(unWrapMessageContent(in.getContent()));
117: ScriptUtility.getInstance().processScript("run", logger,
118: endpoint, channel, exchange);
119:
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.BaseProviderProcessor#processInOutMessage(javax.xml.namespace.QName,
126: * javax.xml.namespace.QName, javax.jbi.messaging.NormalizedMessage,
127: * javax.jbi.messaging.NormalizedMessage, boolean)
128: */
129: public boolean processInOutMessage(QName service, QName operation,
130: NormalizedMessage in, NormalizedMessage out,
131: boolean optionalOut, MessageExchange exchange)
132: throws CbesbException, JBIException {
133:
134: in.setContent(unWrapMessageContent(in.getContent()));
135: exchange.setMessage(out, "out");
136: ScriptUtility.getInstance().processScript("run", logger,
137: endpoint, channel, exchange);
138:
139: ServiceDescriptionHandler svcDescHandler = getProviderSvcDescHandlerInstance();
140: if (svcDescHandler.isWsdl11()) {
141: JbiWrapperHandler jwh = new JbiWrapperHandler();
142: jwh.appendPart(out.getContent());
143: jwh.setType(svcDescHandler
144: .getOutputMessageQName(svcDescHandler
145: .getFirstOperation()));
146:
147: out.setContent(jwh.toSource());
148: }
149: return true;
150:
151: }
152:
153: static Source unWrapMessageContent(Source content)
154: throws CbesbException {
155: // if (content != null && !(content instanceof DOMSource)) {
156: // //convert StreamSource into a DOMSource
157: // try {
158: // DOMResult dr = new DOMResult();
159: // TransformerFactory tf = TransformerFactory.newInstance();
160: // Transformer t = tf.newTransformer();
161: // t.transform(content, dr);
162: // content = new DOMSource(dr.getNode());
163: // }
164: // catch (Exception e) {
165: // ErrorUtil.printError("Exception converting content to DOMSource: ",e);
166: // }
167: // }
168:
169: NormalizedMessageUtil.convertContentAsDOMSource(content);
170: if (content instanceof DOMSource) {
171: try {
172: Node node = ((DOMSource) content).getNode();
173: if (node instanceof Document) {
174: node = ((Document) node).getDocumentElement();
175: }
176: if (node instanceof Element) {
177: Element root = (Element) node;
178: if (JbiWrapperHandler.JBI_WRAPPER_NS.equals(root
179: .getNamespaceURI())
180: && JbiWrapperHandler.JBI_MESSAGE
181: .equals(root.getLocalName())) {
182: JbiWrapperHandler jwh = JbiWrapperHandler
183: .createFromSource(content);
184: if (jwh.getPartCount() > 0) {
185: content = jwh.getPartAtIndex(0);
186: }
187: }
188: }
189: } catch (Exception e) {
190: throw new DataContentException(
191: "Exception unwrapping message content. - "
192: + e.getMessage(),
193: "Check the message content.", e);
194: }
195: }
196: return content;
197:
198: }
199:
200: }
|