1 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 ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import javax.jbi.management.DeploymentException; 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 35 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 task; 45 private String result; 46 private Exception exception; 47 private String type; 48 private String message; 49 private String component; 50 private String locale; 51 52 public Exception getException() { 53 return exception; 54 } 55 public void setException(Exception 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 getMessage() { 65 return message; 66 } 67 public void setMessage(String message) { 68 this.message = message; 69 } 70 public String getResult() { 71 return result; 72 } 73 public void setResult(String result) { 74 this.result = result; 75 } 76 public String getTask() { 77 return task; 78 } 79 public void setTask(String task) { 80 this.task = task; 81 } 82 public String getType() { 83 return type; 84 } 85 public void setType(String type) { 86 this.type = type; 87 } 88 public String getComponent() { 89 return component; 90 } 91 public void setComponent(String component) { 92 this.component = component; 93 } 94 public String getLocale() { 95 return locale; 96 } 97 public void setLocale(String locale) { 98 this.locale = locale; 99 } 100 } 101 102 public static Exception failure(String task, String info) throws Exception { 103 return failure(task, info, null, null); 104 } 105 106 public static Exception failure(String task, List componentResults) throws Exception { 107 return failure(task, null, null, componentResults); 108 } 109 110 public static Exception failure(String task, String info, Exception e) throws Exception { 111 return failure(task, info, e, null); 112 } 113 114 public static Exception failure(String task, String info, Exception e, List componentResults) throws Exception { 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 (ManagementSupport.createFrameworkMessage(msg, componentResults)); 122 } 123 124 public static String createSuccessMessage(String task) { 125 return createSuccessMessage(task, null, null); 126 } 127 128 public static String createSuccessMessage(String task, List componentResults) { 129 return createSuccessMessage(task, null, componentResults); 130 } 131 132 public static String createSuccessMessage(String task, String info) { 133 return createSuccessMessage(task, info, null); 134 } 135 136 public static String createSuccessMessage(String task, String info, List 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 createWarningMessage(String task, String info, List 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 createFrameworkMessage(Message fmkMsg, List componentResults) { 154 try { 155 Document doc = createDocument(); 156 Element 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 jbiTaskResult = createChild(jbiTask, "jbi-task-result"); 160 Element frmkTaskResult = createChild(jbiTaskResult, "frmwk-task-result"); 161 Element 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 iter = componentResults.iterator(); iter.hasNext();) { 168 Element element = (Element ) iter.next(); 169 jbiTaskResult.appendChild(doc.importNode(element, true)); 170 } 171 } 172 return DOMUtil.asIndentedXML(doc); 173 } catch (Exception e) { 174 logger.error("Error", e); 175 return null; 176 } 177 } 178 179 private static Document createDocument() { 180 try { 181 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 182 factory.setNamespaceAware(true); 183 DocumentBuilder builder = factory.newDocumentBuilder(); 184 return builder.newDocument(); 185 } catch (Exception e) { 186 throw new RuntimeException ("Could not create DOM document", e); 187 } 188 } 189 190 private static Element createChild(Node parent, String name) { 191 return createChild(parent, name, null); 192 } 193 194 private static Element createChild(Node parent, String name, String text) { 195 Document doc = parent instanceof Document ? (Document ) parent : parent.getOwnerDocument(); 196 Element 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 root, Message fmkMsg) { 205 Element 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 if (fmkMsg.getMessage() != null) { 213 Element taskStatusMessage = createChild(taskResultDetails, "task-status-msg"); 214 Element msgLocInfo = createChild(taskStatusMessage, "msg-loc-info"); 215 createChild(msgLocInfo, "loc-token"); 216 createChild(msgLocInfo, "loc-message", fmkMsg.getMessage()); 217 } 218 if (fmkMsg.getException() != null) { 220 Element 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 stackTrace = createChild(exceptionInfo, "stack-trace"); 225 StringWriter sw2 = new StringWriter (); 226 PrintWriter pw = new PrintWriter (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 task, String component, String info) { 234 try { 235 Element e = createComponentFailure(task, component, info, null); 236 return new DeploymentException(DOMUtil.asXML(e)); 237 } catch (Exception 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 createComponentMessage(Message msg) { 246 Document doc = createDocument(); 247 Element componentTaskResult = createChild(doc, "component-task-result"); 248 createChild(componentTaskResult, "component-name", msg.getComponent()); 249 Element componentTaskResultDetails = createChild(componentTaskResult, "component-task-result-details"); 250 appendTaskResultDetails(componentTaskResultDetails, msg); 251 return componentTaskResult; 252 } 253 254 public static Element createComponentSuccess(String task, String 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 createComponentFailure(String task, String component, String info, Exception 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 createComponentWarning(String task, String component, String info, Exception 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 |