KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > service > util > XMLResult


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: XMLResult.java 154 2006-03-27 15:30:10Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.service.util;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import javax.xml.XMLConstants JavaDoc;
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.transform.dom.DOMSource JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32 import javax.xml.validation.Schema JavaDoc;
33 import javax.xml.validation.SchemaFactory JavaDoc;
34 import javax.xml.validation.Validator JavaDoc;
35
36 import org.w3c.dom.Document JavaDoc;
37
38 /**
39  * This class gathers the dtd used for the management message
40  *
41  * @author ddesjardins - eBMWebsourcing
42  * @author wjoseph - eBMWebsourcing
43  */

44 public class XMLResult {
45
46     public static enum CauseFramework {
47         NO, YES
48     };
49
50     public static enum MessageType {
51         ERROR, INFO, WARNING
52     };
53
54     public static enum TaskResult {
55         FAILED, SUCCESS
56     };
57
58     private static final String JavaDoc EXCEPTION_INFO = "exception-info";
59
60     private static final String JavaDoc FRMWK_TASK_RESULT = "frmwk-task-result";
61
62     private static final String JavaDoc FRMWK_TASK_RESULT_DETAILS = "frmwk-task-result-details";
63
64     private static final String JavaDoc IS_CAUSE_FRAMEWORK = "is-cause-framework";
65
66     private static final String JavaDoc JBI_TASK = "jbi-task";
67
68     private static final String JavaDoc JBI_TASK_RESULT = "jbi-task-result";
69
70     private static final String JavaDoc LOC_MESSAGE = "loc-message";
71
72     private static final String JavaDoc LOC_PARAM = "loc-param";
73
74     private static final String JavaDoc LOC_TOKEN = "loc-token";
75
76     private static final String JavaDoc LOCALE = "locale";
77
78     private static final String JavaDoc MESSAGE_TYPE = "message-type";
79
80     private static final String JavaDoc MSG_LOC_INFO = "msg-loc-info";
81
82     private static final String JavaDoc TASK_STATUS_MSG = "task-status-msg";
83
84     private static final String JavaDoc NESTING_LEVEL = "nesting-level";
85
86     private static final String JavaDoc STACK_TRACE = "stack-trace";
87
88     private static final String JavaDoc TASK_ID = "task-id";
89
90     private static final String JavaDoc TASK_RESULT = "task-result";
91
92     private static final String JavaDoc TASK_RESULT_DETAILS = "task-result-details";
93
94     private static final String JavaDoc XMLNS = "http://java.sun.com/xml/ns/jbi/management-message";
95
96     private static final String JavaDoc COMPONENT_TASK_RESULT = "component-task-result";
97
98     private static final String JavaDoc COMPONENT_TASK_RESULT_DETAILS = "component-task-result-details";
99
100     private static final String JavaDoc COMPONENT_NAME = "component-name";
101
102     protected StringBuffer JavaDoc xmlBuffer;
103
104     protected String JavaDoc frameworkTaskId;
105
106     public XMLResult(String JavaDoc frameworkTaskId) {
107         xmlBuffer = new StringBuffer JavaDoc();
108         this.frameworkTaskId = frameworkTaskId;
109         xmlBuffer
110             .append("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n");
111         xmlBuffer.append("<" + XMLResult.JBI_TASK + " version='1.0' xmlns='"
112             + XMLResult.XMLNS + "'>\n");
113         xmlBuffer.append("\t<" + XMLResult.JBI_TASK_RESULT + ">\n");
114     }
115
116     /**
117      * Add a framework task result to a xml management message
118      *
119      * @param taskResult
120      * result of the task
121      * @param causeFramework
122      * causeFramework
123      */

124     public void addFrameworkTaskResult(TaskResult taskResult,
125         CauseFramework causeFramework) {
126         addFrameworkTaskResult(taskResult, null, null, null, null, null, null,
127             null, causeFramework);
128     }
129
130     /**
131      * Add a framework task result to a xml management message
132      *
133      * @param taskResult
134      * result of the task
135      * @param messageType
136      * type of message
137      * @param message
138      * message
139      * @param messageParams
140      * list of param for the message
141      * @param causeFramework
142      * causeFramework
143      */

144     public void addFrameworkTaskResult(TaskResult taskResult,
145         MessageType messageType, String JavaDoc message, String JavaDoc[] messageParams,
146         CauseFramework causeFramework) {
147         addFrameworkTaskResult(taskResult, messageType, message, messageParams,
148             null, null, null, null, causeFramework);
149     }
150
151     /**
152      * Add a framework task result to a xml management message
153      *
154      * @param taskResult
155      * result of the task
156      * @param messageType
157      * type of message
158      * @param message
159      * message
160      * @param messageParams
161      * list of param for the message
162      * @param nestingLevel
163      * nesting level of the exception
164      * @param exceptionMessage
165      * message of the exception
166      * @param exceptionMessageParams
167      * list of the param for the exception message
168      * @param causeFramework
169      * causeFramework
170      */

171     public void addFrameworkTaskResult(TaskResult taskResult,
172         MessageType messageType, String JavaDoc message, String JavaDoc[] messageParams,
173         String JavaDoc nestingLevel, String JavaDoc exceptionMessage,
174         String JavaDoc[] exceptionMessageParams, CauseFramework causeFramework) {
175         addFrameworkTaskResult(taskResult, messageType, message, messageParams,
176             nestingLevel, exceptionMessage, exceptionMessageParams, null,
177             causeFramework);
178     }
179
180     /**
181      * Added a component task result to this management message
182      *
183      * @param componentTaskString
184      * component task result xml
185      */

186     public void addComponentTaskResult(StringBuffer JavaDoc componentTaskString) {
187         xmlBuffer.append(componentTaskString);
188     }
189
190     /**
191      * Add a framework task result to a xml management message
192      *
193      * @param taskResult
194      * result of the task
195      * @param messageType
196      * type of message
197      * @param message
198      * message
199      * @param messageParams
200      * list of param for the message
201      * @param nestingLevel
202      * nesting level of the exception
203      * @param exceptionMessage
204      * message of the exception
205      * @param exceptionMessageParams
206      * list of the param for the exception message
207      * @param stackStrace
208      * stacktrace
209      * @param causeFramework
210      * causeFramework
211      */

212     public void addFrameworkTaskResult(TaskResult taskResult,
213         MessageType messageType, String JavaDoc message, String JavaDoc[] messageParams,
214         String JavaDoc nestingLevel, String JavaDoc exceptionMessage,
215         String JavaDoc[] exceptionMessageParams, String JavaDoc stackStrace,
216         CauseFramework causeFramework) {
217         xmlBuffer.append("\t\t<" + XMLResult.FRMWK_TASK_RESULT + ">\n");
218         xmlBuffer.append("\t\t\t<" + XMLResult.FRMWK_TASK_RESULT_DETAILS
219             + ">\n");
220         xmlBuffer.append("\t\t\t\t<" + XMLResult.TASK_RESULT_DETAILS + ">\n");
221         xmlBuffer.append("\t\t\t\t\t<" + XMLResult.TASK_ID + ">"
222             + frameworkTaskId + "</" + XMLResult.TASK_ID + ">\n");
223         xmlBuffer.append("\t\t\t\t\t<" + XMLResult.TASK_RESULT + ">"
224             + taskResult + "</" + XMLResult.TASK_RESULT + ">\n");
225         if (messageType != null) {
226             xmlBuffer.append("\t\t\t\t\t<" + XMLResult.MESSAGE_TYPE + ">"
227                 + messageType + "</" + XMLResult.MESSAGE_TYPE + ">\n");
228             if (message != null) {
229                 xmlBuffer.append("\t\t\t\t\t<" + TASK_STATUS_MSG + ">\n");
230                 xmlBuffer.append("\t\t\t\t\t\t<" + MSG_LOC_INFO + ">\n");
231                 // TODO change token with a key message for looking up localized
232
// text
233
// for the message
234
xmlBuffer.append("\t\t\t\t\t\t\t<" + XMLResult.LOC_TOKEN
235                     + ">jbimsg</" + XMLResult.LOC_TOKEN + ">\n");
236                 xmlBuffer.append("\t\t\t\t\t\t\t<" + XMLResult.LOC_MESSAGE
237                     + ">" + message + "</" + XMLResult.LOC_MESSAGE + ">\n");
238                 for (String JavaDoc string : messageParams) {
239                     xmlBuffer.append("\t\t\t\t\t\t\t<" + XMLResult.LOC_PARAM
240                         + ">" + string + "</" + XMLResult.LOC_PARAM + ">\n");
241                 }
242                 xmlBuffer.append("\t\t\t\t\t\t</" + MSG_LOC_INFO + ">\n");
243                 xmlBuffer.append("\t\t\t\t\t</" + TASK_STATUS_MSG + ">\n");
244             }
245             if (exceptionMessage != null) {
246                 xmlBuffer.append("\t\t\t\t\t<" + XMLResult.EXCEPTION_INFO
247                     + ">\n");
248                 xmlBuffer.append("\t\t\t\t\t\t<" + XMLResult.NESTING_LEVEL
249                     + ">" + nestingLevel + "</" + XMLResult.NESTING_LEVEL
250                     + ">\n");
251                 xmlBuffer.append("\t\t\t\t\t\t<" + XMLResult.MSG_LOC_INFO
252                     + ">\n");
253                 // TODO change token with a key message for looking up localized
254
// text
255
// for the message
256
xmlBuffer.append("\t\t\t\t\t\t\t<" + XMLResult.LOC_TOKEN
257                     + ">jbimsg</" + XMLResult.LOC_TOKEN + ">\n");
258                 xmlBuffer.append("\t\t\t\t\t\t\t<" + XMLResult.LOC_MESSAGE
259                     + ">" + exceptionMessage + "</" + XMLResult.LOC_MESSAGE
260                     + ">\n");
261                 for (String JavaDoc string : exceptionMessageParams) {
262                     xmlBuffer.append("\t\t\t\t\t\t\t<" + XMLResult.LOC_PARAM
263                         + ">" + string + "</" + XMLResult.LOC_PARAM + ">\n");
264                 }
265                 xmlBuffer.append("\t\t\t\t\t\t</" + XMLResult.MSG_LOC_INFO
266                     + ">\n");
267                 if (stackStrace == null) {
268                     stackStrace = "";
269                 }
270                 xmlBuffer.append("\t\t\t\t\t\t<" + XMLResult.STACK_TRACE + ">"
271                     + stackStrace + "</" + XMLResult.STACK_TRACE + ">\n");
272                 xmlBuffer.append("\t\t\t\t</" + XMLResult.EXCEPTION_INFO
273                     + ">\n");
274             }
275         }
276         xmlBuffer.append("\t\t\t\t</" + XMLResult.TASK_RESULT_DETAILS + ">\n");
277         xmlBuffer.append("\t\t\t\t<" + XMLResult.LOCALE + ">en</"
278             + XMLResult.LOCALE + ">\n");
279         xmlBuffer.append("\t\t\t</" + XMLResult.FRMWK_TASK_RESULT_DETAILS
280             + ">\n");
281         xmlBuffer.append("\t\t\t<" + XMLResult.IS_CAUSE_FRAMEWORK + ">"
282             + causeFramework + "</" + XMLResult.IS_CAUSE_FRAMEWORK + ">\n");
283         xmlBuffer.append("\t\t</" + XMLResult.FRMWK_TASK_RESULT + ">\n");
284     }
285
286     /**
287      * Close the XMLResult and return the String
288      *
289      */

290     public String JavaDoc getString() {
291         xmlBuffer.append("\t</" + XMLResult.JBI_TASK_RESULT + ">\n");
292         xmlBuffer.append("</" + XMLResult.JBI_TASK + ">\n");
293         return xmlBuffer.toString();
294     }
295
296     /**
297      * Check if a component task result is valid against a xml schema
298      *
299      * @param componentTask
300      * xml of the component task
301      * @return true if valid, false if not valid
302      */

303     public static boolean isValidComponentTask(String JavaDoc componentTask,
304         InputStream JavaDoc xsd) {
305         try {
306             validateManagementMessage(componentTask, xsd);
307         } catch (Exception JavaDoc e) {
308             return false;
309         }
310         return true;
311     }
312
313     /**
314      * Check if a management message task result is valid against a xml schema
315      *
316      * @param managementMessage
317      * message xml of the management message
318      */

319     public static void validateManagementMessage(String JavaDoc managementMessage,
320         InputStream JavaDoc xsd) throws Exception JavaDoc {
321         DocumentBuilder JavaDoc parser;
322         parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
323         Document JavaDoc document = parser.parse(new ByteArrayInputStream JavaDoc(
324             managementMessage.getBytes()));
325         SchemaFactory JavaDoc factory = SchemaFactory
326             .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
327
328         StreamSource JavaDoc schemaFile = new StreamSource JavaDoc(xsd);
329         Schema JavaDoc schema = factory.newSchema(schemaFile);
330         Validator JavaDoc validator = schema.newValidator();
331         validator.validate(new DOMSource JavaDoc(document));
332     }
333
334     /**
335      * Return a component task result xml
336      *
337      * @param componentName
338      * component name
339      * @param taskId
340      * id of the task
341      * @param taskResult
342      * result of the task
343      * @param messageType
344      * type of message
345      * @param message
346      * message
347      * @param messageParams
348      * list of param for the message
349      * @param nestingLevel
350      * nesting level of the exception
351      * @param exceptionMessage
352      * message of the exception
353      * @param exceptionMessageParams
354      * list of the param for the exception message
355      * @param stackStrace
356      * stacktrace
357      * @return xml string
358      */

359     public String JavaDoc wrapComponentTaskResult(String JavaDoc componentName, String JavaDoc taskId,
360         String JavaDoc message) {
361         StringBuffer JavaDoc xmlInput = new StringBuffer JavaDoc();
362         xmlInput.append("\t\t<" + XMLResult.COMPONENT_TASK_RESULT + ">\n");
363         xmlInput.append("\t\t\t<" + XMLResult.COMPONENT_NAME + ">"
364             + componentName + "</" + XMLResult.COMPONENT_NAME + ">\n");
365         xmlInput.append("\t\t\t<" + XMLResult.COMPONENT_TASK_RESULT_DETAILS
366             + " xmlns='" + XMLResult.XMLNS + "'>\n");
367         xmlInput.append("\t\t\t\t<" + XMLResult.TASK_RESULT_DETAILS + ">\n");
368         xmlInput.append("\t\t\t\t\t<" + XMLResult.TASK_ID + ">" + taskId + "</"
369             + XMLResult.TASK_ID + ">\n");
370         xmlInput.append("\t\t\t\t\t<" + XMLResult.TASK_RESULT + ">"
371             + XMLResult.TaskResult.SUCCESS + "</" + XMLResult.TASK_RESULT
372             + ">\n");
373         xmlInput.append("\t\t\t\t\t<" + XMLResult.MESSAGE_TYPE + ">"
374             + XMLResult.MessageType.WARNING + "</" + XMLResult.MESSAGE_TYPE
375             + ">\n");
376         if (message != null) {
377             xmlInput.append("\t\t\t\t\t<" + XMLResult.MSG_LOC_INFO + ">\n");
378             // TODO change token with a key message for looking up localized
379
// text
380
// for the message
381
xmlInput.append("\t\t\t\t\t\t<" + XMLResult.LOC_TOKEN + ">jbimsg</"
382                 + XMLResult.LOC_TOKEN + ">\n");
383             xmlInput.append("\t\t\t\t\t\t<" + XMLResult.LOC_MESSAGE + ">"
384                 + "![CDATA[" + message + "]]" + "</" + XMLResult.LOC_MESSAGE
385                 + ">\n");
386             xmlInput.append("\t\t\t\t\t</" + XMLResult.MSG_LOC_INFO + ">\n");
387         }
388         xmlInput.append("\t\t\t\t</" + XMLResult.TASK_RESULT_DETAILS + ">\n");
389         xmlInput.append("\t\t\t</" + XMLResult.COMPONENT_TASK_RESULT_DETAILS
390             + ">\n");
391         xmlInput.append("\t\t</" + XMLResult.COMPONENT_TASK_RESULT + ">\n");
392         return xmlInput.toString();
393     }
394
395 }
396
Popular Tags