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: ErrorDb.java 11170 2007-12-29 08:40:41Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.lib;
025:
026: import java.sql.SQLException;
027: import java.util.Iterator;
028:
029: import javax.activation.DataHandler;
030: import javax.activation.DataSource;
031: import javax.jbi.messaging.MessageExchange;
032: import javax.jbi.messaging.NormalizedMessage;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import com.bostechcorp.cbesb.runtime.ccsl.errordb.AttachmentVO;
038: import com.bostechcorp.cbesb.runtime.ccsl.errordb.ByteContentVO;
039: import com.bostechcorp.cbesb.runtime.ccsl.errordb.DaoConfig;
040: import com.bostechcorp.cbesb.runtime.ccsl.errordb.ErrorVO;
041: import com.bostechcorp.cbesb.runtime.ccsl.errordb.ExchangePropertyVO;
042: import com.bostechcorp.cbesb.runtime.ccsl.errordb.ExchangeVO;
043: import com.bostechcorp.cbesb.runtime.ccsl.errordb.MessagePropertyVO;
044: import com.bostechcorp.cbesb.runtime.ccsl.errordb.NormalizedMessageVO;
045: import com.bostechcorp.cbesb.runtime.ccsl.errordb.StringContentVO;
046: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.ByteArrayDataSource;
047: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringDataSource;
048: import com.ibatis.sqlmap.client.SqlMapClient;
049:
050: public class ErrorDb {
051: protected static transient Log logger = LogFactory
052: .getLog(ErrorDb.class);
053:
054: public static synchronized void write(Exception e,
055: MessageExchange me, Log log) {
056: if (me == null)
057: return;
058: long exchangeId = -1;
059: log.debug("writing message to error database");
060: SqlMapClient sqlMap = null;
061: try {
062: // System.out.println("getting sqlmapclient");
063: DaoConfig daoConfig = new DaoConfig();
064: sqlMap = daoConfig.getSqlMapInstance();
065: // System.out.println("starting transaction");
066: sqlMap.startTransaction();
067: // System.out.println("OK");
068:
069: // write Exchange table
070: ExchangeVO exchangeVO = new ExchangeVO(me);
071: // System.out.println("write exchange table,status="+exchangeVO.getExchangeStatus());
072: sqlMap.insert("insertExchange", exchangeVO);
073: exchangeId = exchangeVO.getExchangeId();
074:
075: // write Error table
076: // System.out.println("write error table");
077: ErrorVO errorVO = new ErrorVO(e, exchangeId);
078: sqlMap.insert("insertError", errorVO);
079: log.info(e.toString());
080: //log.info(errorVO.getStackTrace());
081: // write ExchangeProperty table
082: for (Iterator i = me.getPropertyNames().iterator(); i
083: .hasNext();) {
084: String propertyName = (String) i.next();
085: // System.out.println("write property "+propertyName);
086: Object propertyValue = me.getProperty(propertyName);
087: // System.out.println("value="+propertyValue);
088: ExchangePropertyVO epvo = new ExchangePropertyVO(
089: exchangeId, propertyName, propertyValue);
090: sqlMap.insert("insertExchangeProperty", epvo);
091: // System.out.println("done");
092: }
093:
094: // write NormalizedMessage table
095: NormalizedMessage mess = null;
096: NormalizedMessageVO messVO = null;
097: final String[] messageTypes = { "in", "out", "fault" };
098: for (int i = 0; i < messageTypes.length; i++) {
099: // System.out.println("write "+messageTypes[i]+" message");
100: mess = me.getMessage(messageTypes[i]);
101: if (mess == null)
102: continue;
103:
104: messVO = new NormalizedMessageVO(exchangeId,
105: messageTypes[i], mess);
106: sqlMap.insert("insertNormalizedMessage", messVO);
107: // write MessageProperty table
108: for (Iterator it = mess.getPropertyNames().iterator(); it
109: .hasNext();) {
110: String propertyName = (String) it.next();
111: Object propertyValue = mess
112: .getProperty(propertyName);
113: MessagePropertyVO mpvo = new MessagePropertyVO(
114: exchangeId, messageTypes[i], propertyName,
115: propertyValue);
116: sqlMap.insert("insertMessageProperty", mpvo);
117: }
118:
119: // write Attachment table
120: for (Iterator it = mess.getAttachmentNames().iterator(); it
121: .hasNext();) {
122: String attachmentName = (String) it.next();
123: DataHandler dh = mess.getAttachment(attachmentName);
124: DataSource ds = dh.getDataSource();
125: String contentType = "unknown";
126: if (ds instanceof StringDataSource) {
127: contentType = "string";
128: } else if (ds instanceof ByteArrayDataSource) {
129: contentType = "byte";
130: }
131:
132: AttachmentVO avo = new AttachmentVO(exchangeId,
133: messageTypes[i], attachmentName,
134: contentType);
135: sqlMap.insert("insertAttachment", avo);
136:
137: if (ds instanceof StringDataSource) {
138: // write StringContent table
139: StringContentVO svo = new StringContentVO(
140: exchangeId, messageTypes[i],
141: attachmentName, dh);
142: sqlMap.insert("insertStringContent", svo);
143: } else if (ds instanceof ByteArrayDataSource) {
144: // write ByteContent table
145: ByteContentVO bvo = new ByteContentVO(
146: exchangeId, messageTypes[i],
147: attachmentName, dh);
148: sqlMap.insert("insertByteContent", bvo);
149: }
150: }
151: }
152:
153: // System.out.println("done writing error, committing\n\n\n");
154: sqlMap.commitTransaction();
155: // System.out.println("OK\n\n\n");
156:
157: } catch (SQLException se) {
158: logger.error("\n\nSQLException writing error database: "
159: + se.toString());
160: if (logger.isDebugEnabled()) {
161: logger.debug(
162: "\n\nSQLException writing error database:", se);
163: }
164: } finally {
165: System.out.println("end transaction sqlMap=" + sqlMap);
166: try {
167: if (sqlMap != null)
168: sqlMap.endTransaction();
169: } catch (Exception e2) {
170:
171: logger.error("error ending transaction: "
172: + e.getMessage());
173: if (logger.isDebugEnabled()) {
174: logger.debug("error ending transaction:", e);
175: }
176: }
177: //System.out.println("OK");
178: }
179: }
180: }
|