001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)MessageHelper.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.message;
030:
031: import java.util.List;
032: import java.util.logging.Logger;
033: import java.util.logging.Level;
034:
035: import javax.xml.bind.JAXBContext;
036: import javax.xml.bind.Marshaller;
037: import javax.xml.bind.Unmarshaller;
038: import javax.xml.transform.stream.StreamSource;
039:
040: import com.sun.jbi.StringTranslator;
041: import com.sun.jbi.EnvironmentContext;
042: import com.sun.jbi.management.LocalStringKeys;
043: import com.sun.jbi.management.system.ManagementException;
044:
045: /**
046: * Helper class to format Management XML messages for logging
047: */
048:
049: public class MessageHelper {
050:
051: private static JAXBContext mJaxbContext = null;
052: private static Marshaller mWriter;
053: private static Unmarshaller mReader;
054:
055: private static String TAB = "\t";
056: private static String NEWLINE = System
057: .getProperty("line.separator");
058: private static String SPACE = " ";
059: private static String COLON = ":";
060:
061: /**
062: * This method is used to extract a message in the
063: * given exception. If the exception contains a XML string
064: * then it is parsed to look for the real message.
065: * @param Exception
066: * @return the message in the exception
067: */
068: public static String getMsgString(Exception ex) {
069: String message = ex.getMessage();
070: if (!MessageBuilder.isXmlString(message)) {
071: return message;
072: } else {
073: return extractMessageFromXML(message, null);
074: }
075: }
076:
077: /**
078: * This method is used to extract a message in the
079: * given exception. If the exception contains a XML string
080: * then it is parsed to look for the real message.
081: * @param Exception
082: * @param logLevel the logLevel, if the log level is fine or lower the
083: * stack trace will be printed
084: * @return the message in the exception
085: */
086: public static String getMsgString(Exception ex, Level logLevel) {
087: String message = ex.getMessage();
088: if (!MessageBuilder.isXmlString(message)) {
089: return message;
090: } else {
091: return extractMessageFromXML(message, logLevel);
092: }
093: }
094:
095: /**
096: * This method is used to extract the message from a given
097: * XML string
098: * @param the XML string
099: * @param logLevel the logLevel, if the log level is fine or lower the
100: * stack trace will be printed
101: * @return the message in the XML
102: */
103: public static String extractMessageFromXML(String xmlString,
104: Level logLevel) {
105: StringBuffer messageContent = new StringBuffer();
106: Logger mLog = null;
107: try {
108: mLog = Logger.getLogger("com.sun.jbi.management");
109:
110: StringBuffer strBuf = new StringBuffer(xmlString);
111: JbiTask jbiTask = (JbiTask) unmarshal(new StreamSource(
112: new java.io.StringReader(strBuf.toString())));
113: JbiTaskResultElement taskResult = jbiTask
114: .getJbiTaskResult();
115: messageContent.append(NEWLINE);
116: messageContent.append(getFrameWorkMessages(jbiTask,
117: logLevel));
118: messageContent.append(getComponentMessages(jbiTask,
119: logLevel));
120:
121: return messageContent.toString();
122:
123: } catch (Exception ex) {
124: mLog.warning(ex.getMessage());
125: return xmlString;
126: }
127:
128: }
129:
130: /**
131: * This method is used to get the framework task result details
132: * messges from a JbiTask object
133: * @param jbiTask jbiTask
134: * @param logLevel the logLevel, if the log level is fine or lower the
135: * stack trace will be printed
136: * @return the formatted message
137: */
138: private static String getFrameWorkMessages(JbiTask jbiTask,
139: Level logLevel) throws Exception {
140: StringBuffer result = new StringBuffer();
141:
142: JbiTaskResultElement taskResult = jbiTask.getJbiTaskResult();
143: if (taskResult == null) {
144: return result.toString();
145: }
146: FrmwkTaskResult frmwkTaskResult = taskResult
147: .getFrmwkTaskResult();
148: if (frmwkTaskResult == null) {
149: return result.toString();
150: }
151: TaskResultDetails taskResultDetails = frmwkTaskResult
152: .getFrmwkTaskResultDetails();
153:
154: if (taskResultDetails == null) {
155: return result.toString();
156: }
157:
158: TaskResultDetailsElement taskResultDetailsElement = taskResultDetails
159: .getTaskResultDetails();
160:
161: if (taskResultDetailsElement == null) {
162: return result.toString();
163: }
164:
165: String taskId = taskResultDetailsElement.getTaskId();
166: String taskResultString = taskResultDetailsElement
167: .getTaskResult();
168:
169: if (taskId != null) {
170: result.append(getLocalizedString(LocalStringKeys.TASK_ID)
171: + SPACE + taskId + SPACE);
172: }
173: if (taskResultString != null) {
174: result.append(taskResultString.toLowerCase());
175: }
176: result.append(NEWLINE);
177:
178: String taskStatusMessages = getTaskStatusMessages(taskResultDetailsElement);
179: if (taskStatusMessages != null
180: && taskStatusMessages.length() > 0) {
181:
182: String messageType = taskResultDetailsElement
183: .getMessageType();
184: if (messageType != null) {
185: result.append(format(messageType + COLON + SPACE, 1));
186: }
187: result.append(NEWLINE);
188: result.append(format(taskStatusMessages, 1));
189: result.append(NEWLINE);
190: }
191:
192: String exceptionMessages = getExceptionMessages(
193: taskResultDetailsElement, logLevel);
194: if (exceptionMessages != null && exceptionMessages.length() > 0) {
195: result.append(format(
196: getLocalizedString(LocalStringKeys.EXCEPTION_INFO)
197: + COLON, 2));
198: result.append(NEWLINE);
199: result.append(format(exceptionMessages, 3));
200: result.append(NEWLINE);
201: }
202: return result.toString();
203: }
204:
205: /**
206: * This method is used to extract the framework task status messages from
207: * framework task result details element
208: * @param taskResultDetailsElement
209: * @return the formatted message
210: */
211: public static String getTaskStatusMessages(
212: TaskResultDetailsElement taskResultDetailsElement)
213: throws Exception {
214: StringBuffer result = new StringBuffer();
215:
216: if (taskResultDetailsElement == null) {
217: return result.toString();
218: }
219: List<TaskStatusMsg> taskStatusMsgs = taskResultDetailsElement
220: .getTaskStatusMsg();
221: if (taskStatusMsgs != null) {
222: for (TaskStatusMsg taskStatusMsg : taskStatusMsgs) {
223: MsgLocInfo msgLoc = taskStatusMsg.getMsgLocInfo();
224: if (msgLoc != null) {
225: if (result != null && result.length() > 0) {
226: result.append(NEWLINE);
227: }
228: result.append(msgLoc.getLocMessage());
229: }
230: }
231: }
232:
233: return result.toString();
234: }
235:
236: /**
237: * This method is used to get the framework exception messages
238: * messges from a framework task result details element
239: * @param taskResultDetailsElement
240: * @param logLevel the logLevel, if the log level is fine or lower the
241: * stack trace will be printed
242: * @return the formatted message
243: */
244: private static String getExceptionMessages(
245: TaskResultDetailsElement taskResultDetailsElement,
246: Level logLevel) throws Exception {
247: StringBuffer result = new StringBuffer();
248: if (taskResultDetailsElement == null) {
249: return result.toString();
250: }
251:
252: List<ExceptionInfo> exceptionsInfo = taskResultDetailsElement
253: .getExceptionInfo();
254: if (exceptionsInfo != null) {
255: for (ExceptionInfo exceptionInfo : exceptionsInfo) {
256: MsgLocInfo msgLoc = exceptionInfo.getMsgLocInfo();
257: if (msgLoc != null) {
258: if (result != null && result.length() > 0) {
259: result.append(NEWLINE);
260: }
261: result.append(msgLoc.getLocMessage());
262:
263: //if the log level is fine or below print the stack trace
264: if (logLevel != null
265: && logLevel.intValue() <= Level.FINE
266: .intValue()) {
267: result.append(NEWLINE);
268: result.append(exceptionInfo.getStackTrace());
269: }
270: }
271: }
272: }
273:
274: return result.toString();
275: }
276:
277: /**
278: * This method is used to get the framework task result details
279: * messges from a JbiTask object
280: * @param jbiTask
281: * @param logLevel the logLevel, if the log level is fine or lower the
282: * stack trace will be printed
283: * @return the formatted message
284: */
285: private static String getComponentMessages(JbiTask jbiTask,
286: Level logLevel) throws Exception {
287: StringBuffer result = new StringBuffer();
288: JbiTaskResultElement taskResult = jbiTask.getJbiTaskResult();
289: if (taskResult == null) {
290: return result.toString();
291: }
292: List<ComponentTaskResult> compTaskResults = taskResult
293: .getComponentTaskResult();
294: for (ComponentTaskResult compTaskResult : compTaskResults) {
295: if (compTaskResult != null) {
296: TaskResultDetails compTaskResultDetails = compTaskResult
297: .getComponentTaskResultDetails();
298:
299: if (compTaskResultDetails != null) {
300: TaskResultDetailsElement taskResultDetailsElement = compTaskResultDetails
301: .getTaskResultDetails();
302:
303: if (taskResultDetailsElement != null) {
304: String taskId = taskResultDetailsElement
305: .getTaskId();
306: String taskResultString = taskResultDetailsElement
307: .getTaskResult();
308: String compName = compTaskResult
309: .getComponentName();
310: if (compName != null) {
311: result
312: .append(format(
313: getLocalizedString(LocalStringKeys.COMPONENT_NAME)
314: + COLON
315: + SPACE
316: + compName, 3));
317: result.append(NEWLINE);
318: }
319: if (taskId != null) {
320: result
321: .append(format(
322: getLocalizedString(LocalStringKeys.TASK_ID)
323: + SPACE
324: + taskId
325: + SPACE, 4));
326: }
327: if (taskResultString != null) {
328: result.append(taskResultString
329: .toLowerCase());
330: }
331:
332: result.append(NEWLINE);
333: String messageType = taskResultDetailsElement
334: .getMessageType();
335: if (messageType != null) {
336: result.append(format(messageType + COLON
337: + SPACE, 5));
338: }
339: result.append(NEWLINE);
340:
341: String taskStatusMessages = getTaskStatusMessages(taskResultDetailsElement);
342: if (taskStatusMessages != null
343: && taskStatusMessages.length() > 0) {
344: result
345: .append(format(taskStatusMessages,
346: 5));
347: result.append(NEWLINE);
348: }
349:
350: String exceptionMessages = getExceptionMessages(
351: taskResultDetailsElement, logLevel);
352: if (exceptionMessages != null
353: && exceptionMessages.length() > 0) {
354: result
355: .append(format(
356: getLocalizedString(LocalStringKeys.EXCEPTION_INFO)
357: + COLON, 5));
358: result.append(NEWLINE);
359: result.append(format(exceptionMessages, 6));
360: result.append(NEWLINE);
361: }
362: }
363: }
364: }
365: }
366: return result.toString();
367: }
368:
369: /**
370: * This method is used to format a given string using indentations
371: * @param message the string to be formatted
372: * @param indentation the number of tabs to be put before every line
373: * @return the formatted message
374: */
375: private static String format(String message, int indentationlevel) {
376: StringBuffer buffer = new StringBuffer();
377: for (int i = 0; i < indentationlevel; i++) {
378: buffer.append(TAB);
379: }
380: for (int i = 0; i < message.length(); i++) {
381: buffer.append(message.charAt(i));
382: if (message.charAt(i) == '\n') {
383: for (int j = 0; j < indentationlevel; j++) {
384: buffer.append(TAB);
385: }
386: }
387:
388: }
389: return buffer.toString();
390: }
391:
392: /**
393: * This method is used to return localized messages that
394: * correspond to the given key
395: * @param key the key
396: * @return the message
397: */
398: private static String getLocalizedString(String key) {
399: StringTranslator mTranslator = com.sun.jbi.util.EnvironmentAccess
400: .getContext().getStringTranslator(
401: "com.sun.jbi.management");
402: return mTranslator.getString(key);
403: }
404:
405: /**
406: * Provides a reference to the JAXB context for management messages,
407: * initializing one if necessary.
408: * @throws Excepion if the JAXB Context cannot be initialized
409: */
410: private static synchronized JAXBContext getJaxbContext()
411: throws Exception {
412: if (mJaxbContext == null) {
413: ClassLoader cl = Class.forName(
414: "com.sun.jbi.management.message.JbiTaskResult")
415: .getClassLoader();
416: mJaxbContext = JAXBContext.newInstance(
417: "com.sun.jbi.management.message", cl);
418: }
419:
420: return mJaxbContext;
421: }
422:
423: /**
424: * Synchronizes access to the non thread-safe Marshaller for this context.
425: */
426: private static synchronized void marshal(Object jaxbElement,
427: java.io.Writer writer) throws Exception {
428: if (mWriter == null) {
429: mWriter = getJaxbContext().createMarshaller();
430: }
431:
432: mWriter.marshal(jaxbElement, writer);
433: }
434:
435: /**
436: * Synchronizes access to the non thread-safe Unmarshaller for this context.
437: */
438: private static synchronized Object unmarshal(StreamSource input)
439: throws Exception {
440: if (mReader == null) {
441: mReader = getJaxbContext().createUnmarshaller();
442: }
443:
444: return mReader.unmarshal(input);
445: }
446:
447: }
|