001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.jbi.framework;
018:
019: import java.io.PrintWriter;
020: import java.io.StringWriter;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.jbi.management.DeploymentException;
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027:
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.servicemix.jbi.util.DOMUtil;
035:
036: /**
037: * ManagementMessageHelper is a class that ease the building of management messages.
038: */
039: public final class ManagementSupport {
040:
041: private static final Log LOG = LogFactory
042: .getLog(ManagementSupport.class);
043:
044: private ManagementSupport() {
045: }
046:
047: public static class Message {
048: private boolean isCauseFramework;
049: private String task;
050: private String result;
051: private Exception exception;
052: private String type;
053: private String message;
054: private String component;
055: private String locale;
056:
057: public Exception getException() {
058: return exception;
059: }
060:
061: public void setException(Exception exception) {
062: this .exception = exception;
063: }
064:
065: public boolean isCauseFramework() {
066: return isCauseFramework;
067: }
068:
069: public void setCauseFramework(boolean value) {
070: this .isCauseFramework = value;
071: }
072:
073: public String getMessage() {
074: return message;
075: }
076:
077: public void setMessage(String message) {
078: this .message = message;
079: }
080:
081: public String getResult() {
082: return result;
083: }
084:
085: public void setResult(String result) {
086: this .result = result;
087: }
088:
089: public String getTask() {
090: return task;
091: }
092:
093: public void setTask(String task) {
094: this .task = task;
095: }
096:
097: public String getType() {
098: return type;
099: }
100:
101: public void setType(String type) {
102: this .type = type;
103: }
104:
105: public String getComponent() {
106: return component;
107: }
108:
109: public void setComponent(String component) {
110: this .component = component;
111: }
112:
113: public String getLocale() {
114: return locale;
115: }
116:
117: public void setLocale(String locale) {
118: this .locale = locale;
119: }
120: }
121:
122: public static Exception failure(String task, String info)
123: throws Exception {
124: return failure(task, info, null, null);
125: }
126:
127: public static Exception failure(String task, List componentResults)
128: throws Exception {
129: return failure(task, null, null, componentResults);
130: }
131:
132: public static Exception failure(String task, String info,
133: Exception e) throws Exception {
134: return failure(task, info, e, null);
135: }
136:
137: public static Exception failure(String task, String info,
138: Exception e, List componentResults) throws Exception {
139: ManagementSupport.Message msg = new ManagementSupport.Message();
140: msg.setTask(task);
141: msg.setResult("FAILED");
142: msg.setType("ERROR");
143: msg.setException(e);
144: msg.setMessage(info);
145: return new Exception(ManagementSupport.createFrameworkMessage(
146: msg, componentResults));
147: }
148:
149: public static String createSuccessMessage(String task) {
150: return createSuccessMessage(task, null, null);
151: }
152:
153: public static String createSuccessMessage(String task,
154: List componentResults) {
155: return createSuccessMessage(task, null, componentResults);
156: }
157:
158: public static String createSuccessMessage(String task, String info) {
159: return createSuccessMessage(task, info, null);
160: }
161:
162: public static String createSuccessMessage(String task, String info,
163: List componentResults) {
164: ManagementSupport.Message msg = new ManagementSupport.Message();
165: msg.setTask(task);
166: msg.setResult("SUCCESS");
167: msg.setMessage(info);
168: return ManagementSupport.createFrameworkMessage(msg,
169: componentResults);
170: }
171:
172: public static String createWarningMessage(String task, String info,
173: List componentResults) {
174: ManagementSupport.Message msg = new ManagementSupport.Message();
175: msg.setTask(task);
176: msg.setResult("SUCCESS");
177: msg.setType("WARNING");
178: msg.setMessage(info);
179: return ManagementSupport.createFrameworkMessage(msg,
180: componentResults);
181: }
182:
183: public static String createFrameworkMessage(Message fmkMsg,
184: List componentResults) {
185: try {
186: Document doc = createDocument();
187: Element jbiTask = createChild(doc, "jbi-task");
188: jbiTask
189: .setAttribute("xmlns",
190: "http://java.sun.com/xml/ns/jbi/management-message");
191: jbiTask.setAttribute("version", "1.0");
192: Element jbiTaskResult = createChild(jbiTask,
193: "jbi-task-result");
194: Element frmkTaskResult = createChild(jbiTaskResult,
195: "frmwk-task-result");
196: Element frmkTaskResultDetails = createChild(frmkTaskResult,
197: "frmwk-task-result-details");
198: appendTaskResultDetails(frmkTaskResultDetails, fmkMsg);
199: if (fmkMsg.getLocale() != null) {
200: createChild(frmkTaskResult, "locale", fmkMsg
201: .getLocale());
202: }
203: if (componentResults != null) {
204: for (Iterator iter = componentResults.iterator(); iter
205: .hasNext();) {
206: Element element = (Element) iter.next();
207: jbiTaskResult.appendChild(doc.importNode(element,
208: true));
209: }
210: }
211: return DOMUtil.asIndentedXML(doc);
212: } catch (Exception e) {
213: LOG.error("Error", e);
214: return null;
215: }
216: }
217:
218: private static Document createDocument() {
219: try {
220: DocumentBuilderFactory factory = DocumentBuilderFactory
221: .newInstance();
222: factory.setNamespaceAware(true);
223: DocumentBuilder builder = factory.newDocumentBuilder();
224: return builder.newDocument();
225: } catch (Exception e) {
226: throw new RuntimeException("Could not create DOM document",
227: e);
228: }
229: }
230:
231: private static Element createChild(Node parent, String name) {
232: return createChild(parent, name, null);
233: }
234:
235: private static Element createChild(Node parent, String name,
236: String text) {
237: Document doc = parent instanceof Document ? (Document) parent
238: : parent.getOwnerDocument();
239: Element child = doc.createElementNS(
240: "http://java.sun.com/xml/ns/jbi/management-message",
241: name);
242: if (text != null) {
243: child.appendChild(doc.createTextNode(text));
244: }
245: parent.appendChild(child);
246: return child;
247: }
248:
249: private static void appendTaskResultDetails(Element root,
250: Message fmkMsg) {
251: Element taskResultDetails = createChild(root,
252: "task-result-details");
253: createChild(taskResultDetails, "task-id", fmkMsg.getTask());
254: createChild(taskResultDetails, "task-result", fmkMsg
255: .getResult());
256: if (fmkMsg.getType() != null) {
257: createChild(taskResultDetails, "message-type", fmkMsg
258: .getType());
259: }
260: // task-status-message
261: if (fmkMsg.getMessage() != null) {
262: Element taskStatusMessage = createChild(taskResultDetails,
263: "task-status-msg");
264: Element msgLocInfo = createChild(taskStatusMessage,
265: "msg-loc-info");
266: createChild(msgLocInfo, "loc-token");
267: createChild(msgLocInfo, "loc-message", fmkMsg.getMessage());
268: }
269: // exception-info
270: if (fmkMsg.getException() != null) {
271: Element exceptionInfo = createChild(taskResultDetails,
272: "exception-info");
273: createChild(exceptionInfo, "nesting-level", "1");
274: createChild(exceptionInfo, "loc-token");
275: createChild(exceptionInfo, "loc-message", fmkMsg
276: .getException().getMessage());
277: Element stackTrace = createChild(exceptionInfo,
278: "stack-trace");
279: StringWriter sw2 = new StringWriter();
280: PrintWriter pw = new PrintWriter(sw2);
281: fmkMsg.getException().printStackTrace(pw);
282: pw.close();
283: stackTrace.appendChild(root.getOwnerDocument()
284: .createCDATASection(sw2.toString()));
285: }
286: }
287:
288: public static DeploymentException componentFailure(String task,
289: String component, String info) {
290: try {
291: Element e = createComponentFailure(task, component, info,
292: null);
293: return new DeploymentException(DOMUtil.asXML(e));
294: } catch (Exception e) {
295: if (LOG.isDebugEnabled()) {
296: LOG.debug("Error creating management message", e);
297: }
298: return new DeploymentException(info);
299: }
300: }
301:
302: public static Element createComponentMessage(Message msg) {
303: Document doc = createDocument();
304: Element componentTaskResult = createChild(doc,
305: "component-task-result");
306: createChild(componentTaskResult, "component-name", msg
307: .getComponent());
308: Element componentTaskResultDetails = createChild(
309: componentTaskResult, "component-task-result-details");
310: appendTaskResultDetails(componentTaskResultDetails, msg);
311: return componentTaskResult;
312: }
313:
314: public static Element createComponentSuccess(String task,
315: String component) {
316: ManagementSupport.Message msg = new ManagementSupport.Message();
317: msg.setTask(task);
318: msg.setResult("SUCCESS");
319: msg.setComponent(component);
320: return createComponentMessage(msg);
321: }
322:
323: public static Element createComponentFailure(String task,
324: String component, String info, Exception e) {
325: ManagementSupport.Message msg = new ManagementSupport.Message();
326: msg.setTask(task);
327: msg.setResult("FAILED");
328: msg.setType("ERROR");
329: msg.setException(e);
330: msg.setMessage(info);
331: msg.setComponent(component);
332: return createComponentMessage(msg);
333: }
334:
335: public static Element createComponentWarning(String task,
336: String component, String info, Exception e) {
337: ManagementSupport.Message msg = new ManagementSupport.Message();
338: msg.setTask(task);
339: msg.setResult("SUCCESS");
340: msg.setType("WARNING");
341: msg.setException(e);
342: msg.setMessage(info);
343: msg.setComponent(component);
344: return createComponentMessage(msg);
345: }
346:
347: }
|