1 22 package org.objectweb.petals.jbi.management.service.util; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.InputStream ; 26 27 import javax.xml.XMLConstants ; 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.transform.dom.DOMSource ; 31 import javax.xml.transform.stream.StreamSource ; 32 import javax.xml.validation.Schema ; 33 import javax.xml.validation.SchemaFactory ; 34 import javax.xml.validation.Validator ; 35 36 import org.w3c.dom.Document ; 37 38 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 EXCEPTION_INFO = "exception-info"; 59 60 private static final String FRMWK_TASK_RESULT = "frmwk-task-result"; 61 62 private static final String FRMWK_TASK_RESULT_DETAILS = "frmwk-task-result-details"; 63 64 private static final String IS_CAUSE_FRAMEWORK = "is-cause-framework"; 65 66 private static final String JBI_TASK = "jbi-task"; 67 68 private static final String JBI_TASK_RESULT = "jbi-task-result"; 69 70 private static final String LOC_MESSAGE = "loc-message"; 71 72 private static final String LOC_PARAM = "loc-param"; 73 74 private static final String LOC_TOKEN = "loc-token"; 75 76 private static final String LOCALE = "locale"; 77 78 private static final String MESSAGE_TYPE = "message-type"; 79 80 private static final String MSG_LOC_INFO = "msg-loc-info"; 81 82 private static final String TASK_STATUS_MSG = "task-status-msg"; 83 84 private static final String NESTING_LEVEL = "nesting-level"; 85 86 private static final String STACK_TRACE = "stack-trace"; 87 88 private static final String TASK_ID = "task-id"; 89 90 private static final String TASK_RESULT = "task-result"; 91 92 private static final String TASK_RESULT_DETAILS = "task-result-details"; 93 94 private static final String XMLNS = "http://java.sun.com/xml/ns/jbi/management-message"; 95 96 private static final String COMPONENT_TASK_RESULT = "component-task-result"; 97 98 private static final String COMPONENT_TASK_RESULT_DETAILS = "component-task-result-details"; 99 100 private static final String COMPONENT_NAME = "component-name"; 101 102 protected StringBuffer xmlBuffer; 103 104 protected String frameworkTaskId; 105 106 public XMLResult(String frameworkTaskId) { 107 xmlBuffer = new StringBuffer (); 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 124 public void addFrameworkTaskResult(TaskResult taskResult, 125 CauseFramework causeFramework) { 126 addFrameworkTaskResult(taskResult, null, null, null, null, null, null, 127 null, causeFramework); 128 } 129 130 144 public void addFrameworkTaskResult(TaskResult taskResult, 145 MessageType messageType, String message, String [] messageParams, 146 CauseFramework causeFramework) { 147 addFrameworkTaskResult(taskResult, messageType, message, messageParams, 148 null, null, null, null, causeFramework); 149 } 150 151 171 public void addFrameworkTaskResult(TaskResult taskResult, 172 MessageType messageType, String message, String [] messageParams, 173 String nestingLevel, String exceptionMessage, 174 String [] exceptionMessageParams, CauseFramework causeFramework) { 175 addFrameworkTaskResult(taskResult, messageType, message, messageParams, 176 nestingLevel, exceptionMessage, exceptionMessageParams, null, 177 causeFramework); 178 } 179 180 186 public void addComponentTaskResult(StringBuffer componentTaskString) { 187 xmlBuffer.append(componentTaskString); 188 } 189 190 212 public void addFrameworkTaskResult(TaskResult taskResult, 213 MessageType messageType, String message, String [] messageParams, 214 String nestingLevel, String exceptionMessage, 215 String [] exceptionMessageParams, String 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 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 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 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 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 290 public String getString() { 291 xmlBuffer.append("\t</" + XMLResult.JBI_TASK_RESULT + ">\n"); 292 xmlBuffer.append("</" + XMLResult.JBI_TASK + ">\n"); 293 return xmlBuffer.toString(); 294 } 295 296 303 public static boolean isValidComponentTask(String componentTask, 304 InputStream xsd) { 305 try { 306 validateManagementMessage(componentTask, xsd); 307 } catch (Exception e) { 308 return false; 309 } 310 return true; 311 } 312 313 319 public static void validateManagementMessage(String managementMessage, 320 InputStream xsd) throws Exception { 321 DocumentBuilder parser; 322 parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 323 Document document = parser.parse(new ByteArrayInputStream ( 324 managementMessage.getBytes())); 325 SchemaFactory factory = SchemaFactory 326 .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 327 328 StreamSource schemaFile = new StreamSource (xsd); 329 Schema schema = factory.newSchema(schemaFile); 330 Validator validator = schema.newValidator(); 331 validator.validate(new DOMSource (document)); 332 } 333 334 359 public String wrapComponentTaskResult(String componentName, String taskId, 360 String message) { 361 StringBuffer xmlInput = new StringBuffer (); 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 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 |