001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JBIResultXmlBuilder.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.common;
030:
031: import java.io.PrintWriter;
032: import java.io.StringWriter;
033: import java.util.ArrayList;
034: import java.util.List;
035:
036: /**
037: * This class is used to create a JBI Management message that contains the
038: * messages and exception under the framework tags to help the ui messages
039: * formatted in the standard message format as the any other result messages
040: * comming from the jbi runtime in the form of jbi manamgment message xml.
041: *
042: * @author Sun Microsystems, Inc.
043: */
044: public class JBIResultXmlBuilder {
045:
046: /**
047: * success result
048: */
049: public final static boolean SUCCESS_RESULT = true;
050:
051: /**
052: * failed result
053: */
054: public final static boolean FAILED_RESULT = false;
055:
056: /**
057: * info message type
058: */
059: public final static String INFO_MSG_TYPE = "INFO";
060:
061: /**
062: * warning message type
063: */
064: public final static String WARNING_MSG_TYPE = "WARNING";
065:
066: /**
067: * error message type
068: */
069: public final static String ERROR_MSG_TYPE = "ERROR";
070:
071: /**
072: * default task id
073: */
074: public final static String DEFAULT_TASK_ID = "UI_COMMON_TASK";
075:
076: /**
077: * default message code
078: */
079: public final static String DEFAULT_MSG_CODE = "UICMN0000";
080:
081: /**
082: * jbi mgmt xml prefix
083: */
084: private final static String JBI_MGMT_XML_BEGIN = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
085: + "<jbi-task version=\"1.0\" "
086: + "xmlns=\"http://java.sun.com/xml/ns/jbi/management-message\" "
087: + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" > "
088: + "<jbi-task-result>"
089: + "<frmwk-task-result>"
090: + "<frmwk-task-result-details>" + "<task-result-details>";
091:
092: /**
093: * jbi mgmt xml end
094: */
095: private final static String JBI_MGMT_XML_END = "</task-result-details>"
096: + "<locale></locale>"
097: + "</frmwk-task-result-details>"
098: + "<is-cause-framework>YES</is-cause-framework>"
099: + "</frmwk-task-result>"
100: + "</jbi-task-result>"
101: + "</jbi-task>";
102:
103: /**
104: * static instance of this class
105: */
106: private static JBIResultXmlBuilder sJbiResultXmlBuilder = null;
107:
108: /** Creates a new instance of EsbResultXmlBuilder */
109: protected JBIResultXmlBuilder() {
110: }
111:
112: /**
113: * returns the instance of this class
114: *
115: * @return instance of this class
116: */
117: public static JBIResultXmlBuilder getInstance() {
118: if (sJbiResultXmlBuilder == null) {
119: sJbiResultXmlBuilder = new JBIResultXmlBuilder();
120: }
121: return sJbiResultXmlBuilder;
122: }
123:
124: /**
125: * returns the exception cause chain in a list including this exception
126: *
127: * @param ex
128: * exception
129: * @return list contain the exception chain
130: */
131: protected List /* <Throwable> */flatExceptionChain(Throwable ex) {
132: List list = new ArrayList();
133: list.add(ex);
134: for (Throwable cause = ex.getCause(); cause != null; cause = cause
135: .getCause()) {
136: list.add(cause);
137: }
138: return list;
139: }
140:
141: /**
142: * return the xml chunk that has task info elements
143: *
144: * @param taskId
145: * task id
146: * @param successResult
147: * true for SUCCESS, false for FAILED
148: * @param msgType
149: * one of INFO, ERROR, WARNING strings
150: * @return the xml chunk
151: */
152: protected String getTaskInfoXml(String taskId,
153: boolean successResult, String msgType) {
154: String taskResult = (successResult) ? "SUCCESS" : "FAILED";
155:
156: return "<task-id>" + taskId + "</task-id>" + "<task-result>"
157: + taskResult + "</task-result>" + "<message-type>"
158: + msgType + "</message-type>";
159: }
160:
161: /**
162: * return the xml chunk contains the message info elements
163: *
164: * @param msgCode
165: * i18n message code
166: * @param msg
167: * l10n message
168: * @param args
169: * arguments for i18n message
170: * @return the xml chunk
171: */
172: protected String getMsgLocInfoXml(String msgCode, String msg,
173: Object[] args) {
174: StringBuffer buff = new StringBuffer();
175:
176: buff.append("<msg-loc-info>" + "<loc-token>" + msgCode
177: + "</loc-token>" + "<loc-message>"
178: + DOMUtil.replaceXmlEscapeCharsToEntityRefereces(msg)
179: + "</loc-message>");
180: if (args != null) {
181: for (int i = 0; i < args.length; ++i) {
182: buff.append("<loc-param>" + args[i] + "</loc-param>");
183: }
184: }
185: buff.append("</msg-loc-info>");
186:
187: return buff.toString();
188:
189: }
190:
191: /**
192: * return the xml chuck for status message elements
193: *
194: * @param msgCode
195: * i18n message code
196: * @param msg
197: * l10n message
198: * @param args
199: * arguments for i18n message
200: * @return the xml chunk
201: */
202: protected String getTaskStatusMsgXml(String msgCode, String msg,
203: Object[] args) {
204: return "<task-status-msg>"
205: + getMsgLocInfoXml(msgCode, msg, args)
206: + "</task-status-msg>";
207: }
208:
209: /**
210: * return the excpetion stacktrace as a string
211: *
212: * @param ex
213: * exception
214: * @return string contains the exception stacktrace
215: */
216: protected String getExceptionStackTrace(Throwable ex) {
217:
218: StringWriter buff = new StringWriter();
219: PrintWriter out = new PrintWriter(buff);
220: String stackTrace = "";
221:
222: StackTraceElement[] stEl = ex.getStackTrace();
223: if (stEl == null || stEl.length == 0) {
224: return stackTrace;
225: }
226: for (int i = 0; i < stEl.length; ++i) {
227: out.println(stEl[i].toString());
228: }
229: out.close();
230: stackTrace = buff.getBuffer().toString();
231:
232: return stackTrace;
233: }
234:
235: /**
236: * returns exceptin info xml chunk
237: *
238: * @param msgCode
239: * i18n message code
240: * @param nestingLevel
241: * level in the exception chain
242: * @param ex
243: * exception for stacktrace
244: * @return xml chunk
245: */
246: protected String getExceptionInfoXml(String msgCode,
247: int nestingLevel, Throwable ex) {
248: String stackTrace = getExceptionStackTrace(ex);
249:
250: return "<exception-info>"
251: + "<nesting-level>"
252: + nestingLevel
253: + "</nesting-level>"
254: + getMsgLocInfoXml(msgCode, ex.getMessage(), null)
255: + "<stack-trace>"
256: + DOMUtil
257: .replaceXmlEscapeCharsToEntityRefereces(stackTrace)
258: + "</stack-trace>" + "</exception-info>";
259: }
260:
261: /**
262: * returns exceptin info xml chunk
263: *
264: * @param msgCode
265: * i18n message code
266: * @param locMsg
267: * localized message
268: * @param nestingLevel
269: * level in the exception chain
270: * @param stackTrace
271: * stacktrace string
272: * @return xml chunk
273: */
274: protected String getExceptionInfoXml(String msgCode, String locMsg,
275: int nestingLevel, String stackTrace) {
276:
277: return "<exception-info>" + "<nesting-level>" + nestingLevel
278: + "</nesting-level>"
279: + getMsgLocInfoXml(msgCode, locMsg, null)
280: + "<stack-trace>" + stackTrace + "</stack-trace>"
281: + "</exception-info>";
282: }
283:
284: /**
285: * returns the xml chunk contains exception message info for the exception
286: * chain
287: *
288: * @param msgCode
289: * i18n code
290: * @param ex
291: * exception
292: * @return xml chunk
293: */
294: protected String getChainedExceptionInfoXml(String msgCode,
295: Throwable ex) {
296: StringBuffer buff = new StringBuffer();
297:
298: if (msgCode == null) {
299: msgCode = DEFAULT_MSG_CODE; // UNKNOWN MSG CODE
300: }
301: List list = this .flatExceptionChain(ex);
302: int listSize = list.size();
303: for (int i = 0; i < listSize; ++i) {
304: buff.append(getExceptionInfoXml(msgCode, i + 1,
305: (Throwable) list.get(i)));
306: }
307:
308: return buff.toString();
309: }
310:
311: /**
312: * creates the jbi management xml
313: *
314: * @param taskId
315: * task id
316: * @param successResult
317: * true for SUCCESS, false for FAILIED message
318: * @param msgType
319: * one of INFO, ERROR, WARNING
320: * @param msgCode
321: * i18n message code
322: * @param msg
323: * message
324: * @param args
325: * i18n message arguments
326: * @param ex
327: * exception
328: * @return jbi mgmt xml
329: */
330: public String createJbiResultXml(String taskId,
331: boolean successResult, String msgType, String msgCode,
332: String msg, Object[] args, Throwable ex) {
333: StringBuffer buff = new StringBuffer();
334:
335: buff.append(JBI_MGMT_XML_BEGIN);
336: buff.append(getTaskInfoXml(taskId, successResult, msgType));
337: buff.append(getTaskStatusMsgXml(msgCode, msg, args));
338: if (ex != null) {
339: String expMsgCode = DEFAULT_MSG_CODE;
340: if (msg == null) {
341: // if msg is null, assume msgCode passed is for expception
342: expMsgCode = msgCode;
343: }
344: buff.append(getChainedExceptionInfoXml(expMsgCode, ex));
345: }
346: buff.append(JBI_MGMT_XML_END);
347:
348: return buff.toString();
349: }
350:
351: /**
352: * creates the jbi management xml
353: *
354: * @param taskId
355: * task id
356: * @param successResult
357: * true for SUCCESS, false for FAILIED message
358: * @param msgType
359: * one of INFO, ERROR, WARNING
360: * @param msgCode
361: * i18n message code
362: * @param msg
363: * message
364: * @param args
365: * i18n message arguments
366: * @return jbi mgmt xml
367: */
368: public String createJbiResultXml(String taskId,
369: boolean successResult, String msgType, String msgCode,
370: String msg, Object[] args) {
371: return createJbiResultXml(taskId, successResult, msgType,
372: msgCode, msg, args, null);
373: }
374:
375: /**
376: * creates the jbi management xml
377: *
378: * @param taskId
379: * task id
380: * @param msgCode
381: * i18n message code
382: * @param ex
383: * exception
384: * @return jbi mgmt xml
385: */
386: public String createJbiResultXml(String taskId, String msgCode,
387: Throwable ex) {
388: return createJbiResultXml(taskId, false, "ERROR", msgCode,
389: null, null, ex);
390: }
391:
392: /**
393: * creates the jbi management xml
394: *
395: * @param i18nBundle
396: * i18n bundle object
397: * @param i18nKey
398: * key to look for i18n msg
399: * @param args
400: * i18n args
401: * @param taskId
402: * task id
403: * @param successResult
404: * true or false
405: * @param msgType
406: * message type
407: * @param ex
408: * exception
409: * @return the jbi management xml
410: */
411: public static String createJbiResultXml(I18NBundle i18nBundle,
412: String i18nKey, Object[] args, String taskId,
413: boolean successResult, String msgType, Throwable ex) {
414:
415: String msgCode = i18nBundle.getMessage(i18nKey + ".ID");
416: String msg = i18nBundle.getMessage(i18nKey, args);
417:
418: String jbiResultXml = JBIResultXmlBuilder.getInstance()
419: .createJbiResultXml(taskId, successResult, msgType,
420: msgCode, msg, args, ex);
421:
422: if (jbiResultXml == null) {
423: return msg;
424: } else {
425: return jbiResultXml;
426: }
427:
428: }
429:
430: /**
431: * creates the jbi management xml
432: *
433: * @param i18nBundle
434: * i18n bundle object
435: * @param i18nKey
436: * i18n key in the bundle
437: * @param args
438: * i18n args
439: * @param ex
440: * exception
441: * @return jbi mgmt xml
442: */
443: public static String createJbiResultXml(I18NBundle i18nBundle,
444: String i18nKey, Object[] args, Throwable ex) {
445: return createJbiResultXml(i18nBundle, i18nKey, args,
446: "JBI_UI_COMMON_TASKS",
447: JBIResultXmlBuilder.FAILED_RESULT,
448: JBIResultXmlBuilder.ERROR_MSG_TYPE, ex);
449: }
450:
451: /**
452: * Creates a XML JBI Management message from the JBI Management Message
453: *
454: * @return the jbi management xml
455: * @param msg - JBIManagementMessage instance to create the xml message from
456: */
457: public String createJbiResultXml(JBIManagementMessage msg) {
458: String resultXml = null;
459:
460: if (msg != null) {
461: JBIManagementMessage.FrameworkTaskResult fwkRslt = msg
462: .getFrameworkTaskResult();
463: JBIManagementMessage.TaskResultInfo tskRsltInfo = fwkRslt
464: .getTaskResultInfo();
465:
466: // Header
467: StringBuffer jbiXmlBuf = new StringBuffer(
468: JBI_MGMT_XML_BEGIN);
469:
470: // TaskResult
471: jbiXmlBuf.append(getTaskInfoXml(tskRsltInfo.getTaskId(),
472: msg.isSuccessMsg(), tskRsltInfo.getMessageType()));
473:
474: // TaskStatusMsgs
475: List<JBIManagementMessage.MessageInfo> msgInfos = tskRsltInfo
476: .getStatusMessageInfoList();
477: for (JBIManagementMessage.MessageInfo msgInfo : msgInfos) {
478: Object[] params = new Object[msgInfo.getl10nMsgParams()
479: .size()];
480: params = msgInfo.getl10nMsgParams().toArray(params);
481: jbiXmlBuf
482: .append(getTaskStatusMsgXml(
483: msgInfo.getI18nId(), msgInfo
484: .getLocalizedMsg(), params));
485: }
486: // ExceptionInfos
487:
488: List<JBIManagementMessage.ExceptionInfo> exInfos = tskRsltInfo
489: .getExceptionInfoList();
490: for (JBIManagementMessage.ExceptionInfo exInfo : exInfos) {
491: jbiXmlBuf.append(getExceptionInfoXml(exInfo
492: .getMessageInfo().getI18nId(), exInfo
493: .getMessageInfo().getLocalizedMsg(), exInfo
494: .getLevel(), exInfo.getFormattedStackTrace()));
495: }
496: // Trailer
497: jbiXmlBuf.append(JBI_MGMT_XML_END);
498:
499: resultXml = jbiXmlBuf.toString();
500: }
501: return resultXml;
502: }
503:
504: /**
505: * creates the jbi management xml
506: *
507: * @param i18nBundle
508: * i18n bundle object
509: * @param i18nKey
510: * i18n key in the bundle
511: * @param args
512: * i18n args
513: * @return jbi mgmt xml
514: */
515: public static String createFailedJbiResultXml(
516: I18NBundle i18nBundle, String i18nKey, Object[] args) {
517: return createJbiResultXml(i18nBundle, i18nKey, args,
518: "JBI_UI_COMMON_TASKS",
519: JBIResultXmlBuilder.FAILED_RESULT,
520: JBIResultXmlBuilder.ERROR_MSG_TYPE, null);
521: }
522:
523: }
|