0001: /*
0002: * BEGIN_HEADER - DO NOT EDIT
0003: *
0004: * The contents of this file are subject to the terms
0005: * of the Common Development and Distribution License
0006: * (the "License"). You may not use this file except
0007: * in compliance with the License.
0008: *
0009: * You can obtain a copy of the license at
0010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0011: * See the License for the specific language governing
0012: * permissions and limitations under the License.
0013: *
0014: * When distributing Covered Code, include this CDDL
0015: * HEADER in each file and include the License file at
0016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0017: * If applicable add the following below this CDDL HEADER,
0018: * with the fields enclosed by brackets "[]" replaced with
0019: * your own identifying information: Portions Copyright
0020: * [year] [name of copyright owner]
0021: */
0022:
0023: /*
0024: * @(#)ESBResultFormatter.java
0025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
0026: *
0027: * END_HEADER - DO NOT EDIT
0028: */
0029: package com.sun.jbi.ui.common;
0030:
0031: import com.sun.jbi.ui.common.JBIManagementMessage.ComponentTaskResult;
0032: import com.sun.jbi.ui.common.JBIManagementMessage.ESBTaskResult;
0033: import com.sun.jbi.ui.common.JBIManagementMessage.ExceptionInfo;
0034: import com.sun.jbi.ui.common.JBIManagementMessage.FrameworkTaskResult;
0035: import com.sun.jbi.ui.common.JBIManagementMessage.InstanceTaskResult;
0036: import com.sun.jbi.ui.common.JBIManagementMessage.JBITaskResult;
0037: import com.sun.jbi.ui.common.JBIManagementMessage.MessageInfo;
0038: import com.sun.jbi.ui.common.JBIManagementMessage.TaskResultInfo;
0039: import java.io.PrintWriter;
0040: import java.io.StringWriter;
0041: import java.util.ArrayList;
0042: import java.util.Collections;
0043: import java.util.Comparator;
0044: import java.util.HashMap;
0045: import java.util.HashSet;
0046: import java.util.Iterator;
0047: import java.util.List;
0048: import java.util.Map;
0049: import java.util.Set;
0050:
0051: /**
0052: * this class creates formatted text from the esb admin result.
0053: * @author Sun Microsystems, Inc.
0054: */
0055: public class ESBResultFormatter {
0056: /**
0057: * same as cas msg format
0058: */
0059: public static final int TOP_MSG_FORMAT = 0;
0060:
0061: /**
0062: * cas msg format
0063: */
0064: public static final int CAS_MSG_FORMAT = 1;
0065: /**
0066: * instance msg format
0067: */
0068: public static final int INST_MSG_FORMAT = 2;
0069: /**
0070: * component msg format
0071: */
0072: public static final int COMP_MSG_FORMAT = 3;
0073:
0074: /**
0075: * failed status
0076: */
0077: public static final String FAILED = "FAILED";
0078: /**
0079: * success status
0080: */
0081: public static final String SUCCESS = "SUCCESS";
0082: /**
0083: * info message type
0084: */
0085: public static final String INFO = "INFO";
0086: /**
0087: * warning message type
0088: */
0089: public static final String WARNING = "WARNING";
0090: /**
0091: * error message type
0092: */
0093: public static final String ERROR = "ERROR";
0094: /**
0095: * error message type
0096: */
0097: public static final String EXCEPTION = "EXCEPTION";
0098: /**
0099: * unknown value for any unknonw value of any msg status or type.
0100: */
0101: public static final String UNKNOWN = "UNKNOWN";
0102:
0103: /**
0104: * instance name marker in the msg/exp list
0105: */
0106: public static final String INSTANCE_MARKER_ID = "JBI_INSTANCE_NAME";
0107:
0108: /**
0109: * application varaible name marker in the msg/exp list
0110: */
0111: public static final String APP_VAR_MARKER_ID = "APPLICATION_VARIABLE_NAME";
0112:
0113: /**
0114: * CAS msg/exp list key in the instance Maps
0115: */
0116: public static final String CAS_KEY = "";
0117:
0118: public static final String OUT_NORMAL = "normal";
0119: public static final String OUT_VERBOSE = "terse";
0120: public static final String OUT_DEBUG = "verbose";
0121:
0122: /**
0123: * print level
0124: */
0125: private String mOutLevel = OUT_VERBOSE;
0126: /**
0127: * resource bundle
0128: */
0129: private static I18NBundle sI18NBundle = null;
0130:
0131: public ESBResultFormatter() {
0132: this .mOutLevel = OUT_VERBOSE;
0133: }
0134:
0135: /** Creates a new instance of AdminResultFormatter */
0136: public ESBResultFormatter(String outLevel) {
0137: this .mOutLevel = outLevel;
0138: }
0139:
0140: /** gives the I18N bundle
0141: *@return I18NBundle object
0142: */
0143: public static I18NBundle getI18NBundle() {
0144: // lazzy initialize the JBI Client
0145: if (sI18NBundle == null) {
0146: sI18NBundle = new I18NBundle("com.sun.jbi.ui.common");
0147: }
0148: return sI18NBundle;
0149: }
0150:
0151: /**
0152: * creates l10n message
0153: * @param code message code
0154: * @param msg message
0155: * @return formatted message
0156: */
0157: protected String getL10NMessage(String code, String msg) {
0158: // TODO: here is the chance to load and format the message using code from the
0159: // client's resource bundle for displaying the msg in client locale.
0160: // For now, we stick with l10ned msg passed from the server.
0161: // Assumption is that the client and server is in the same locale
0162: return getI18NBundle().getMessage("esb.result.msg.format",
0163: code, msg);
0164: }
0165:
0166: /**
0167: * creates l10n message
0168: * @param appVarName
0169: * @return formatted message
0170: */
0171: protected String getL10NMessageForAppVar(String appVarName) {
0172: return getI18NBundle().getMessage(
0173: "esb.result.app.variable.msg.format", appVarName);
0174: }
0175:
0176: /**
0177: * creates l10n message in exceptin format
0178: * @param code message code
0179: * @param msg message
0180: * @return formatted message
0181: */
0182: protected String getL10NExptionMessage(String code, String msg) {
0183: String formattedMsg = getL10NMessage(code, msg);
0184: return getI18NBundle().getMessage("esb.result.ex.msg.format",
0185: formattedMsg);
0186: }
0187:
0188: /**
0189: * creates l10n message in exceptin format
0190: * @param appVarName
0191: * @return formatted message
0192: */
0193: protected String getL10NExptionMessageForAppVar(String appVarName) {
0194: return getI18NBundle().getMessage(
0195: "esb.result.app.variable.ex.msg.format", appVarName);
0196: }
0197:
0198: /**
0199: * creates l10n message in exceptin format
0200: * @param appVarName
0201: * @return formatted message
0202: */
0203: protected String getL10N2ndExptionMessageForAppVar(String appVarName) {
0204: return getI18NBundle()
0205: .getMessage(
0206: "esb.result.app.variable.2nd.ex.msg.format",
0207: appVarName);
0208: }
0209:
0210: /**
0211: * message type from i18n bundle
0212: * @param msgType message type
0213: * @return message type
0214: */
0215: protected String getL10NMessageType(String msgType) {
0216: String l10nMsgType = null;
0217: if (ERROR.equalsIgnoreCase(msgType)) {
0218: l10nMsgType = getI18NBundle().getMessage(
0219: "esb.result.type.error");
0220: } else if (WARNING.equalsIgnoreCase(msgType)) {
0221: l10nMsgType = getI18NBundle().getMessage(
0222: "esb.result.type.warning");
0223: } else if (INFO.equalsIgnoreCase(msgType)) {
0224: l10nMsgType = getI18NBundle().getMessage(
0225: "esb.result.type.info");
0226: } else {
0227: l10nMsgType = getI18NBundle().getMessage(
0228: "esb.result.type.unknown");
0229: }
0230: return l10nMsgType;
0231: }
0232:
0233: /**
0234: * formats the message
0235: * @param msgType message type
0236: * @param code message code
0237: * @param msg message
0238: * @return formatted message
0239: */
0240: protected String getL10NMainMessage(String msgType, String code,
0241: String msg) {
0242: String formattedMsg = getL10NMessage(code, msg);
0243: String l10nType = getL10NMessageType(msgType);
0244: return getI18NBundle().getMessage("esb.result.1st.msg.format",
0245: l10nType, formattedMsg);
0246: }
0247:
0248: /**
0249: * formats the message
0250: * @param msgType message type
0251: * @param appVarName message
0252: * @return formatted message
0253: */
0254: protected String getL10NMainMessageForAppVar(String msgType,
0255: String appVarName) {
0256: String l10nType = getL10NMessageType(msgType);
0257: return getI18NBundle().getMessage(
0258: "esb.result.1st.app.variable.msg.format", l10nType,
0259: appVarName);
0260: }
0261:
0262: /**
0263: * message format type i18n key
0264: * @param formatType format type
0265: * @return i18n key
0266: */
0267: protected String getMainMessageI18NKey(int formatType) {
0268: switch (formatType) {
0269: case CAS_MSG_FORMAT:
0270: return "esb.result.cas.msg";
0271: case INST_MSG_FORMAT:
0272: return "esb.result.inst.msg";
0273: case COMP_MSG_FORMAT:
0274: return "esb.result.comp.msg";
0275: default:
0276: return "esb.result.cas.msg";
0277: }
0278: }
0279:
0280: /**
0281: * message format type i18n key
0282: * @param formatType format type
0283: * @return i18n key
0284: */
0285: protected String get2ndMessageI18NKey(int formatType) {
0286: switch (formatType) {
0287: case CAS_MSG_FORMAT:
0288: return "esb.result.cas.2nd.msg";
0289: case INST_MSG_FORMAT:
0290: return "esb.result.inst.2nd.msg";
0291: case COMP_MSG_FORMAT:
0292: return "esb.result.comp.2nd.msg";
0293: default:
0294: return "esb.result.cas.2nd.msg";
0295: }
0296: }
0297:
0298: /**
0299: * message format type i18n key
0300: * @param formatType format type
0301: * @return i18n key
0302: */
0303: protected String getExceptionMsgI18NKey(int formatType) {
0304: switch (formatType) {
0305: case CAS_MSG_FORMAT:
0306: return "esb.result.cas.ex.msg";
0307: case INST_MSG_FORMAT:
0308: return "esb.result.inst.ex.msg";
0309: case COMP_MSG_FORMAT:
0310: return "esb.result.comp.ex.msg";
0311: default:
0312: return "esb.result.cas.ex.msg";
0313: }
0314: }
0315:
0316: /**
0317: * message format type i18n key
0318: * @param formatType format type
0319: * @return i18n key
0320: */
0321: protected String get2ndExceptionMsgI18NKey(int formatType) {
0322: switch (formatType) {
0323: case CAS_MSG_FORMAT:
0324: return "esb.result.cas.2nd.ex.msg";
0325: case INST_MSG_FORMAT:
0326: return "esb.result.inst.2nd.ex.msg";
0327: case COMP_MSG_FORMAT:
0328: return "esb.result.comp.2nd.ex.msg";
0329: default:
0330: return "esb.result.cas.2nd.ex.msg";
0331: }
0332: }
0333:
0334: /**
0335: * prints message to the output
0336: * @param out print writer to print the message
0337: * @param formatType format type
0338: * @param msgType message type
0339: * @param msgCode message code
0340: * @param msg message
0341: */
0342: protected void printMainMessage(PrintWriter out, int formatType,
0343: String msgType, String msgCode, String msg) {
0344: String fmtMsg = null;
0345: if ((msgCode != null)
0346: && (msgCode.compareTo(APP_VAR_MARKER_ID) == 0)) {
0347: fmtMsg = this .getL10NMainMessageForAppVar(msgType, msg);
0348: } else {
0349: fmtMsg = this .getL10NMainMessage(msgType, msgCode, msg);
0350: }
0351: out.print(getI18NBundle().getMessage(
0352: this .getMainMessageI18NKey(formatType), fmtMsg));
0353: }
0354:
0355: /**
0356: * prints message to the output
0357: * @param out print writer to print the message
0358: * @param formatType format type
0359: * @param msgCode message code
0360: * @param msg message
0361: */
0362: protected void print2ndMessage(PrintWriter out, int formatType,
0363: String msgCode, String msg) {
0364: String fmtMsg = null;
0365: if ((msgCode != null)
0366: && (msgCode.compareTo(APP_VAR_MARKER_ID) == 0)) {
0367: fmtMsg = this .getL10NMessageForAppVar(msg);
0368: } else {
0369: fmtMsg = this .getL10NMessage(msgCode, msg);
0370: }
0371: out.print(getI18NBundle().getMessage(
0372: this .get2ndMessageI18NKey(formatType), fmtMsg));
0373: }
0374:
0375: /**
0376: * prints message to the output
0377: * @param out print writer to print the message
0378: * @param formatType format type
0379: * @param msgCode message code
0380: * @param msg message
0381: */
0382: protected void printExceptionMessage(PrintWriter out,
0383: int formatType, String msgCode, String msg) {
0384: String fmtMsg = null;
0385: if ((msgCode != null)
0386: && (msgCode.compareTo(APP_VAR_MARKER_ID) == 0)) {
0387: fmtMsg = this .getL10NExptionMessageForAppVar(msg);
0388: } else {
0389: fmtMsg = this .getL10NExptionMessage(msgCode, msg);
0390: }
0391: out.print(getI18NBundle().getMessage(
0392: this .getExceptionMsgI18NKey(formatType), fmtMsg));
0393: }
0394:
0395: /**
0396: * prints message to the output
0397: * @param out print writer to print the message
0398: * @param formatType format type
0399: * @param msgCode message code
0400: * @param msg message
0401: */
0402: protected void print2ndExceptionMessage(PrintWriter out,
0403: int formatType, String msgCode, String msg) {
0404: String fmtMsg = null;
0405: if ((msgCode != null)
0406: && (msgCode.compareTo(APP_VAR_MARKER_ID) == 0)) {
0407: fmtMsg = this .getL10N2ndExptionMessageForAppVar(msg);
0408: } else {
0409: fmtMsg = this .getL10NMessage(msgCode, msg);
0410: }
0411: out.print(getI18NBundle().getMessage(
0412: this .get2ndExceptionMsgI18NKey(formatType), fmtMsg));
0413: }
0414:
0415: /**
0416: * prints the messages from message list
0417: * @return true if any message is printed, false if no msg is printed.
0418: * @param out print writer to print the message
0419: * @param formatType format type
0420: * @param msgType message type
0421: * @param msgList list of TaskStatusMsgType objects
0422: */
0423: protected boolean printFormattedStatusMessages(PrintWriter out,
0424: int formatType, String msgType,
0425: List/*<MessageInfo>*/msgList) {
0426: boolean msgPrinted = false;
0427:
0428: if (msgList == null || msgList.size() == 0) {
0429: // do nothing
0430: return msgPrinted;
0431: }
0432: // print messages from msgList
0433: for (int msgIdx = 0; msgIdx < msgList.size(); ++msgIdx) {
0434: MessageInfo msgInfo = (MessageInfo) msgList.get(msgIdx);
0435: String msgCode = msgInfo.getI18nId();
0436: String msg = msgInfo.getLocalizedMsg();
0437:
0438: if (msgIdx == 0) {
0439: printMainMessage(out, formatType, msgType, msgCode, msg);
0440: } else {
0441: out.println();
0442: print2ndMessage(out, formatType, msgCode, msg);
0443: }
0444: msgPrinted = true;
0445: }
0446:
0447: return msgPrinted;
0448: }
0449:
0450: /**
0451: * prints the messages from message list
0452: * @return true if any message is printed, false if no msg is printed.
0453: * @param out print writer to print the message
0454: * @param formatType format type
0455: * @param msgType message type
0456: * @param expList list of ExceptionInfoType objects
0457: * @param printMainMsg true prints the first message as main message format
0458: */
0459: protected boolean printFormattedExceptionMessages(PrintWriter out,
0460: int formatType, String msgType,
0461: List/*<ExceptionInfo>*/expList, boolean printMainMsg) {
0462: boolean msgPrinted = false;
0463: boolean mainMsgPrinted = false;
0464:
0465: if (expList == null || expList.size() == 0) {
0466: // do nothing
0467: return msgPrinted;
0468: }
0469:
0470: // print messages from msgList
0471: for (int expIdx = 0; expIdx < expList.size(); ++expIdx) {
0472: ExceptionInfo expInfo = (ExceptionInfo) expList.get(expIdx);
0473: String msgCode = expInfo.getMessageInfo().getI18nId();
0474: String msg = expInfo.getMessageInfo().getLocalizedMsg();
0475:
0476: if (expIdx == 0) {
0477: // if asked for printing main message, format the first msg as main message
0478: // else format the first msg as exception msg
0479: if (printMainMsg) {
0480: // print as main message
0481: printMainMessage(out, formatType, msgType, msgCode,
0482: msg);
0483: mainMsgPrinted = true;
0484: } else {
0485: out.println();
0486: printExceptionMessage(out, formatType, msgCode, msg);
0487: }
0488: } else if (mainMsgPrinted && expIdx == 1) {
0489: // if main msg is printed print the second msg as exception message
0490: // and print the subsequent exception messages ( 2,3,...) as secondary messages
0491: out.println();
0492: printExceptionMessage(out, formatType, msgCode, msg);
0493: } else {
0494: // print second excpetion message
0495: out.println();
0496: print2ndExceptionMessage(out, formatType, msgCode, msg);
0497: }
0498: msgPrinted = true;
0499: }
0500:
0501: return msgPrinted;
0502: }
0503:
0504: /**
0505: * prints the messages from message list
0506: * @return true if any message is printed, false if no msg is printed.
0507: * @param out print writer to print the message
0508: * @param formatType format type
0509: * @param msgType message type
0510: * @param msgList list of TaskStatusMsgType objects
0511: * @param expList list of ExceptionInfoType objects
0512: * @param startWithNewLine true prints the first message in new line
0513: */
0514: protected boolean printAllFormattedMessages(PrintWriter out,
0515: int formatType, String msgType, List msgList, List expList,
0516: boolean startWithNewLine) {
0517: boolean msgPrinted = false;
0518: boolean statusMsgPrinted = false;
0519: boolean expMsgPrinted = false;
0520:
0521: if ((msgList == null || msgList.size() == 0)
0522: && (expList == null || expList.size() == 0)) {
0523: // do nothing
0524: return msgPrinted;
0525: }
0526:
0527: if (startWithNewLine) {
0528: out.println();
0529: }
0530: statusMsgPrinted = this .printFormattedStatusMessages(out,
0531: formatType, msgType, msgList);
0532: boolean printMainMsg = false;
0533: if (!statusMsgPrinted) {
0534: printMainMsg = true;
0535: }
0536: // if not mainMsgPrinted print first exception message as main message
0537: expMsgPrinted = this .printFormattedExceptionMessages(out,
0538: formatType, msgType, expList, printMainMsg);
0539:
0540: msgPrinted = (statusMsgPrinted || expMsgPrinted);
0541: return msgPrinted;
0542: }
0543:
0544: /**
0545: * prints the messages
0546: * @return true if any message is printed, false if no msg is printed.
0547: * @param out print writer to print the message
0548: * @param formatType format type
0549: * @param taskResultDetailsType TaskResultDetailsType object
0550: */
0551: public boolean printTaskResultMessages(PrintWriter out,
0552: int formatType, TaskResultInfo taskResultInfo) {
0553: boolean printed = false;
0554: boolean startWithNewLine = false;
0555: String msgType = taskResultInfo.getMessageType();
0556: List msgList = taskResultInfo.getStatusMessageInfoList();
0557: List expList = taskResultInfo.getExceptionInfoList();
0558:
0559: if ((msgList != null && msgList.size() > 0)
0560: || (expList != null && expList.size() > 0)) {
0561: printed = printAllFormattedMessages(out, formatType,
0562: msgType, msgList, expList, startWithNewLine);
0563: }
0564:
0565: return printed;
0566: }
0567:
0568: /**
0569: * prints the messages
0570: * @return true if any message is printed, false if no msg is printed.
0571: * @param taskResultDetailsType TaskResultDetailsType object
0572: * @param out print writer to print the message
0573: */
0574: public boolean printTaskResultMessages(PrintWriter out,
0575: TaskResultInfo taskResultInfo) {
0576: return printTaskResultMessages(out, this .TOP_MSG_FORMAT,
0577: taskResultInfo);
0578: }
0579:
0580: /**
0581: * prints the messages
0582: * @return true if any message is printed, false if no msg is printed.
0583: * @param fwTaskResultType frmwkTaskResultType object
0584: * @param out print writer to print the message
0585: */
0586: public boolean printFrameworkLevelMessages(PrintWriter out,
0587: FrameworkTaskResult fwTaskResult) {
0588: TaskResultInfo taskResultInfo = fwTaskResult
0589: .getTaskResultInfo();
0590: return printTaskResultMessages(out, this .INST_MSG_FORMAT,
0591: taskResultInfo);
0592: }
0593:
0594: /**
0595: * prints the messages
0596: * @return true if any message is printed, false if no msg is printed.
0597: * @param compTaskResultType ComponentTaskResultType object
0598: * @param out print writer to print the message
0599: */
0600: public boolean printComponentLevelMessages(PrintWriter out,
0601: ComponentTaskResult compTaskResult) {
0602: TaskResultInfo taskResultInfo = compTaskResult
0603: .getTaskResultInfo();
0604: return printTaskResultMessages(out, this .COMP_MSG_FORMAT,
0605: taskResultInfo);
0606: }
0607:
0608: /**
0609: * sort the list
0610: * @param compTaskResultList list.
0611: */
0612: public void sortComponentTaskResultList(
0613: List /*<ComponentTaskResult>*/compTaskResultList) {
0614: try {
0615: Collections.sort(compTaskResultList, new Comparator() {
0616: public int compare(Object o1, Object o2) {
0617: return ((ComponentTaskResult) o1)
0618: .getComponentName().compareTo(
0619: ((ComponentTaskResult) o2)
0620: .getComponentName());
0621: }
0622: });
0623: } catch (ClassCastException ccEx) {
0624: // log and // do nothing.
0625: } catch (UnsupportedOperationException unsupEx) {
0626: // log and // do nothing.
0627: } catch (Exception ex) {
0628: // log and // do nothing
0629: }
0630: }
0631:
0632: /**
0633: * prints the messages
0634: * @return true if any message is printed, false if no msg is printed.
0635: * @param jbiTaskResultType JbiTaskResultType object
0636: * @param out print writer to print the message
0637: */
0638: public boolean printJbiTaskResultMessages(PrintWriter out,
0639: JBITaskResult jbiTaskResult) {
0640: boolean msgPrinted = false;
0641:
0642: boolean fwMsgPrinted = false;
0643: boolean firstCompMsgPrinted = false;
0644:
0645: fwMsgPrinted = printFrameworkLevelMessages(out, jbiTaskResult
0646: .getFrameworkTaskResult());
0647:
0648: // print com msgs into comp writer
0649: List compList = jbiTaskResult.getComponentTaskResultList();
0650: sortComponentTaskResultList(compList);
0651: if (compList != null && compList.size() > 0) {
0652: for (int compIdx = 0; compIdx < compList.size(); ++compIdx) {
0653: boolean compMsgPrinted = false;
0654: StringWriter compBuff = new StringWriter();
0655: PrintWriter compOut = new PrintWriter(compBuff);
0656: ComponentTaskResult compTaskResult = (ComponentTaskResult) compList
0657: .get(compIdx);
0658: compMsgPrinted = printComponentLevelMessages(compOut,
0659: compTaskResult);
0660: compOut.close();
0661: if (!compMsgPrinted) {
0662: continue;
0663: }
0664: // check if new line need to be printed
0665: if (firstCompMsgPrinted || fwMsgPrinted) {
0666: out.println(); // printing a second message to the out
0667: }
0668: // comp msgs available.
0669: // print comp header ( comp name )
0670: String compName = compTaskResult.getComponentName();
0671: out.println(getI18NBundle().getMessage(
0672: "esb.result.comp.msg.from.comp", compName));
0673: // print comp msgs to out
0674: out.print(compBuff.getBuffer().toString());
0675: firstCompMsgPrinted = true;
0676: }
0677: }
0678:
0679: msgPrinted = (fwMsgPrinted || firstCompMsgPrinted);
0680: return msgPrinted;
0681: }
0682:
0683: /**
0684: * prints the messages
0685: * @return true if any message is printed, false if no msg is printed.
0686: * @param instResultDetailType InstanceResultDetailType object
0687: * @param printInstanceName true prints the instance name, false prints no instance name.
0688: * @param out print writer to print the message
0689: */
0690: public boolean printInstanceLevelMessages(PrintWriter out,
0691: InstanceTaskResult instResult, boolean printInstanceName) {
0692: boolean instMsgPrinted = false;
0693: StringWriter instBuff = new StringWriter();
0694: PrintWriter instOut = new PrintWriter(instBuff);
0695:
0696: String instanceName = instResult.getInstanceName();
0697: JBITaskResult jbiTaskResult = instResult.getJBITaskResult();
0698: instMsgPrinted = printJbiTaskResultMessages(instOut,
0699: jbiTaskResult);
0700: instOut.close();
0701:
0702: if (instMsgPrinted) {
0703: if (printInstanceName) {
0704: // print isntance name to out
0705: out.println(getI18NBundle().getMessage(
0706: "esb.result.inst.msg.from.inst", instanceName));
0707: }
0708: // print instance msg to out
0709: out.print(instBuff.getBuffer().toString());
0710: }
0711: return instMsgPrinted;
0712: }
0713:
0714: /**
0715: * prints the messages
0716: * @return true if any message is printed, false if no msg is printed.
0717: * @param instResultDetailType InstanceResultDetailType object
0718: * @param out print writer to print the message
0719: */
0720: public boolean printInstanceLevelMessages(PrintWriter out,
0721: InstanceTaskResult instResult) {
0722: boolean printInstanceName = true;
0723: return printInstanceLevelMessages(out, instResult,
0724: printInstanceName);
0725: }
0726:
0727: /**
0728: * sort the list
0729: * @param instResultList list.
0730: */
0731: public void sortInstanceResultList(
0732: List /*<ESBInstanceTaskResult>*/instResultList) {
0733: try {
0734: Collections.sort(instResultList, new Comparator() {
0735: public int compare(Object o1, Object o2) {
0736: return ((InstanceTaskResult) o1).getInstanceName()
0737: .compareTo(
0738: ((InstanceTaskResult) o2)
0739: .getInstanceName());
0740: }
0741: });
0742: } catch (ClassCastException ccEx) {
0743: // log and // do nothing.
0744: } catch (UnsupportedOperationException unsupEx) {
0745: // log and // do nothing.
0746: } catch (Exception ex) {
0747: // log and // do nothing
0748: }
0749: }
0750:
0751: /**
0752: * prints the messages
0753: * @return true if any message is printed, false if no msg is printed.
0754: * @param esbResultType EsbTaskResultType object
0755: * @param out print writer to print the message
0756: */
0757: public boolean printESBTaskResult(PrintWriter out,
0758: ESBTaskResult esbResult) {
0759: boolean msgPrinted = false;
0760:
0761: boolean casMsgPrinted = false;
0762: casMsgPrinted = printJbiTaskResultMessages(out, esbResult
0763: .getCASTaskResult());
0764:
0765: boolean firstInstMsgPrinted = false;
0766:
0767: // print Instance level messages
0768: List instList = esbResult.getInstanceTaskResultList();
0769: sortInstanceResultList(instList);
0770: if (instList != null && instList.size() > 0) {
0771: // no instance messages header as the instance name separates the per instance messages
0772: for (int instIdx = 0; instIdx < instList.size(); ++instIdx) {
0773: boolean instMsgPrinted = false;
0774: StringWriter instBuff = new StringWriter();
0775: PrintWriter instOut = new PrintWriter(instBuff);
0776: InstanceTaskResult instResult = (InstanceTaskResult) instList
0777: .get(instIdx);
0778: boolean printInstaceName = true;
0779: instMsgPrinted = printInstanceLevelMessages(instOut,
0780: instResult, printInstaceName);
0781: instOut.close();
0782: if (!instMsgPrinted) {
0783: continue;
0784: }
0785: // check if new line need to be printed
0786: if (firstInstMsgPrinted || casMsgPrinted) {
0787: out.println(); // printing a second message to the out
0788: }
0789: // inst msg available here.
0790: // print inst msgs to out
0791: out.print(instBuff.getBuffer().toString());
0792: firstInstMsgPrinted = true;
0793: }
0794: }
0795:
0796: msgPrinted = (casMsgPrinted || firstInstMsgPrinted);
0797: return msgPrinted;
0798: }
0799:
0800: /**
0801: * This method iterates through the TaskResultInfo and sepearates the messages into per instance msgs and expt in
0802: * a map {<instanceName>, <List<MessageInfo>>} or {<instanceName>, <List<ExceptionInfo>>}, if the messages are
0803: * not started with the instance marker message, they will be added into the value against empty key in the Map
0804: * which represent CAS level messages.
0805: * @return a Map <instancename, msg/expInfo List >
0806: */
0807: private Map getInstanceMessagesMap(TaskResultInfo taskResult,
0808: boolean isExpInfo) {
0809:
0810: List infoList = null;
0811:
0812: Map instMap = new HashMap();
0813:
0814: List instInfoList = null;
0815: String instanceName = CAS_KEY; // must initialize
0816:
0817: infoList = (isExpInfo) ? taskResult.getExceptionInfoList()
0818: : taskResult.getStatusMessageInfoList();
0819:
0820: if (infoList == null || infoList.isEmpty()) {
0821: // xml with no msg/expt info, but just has status. they go under CAS
0822: instInfoList = new ArrayList();
0823: instMap.put(CAS_KEY, instInfoList);
0824: return instMap;
0825: }
0826:
0827: for (int i = 0; i < infoList.size(); ++i) {
0828:
0829: Object info = infoList.get(i);
0830: MessageInfo msgInfo = (isExpInfo) ? ((ExceptionInfo) info)
0831: .getMessageInfo() : (MessageInfo) info;
0832:
0833: String marker = msgInfo.getI18nId();
0834:
0835: if (INSTANCE_MARKER_ID.equals(marker)) {
0836: instanceName = msgInfo.getLocalizedMsg();
0837: instInfoList = (List) instMap.get(instanceName);
0838: // skip the marker msg info
0839: continue;
0840: }
0841:
0842: if (instInfoList == null) {
0843: instInfoList = new ArrayList();
0844: instMap.put(instanceName, instInfoList);
0845: }
0846: instInfoList.add(info);
0847: }
0848:
0849: return instMap;
0850: }
0851:
0852: /**
0853: * copy the id, resutl, msgtype from the taskresultinfo passed and create a new TaskResultInfo with them
0854: * and the passed msg, exp lists. This will be used in re-creating the taskresultinfo per instance from
0855: * the main jbi mgmt msg task result info at framework level and component level
0856: */
0857: public TaskResultInfo createTaskResultInfo(
0858: TaskResultInfo taskResultInfo, List msgList, List expList) {
0859:
0860: List mList = msgList;
0861: List eList = expList;
0862:
0863: if (mList == null) {
0864: mList = new ArrayList();
0865: }
0866: if (eList == null) {
0867: eList = new ArrayList();
0868: }
0869: return new TaskResultInfo(taskResultInfo.getTaskId(),
0870: taskResultInfo.getResult(), taskResultInfo
0871: .getMessageType(), mList, eList);
0872: }
0873:
0874: public void updateInstanceMap(Map instMap,
0875: FrameworkTaskResult fwTaskResult, Map fwInstMsgMap,
0876: Map fwInstExpMap) {
0877:
0878: Set instSet = new HashSet();
0879: instSet.addAll(fwInstMsgMap.keySet());
0880: instSet.addAll(fwInstExpMap.keySet());
0881:
0882: for (Iterator itr = instSet.iterator(); itr.hasNext();) {
0883: String instName = (String) itr.next();
0884:
0885: InstanceTaskResult instResult = (InstanceTaskResult) instMap
0886: .get(instName);
0887: if (instResult == null) {
0888: instResult = new InstanceTaskResult();
0889: instResult.setInstanceName(instName);
0890: instMap.put(instName, instResult);
0891: }
0892:
0893: TaskResultInfo taskResultInfo = createTaskResultInfo(
0894: fwTaskResult.getTaskResultInfo(),
0895: (List) fwInstMsgMap.get(instName),
0896: (List) fwInstExpMap.get(instName));
0897: FrameworkTaskResult instFwTaskResult = new FrameworkTaskResult(
0898: taskResultInfo, fwTaskResult.isCauseFramework(),
0899: fwTaskResult.getLocale());
0900: instResult.getJBITaskResult().setFrameworkTaskResult(
0901: instFwTaskResult);
0902:
0903: }
0904: }
0905:
0906: public void updateInstanceMap(Map instMap,
0907: ComponentTaskResult compResult, Map msgMap, Map expMap) {
0908:
0909: Set instSet = new HashSet();
0910: instSet.addAll(msgMap.keySet());
0911: instSet.addAll(expMap.keySet());
0912:
0913: for (Iterator itr = instSet.iterator(); itr.hasNext();) {
0914: String instName = (String) itr.next();
0915: InstanceTaskResult instResult = (InstanceTaskResult) instMap
0916: .get(instName);
0917: if (instResult == null) {
0918: instResult = new InstanceTaskResult();
0919: instResult.setInstanceName(instName);
0920: instMap.put(instName, instResult);
0921: }
0922:
0923: TaskResultInfo taskResultInfo = createTaskResultInfo(
0924: compResult.getTaskResultInfo(), (List) msgMap
0925: .get(instName), (List) expMap.get(instName));
0926: ComponentTaskResult instCompTaskResult = new ComponentTaskResult(
0927: taskResultInfo, compResult.getComponentName());
0928: instResult.getJBITaskResult().getComponentTaskResultList()
0929: .add(instCompTaskResult);
0930: }
0931: }
0932:
0933: public ESBTaskResult toESBTaskResult(JBIManagementMessage jbiMsg) {
0934:
0935: FrameworkTaskResult fwResult = jbiMsg.getFrameworkTaskResult();
0936: List compResultList = jbiMsg.getComponentTaskResultList();
0937:
0938: TaskResultInfo fwTaskResultInfo = fwResult.getTaskResultInfo();
0939: Map fwInstMsgMap = getInstanceMessagesMap(fwTaskResultInfo,
0940: false); // get status msg
0941: Map fwInstExpMap = getInstanceMessagesMap(fwTaskResultInfo,
0942: true); // get status msg
0943:
0944: // create instance level framewrok task result
0945: Map instMap = new HashMap();
0946: updateInstanceMap(instMap, fwResult, fwInstMsgMap, fwInstExpMap);
0947: // update component results per instance
0948: for (int i = 0; i < compResultList.size(); ++i) {
0949: ComponentTaskResult compResult = (ComponentTaskResult) compResultList
0950: .get(i);
0951: Map compInstMsgMap = getInstanceMessagesMap(compResult
0952: .getTaskResultInfo(), false); // get statusp msg map
0953: Map compInstExpMap = getInstanceMessagesMap(compResult
0954: .getTaskResultInfo(), true); // get exp msg map
0955: updateInstanceMap(instMap, compResult, compInstMsgMap,
0956: compInstExpMap);
0957: }
0958:
0959: // now, the instance map contains the instance result with key CAS_KEY which is CAS level messages. So,
0960: // remove them from the instance level messages and keep it as a CAS result
0961: InstanceTaskResult casInstResult = (InstanceTaskResult) instMap
0962: .get(CAS_KEY);
0963: instMap.remove(CAS_KEY);
0964:
0965: List instTaskResultList = new ArrayList(instMap.values());
0966: JBITaskResult casTaskResult = null;
0967: if (casInstResult != null) {
0968: casTaskResult = casInstResult.getJBITaskResult();
0969: } else {
0970: casTaskResult = new JBITaskResult();
0971: }
0972: ESBTaskResult esbTaskResult = new ESBTaskResult(casTaskResult,
0973: instTaskResultList);
0974:
0975: return esbTaskResult;
0976: }
0977:
0978: /**
0979: * creates formatted esb results
0980: * @param esbResult esb results object
0981: * @return formatted text
0982: */
0983: public String getFormattedResult(ESBTaskResult esbResult) {
0984:
0985: if (esbResult == null) {
0986: return null;
0987: }
0988:
0989: String formattedMsg = null;
0990:
0991: StringWriter buff = new StringWriter();
0992: PrintWriter out = new PrintWriter(buff);
0993:
0994: // print ESB Result messages
0995: printESBTaskResult(out, esbResult);
0996:
0997: // buff.close(); // has no effect as it is a string writer.
0998: out.close();
0999: formattedMsg = buff.getBuffer().toString();
1000:
1001: return formattedMsg;
1002: }
1003:
1004: /**
1005: * return the formatted message if you can create. else return null
1006: * @param jbiMgmtMsgXml string contains the xml confirms to the JbiManagementMessage schema
1007: * @return the formatted message if you can create. else return null
1008: */
1009: public String getFormattedESBResult(JBIManagementMessage jbiMsg) {
1010:
1011: if (jbiMsg == null) {
1012: return null;
1013: }
1014:
1015: String formattedMsg = null;
1016: ESBTaskResult esbTaskResult = toESBTaskResult(jbiMsg);
1017: formattedMsg = getFormattedResult(esbTaskResult);
1018:
1019: return formattedMsg;
1020: }
1021:
1022: /**
1023: * return the formatted message if you can create. else return null
1024: * @param jbiMgmtMsgXml string contains the xml confirms to the JbiManagementMessage schema
1025: * @return the formatted message if you can create. else return null
1026: */
1027: public String getFormattedESBResult(String jbiMgmtMsgXml) {
1028: JBIManagementMessage jbiMsg = JBIManagementMessage
1029: .createJBIManagementMessage(jbiMgmtMsgXml);
1030: return getFormattedESBResult(jbiMsg);
1031: }
1032:
1033: }
|