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: * @(#)JBIManagementMessage.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 java.io.BufferedReader;
0032: import java.io.IOException;
0033: import java.io.PrintStream;
0034: import java.io.PrintWriter;
0035: import java.io.StringReader;
0036: import java.io.StringWriter;
0037: import java.io.Writer;
0038: import java.util.ArrayList;
0039: import java.util.Collections;
0040: import java.util.Comparator;
0041: import java.util.HashMap;
0042: import java.util.Iterator;
0043: import java.util.List;
0044: import java.util.Locale;
0045: import java.util.Map;
0046: import java.util.Set;
0047: import javax.xml.parsers.DocumentBuilder;
0048: import javax.xml.parsers.DocumentBuilderFactory;
0049: import org.w3c.dom.Document;
0050: import org.w3c.dom.Element;
0051: import org.w3c.dom.NodeList;
0052: import org.xml.sax.InputSource;
0053: import org.xml.sax.SAXException;
0054: import org.xml.sax.SAXParseException;
0055: import org.xml.sax.helpers.DefaultHandler;
0056:
0057: /**
0058: * This class represents the management message object that will be constructed
0059: * from the xml string returned by the management apis to access various parts of
0060: * the message. The factory method create is used to construct the dom for the xml
0061: * text and parse the dom to construct this object.
0062: *
0063: * @author Sun Microsystems, Inc.
0064: */
0065: public class JBIManagementMessage {
0066: /**
0067: * failed status
0068: */
0069: public static final String FAILED = "FAILED";
0070: /**
0071: * success status
0072: */
0073: public static final String SUCCESS = "SUCCESS";
0074: /**
0075: * info message type
0076: */
0077: public static final String INFO = "INFO";
0078: /**
0079: * warning message type
0080: */
0081: public static final String WARNING = "WARNING";
0082: /**
0083: * error message type
0084: */
0085: public static final String ERROR = "ERROR";
0086: /**
0087: * error message type
0088: */
0089: public static final String EXCEPTION = "EXCEPTION";
0090: /**
0091: * unknown value for any unknonw value of any msg status or type.
0092: */
0093: public static final String UNKNOWN = "UNKNOWN";
0094:
0095: /**
0096: * result object.
0097: */
0098: private FrameworkTaskResult mFrameworkTaskResult;
0099: /**
0100: * list.
0101: */
0102: private List /*<ComponentTaskResult>*/mCompTaskResultList;
0103:
0104: /** Creates a new instance of JBIManagementMessage */
0105: protected JBIManagementMessage() {
0106: initMessageData();
0107: }
0108:
0109: /**
0110: * initializes fields
0111: */
0112: private void initMessageData() {
0113: this .mFrameworkTaskResult = null;
0114: this .mCompTaskResultList = new ArrayList();
0115: }
0116:
0117: /**
0118: * sets fw result.
0119: * @param frameworkTaskResult fw result object.
0120: */
0121: protected void setFrameworkTaskResult(
0122: FrameworkTaskResult frameworkTaskResult) {
0123: this .mFrameworkTaskResult = frameworkTaskResult;
0124: }
0125:
0126: /**
0127: * get fw result object.
0128: * @return fw result object.
0129: */
0130: protected FrameworkTaskResult getFrameworkTaskResult() {
0131: return this .mFrameworkTaskResult;
0132: }
0133:
0134: /**
0135: * returns result list.
0136: * @return result list.
0137: */
0138: protected List getComponentTaskResultList() {
0139: return this .mCompTaskResultList;
0140: }
0141:
0142: /**
0143: * returns result list.
0144: * @return result list.
0145: */
0146: protected List getSortedComponentTaskResultList() {
0147: try {
0148: Collections.sort(this .mCompTaskResultList,
0149: new Comparator() {
0150: public int compare(Object o1, Object o2) {
0151: return ((ComponentTaskResult) o1)
0152: .getComponentId().compareTo(
0153: ((ComponentTaskResult) o2)
0154: .getComponentId());
0155: }
0156: });
0157: } catch (ClassCastException ccEx) {
0158: // log and // do nothing.
0159: } catch (UnsupportedOperationException unsupEx) {
0160: // log and // do nothing.
0161: } catch (Exception ex) {
0162: // log and // do nothing.
0163: }
0164:
0165: return this .mCompTaskResultList;
0166: }
0167:
0168: /**
0169: * check message status
0170: * @return true if status is SUCCESS else fasle
0171: */
0172: public boolean isSuccessMsg() {
0173: String result = this .mFrameworkTaskResult.getResult();
0174: return SUCCESS.equalsIgnoreCase(result);
0175: }
0176:
0177: /**
0178: * check message status
0179: * @return true if status is FAILED else fasle
0180: */
0181: public boolean isFailedMsg() {
0182: // if not success, it is failed message
0183: return !isSuccessMsg();
0184: }
0185:
0186: /**
0187: * check message status
0188: * @return true if it is warning message else fasle
0189: */
0190: public boolean isWarningMsg() {
0191: return (isSuccessMsg() && isWarningMsgType());
0192: }
0193:
0194: /**
0195: * check message type
0196: * @return msg type or null if no message type present
0197: */
0198: public String getMessageType() {
0199: String msgType = this .mFrameworkTaskResult.getTaskResultInfo()
0200: .getMessageType();
0201: return msgType;
0202: }
0203:
0204: /**
0205: * check message type
0206: * @return true if it is warning message type else fasle
0207: */
0208: public boolean isWarningMsgType() {
0209: return WARNING.equalsIgnoreCase(getMessageType());
0210: }
0211:
0212: /**
0213: * check message type
0214: * @return true if it is info message type else fasle
0215: */
0216: public boolean isInfoMsgType() {
0217: return INFO.equalsIgnoreCase(getMessageType());
0218: }
0219:
0220: /**
0221: * check message type
0222: * @return true if it is error message type else fasle
0223: */
0224: public boolean isErrorMsgType() {
0225: return ERROR.equalsIgnoreCase(getMessageType());
0226: }
0227:
0228: /**
0229: * check message type
0230: * @return true if it is error message type else fasle
0231: */
0232: public boolean isExceptionMsgType() {
0233: String type = getMessageType();
0234: if (type == null || this .UNKNOWN.equalsIgnoreCase(type)
0235: || this .EXCEPTION.equalsIgnoreCase(type)) {
0236: return this .getFrameworkTaskResult().isException();
0237: } else {
0238: return false;
0239: }
0240: }
0241:
0242: /**
0243: * override method
0244: * @param s stream
0245: */
0246: public void printMessage(PrintStream s) {
0247: PrintWriter writer = new PrintWriter(s);
0248: printMessage(writer);
0249: }
0250:
0251: /**
0252: * Returns a verbose output.
0253: * @return debug message.
0254: */
0255: public String getDebugMessage() {
0256: StringWriter msgBuffer = new StringWriter();
0257: PrintWriter msgWriter = new PrintWriter(msgBuffer);
0258:
0259: String verboseString = "";
0260:
0261: msgWriter.println(Util.getCommonI18NBundle().getMessage(
0262: "jbi.mgmt.msg.debug.fw.msg.header"));
0263:
0264: String formattedFrameworkMsg = Util
0265: .getCommonI18NBundle()
0266: .getMessage(
0267: "jbi.mgmt.msg.debug.fw.msg",
0268: new Object[] {
0269: this .mFrameworkTaskResult.getTaskId(),
0270: this .mFrameworkTaskResult.getResult(), // TODO i18n
0271: this .mFrameworkTaskResult.getMessage() });
0272:
0273: msgWriter.println(formattedFrameworkMsg);
0274:
0275: msgWriter.println(Util.getCommonI18NBundle().getMessage(
0276: "jbi.mgmt.msg.debug.comp.msg.header"));
0277:
0278: List compTaskResultList = this .getComponentTaskResultList();
0279: for (int i = 0; i < compTaskResultList.size(); ++i) {
0280: ComponentTaskResult compTaskResult = (ComponentTaskResult) compTaskResultList
0281: .get(i);
0282: String compName = compTaskResult.getComponentId();
0283:
0284: // print component name
0285: String compMsgTitle = Util
0286: .getCommonI18NBundle()
0287: .getMessage("jbi.mgmt.msg.debug.for.comp", compName);
0288: msgWriter.println(compMsgTitle);
0289: // print component messages
0290: String formattedCompMsg = Util.getCommonI18NBundle()
0291: .getMessage(
0292: "jbi.mgmt.msg.debug.comp.msg",
0293: new Object[] { compTaskResult.getTaskId(),
0294: compTaskResult.getResult(), // todo i18n
0295: compTaskResult.getMessage() });
0296: msgWriter.println(formattedCompMsg);
0297: }
0298:
0299: closeWriter(msgWriter);
0300: closeWriter(msgBuffer);
0301: return msgBuffer.getBuffer().toString();
0302: }
0303:
0304: /**
0305: * prints message
0306: * @param writer writer
0307: */
0308: public void printDebugMessage(PrintWriter writer) {
0309: String outMsg = getDebugMessage();
0310: writer.println(outMsg);
0311: }
0312:
0313: /**
0314: * closes the wirter without generationg the ioexception
0315: * @param writer Writer object
0316: */
0317: protected void closeWriter(Writer writer) {
0318: try {
0319: writer.close();
0320: } catch (IOException ex) {
0321: // ignore
0322: }
0323: }
0324:
0325: /**
0326: *
0327: * @param out
0328: * @param msgList
0329: */
0330: protected void printMessageList(PrintWriter out, List msgList) {
0331: String msg = (String) msgList.get(0);
0332: out.print(msg);
0333: for (int i = 1; i < msgList.size(); ++i) {
0334: out.println();
0335: msg = (String) msgList.get(i);
0336: out.print(msg);
0337: }
0338: }
0339:
0340: /**
0341: *
0342: * @param compTaskResult
0343: * @return
0344: */
0345: protected List getAllMessagesForComponent(
0346: ComponentTaskResult compTaskResult) {
0347: boolean compFormat = true;
0348: if (compTaskResult == null) {
0349: return new ArrayList();
0350: }
0351: return compTaskResult.getTaskResultInfo()
0352: .getAllFormattedMessages(compFormat);
0353: }
0354:
0355: /**
0356: * returns the formatted message as a string
0357: * @param level Specifies the level of output ("verbose","normal","terse")
0358: * @return message.
0359: * @deprecated
0360: */
0361: public String getMessageDeprecated(String level) {
0362: // Make sure the level is a valid value (Defauld to "verbose")
0363: if ((!(level.equalsIgnoreCase("normal")))
0364: && (!(level.equalsIgnoreCase("terse")))) {
0365: level = "verbose";
0366: }
0367:
0368: StringWriter msgBuffer = new StringWriter();
0369: PrintWriter msgWriter = new PrintWriter(msgBuffer);
0370:
0371: StringWriter fwMsgBuffer = new StringWriter();
0372: PrintWriter fwMsgWriter = new PrintWriter(fwMsgBuffer);
0373:
0374: StringWriter compMsgBuffer = new StringWriter();
0375: PrintWriter compMsgWriter = new PrintWriter(compMsgBuffer);
0376:
0377: boolean containsFwMsgs = false;
0378: boolean containsCompMsgs = false;
0379:
0380: boolean compFormat = true;
0381:
0382: // retrieve fw messages
0383: List fwMsgList = this .getFrameworkTaskResult()
0384: .getTaskResultInfo().getAllFormattedMessages(
0385: !compFormat);
0386: // print fw messages
0387: if (fwMsgList != null && fwMsgList.size() > 0) {
0388: printMessageList(fwMsgWriter, fwMsgList);
0389: containsFwMsgs = true;
0390: }
0391:
0392: boolean firstCompMsgPrinted = false;
0393:
0394: // retrieve comp messages
0395:
0396: List compTaskResultList = this
0397: .getSortedComponentTaskResultList();
0398:
0399: for (int i = 0; i < compTaskResultList.size(); ++i) {
0400: ComponentTaskResult compTaskResult = (ComponentTaskResult) compTaskResultList
0401: .get(i);
0402: String compName = compTaskResult.getComponentId();
0403: List compMsgList = getAllMessagesForComponent(compTaskResult);
0404: if (compMsgList != null && compMsgList.size() > 0) {
0405: if (firstCompMsgPrinted) {
0406: compMsgWriter.println();
0407: }
0408: // print component name
0409: String compMsgTitle = Util.getCommonI18NBundle()
0410: .getMessage("jbi.result.comp.msg.from.comp",
0411: compName);
0412: compMsgWriter.println(compMsgTitle);
0413: // print component messages
0414: printMessageList(compMsgWriter, compMsgList);
0415: firstCompMsgPrinted = true;
0416: containsCompMsgs = true;
0417: }
0418: }
0419:
0420: closeWriter(fwMsgWriter);
0421: closeWriter(compMsgWriter);
0422: closeWriter(fwMsgBuffer);
0423: closeWriter(compMsgBuffer);
0424:
0425: // print fw msgs
0426: if (containsFwMsgs) {
0427: msgWriter.print(fwMsgBuffer.getBuffer().toString());
0428: }
0429:
0430: // print comp msgs
0431: if (containsCompMsgs) {
0432: if (containsFwMsgs) {
0433: msgWriter.println();
0434: }
0435: msgWriter.print(compMsgBuffer.getBuffer().toString());
0436: }
0437:
0438: closeWriter(msgWriter);
0439: closeWriter(msgBuffer);
0440:
0441: return msgBuffer.getBuffer().toString();
0442: }
0443:
0444: /**
0445: * returns the formatted message as a string
0446: * @param level Specifies the level of output ("verbose","normal","terse")
0447: * @return message.
0448: */
0449: public String getMessage(String level) {
0450: // Make sure the level is a valid value (Defauld to "verbose")
0451: if ((!(level.equalsIgnoreCase("normal")))
0452: && (!(level.equalsIgnoreCase("terse")))) {
0453: level = "verbose";
0454: }
0455:
0456: // ESBResultFormatter
0457: ESBResultFormatter formatter = new ESBResultFormatter(level);
0458: return formatter.getFormattedESBResult(this );
0459: }
0460:
0461: /**
0462: * returns the formatted message as a string
0463: * @return text
0464: */
0465: public String getMessage() {
0466: return (getMessage("verbose"));
0467: }
0468:
0469: /**
0470: * prints message
0471: * @param writer writer
0472: */
0473: public void printMessage(PrintWriter writer) {
0474: String outMsg = getMessage();
0475: writer.println(outMsg);
0476: }
0477:
0478: /**
0479: * override method
0480: * @param s stream
0481: */
0482: public void printStackTrace(PrintStream s) {
0483: PrintWriter writer = new PrintWriter(s, true);
0484: printStackTrace(writer);
0485: }
0486:
0487: /**
0488: * write string to the writer
0489: * @param prefix string
0490: * @param msg string
0491: * @param writer Print Writer
0492: */
0493: public static void printWithIndentation(String prefix, String msg,
0494: PrintWriter writer) {
0495: if (msg == null) {
0496: return;
0497: }
0498:
0499: BufferedReader reader = new BufferedReader(
0500: new StringReader(msg));
0501: String line = null;
0502: try {
0503: while ((line = reader.readLine()) != null) {
0504: writer.println(prefix + line);
0505: }
0506: } catch (IOException ex) {
0507: // will never happen. ignore
0508:
0509: }
0510: }
0511:
0512: /**
0513: * prints stacktrace
0514: * @param writer writer
0515: */
0516: public void printStackTrace(PrintWriter writer) {
0517: String fwSTPrefix = Util.getCommonI18NBundle().getMessage(
0518: "jbi.mgmt.stack.fw.stack.prefix");
0519:
0520: String compSTPrefix = Util.getCommonI18NBundle().getMessage(
0521: "jbi.mgmt.stack.comp.stack.prefix");
0522:
0523: writer.println(Util.getCommonI18NBundle().getMessage(
0524: "jbi.mgmt.stack.fw.stack.header"));
0525: writer.print(this .mFrameworkTaskResult.getMessage());
0526: String frameworkStack = this .mFrameworkTaskResult
0527: .getStackTrace();
0528: printWithIndentation(fwSTPrefix, frameworkStack, writer);
0529:
0530: writer.println(Util.getCommonI18NBundle().getMessage(
0531: "jbi.mgmt.stack.comp.stack.header"));
0532:
0533: List compTaskResultList = this .getComponentTaskResultList();
0534: for (int i = 0; i < compTaskResultList.size(); ++i) {
0535: ComponentTaskResult compTaskResult = (ComponentTaskResult) compTaskResultList
0536: .get(i);
0537: String compName = compTaskResult.getComponentId();
0538:
0539: // print component name
0540: String compMsgTitle = Util.getCommonI18NBundle()
0541: .getMessage("jbi.mgmt.stack.for.comp", compName);
0542: writer.println(compMsgTitle);
0543: // print component stack
0544: String compStack = compTaskResult.getStackTrace();
0545: writer.print(compTaskResult.getMessage());
0546: printWithIndentation(compSTPrefix, compStack, writer);
0547: }
0548:
0549: }
0550:
0551: /**
0552: * stacktrace
0553: * @return text
0554: */
0555: public String getStackTrace() {
0556: StringWriter strWriter = new StringWriter();
0557: PrintWriter writer = new PrintWriter(strWriter, true);
0558: printStackTrace(writer);
0559: writer.close();
0560: return strWriter.getBuffer().toString();
0561: }
0562:
0563: /**
0564: * adds comp result object.
0565: * @param compTaskResult comp result object.
0566: */
0567: private void addToComponentResultList(
0568: ComponentTaskResult compTaskResult) {
0569: if (compTaskResult == null) {
0570: return;
0571: }
0572:
0573: this .mCompTaskResultList.add(compTaskResult);
0574:
0575: }
0576:
0577: /**
0578: * factory method to create the message object
0579: * @param xmlText xml text
0580: * @return Message Object
0581: */
0582: public static JBIManagementMessage createJBIManagementMessage(
0583: String xmlText) {
0584: JBIManagementMessageUnmarshaller unmarshaller = new JBIManagementMessageUnmarshaller();
0585: try {
0586: return unmarshaller.unmarshal(xmlText);
0587: } catch (Exception ex) {
0588: // don't care the exception. we either create or don't create the
0589: // mgmt object out of the string. code using this api will take care of why
0590: return null;
0591: }
0592: }
0593:
0594: /**
0595: * returns the l10n msgtyep string for msgType
0596: * @param msgType is one of the INFO, ERROR, WARNING
0597: * @return
0598: */
0599: protected static String getL10NMessageType(String msgType) {
0600:
0601: String l10nMsgType = null;
0602: if (ERROR.equalsIgnoreCase(msgType)) {
0603: l10nMsgType = Util.getCommonI18NBundle().getMessage(
0604: "jbi.result.msg.type.error");
0605: } else if (WARNING.equalsIgnoreCase(msgType)) {
0606: l10nMsgType = Util.getCommonI18NBundle().getMessage(
0607: "jbi.result.msg.type.warning");
0608: } else if (INFO.equalsIgnoreCase(msgType)) {
0609: l10nMsgType = Util.getCommonI18NBundle().getMessage(
0610: "jbi.result.msg.type.info");
0611: } else {
0612: l10nMsgType = Util.getCommonI18NBundle().getMessage(
0613: "jbi.result.msg.type.unknown");
0614: }
0615: return l10nMsgType;
0616:
0617: }
0618:
0619: /**
0620: * class for message inf object.
0621: */
0622: public static class MessageInfo {
0623: /**
0624: * id.
0625: */
0626: private String mI18nId;
0627: /**
0628: * msg.
0629: */
0630: private String mLocalizedMsg;
0631: /**
0632: * params.
0633: */
0634: private List ml10nMsgParams;
0635:
0636: /**
0637: * constructor.
0638: * @param id id.
0639: * @param locMsg msg.
0640: * @param locMsgParams params.
0641: */
0642: public MessageInfo(String id, String locMsg, List locMsgParams) {
0643: this .mI18nId = id;
0644: this .mLocalizedMsg = locMsg;
0645:
0646: if (locMsgParams != null) {
0647: this .ml10nMsgParams = Collections
0648: .unmodifiableList(locMsgParams);
0649: } else {
0650: this .ml10nMsgParams = new ArrayList();
0651: }
0652: }
0653:
0654: /**
0655: * return id.
0656: * @return id.
0657: */
0658: public String getI18nId() {
0659: return this .mI18nId;
0660: }
0661:
0662: /**
0663: * returns message.
0664: * @return message.
0665: */
0666: public String getLocalizedMsg() {
0667: return this .mLocalizedMsg;
0668: }
0669:
0670: /**
0671: * returns params.
0672: * @return list of params.
0673: */
0674: public List getl10nMsgParams() {
0675: return this .ml10nMsgParams;
0676: }
0677:
0678: /**
0679: * returns formatted message.
0680: * @paramn isExpFormat true if the format is as a exception msg
0681: * @return formatted message
0682: * @param isExpFormat
0683: */
0684: public String getFormattedMessage(boolean isExpFormat) {
0685: //TODO define a static initilizer for the i18nMsgType
0686: String formattedMsg = null;
0687: formattedMsg = Util.getCommonI18NBundle().getMessage(
0688: "jbi.result.msg.format", this .getI18nId(),
0689: this .getLocalizedMsg());
0690:
0691: if (isExpFormat) {
0692: // put the formatted msg as expcetion msg
0693: formattedMsg = Util.getCommonI18NBundle().getMessage(
0694: "jbi.result.ex.msg.format", formattedMsg);
0695: }
0696:
0697: return formattedMsg;
0698: }
0699: }
0700:
0701: /**
0702: * class for Exception info storage.
0703: */
0704: public static class ExceptionInfo {
0705: /**
0706: * level.
0707: */
0708: private Integer mLevel;
0709: /**
0710: * msg info object.
0711: */
0712: private MessageInfo mMsgInfo;
0713: /**
0714: * stacktrace.
0715: */
0716: private String mStacktrace;
0717:
0718: /**
0719: * constructor.
0720: * @param level level.
0721: * @param msgInfo msg info object.
0722: * @param stackTrace stacktrace string.
0723: */
0724: public ExceptionInfo(String level, MessageInfo msgInfo,
0725: String stackTrace) {
0726: try {
0727: this .mLevel = Integer.valueOf(level);
0728: } catch (NumberFormatException ex) {
0729: this .mLevel = new Integer(-1);
0730: }
0731: this .mMsgInfo = msgInfo;
0732: this .mStacktrace = stackTrace;
0733: }
0734:
0735: /**
0736: * returns level.
0737: * @return Integer level.
0738: */
0739: public Integer getLevel() {
0740: return this .mLevel;
0741: }
0742:
0743: /**
0744: * returns stacktrace string.
0745: * @return text for stacktrace.
0746: */
0747: public String getStackTrace() {
0748: return this .mStacktrace;
0749: }
0750:
0751: /**
0752: * returns message info object.
0753: * @return MessageInfo object.
0754: */
0755: public MessageInfo getMessageInfo() {
0756: return this .mMsgInfo;
0757: }
0758:
0759: /**
0760: * returns message.
0761: * @return message.
0762: */
0763: public String getLocalizedMsg() {
0764: if (this .mMsgInfo != null) {
0765: return this .mMsgInfo.getLocalizedMsg();
0766: } else {
0767: return null;
0768: }
0769: }
0770:
0771: /**
0772: * returns formatted message.
0773: * @paramn isExpFormat true if the format is as a exception msg
0774: * @return formatted message
0775: * @param isExpFormat
0776: */
0777: public String getFormattedMessage(boolean isExpFormat) {
0778: if (this .mMsgInfo != null) {
0779: return this .mMsgInfo.getFormattedMessage(isExpFormat);
0780: } else {
0781: return null;
0782: }
0783: }
0784:
0785: /**
0786: * returns formatted stacktrace.
0787: * @return formatted stacktrace.
0788: */
0789: public String getFormattedStackTrace() {
0790: //TODO add "at " prefix to every line of the stacktrace.
0791: return this .mStacktrace;
0792: }
0793: }
0794:
0795: /**
0796: * class for task result object
0797: */
0798: public static class TaskResultInfo {
0799:
0800: /**
0801: * TaskId.
0802: */
0803: private String mTaskId;
0804: /**
0805: * Result
0806: */
0807: private String mResult;
0808: /**
0809: * MsgType;
0810: */
0811: private String mMsgType;
0812: /**
0813: * MsgInfoList
0814: */
0815: private List mMsgInfoList;
0816:
0817: /**
0818: * ExInfoList
0819: */
0820: private List mExInfoList;
0821:
0822: /**
0823: * default constructor
0824: */
0825: public TaskResultInfo() {
0826: this .mTaskId = UNKNOWN;
0827: this .mResult = UNKNOWN;
0828: this .mMsgType = UNKNOWN;
0829: this .mMsgInfoList = new ArrayList();
0830: this .mExInfoList = new ArrayList();
0831: }
0832:
0833: /**
0834: * constructor
0835: * @param taskId id.
0836: * @param result result.
0837: * @param msgType type.
0838: * @param msgInfoList a List of MessageInfo.
0839: * @param exInfoList list.
0840: */
0841: public TaskResultInfo(String taskId, String result,
0842: String msgType, List msgInfoList, List exInfoList) {
0843: this .mTaskId = taskId;
0844: this .mResult = result;
0845: this .mMsgType = msgType;
0846:
0847: if (msgInfoList != null) {
0848: this .mMsgInfoList = Collections
0849: .unmodifiableList(msgInfoList);
0850: } else {
0851: this .mMsgInfoList = new ArrayList();
0852: }
0853:
0854: if (exInfoList != null) {
0855: this .mExInfoList = Collections
0856: .unmodifiableList(exInfoList);
0857: } else {
0858: this .mExInfoList = new ArrayList();
0859: }
0860:
0861: }
0862:
0863: /**
0864: * returns id
0865: * @return id
0866: */
0867: public String getTaskId() {
0868: return this .mTaskId;
0869: }
0870:
0871: public void setTaskId(String taskId) {
0872: this .mTaskId = taskId;
0873: }
0874:
0875: /**
0876: * returns result
0877: * @return result
0878: */
0879: public String getResult() {
0880: return this .mResult;
0881: }
0882:
0883: public void setResult(String result) {
0884: this .mResult = result;
0885: }
0886:
0887: /**
0888: * returns message type
0889: * @return type
0890: */
0891: public String getMessageType() {
0892: return this .mMsgType;
0893: }
0894:
0895: public void setMessageType(String msgType) {
0896: this .mMsgType = msgType;
0897: }
0898:
0899: /**
0900: * returns message info list
0901: * @return message info list
0902: */
0903: public List getStatusMessageInfoList() {
0904: return this .mMsgInfoList;
0905: }
0906:
0907: public void setStatusMessageInfoList(
0908: List /*<MessageInfo>*/msgInfoList) {
0909: this .mMsgInfoList = msgInfoList;
0910: }
0911:
0912: /**
0913: * returns exception info list
0914: * @return exception info list
0915: */
0916: public List getExceptionInfoList() {
0917: return this .mExInfoList;
0918: }
0919:
0920: public void setExceptionInfoList(
0921: List /*<ExceptionInfo>*/expInfoList) {
0922: this .mExInfoList = expInfoList;
0923: }
0924:
0925: /**
0926: * return status message
0927: * @return status message
0928: */
0929: public String getStatusMessage() {
0930: if (this .mMsgInfoList == null
0931: || this .mMsgInfoList.size() <= 0) {
0932: return null;
0933: }
0934: StringWriter strWriter = new StringWriter();
0935: PrintWriter prnWriter = new PrintWriter(strWriter);
0936: for (Iterator itr = this .mMsgInfoList.iterator(); itr
0937: .hasNext();) {
0938: MessageInfo msgInfo = (MessageInfo) itr.next();
0939: String msg = msgInfo.getFormattedMessage(false);
0940: prnWriter.println(msg);
0941: }
0942:
0943: try {
0944: prnWriter.close();
0945: strWriter.close();
0946: } catch (IOException ex) {
0947: // ignore
0948: }
0949: return strWriter.getBuffer().toString();
0950: }
0951:
0952: /**
0953: * returns exception message
0954: * @return exception messgae
0955: */
0956: public String getExceptionMessage() {
0957: if (this .mExInfoList == null
0958: || this .mExInfoList.size() <= 0) {
0959: return null;
0960: }
0961: StringWriter strWriter = new StringWriter();
0962: PrintWriter prnWriter = new PrintWriter(strWriter);
0963: for (Iterator itr = this .mExInfoList.iterator(); itr
0964: .hasNext();) {
0965: ExceptionInfo exInfo = (ExceptionInfo) itr.next();
0966: String exMsg = exInfo.getFormattedMessage(true);
0967: // TODO format the chained exception message here
0968: prnWriter.println(exMsg);
0969: }
0970:
0971: try {
0972: prnWriter.close();
0973: strWriter.close();
0974: } catch (IOException ex) {
0975: // ignore
0976: }
0977: return strWriter.getBuffer().toString();
0978: }
0979:
0980: /**
0981: * returns the status msg if present else returns the exception message
0982: * @return message.
0983: */
0984: public String getMessage() {
0985: String msg = this .getStatusMessage();
0986: if (msg != null) {
0987: return msg;
0988: }
0989: msg = this .getExceptionMessage();
0990: return msg;
0991: }
0992:
0993: /**
0994: * check if this message is from exception
0995: * @return true if it is exception else false.
0996: */
0997: public boolean isException() {
0998: return (this .mExInfoList != null && this .mExInfoList.size() > 0);
0999: }
1000:
1001: /**
1002: * return stacktrace
1003: * @return stacktrace
1004: */
1005: public String getStackTrace() {
1006: if (this .mExInfoList == null
1007: || this .mExInfoList.size() <= 0) {
1008: return null;
1009: }
1010: StringWriter strWriter = new StringWriter();
1011: PrintWriter prnWriter = new PrintWriter(strWriter);
1012: for (Iterator itr = this .mExInfoList.iterator(); itr
1013: .hasNext();) {
1014: ExceptionInfo exInfo = (ExceptionInfo) itr.next();
1015: String stacktrace = exInfo.getStackTrace();
1016: prnWriter.println(stacktrace);
1017: }
1018:
1019: try {
1020: prnWriter.close();
1021: strWriter.close();
1022: } catch (IOException ex) {
1023: // ignore
1024: }
1025: return strWriter.getBuffer().toString();
1026: }
1027:
1028: /**
1029: * returns the msg formatted as <msgtype>:(<msgcode>)<msg>
1030: * @return formatted 1st message
1031: * @param formattedMsg
1032: * @param msgType message type from one of ERROR, WARNING, INFO
1033: */
1034: protected String getFormattedMsgWithType(String msgType,
1035: String formattedMsg) {
1036: String l10nType = getL10NMessageType(msgType);
1037: return Util.getCommonI18NBundle()
1038: .getMessage("jbi.result.1st.msg.format", l10nType,
1039: formattedMsg);
1040: }
1041:
1042: /**
1043: * formats the indentation of the formattedMsg as framework or component message indentation
1044: * @param isCompFormat true for component indentation, false for framework indentation
1045: * @param formattedMsg message that is formatted with low level format.
1046: * @return
1047: */
1048: protected String getFormatted1stMessage(boolean isCompFormat,
1049: String formattedMsg) {
1050: String i18nKey = "jbi.result.fw.msg";
1051: if (isCompFormat) {
1052: i18nKey = "jbi.result.comp.msg";
1053: }
1054: return Util.getCommonI18NBundle().getMessage(i18nKey,
1055: formattedMsg);
1056: }
1057:
1058: /**
1059: * formats the indentation of the formattedMsg as framework or component message indentation
1060: * @param isCompFormat true for component indentation, false for framework indentation
1061: * @param formattedMsg message that is formatted with low level format.
1062: * @return
1063: */
1064: protected String getFormatted2ndMessage(boolean isCompFormat,
1065: String formattedMsg) {
1066: String i18nKey = "jbi.result.fw.2nd.msg";
1067: if (isCompFormat) {
1068: i18nKey = "jbi.result.comp.2nd.msg";
1069: }
1070: return Util.getCommonI18NBundle().getMessage(i18nKey,
1071: formattedMsg);
1072: }
1073:
1074: /**
1075: * formats the indentation of the formattedMsg as framework or component message indentation
1076: * @param isCompFormat true for component indentation, false for framework indentation
1077: * @param formattedMsg message that is formatted with low level format.
1078: * @return
1079: */
1080: protected String getFormatted1stExpMessage(
1081: boolean isCompFormat, String formattedMsg) {
1082: String i18nKey = "jbi.result.fw.ex.msg";
1083: if (isCompFormat) {
1084: i18nKey = "jbi.result.comp.ex.msg";
1085: }
1086: return Util.getCommonI18NBundle().getMessage(i18nKey,
1087: formattedMsg);
1088: }
1089:
1090: /**
1091: * formats the indentation of the formattedMsg as framework or component message indentation
1092: * @param isCompFormat true for component indentation, false for framework indentation
1093: * @param formattedMsg message that is formatted with low level format.
1094: * @return
1095: */
1096: protected String getFormatted2ndExpMessage(
1097: boolean isCompFormat, String formattedMsg) {
1098: String i18nKey = "jbi.result.fw.2nd.ex.msg";
1099: if (isCompFormat) {
1100: i18nKey = "jbi.result.comp.2nd.ex.msg";
1101: }
1102: return Util.getCommonI18NBundle().getMessage(i18nKey,
1103: formattedMsg);
1104: }
1105:
1106: /**
1107: *
1108: * @param msgInfoList
1109: * @param msgType
1110: * @param isCompFormat
1111: * @return
1112: */
1113: protected List getFormattedStatusMessages(List msgInfoList,
1114: String msgType, boolean isCompFormat) {
1115: List msgList = new ArrayList();
1116: String formattedMsg = null;
1117: boolean expFormat = true;
1118: // now get the formatted messages into the list
1119:
1120: int msgInfoListSize = msgInfoList.size();
1121: for (int i = 0; i < msgInfoListSize; ++i) {
1122: MessageInfo msgInfo = (MessageInfo) msgInfoList.get(i);
1123: formattedMsg = null;
1124: if (i == 0) {
1125:
1126: // add as a ist msg
1127: formattedMsg = getFormattedMsgWithType(msgType,
1128: msgInfo.getFormattedMessage(!expFormat));
1129: formattedMsg = getFormatted1stMessage(isCompFormat,
1130: formattedMsg);
1131:
1132: } else {
1133: // add all other msgs as normal msgs and as 2nd msg
1134: formattedMsg = msgInfo
1135: .getFormattedMessage(!expFormat);
1136: formattedMsg = getFormatted2ndMessage(isCompFormat,
1137: formattedMsg);
1138: }
1139: msgList.add(formattedMsg);
1140: }
1141: return msgList;
1142: }
1143:
1144: /**
1145: *
1146: * @param exList
1147: * @param msgType
1148: * @param isCompFormat
1149: * @param printExAsMainMsg
1150: * @return
1151: */
1152: protected List getFormattedExceptionMessages(List exList,
1153: String msgType, boolean isCompFormat,
1154: boolean printExAsMainMsg) {
1155:
1156: List msgList = new ArrayList();
1157: String formattedMsg = null;
1158: boolean expFormat = true;
1159: // now get the exception messages into the list
1160: boolean exAsMainMsgPrinted = false;
1161: int expListSize = exList.size();
1162: for (int i = 0; i < expListSize; ++i) {
1163: ExceptionInfo exInfo = (ExceptionInfo) exList.get(i);
1164: formattedMsg = null;
1165: if (i == 0) {
1166: if (printExAsMainMsg) {
1167: // no status messages. so add first except as 1st main message
1168: formattedMsg = getFormattedMsgWithType(msgType,
1169: exInfo.getFormattedMessage(!expFormat));
1170: // get formatted fw 1st msg
1171: formattedMsg = getFormatted1stMessage(
1172: isCompFormat, formattedMsg);
1173: exAsMainMsgPrinted = true;
1174: } else {
1175: // add the first exception as exp message
1176: formattedMsg = exInfo
1177: .getFormattedMessage(expFormat);
1178: formattedMsg = getFormatted1stExpMessage(
1179: isCompFormat, formattedMsg);
1180:
1181: }
1182: } else if (exAsMainMsgPrinted && i == 1) {
1183: // print the 2nd exception message as exception message
1184: formattedMsg = exInfo
1185: .getFormattedMessage(expFormat);
1186: formattedMsg = getFormatted1stExpMessage(
1187: isCompFormat, formattedMsg);
1188: } else {
1189: // add all exceptions as normal msgs and as 2nd msg
1190: formattedMsg = exInfo
1191: .getFormattedMessage(!expFormat);
1192: formattedMsg = getFormatted2ndExpMessage(
1193: isCompFormat, formattedMsg);
1194: }
1195: msgList.add(formattedMsg);
1196: }
1197: return msgList;
1198: }
1199:
1200: /**
1201: * return formatted messages.
1202: * @return list.
1203: * @param isCompFormat
1204: */
1205: public List getAllFormattedMessages(boolean isCompFormat) {
1206: List fmtMsgList = new ArrayList();
1207:
1208: String msgType = this .getMessageType();
1209: List msgInfoList = this .getStatusMessageInfoList();
1210: List exInfoList = this .getExceptionInfoList();
1211:
1212: boolean printExAsMainMsg = false;
1213:
1214: // get the status messages into the list
1215:
1216: List fmtStatusMsgList = getFormattedStatusMessages(
1217: msgInfoList, msgType, isCompFormat);
1218:
1219: if (fmtStatusMsgList.size() == 0) {
1220: printExAsMainMsg = true;
1221: }
1222:
1223: List fmtExMsgList = this
1224: .getFormattedExceptionMessages(exInfoList, msgType,
1225: isCompFormat, printExAsMainMsg);
1226:
1227: fmtMsgList.addAll(fmtStatusMsgList);
1228: fmtMsgList.addAll(fmtExMsgList);
1229:
1230: return fmtMsgList;
1231:
1232: }
1233:
1234: /**
1235: * return formatted stacktraces.
1236: * @return list.
1237: */
1238: public List getFormattedStacktraces() {
1239: String msgType = this .getMessageType();
1240: List list = new ArrayList();
1241: List exList = this .getExceptionInfoList();
1242:
1243: for (Iterator itr = exList.iterator(); itr.hasNext();) {
1244: ExceptionInfo exInfo = (ExceptionInfo) itr.next();
1245: list.add(exInfo.getFormattedStackTrace());
1246: }
1247: return list;
1248: }
1249:
1250: }
1251:
1252: /**
1253: * base class for task result object.
1254: */
1255: public static abstract class AbstractTaskResult {
1256: /**
1257: * TaskResultInfo object.
1258: */
1259: private TaskResultInfo mTaskResultInfo;
1260:
1261: protected AbstractTaskResult() {
1262: this .mTaskResultInfo = new TaskResultInfo();
1263: }
1264:
1265: /**
1266: * return TaskResultInfo
1267: * @return TaskResultInfo
1268: */
1269: public TaskResultInfo getTaskResultInfo() {
1270: return this .mTaskResultInfo;
1271: }
1272:
1273: public void setTaskResultInfo(TaskResultInfo taskResultInfo) {
1274: this .mTaskResultInfo = taskResultInfo;
1275: }
1276:
1277: /**
1278: * TaskId
1279: * @return TaskId
1280: */
1281: public String getTaskId() {
1282: return this .getTaskResultInfo().getTaskId();
1283: }
1284:
1285: /**
1286: * Result
1287: * @return Result
1288: */
1289: public String getResult() {
1290: return this .getTaskResultInfo().getResult();
1291: }
1292:
1293: /**
1294: * Message
1295: * @return Message
1296: */
1297: public String getMessage() {
1298: return this .getTaskResultInfo().getMessage();
1299: }
1300:
1301: /**
1302: * Exception
1303: * @return true if exception else false
1304: */
1305: public boolean isException() {
1306: return this .getTaskResultInfo().isException();
1307: }
1308:
1309: /**
1310: * StackTrace.
1311: * @return StackTrace
1312: */
1313: public String getStackTrace() {
1314: return this .getTaskResultInfo().getStackTrace();
1315: }
1316:
1317: }
1318:
1319: /**
1320: * class for Framework task results object
1321: */
1322: public static class FrameworkTaskResult extends AbstractTaskResult {
1323: /**
1324: * IsCauseFramework
1325: */
1326: private boolean mIsCauseFramework;
1327: /**
1328: * Locale
1329: */
1330: private String mLocale;
1331:
1332: public FrameworkTaskResult() {
1333: super ();
1334: this .mIsCauseFramework = false;
1335: this .mLocale = Locale.getDefault().toString();
1336: }
1337:
1338: /**
1339: * constructor.
1340: * @param taskResultInfo object.
1341: * @param isCauseFramework cause value.
1342: * @param locale locale.
1343: */
1344: public FrameworkTaskResult(TaskResultInfo taskResultInfo,
1345: boolean isCauseFramework, String locale) {
1346: super ();
1347: this .setTaskResultInfo(taskResultInfo);
1348: this .mIsCauseFramework = isCauseFramework;
1349: this .mLocale = locale;
1350: }
1351:
1352: /**
1353: * return Locale
1354: * @return Locale
1355: */
1356: public String getLocale() {
1357: return this .mLocale;
1358: }
1359:
1360: public void setLocale(String locale) {
1361: this .mLocale = locale;
1362: }
1363:
1364: public void setIsCauseFramework(boolean isCauseFramework) {
1365: this .mIsCauseFramework = isCauseFramework;
1366: }
1367:
1368: /**
1369: * return true if cause is framework else false
1370: * @return true if cause is framework else false
1371: */
1372: public boolean isCauseFramework() {
1373: return this .mIsCauseFramework;
1374: }
1375:
1376: /**
1377: * return true if cause is component else false
1378: * @return true if cause is component else false
1379: */
1380: public boolean isCauseComponent() {
1381: return !isCauseFramework();
1382: }
1383:
1384: }
1385:
1386: /**
1387: * class for component result
1388: */
1389: public static class ComponentTaskResult extends AbstractTaskResult {
1390: /**
1391: * ComponentId
1392: */
1393: private String mComponentId;
1394:
1395: public ComponentTaskResult() {
1396: super ();
1397: this .mComponentId = null;
1398: }
1399:
1400: /**
1401: * constructor
1402: * @param taskResultInfo object.
1403: * @param aComponentId component id.
1404: */
1405: public ComponentTaskResult(TaskResultInfo taskResultInfo,
1406: String aComponentId) {
1407: super ();
1408: this .setTaskResultInfo(taskResultInfo);
1409: this .mComponentId = aComponentId;
1410: }
1411:
1412: /**
1413: * return ComponentId
1414: * @return ComponentId
1415: */
1416: public String getComponentId() {
1417: return this .mComponentId;
1418: }
1419:
1420: /**
1421: * return ComponentId
1422: * @return ComponentId
1423: */
1424: public String getComponentName() {
1425: return getComponentId();
1426: }
1427:
1428: }
1429:
1430: /**
1431: * class for jbi task resutls that consists of {FrameworkTaskResult, ComponentTaskResult*}
1432: */
1433: public static class JBITaskResult {
1434: /**
1435: * result object.
1436: */
1437: private FrameworkTaskResult mFrameworkTaskResult;
1438: /**
1439: * list.
1440: */
1441: private List /*<ComponentTaskResult>*/mCompTaskResultList;
1442:
1443: public JBITaskResult() {
1444: this .mFrameworkTaskResult = new FrameworkTaskResult();
1445: this .mCompTaskResultList = new ArrayList();
1446: }
1447:
1448: public JBITaskResult(FrameworkTaskResult fwTaskResult,
1449: List/*<ComponentTaskResult>*/compTaskResultList) {
1450: this .mFrameworkTaskResult = fwTaskResult;
1451: this .mCompTaskResultList = compTaskResultList;
1452: }
1453:
1454: public FrameworkTaskResult getFrameworkTaskResult() {
1455: return this .mFrameworkTaskResult;
1456: }
1457:
1458: public void setFrameworkTaskResult(
1459: FrameworkTaskResult fwTaskResult) {
1460: this .mFrameworkTaskResult = fwTaskResult;
1461: }
1462:
1463: public List/*<ComponentTaskResult>*/getComponentTaskResultList() {
1464: return this .mCompTaskResultList;
1465: }
1466:
1467: public void setComponentTaskResultList(
1468: List/*<ComponentTaskResult>*/compTaskResultList) {
1469: this .mCompTaskResultList = compTaskResultList;
1470: }
1471:
1472: }
1473:
1474: /**
1475: * class for instance result in ESB
1476: */
1477: public static class InstanceTaskResult {
1478: /**
1479: * name of the instance in the ESB
1480: */
1481: private String mInstanceName;
1482: /**
1483: * result object.
1484: */
1485: private JBITaskResult mJBITaskResult;
1486:
1487: public InstanceTaskResult() {
1488: this .mInstanceName = null;
1489: this .mJBITaskResult = new JBITaskResult();
1490: }
1491:
1492: public InstanceTaskResult(String instanceName,
1493: JBITaskResult jbiTaskResult) {
1494: this .mInstanceName = instanceName;
1495: this .mJBITaskResult = jbiTaskResult;
1496: }
1497:
1498: public String getInstanceName() {
1499: return this .mInstanceName;
1500: }
1501:
1502: public void setInstanceName(String instanceName) {
1503: this .mInstanceName = instanceName;
1504: }
1505:
1506: public JBITaskResult getJBITaskResult() {
1507: return this .mJBITaskResult;
1508: }
1509:
1510: public void setJBITaskResult(JBITaskResult jbiTaskResult) {
1511: this .mJBITaskResult = jbiTaskResult;
1512: }
1513:
1514: }
1515:
1516: /**
1517: * class for esb task resutls that consists of {JBITaskResult, ESBInstanceTaskResult*}
1518: */
1519: public static class ESBTaskResult {
1520: /**
1521: * result object.
1522: */
1523: private JBITaskResult mCASTaskResult;
1524: /**
1525: * list.
1526: */
1527: private List /*<InstanceTaskResult>*/mInstTaskResultList;
1528:
1529: public ESBTaskResult() {
1530: this .mCASTaskResult = new JBITaskResult();
1531: this .mInstTaskResultList = new ArrayList();
1532: }
1533:
1534: public ESBTaskResult(JBITaskResult casTaskResult,
1535: List/*<InstanceTaskResult>*/instTaskResultList) {
1536: this .mCASTaskResult = casTaskResult;
1537: this .mInstTaskResultList = instTaskResultList;
1538: }
1539:
1540: public JBITaskResult getCASTaskResult() {
1541: return this .mCASTaskResult;
1542: }
1543:
1544: public void setCASTaskResult(JBITaskResult casTaskResult) {
1545: this .mCASTaskResult = casTaskResult;
1546: }
1547:
1548: public List/*<InstanceTaskResult>*/getInstanceTaskResultList() {
1549: return this .mInstTaskResultList;
1550: }
1551:
1552: public void setInstanceTaskResultList(
1553: List/*<InstanceTaskResult>*/instTaskResultList) {
1554: this .mInstTaskResultList = instTaskResultList;
1555: }
1556: }
1557:
1558: /**
1559: * to convert the management message xml to the JBIManagementMessageObject.
1560: */
1561: public static class JBIManagementMessageUnmarshaller {
1562: /**
1563: * factory method to create the message object
1564: * @param xmlText xml text
1565: * @return Message Object
1566: */
1567: public JBIManagementMessage unmarshal(String xmlText) {
1568:
1569: Document xmlDoc = null;
1570:
1571: try {
1572: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
1573: .newInstance();
1574:
1575: DocumentBuilder docBuilder = docBuilderFactory
1576: .newDocumentBuilder();
1577: docBuilder.setErrorHandler(new DefaultHandler() {
1578: public void fatalError(SAXParseException e)
1579: throws SAXException {
1580: throw new SAXException(e.getMessage());
1581: }
1582: });
1583:
1584: StringReader reader = new StringReader(xmlText);
1585: InputSource is = new InputSource(reader);
1586: xmlDoc = docBuilder.parse(is);
1587: } catch (Exception ex) {
1588: // any error means the message not understood.
1589: return null;
1590: }
1591:
1592: if (xmlDoc == null) {
1593: return null;
1594: }
1595:
1596: JBIManagementMessage msg = new JBIManagementMessage();
1597:
1598: // check if the xml contains the jbi mgmt msg root elements
1599:
1600: Element rootEl = DOMUtil.UTIL
1601: .getElement(xmlDoc, "jbi-task");
1602:
1603: if (rootEl == null) {
1604: return null;
1605: }
1606:
1607: Element taskResultInfo = DOMUtil.UTIL.getElement(xmlDoc,
1608: "jbi-task-result");
1609:
1610: if (taskResultInfo == null) {
1611: return null;
1612: }
1613:
1614: Element fwInfoEl = DOMUtil.UTIL.getElement(taskResultInfo,
1615: "frmwk-task-result");
1616:
1617: if (fwInfoEl == null) {
1618: return null;
1619: }
1620:
1621: FrameworkTaskResult fwTaskResult = getFrameworkTaskResult(fwInfoEl);
1622: msg.setFrameworkTaskResult(fwTaskResult);
1623:
1624: NodeList compInfoList = DOMUtil.UTIL.getChildElements(
1625: taskResultInfo, "component-task-result");
1626: if (compInfoList != null) {
1627: int size = compInfoList.getLength();
1628: for (int i = 0; i < size; ++i) {
1629: Element compInfoElement = (Element) compInfoList
1630: .item(i);
1631: if (compInfoElement != null) {
1632: ComponentTaskResult compTaskResult = getComponentTaskResult(compInfoElement);
1633: msg.addToComponentResultList(compTaskResult);
1634: }
1635: }
1636: }
1637: return msg;
1638:
1639: }
1640:
1641: /**
1642: * return FrameworkLocale
1643: * @param fwInfoEl xml element
1644: * @return FrameworkLocale
1645: */
1646: private String getFrameworkLocale(Element fwInfoEl) {
1647: String locale = null; // todo defaul en?
1648:
1649: Element localeEl = DOMUtil.UTIL.getElement(fwInfoEl,
1650: "locale");
1651: if (localeEl != null) {
1652: locale = DOMUtil.UTIL.getTextData(localeEl);
1653: }
1654:
1655: return locale;
1656: }
1657:
1658: /**
1659: * return true if cause if component else false
1660: * @param fwInfoEl xml element
1661: * @return true if cause if component else false
1662: */
1663: private boolean isCauseComponent(Element fwInfoEl) {
1664: String value = "NO"; // true, false?
1665:
1666: Element isCauseComponentEl = DOMUtil.UTIL.getElement(
1667: fwInfoEl, "is-cause-component");
1668: if (isCauseComponentEl != null) {
1669: value = DOMUtil.UTIL.getTextData(isCauseComponentEl);
1670: } else {
1671: value = "NO";
1672: }
1673: // anything otherthan yes. is no.
1674: return value.equalsIgnoreCase("YES");
1675: }
1676:
1677: /**
1678: * return true if cause if component else false
1679: * @param fwInfoEl xml element
1680: * @return true if cause if component else false
1681: */
1682: private boolean isCauseFramework(Element fwInfoEl) {
1683: String value = "NO"; // true, false?
1684:
1685: Element isCauseFrameworkEl = DOMUtil.UTIL.getElement(
1686: fwInfoEl, "is-cause-framework");
1687: if (isCauseFrameworkEl != null) {
1688: value = DOMUtil.UTIL.getTextData(isCauseFrameworkEl);
1689: } else {
1690: value = "NO";
1691: }
1692: // anything otherthan yes. is no.
1693: return value.equalsIgnoreCase("YES");
1694: }
1695:
1696: /**
1697: * return MessageInfo
1698: * @param msgInfoEl xml element.
1699: * @return MessageInfo
1700: */
1701: private MessageInfo getMessageInfo(Element msgInfoEl) {
1702: MessageInfo msgInfo = null;
1703: String id = UNKNOWN;
1704: String msg = null;
1705: List params = new ArrayList();
1706:
1707: Element idEl = DOMUtil.UTIL.getElement(msgInfoEl,
1708: "loc-token");
1709:
1710: if (idEl != null) {
1711: id = DOMUtil.UTIL.getTextData(idEl);
1712: }
1713:
1714: Element msgEl = DOMUtil.UTIL.getElement(msgInfoEl,
1715: "loc-message");
1716:
1717: if (msgEl != null) {
1718: msg = DOMUtil.UTIL.getTextData(msgEl);
1719: }
1720:
1721: NodeList paramElList = DOMUtil.UTIL.getChildElements(
1722: msgInfoEl, "loc-param");
1723: int size = paramElList.getLength();
1724: params = new ArrayList();
1725: for (int i = 0; i < size; ++i) {
1726: Element paramEl = (Element) paramElList.item(i);
1727: if (paramEl != null) {
1728: String param = DOMUtil.UTIL.getTextData(paramEl);
1729: params.add(param);
1730: }
1731: }
1732:
1733: return new MessageInfo(id, msg, params);
1734: }
1735:
1736: /**
1737: * returns ExceptionInfo
1738: * @param exInfoEl xml element.
1739: * @return ExceptionInfo
1740: */
1741: private ExceptionInfo getExceptionInfo(Element exInfoEl) {
1742:
1743: String level = "-1";
1744: MessageInfo msgInfo = null;
1745: String stacktrace = null;
1746:
1747: Element levelEl = DOMUtil.UTIL.getElement(exInfoEl,
1748: "nesting-level");
1749: if (levelEl != null) {
1750: level = DOMUtil.UTIL.getTextData(levelEl);
1751: }
1752:
1753: Element msgInfoEl = DOMUtil.UTIL.getElement(exInfoEl,
1754: "msg-loc-info");
1755: if (msgInfoEl != null) {
1756: msgInfo = getMessageInfo(msgInfoEl);
1757: }
1758:
1759: Element stacktraceEl = DOMUtil.UTIL.getElement(exInfoEl,
1760: "stack-trace");
1761: if (stacktraceEl != null) {
1762: stacktrace = DOMUtil.UTIL.getTextData(stacktraceEl);
1763: }
1764: return new ExceptionInfo(level, msgInfo, stacktrace);
1765: }
1766:
1767: /**
1768: * return TaskResultInfo
1769: * @param taskResultInfoEl xml element.
1770: * @return TaskResultInfo.
1771: */
1772: private TaskResultInfo getTaskResultInfo(
1773: Element taskResultInfoEl) {
1774:
1775: String id = "UNKNOWN";
1776: String result = "UNKNOWN";
1777: String msgType = "UNKNOWN";
1778: List msgInfoList = new ArrayList();
1779: List exInfoList = new ArrayList();
1780:
1781: Element idEl = DOMUtil.UTIL.getElement(taskResultInfoEl,
1782: "task-id");
1783: if (idEl != null) {
1784: id = DOMUtil.UTIL.getTextData(idEl);
1785: }
1786:
1787: Element resultEl = DOMUtil.UTIL.getElement(
1788: taskResultInfoEl, "task-result");
1789: if (resultEl != null) {
1790: result = DOMUtil.UTIL.getTextData(resultEl);
1791: }
1792:
1793: Element msgTypeEl = DOMUtil.UTIL.getElement(
1794: taskResultInfoEl, "message-type");
1795: if (msgTypeEl != null) {
1796: msgType = DOMUtil.UTIL.getTextData(msgTypeEl);
1797: }
1798:
1799: // Element msgInfoEl =
1800: // DOMUtil.UTIL.getElement(taskResultInfoEl, "task-status-msg");
1801: // if ( msgInfoEl != null )
1802: // {
1803: // statusMsgInfo = getMessageInfo(msgInfoEl);
1804: // }
1805: // get status msg list
1806:
1807: NodeList msgInfoElList = DOMUtil.UTIL.getChildElements(
1808: taskResultInfoEl, "task-status-msg");
1809: int msgInfoListSize = msgInfoElList.getLength();
1810: msgInfoList = new ArrayList();
1811: for (int i = 0; i < msgInfoListSize; ++i) {
1812: Element msgInfoEl = (Element) msgInfoElList.item(i);
1813: if (msgInfoEl != null) {
1814: MessageInfo statusMsgInfo = getMessageInfo(msgInfoEl);
1815: msgInfoList.add(statusMsgInfo);
1816: }
1817: }
1818:
1819: // get the exception info list
1820: NodeList exInfoElList = DOMUtil.UTIL.getChildElements(
1821: taskResultInfoEl, "exception-info");
1822: int exInfoListSize = exInfoElList.getLength();
1823: exInfoList = new ArrayList();
1824: for (int i = 0; i < exInfoListSize; ++i) {
1825: Element exInfoEl = (Element) exInfoElList.item(i);
1826: if (exInfoEl != null) {
1827: ExceptionInfo exInfo = getExceptionInfo(exInfoEl);
1828: exInfoList.add(exInfo);
1829: }
1830: }
1831:
1832: return new TaskResultInfo(id, result, msgType, msgInfoList,
1833: exInfoList);
1834: }
1835:
1836: /**
1837: * process the dom elements for the framework message and initilizes the
1838: * message object fields.
1839: * @param fwInfoEl framework message root element
1840: * @return FrameworkTaskResult object.
1841: */
1842: private FrameworkTaskResult getFrameworkTaskResult(
1843: Element fwInfoEl) {
1844: String locale = null;
1845: boolean isCuaseFramework = false;
1846: TaskResultInfo taskResultInfo = null;
1847:
1848: isCuaseFramework = isCauseFramework(fwInfoEl);
1849: locale = this .getFrameworkLocale(fwInfoEl);
1850:
1851: Element taskResultDetailsEl = DOMUtil.UTIL.getElement(
1852: fwInfoEl, "task-result-details");
1853: taskResultInfo = getTaskResultInfo(taskResultDetailsEl);
1854:
1855: return new JBIManagementMessage.FrameworkTaskResult(
1856: taskResultInfo, isCuaseFramework, locale);
1857: }
1858:
1859: /**
1860: * reads the dom tree for component messages and initializes the corresponding
1861: * fields of the Message object
1862: * @param compInfoEl component message info element
1863: * @return ComponentTaskResult
1864: */
1865: private ComponentTaskResult getComponentTaskResult(
1866: Element compInfoEl) {
1867: String compId = "UNKNOWN COMPONENT";
1868: TaskResultInfo taskResultInfo = null;
1869:
1870: Element compIdEl = DOMUtil.UTIL.getElement(compInfoEl,
1871: "component-name");
1872: if (compIdEl != null) {
1873: compId = DOMUtil.UTIL.getTextData(compIdEl);
1874: }
1875:
1876: Element taskResultDetailsEl = DOMUtil.UTIL.getElement(
1877: compInfoEl, "task-result-details");
1878: taskResultInfo = getTaskResultInfo(taskResultDetailsEl);
1879:
1880: return new JBIManagementMessage.ComponentTaskResult(
1881: taskResultInfo, compId);
1882:
1883: }
1884: }
1885:
1886: /**
1887: * This is the Exception takes the Manamagement Message object and prints the
1888: * stacktrace information in the message as part of the stacktrace of the
1889: * exception.
1890: */
1891: public static class JBIManagementMessageException extends Exception {
1892: /**
1893: * Message Object
1894: */
1895: JBIManagementMessage mMgmtMsg;
1896:
1897: /**
1898: * constructor that takes message Object
1899: * @param mgmtMsg message Object
1900: */
1901: public JBIManagementMessageException(
1902: JBIManagementMessage mgmtMsg) {
1903: this (mgmtMsg.getMessage(), mgmtMsg);
1904: }
1905:
1906: /**
1907: * constructor that takes custom message and the message object
1908: * @param msg custom message string
1909: * @param mgmtMsg message object
1910: */
1911: public JBIManagementMessageException(String msg,
1912: JBIManagementMessage mgmtMsg) {
1913: super (msg);
1914: this .mMgmtMsg = mgmtMsg;
1915: }
1916:
1917: /**
1918: * override method
1919: * @param s writer
1920: */
1921: public void printStackTrace(PrintWriter s) {
1922: this .mMgmtMsg.printStackTrace(s);
1923: super .printStackTrace(s);
1924: }
1925:
1926: /**
1927: * override method
1928: * @param s stream
1929: */
1930: public void printStackTrace(PrintStream s) {
1931: this .mMgmtMsg.printStackTrace(s);
1932: super .printStackTrace(s);
1933: }
1934:
1935: }
1936:
1937: /**
1938: * Build a single management message from the entries in the map. Each entry in the
1939: * map corresponds to a application variable name and the value is the JBI Management
1940: * message ( or exception message ) response on the target.
1941: *
1942: * @param taskId - task id
1943: * @param responseMap - a map of | application variable name | JBI Mgmt OR Exception
1944: * message |
1945: * @param requireAllSuccess
1946: * boolean flag indicating that all responses need to be SUCCESS for
1947: * the eventual task result to be SUCCESS.
1948: * @return a single management message based on the entries in the map.
1949: */
1950: public static JBIManagementMessage createJBIManagementMessage(
1951: String taskId, Map<String, String> responseMap,
1952: boolean requireAllSuccess) {
1953: /**
1954: * If the target is a cluster then the response message string could have
1955: * instance level details. The composite result will have all the instance level
1956: * details for an attribute combined with the application variable result
1957: * in it i.e. [{I1:v1,v2}, {I2:v1,v2}]
1958: */
1959: Set appVarNames = responseMap.keySet();
1960:
1961: List<JBIManagementMessage.MessageInfo> frmwkMessageInfos = new ArrayList();
1962: List<JBIManagementMessage.ExceptionInfo> frmwkExceptionInfos = new ArrayList();
1963:
1964: Map<String /* Instance Name */, List<JBIManagementMessage.MessageInfo/* Task Status Msgs for the instance */>> instanceMessageInfoMap = new HashMap();
1965: Map<String /* Instance Name */, List<JBIManagementMessage.ExceptionInfo/* ExceptionInfos for the instance */>> instanceExceptionInfoMap = new HashMap();
1966:
1967: List<Boolean> taskResults = new ArrayList();
1968: List<String> msgTypes = new ArrayList();
1969:
1970: for (Iterator itr = appVarNames.iterator(); itr.hasNext();) {
1971: /** Process each application variable result */
1972: String appVarName = (String) itr.next();
1973: String jbiXmlStr = responseMap.get(appVarName);
1974: JBIManagementMessage mgmtMsg = JBIManagementMessage
1975: .createJBIManagementMessage(jbiXmlStr);
1976: boolean instAppMsgInfoAdded = false;
1977: boolean fwkAppMsgInfoAdded = false;
1978: boolean instAppExpInfoAdded = false;
1979: boolean fwkAppExpInfoAdded = false;
1980:
1981: if (mgmtMsg != null) {
1982: taskResults.add(mgmtMsg.isSuccessMsg());
1983:
1984: JBIManagementMessage.TaskResultInfo taskResultInfo = mgmtMsg
1985: .getFrameworkTaskResult().getTaskResultInfo();
1986:
1987: msgTypes.add(taskResultInfo.getMessageType());
1988:
1989: List<JBIManagementMessage.MessageInfo> msgInfos = taskResultInfo
1990: .getStatusMessageInfoList();
1991:
1992: List<JBIManagementMessage.ExceptionInfo> exInfos = taskResultInfo
1993: .getExceptionInfoList();
1994:
1995: boolean processingInstanceMsgs = false;
1996: String instanceName = null;
1997: for (JBIManagementMessage.MessageInfo msgInfo : msgInfos) {
1998: // Collect all the framework level task status msgs, till
1999: // a instance marker is encountered. Once that is encountered
2000: // collect the instance task status msgs
2001: if (msgInfo.getI18nId().equals(
2002: ESBResultFormatter.INSTANCE_MARKER_ID)) {
2003: processingInstanceMsgs = true;
2004: instanceName = msgInfo.getLocalizedMsg();
2005: }
2006: if (processingInstanceMsgs) {
2007: List instMsgInfoList = instanceMessageInfoMap
2008: .get(instanceName);
2009:
2010: if (instMsgInfoList == null) {
2011: instMsgInfoList = new ArrayList<JBIManagementMessage.MessageInfo>();
2012: instanceMessageInfoMap.put(instanceName,
2013: instMsgInfoList);
2014: instMsgInfoList.add(msgInfo);
2015: continue;
2016:
2017: }
2018: // Add Attribute marker ( once at the beginning )
2019: if (!instAppMsgInfoAdded) {
2020: MessageInfo appVarInfo = createAppVarMarkerMsgInfo(appVarName);
2021: instMsgInfoList.add(appVarInfo);
2022: instAppMsgInfoAdded = true;
2023: }
2024: instMsgInfoList.add(msgInfo);
2025:
2026: } else {
2027: // framework result
2028: // Add Attribute marker ( once at the beginning )
2029: if (!fwkAppMsgInfoAdded) {
2030: MessageInfo appVarInfo = createAppVarMarkerMsgInfo(appVarName);
2031: frmwkMessageInfos.add(appVarInfo);
2032: fwkAppMsgInfoAdded = true;
2033: }
2034: frmwkMessageInfos.add(msgInfo);
2035: }
2036: }
2037:
2038: processingInstanceMsgs = false;
2039: instanceName = null;
2040: for (JBIManagementMessage.ExceptionInfo exInfo : exInfos) {
2041: // Collect all the framework level exceptions, till
2042: // a instance marker is encountered. Once that is encountered
2043: // collect the instance exception infos
2044: if (exInfo.getMessageInfo().getI18nId().equals(
2045: ESBResultFormatter.INSTANCE_MARKER_ID)) {
2046: processingInstanceMsgs = true;
2047: instanceName = exInfo.getLocalizedMsg();
2048: }
2049: if (processingInstanceMsgs) {
2050: List instExInfoList = instanceExceptionInfoMap
2051: .get(instanceName);
2052:
2053: if (instExInfoList == null) {
2054: instExInfoList = new ArrayList<JBIManagementMessage.ExceptionInfo>();
2055: instanceExceptionInfoMap.put(instanceName,
2056: instExInfoList);
2057: instExInfoList.add(exInfo);
2058: continue;
2059: }
2060: // Add Attribute marker ( once at the beginning )
2061: if (!instAppExpInfoAdded) {
2062: MessageInfo appVarInfo = createAppVarMarkerMsgInfo(appVarName);
2063: ExceptionInfo appVarExInfo = new ExceptionInfo(
2064: "0", appVarInfo, null);
2065: instExInfoList.add(appVarExInfo);
2066: instAppExpInfoAdded = true;
2067: }
2068: instExInfoList.add(exInfo);
2069: } else {
2070: // framework result
2071: // Add Attribute marker ( once at the beginning )
2072: if (!fwkAppExpInfoAdded) {
2073: MessageInfo appVarInfo = createAppVarMarkerMsgInfo(appVarName);
2074: ExceptionInfo appVarExInfo = new ExceptionInfo(
2075: "0", appVarInfo, null);
2076: frmwkExceptionInfos.add(appVarExInfo);
2077: fwkAppExpInfoAdded = true;
2078: }
2079: frmwkExceptionInfos.add(exInfo);
2080: }
2081: }
2082: } else {
2083: // Create a error response msg info for the operation
2084: taskResults.add(false);
2085: msgTypes.add(ERROR);
2086: MessageInfo appVarInfo = createAppVarMarkerMsgInfo(appVarName);
2087: frmwkMessageInfos.add(appVarInfo);
2088: MessageInfo exMsgInfo = new MessageInfo(
2089: JBIResultXmlBuilder.DEFAULT_MSG_CODE,
2090: jbiXmlStr, null);
2091: frmwkMessageInfos.add(exMsgInfo);
2092: }
2093: }
2094:
2095: boolean isSuccess = requireAllSuccess;
2096: for (Boolean sccss : taskResults) {
2097: if (!requireAllSuccess) {
2098: isSuccess |= sccss;
2099: } else {
2100: isSuccess &= sccss;
2101: }
2102: }
2103:
2104: String effectiveMsgType = INFO;
2105: if (msgTypes.contains(ERROR) || msgTypes.contains(WARNING)) {
2106: if (requireAllSuccess) {
2107: effectiveMsgType = (msgTypes.contains(ERROR) ? ERROR
2108: : (msgTypes.contains(WARNING) ? WARNING : INFO));
2109: } else {
2110: effectiveMsgType = (isSuccess ? WARNING : ERROR);
2111: }
2112: }
2113:
2114: // Create a single task status msg list
2115: Set instanceNames = instanceMessageInfoMap.keySet();
2116: for (Iterator itr = instanceNames.iterator(); itr.hasNext();) {
2117: String instName = (String) itr.next();
2118: frmwkMessageInfos.addAll(instanceMessageInfoMap
2119: .get(instName));
2120: }
2121:
2122: instanceNames = instanceExceptionInfoMap.keySet();
2123: for (Iterator itr = instanceNames.iterator(); itr.hasNext();) {
2124: String instName = (String) itr.next();
2125: frmwkExceptionInfos.addAll(instanceExceptionInfoMap
2126: .get(instName));
2127: }
2128:
2129: String taskResultStr = (isSuccess ? SUCCESS : FAILED);
2130: JBIManagementMessage.TaskResultInfo taskResultInfo = new JBIManagementMessage.TaskResultInfo(
2131: taskId, taskResultStr, effectiveMsgType,
2132: frmwkMessageInfos, frmwkExceptionInfos);
2133: JBIManagementMessage.FrameworkTaskResult taskResult = new JBIManagementMessage.FrameworkTaskResult(
2134: taskResultInfo, false, java.util.Locale.getDefault()
2135: .toString());
2136:
2137: JBIManagementMessage msg = new JBIManagementMessage();
2138: msg.setFrameworkTaskResult(taskResult);
2139: return msg;
2140: }
2141:
2142: /**
2143: * Create a Marker Application Variable Name message info
2144: *
2145: * @param appVarName
2146: * Application Variable Name
2147: */
2148: private static MessageInfo createAppVarMarkerMsgInfo(
2149: String appVarName) {
2150: return new MessageInfo(ESBResultFormatter.APP_VAR_MARKER_ID,
2151: appVarName, null);
2152: }
2153:
2154: }
|