001: package com.bostechcorp.cbesb.custom;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.util.LinkedList;
007: import java.util.Map;
008:
009: import javax.jbi.component.ComponentContext;
010: import javax.jbi.messaging.DeliveryChannel;
011: import javax.jbi.messaging.MessageExchange;
012: import javax.jbi.messaging.NormalizedMessage;
013: import javax.xml.parsers.DocumentBuilder;
014: import javax.xml.parsers.DocumentBuilderFactory;
015: import javax.xml.transform.Source;
016: import javax.xml.transform.Transformer;
017: import javax.xml.transform.TransformerFactory;
018: import javax.xml.transform.dom.DOMSource;
019: import javax.xml.transform.stream.StreamResult;
020:
021: import org.apache.commons.logging.Log;
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.Text;
026:
027: import com.bostechcorp.cbesb.runtime.ccsl.lib.IUpocInterface;
028: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
029: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
030:
031: /*
032: * This class saves message exchanges to a file using an xml format.
033: */
034:
035: public class SaveExchangeToFile implements IUpocInterface {
036: private boolean isInitialized;
037: private File saveFile;
038: protected static final String CBESB_HOME = "CBESB_HOME";
039:
040: protected static final String CBESB_HOME_SYSPROP = "cbesb.home";
041:
042: /**
043: * @param logger - the loggger object
044: * @param rootDir - the root direcory of the SU
045: * @param componentContext - the componentContext object
046: * @param channel - the delivery channel object
047: * @param exchange - the MessageExchange object passed in
048: * @param params - the user paramaters in name/value pair
049: * @return - the LinkList containning one of many MessageExchange objects
050: * @throws Exception
051: */
052: public LinkedList run(Log logger, String rootDir,
053: ComponentContext componentContext, DeliveryChannel channel,
054: MessageExchange exchange, Map<String, String> params)
055: throws Exception {
056:
057: LinkedList<MessageExchange> sendList = new LinkedList<MessageExchange>();
058:
059: if (!isInitialized)
060: initialize(logger, rootDir, exchange.getEndpoint()
061: .getEndpointName());
062:
063: // Output exchange to a DOM tree
064: DocumentBuilderFactory factory = DocumentBuilderFactory
065: .newInstance();
066: DocumentBuilder parser = factory.newDocumentBuilder();
067: Document doc = parser.newDocument();
068: Element exchangeNode = doc.createElement("exchange");
069: doc.appendChild(exchangeNode);
070: Element exchangeId = doc.createElement("exchangeId");
071: exchangeNode.appendChild(exchangeId);
072: Text idValue = doc.createTextNode(exchange.getExchangeId());
073: exchangeId.appendChild(idValue);
074: Element contentNode = doc.createElement("content");
075: exchangeNode.appendChild(contentNode);
076: // Output content elements
077: NormalizedMessage m = exchange.getMessage("in");
078: NormalizedMessageHandler nmh = new NormalizedMessageHandler(m);
079: for (int i = 0; i < nmh.getRecordCount(); i++) {
080: Source src = nmh.getRecordAtIndex(i);
081: if (src instanceof StringSource) {
082: Element e = doc.createElement("string");
083: e.appendChild(doc.createTextNode(((StringSource) src)
084: .getText()));
085: contentNode.appendChild(e);
086: } else if (src instanceof DOMSource) {
087: Element e = doc.createElement("xml");
088: Node n = ((DOMSource) src).getNode();
089: e.appendChild(n.cloneNode(true));
090: contentNode.appendChild(e);
091:
092: }
093: }
094: // transform to file
095: StreamResult r = new StreamResult(
096: new FileWriter(saveFile, true));
097: TransformerFactory tf = TransformerFactory.newInstance();
098: Transformer t = tf.newTransformer();
099: t.setOutputProperty("indent", "yes");
100: t.setOutputProperty("omit-xml-declaration", "yes");
101: t.transform(new DOMSource(doc), r);
102: r.getWriter().close();
103:
104: // send the exchange
105: sendList.add(exchange);
106: return sendList;
107: }
108:
109: private void initialize(Log log, String rootDir, String endpointName)
110: throws Exception {
111: String fileName = getCbesbHomeDir() + File.separator + "save"
112: + File.separator + endpointName + ".sav";
113: saveFile = new File(fileName);
114: log.info(this .getClass().getName() + " initializing");
115: log.info("save file=" + saveFile.getAbsolutePath());
116: isInitialized = true;
117: }
118:
119: public String getCbesbHomeDir() {
120: String esbHome = System.getenv(CBESB_HOME);
121: String esbHomeProp = System.getProperty(CBESB_HOME_SYSPROP);
122:
123: // Always to use the -D option to overwrite the system enviroment
124: if (esbHomeProp != null)
125: esbHome = esbHomeProp;
126: File file = new File(esbHome);
127: try {
128: return file.getCanonicalPath();
129: } catch (IOException e) {
130: //log.error("get cbesb home dir error:",e);
131: }
132:
133: return esbHome;
134: }
135:
136: }
|