KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > 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.common;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24
25 /**
26  * ManagementMessageHelper is a class that ease the building of management messages.
27  */

28 public class ManagementSupport {
29     
30     private static final Log logger = LogFactory.getLog(ManagementSupport.class);
31
32     public static class Message {
33         private String JavaDoc task;
34         private String JavaDoc component;
35         private String JavaDoc result;
36         private Throwable JavaDoc exception;
37         private String JavaDoc type;
38         private String JavaDoc message;
39         
40         public String JavaDoc getComponent() {
41             return component;
42         }
43         public void setComponent(String JavaDoc component) {
44             this.component = component;
45         }
46         public Throwable JavaDoc getException() {
47             return exception;
48         }
49         public void setException(Throwable JavaDoc exception) {
50             this.exception = exception;
51         }
52         public String JavaDoc getResult() {
53             return result;
54         }
55         public void setResult(String JavaDoc result) {
56             this.result = result;
57         }
58         public String JavaDoc getTask() {
59             return task;
60         }
61         public void setTask(String JavaDoc task) {
62             this.task = task;
63         }
64         public String JavaDoc getType() {
65             return type;
66         }
67         public void setType(String JavaDoc type) {
68             this.type = type;
69         }
70         public String JavaDoc getMessage() {
71             return message;
72         }
73         public void setMessage(String JavaDoc message) {
74             this.message = message;
75         }
76     }
77     
78     public static String JavaDoc createComponentMessage(Message msg) {
79         try {
80             StringBuffer JavaDoc sw = new StringBuffer JavaDoc();
81             // component-task-result
82
sw.append("<component-task-result ");
83             sw.append("xmlns=\"http://java.sun.com/xml/ns/jbi/management-message\">");
84             sw.append("\n\t");
85             // component-name
86
sw.append("<component-name>");
87             sw.append(msg.getComponent());
88             sw.append("</component-name>");
89             // component-task-result-details
90
sw.append("\n\t");
91             sw.append("<component-task-result-details>");
92             // task-result-details
93
sw.append("\n\t\t");
94             sw.append("<task-result-details>");
95             // task-id
96
sw.append("\n\t\t\t");
97             sw.append("<task-id>");
98             sw.append(msg.getTask());
99             sw.append("</task-id>");
100             // task-result
101
sw.append("\n\t\t\t");
102             sw.append("<task-result>");
103             sw.append(msg.getResult());
104             sw.append("</task-result>");
105             // message-type
106
if (msg.getType() != null) {
107                 sw.append("\n\t\t\t");
108                 sw.append("<message-type>");
109                 sw.append(msg.getType());
110                 sw.append("</message-type>");
111             }
112             // task-status-message
113
if (msg.getMessage() != null) {
114                 sw.append("\n\t\t\t");
115                 sw.append("<task-status-msg>");
116                 sw.append("<msg-loc-info>");
117                 sw.append("<loc-token/>");
118                 sw.append("<loc-message>");
119                 sw.append(msg.getMessage());
120                 sw.append("</loc-message>");
121                 sw.append("</msg-loc-info>");
122                 sw.append("</task-status-msg>");
123             }
124             // exception-info
125
if (msg.getException() != null) {
126                 sw.append("\n\t\t\t");
127                 sw.append("<exception-info>");
128                 sw.append("\n\t\t\t\t");
129                 sw.append("<nesting-level>1</nesting-level>");
130                 sw.append("\n\t\t\t\t");
131                 sw.append("<msg-loc-info>");
132                 sw.append("\n\t\t\t\t\t");
133                 sw.append("<loc-token />");
134                 sw.append("\n\t\t\t\t\t");
135                 sw.append("<loc-message>");
136                 sw.append(msg.getException().getMessage());
137                 sw.append("</loc-message>");
138                 sw.append("\n\t\t\t\t\t");
139                 sw.append("<stack-trace>");
140                 StringWriter JavaDoc sw2 = new StringWriter JavaDoc();
141                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw2);
142                 msg.getException().printStackTrace(pw);
143                 pw.close();
144                 sw.append("<![CDATA[");
145                 sw.append(sw2.toString());
146                 sw.append("]]>");
147                 sw.append("</stack-trace>");
148                 sw.append("\n\t\t\t\t");
149                 sw.append("</msg-loc-info>");
150                 sw.append("\n\t\t\t");
151                 sw.append("</exception-info>");
152             }
153             // end: task-result-details
154
sw.append("\n\t\t");
155             sw.append("</task-result-details>");
156             // end: component-task-result-details
157
sw.append("\n\t");
158             sw.append("</component-task-result-details>");
159             // end: component-task-result
160
sw.append("\n");
161             sw.append("</component-task-result>");
162             // return result
163
return sw.toString();
164         } catch (Exception JavaDoc e) {
165             logger.warn("Error generating component management message", e);
166             return null;
167         }
168     }
169     
170 }
171
Popular Tags