KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > ManagementSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.framework;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.jbi.util.DOMUtil;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.jbi.management.DeploymentException;
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34
35 /**
36  * ManagementMessageHelper is a class that ease the building of management messages.
37  */

38 public class ManagementSupport {
39     
40     private static final Log logger = LogFactory.getLog(ManagementSupport.class);
41
42     public static class Message {
43         private boolean isCauseFramework;
44         private String JavaDoc task;
45         private String JavaDoc result;
46         private Exception JavaDoc exception;
47         private String JavaDoc type;
48         private String JavaDoc message;
49         private String JavaDoc component;
50         private String JavaDoc locale;
51         
52         public Exception JavaDoc getException() {
53             return exception;
54         }
55         public void setException(Exception JavaDoc exception) {
56             this.exception = exception;
57         }
58         public boolean isCauseFramework() {
59             return isCauseFramework;
60         }
61         public void setCauseFramework(boolean isCauseFramework) {
62             this.isCauseFramework = isCauseFramework;
63         }
64         public String JavaDoc getMessage() {
65             return message;
66         }
67         public void setMessage(String JavaDoc message) {
68             this.message = message;
69         }
70         public String JavaDoc getResult() {
71             return result;
72         }
73         public void setResult(String JavaDoc result) {
74             this.result = result;
75         }
76         public String JavaDoc getTask() {
77             return task;
78         }
79         public void setTask(String JavaDoc task) {
80             this.task = task;
81         }
82         public String JavaDoc getType() {
83             return type;
84         }
85         public void setType(String JavaDoc type) {
86             this.type = type;
87         }
88         public String JavaDoc getComponent() {
89             return component;
90         }
91         public void setComponent(String JavaDoc component) {
92             this.component = component;
93         }
94         public String JavaDoc getLocale() {
95             return locale;
96         }
97         public void setLocale(String JavaDoc locale) {
98             this.locale = locale;
99         }
100     }
101     
102     public static Exception JavaDoc failure(String JavaDoc task, String JavaDoc info) throws Exception JavaDoc {
103         return failure(task, info, null, null);
104     }
105     
106     public static Exception JavaDoc failure(String JavaDoc task, List JavaDoc componentResults) throws Exception JavaDoc {
107         return failure(task, null, null, componentResults);
108     }
109     
110     public static Exception JavaDoc failure(String JavaDoc task, String JavaDoc info, Exception JavaDoc e) throws Exception JavaDoc {
111         return failure(task, info, e, null);
112     }
113     
114     public static Exception JavaDoc failure(String JavaDoc task, String JavaDoc info, Exception JavaDoc e, List JavaDoc componentResults) throws Exception JavaDoc {
115         ManagementSupport.Message msg = new ManagementSupport.Message();
116         msg.setTask(task);
117         msg.setResult("FAILED");
118         msg.setType("ERROR");
119         msg.setException(e);
120         msg.setMessage(info);
121         return new Exception JavaDoc(ManagementSupport.createFrameworkMessage(msg, componentResults));
122     }
123
124     public static String JavaDoc createSuccessMessage(String JavaDoc task) {
125         return createSuccessMessage(task, null, null);
126     }
127     
128     public static String JavaDoc createSuccessMessage(String JavaDoc task, List JavaDoc componentResults) {
129         return createSuccessMessage(task, null, componentResults);
130     }
131     
132     public static String JavaDoc createSuccessMessage(String JavaDoc task, String JavaDoc info) {
133         return createSuccessMessage(task, info, null);
134     }
135
136     public static String JavaDoc createSuccessMessage(String JavaDoc task, String JavaDoc info, List JavaDoc componentResults) {
137         ManagementSupport.Message msg = new ManagementSupport.Message();
138         msg.setTask(task);
139         msg.setResult("SUCCESS");
140         msg.setMessage(info);
141         return ManagementSupport.createFrameworkMessage(msg, componentResults);
142     }
143     
144     public static String JavaDoc createWarningMessage(String JavaDoc task, String JavaDoc info, List JavaDoc componentResults) {
145         ManagementSupport.Message msg = new ManagementSupport.Message();
146         msg.setTask(task);
147         msg.setResult("SUCCESS");
148         msg.setType("WARNING");
149         msg.setMessage(info);
150         return ManagementSupport.createFrameworkMessage(msg, componentResults);
151     }
152
153     public static String JavaDoc createFrameworkMessage(Message fmkMsg, List JavaDoc componentResults) {
154         try {
155             Document JavaDoc doc = createDocument();
156             Element JavaDoc jbiTask = createChild(doc, "jbi-task");
157             jbiTask.setAttribute("xmlns", "http://java.sun.com/xml/ns/jbi/management-message");
158             jbiTask.setAttribute("version", "1.0");
159             Element JavaDoc jbiTaskResult = createChild(jbiTask, "jbi-task-result");
160             Element JavaDoc frmkTaskResult = createChild(jbiTaskResult, "frmwk-task-result");
161             Element JavaDoc frmkTaskResultDetails = createChild(frmkTaskResult, "frmwk-task-result-details");
162             appendTaskResultDetails(frmkTaskResultDetails, fmkMsg);
163             if (fmkMsg.getLocale() != null) {
164                 createChild(frmkTaskResult, "locale", fmkMsg.getLocale());
165             }
166             if (componentResults != null) {
167                 for (Iterator JavaDoc iter = componentResults.iterator(); iter.hasNext();) {
168                     Element JavaDoc element = (Element JavaDoc) iter.next();
169                     jbiTaskResult.appendChild(doc.importNode(element, true));
170                 }
171             }
172             return DOMUtil.asIndentedXML(doc);
173         } catch (Exception JavaDoc e) {
174             logger.error("Error", e);
175             return null;
176         }
177     }
178     
179     private static Document JavaDoc createDocument() {
180         try {
181             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
182             factory.setNamespaceAware(true);
183             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
184             return builder.newDocument();
185         } catch (Exception JavaDoc e) {
186             throw new RuntimeException JavaDoc("Could not create DOM document", e);
187         }
188     }
189     
190     private static Element JavaDoc createChild(Node JavaDoc parent, String JavaDoc name) {
191         return createChild(parent, name, null);
192     }
193     
194     private static Element JavaDoc createChild(Node JavaDoc parent, String JavaDoc name, String JavaDoc text) {
195         Document JavaDoc doc = parent instanceof Document JavaDoc ? (Document JavaDoc) parent : parent.getOwnerDocument();
196         Element JavaDoc child = doc.createElementNS("http://java.sun.com/xml/ns/jbi/management-message", name);
197         if (text != null) {
198             child.appendChild(doc.createTextNode(text));
199         }
200         parent.appendChild(child);
201         return child;
202     }
203     
204     private static void appendTaskResultDetails(Element JavaDoc root, Message fmkMsg) {
205         Element JavaDoc taskResultDetails = createChild(root, "task-result-details");
206         createChild(taskResultDetails, "task-id", fmkMsg.getTask());
207         createChild(taskResultDetails, "task-result", fmkMsg.getResult());
208         if (fmkMsg.getType() != null) {
209             createChild(taskResultDetails, "message-type", fmkMsg.getType());
210         }
211         // task-status-message
212
if (fmkMsg.getMessage() != null) {
213             Element JavaDoc taskStatusMessage = createChild(taskResultDetails, "task-status-msg");
214             Element JavaDoc msgLocInfo = createChild(taskStatusMessage, "msg-loc-info");
215             createChild(msgLocInfo, "loc-token");
216             createChild(msgLocInfo, "loc-message", fmkMsg.getMessage());
217         }
218         // exception-info
219
if (fmkMsg.getException() != null) {
220             Element JavaDoc exceptionInfo = createChild(taskResultDetails, "exception-info");
221             createChild(exceptionInfo, "nesting-level", "1");
222             createChild(exceptionInfo, "loc-token");
223             createChild(exceptionInfo, "loc-message", fmkMsg.getException().getMessage());
224             Element JavaDoc stackTrace = createChild(exceptionInfo, "stack-trace");
225             StringWriter JavaDoc sw2 = new StringWriter JavaDoc();
226             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw2);
227             fmkMsg.getException().printStackTrace(pw);
228             pw.close();
229             stackTrace.appendChild(root.getOwnerDocument().createCDATASection(sw2.toString()));
230         }
231     }
232     
233     public static DeploymentException componentFailure(String JavaDoc task, String JavaDoc component, String JavaDoc info) {
234         try {
235             Element JavaDoc e = createComponentFailure(task, component, info, null);
236             return new DeploymentException(DOMUtil.asXML(e));
237         } catch (Exception JavaDoc e) {
238             if (logger.isDebugEnabled()) {
239                 logger.debug("Error creating management message", e);
240             }
241             return new DeploymentException(info);
242         }
243     }
244     
245     public static Element JavaDoc createComponentMessage(Message msg) {
246         Document JavaDoc doc = createDocument();
247         Element JavaDoc componentTaskResult = createChild(doc, "component-task-result");
248         createChild(componentTaskResult, "component-name", msg.getComponent());
249         Element JavaDoc componentTaskResultDetails = createChild(componentTaskResult, "component-task-result-details");
250         appendTaskResultDetails(componentTaskResultDetails, msg);
251         return componentTaskResult;
252     }
253     
254     public static Element JavaDoc createComponentSuccess(String JavaDoc task, String JavaDoc component) {
255         ManagementSupport.Message msg = new ManagementSupport.Message();
256         msg.setTask(task);
257         msg.setResult("SUCCESS");
258         msg.setComponent(component);
259         return createComponentMessage(msg);
260     }
261     
262     public static Element JavaDoc createComponentFailure(String JavaDoc task, String JavaDoc component, String JavaDoc info, Exception JavaDoc e) {
263         ManagementSupport.Message msg = new ManagementSupport.Message();
264         msg.setTask(task);
265         msg.setResult("FAILED");
266         msg.setType("ERROR");
267         msg.setException(e);
268         msg.setMessage(info);
269         msg.setComponent(component);
270         return createComponentMessage(msg);
271     }
272     
273     public static Element JavaDoc createComponentWarning(String JavaDoc task, String JavaDoc component, String JavaDoc info, Exception JavaDoc e) {
274         ManagementSupport.Message msg = new ManagementSupport.Message();
275         msg.setTask(task);
276         msg.setResult("SUCCESS");
277         msg.setType("WARNING");
278         msg.setException(e);
279         msg.setMessage(info);
280         msg.setComponent(component);
281         return createComponentMessage(msg);
282     }
283     
284 }
285
Popular Tags