001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.common;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    import java.io.PrintWriter;
023    import java.io.StringWriter;
024    
025    import javax.jbi.management.DeploymentException;
026    
027    /**
028     * ManagementMessageHelper is a class that ease the building of management messages. 
029     */
030    public class ManagementSupport {
031        
032        private static final Log logger = LogFactory.getLog(ManagementSupport.class);
033    
034        public static DeploymentException failure(String task, String component, String info, Throwable e) {
035            Message msg = new Message();
036            msg.setComponent(component);
037            msg.setTask(task);
038            msg.setResult("FAILED");
039            msg.setType("ERROR");
040            if (info != null) {
041                msg.setMessage(info);
042            } else if (e != null) {
043                msg.setMessage(e.toString());
044            }
045            msg.setException(e);
046            return new DeploymentException(createComponentMessage(msg));
047        }
048    
049         public static String createComponentMessage(Message msg) {
050            try {
051                StringBuffer sw = new StringBuffer();
052                // component-task-result
053                sw.append("<component-task-result ");
054                sw.append("xmlns=\"http://java.sun.com/xml/ns/jbi/management-message\">");
055                sw.append("\n\t");
056                // component-name
057                sw.append("<component-name>");
058                sw.append(msg.getComponent());
059                sw.append("</component-name>");
060                // component-task-result-details
061                sw.append("\n\t");
062                sw.append("<component-task-result-details>");
063                // task-result-details
064                sw.append("\n\t\t");
065                sw.append("<task-result-details>");
066                // task-id
067                sw.append("\n\t\t\t");
068                sw.append("<task-id>");
069                sw.append(msg.getTask());
070                sw.append("</task-id>");
071                // task-result
072                sw.append("\n\t\t\t");
073                sw.append("<task-result>");
074                sw.append(msg.getResult());
075                sw.append("</task-result>");
076                // message-type
077                if (msg.getType() != null) {
078                    sw.append("\n\t\t\t");
079                    sw.append("<message-type>");
080                    sw.append(msg.getType());
081                    sw.append("</message-type>");
082                }
083                // task-status-message
084                if (msg.getMessage() != null) {
085                    sw.append("\n\t\t\t");
086                    sw.append("<task-status-msg>");
087                    sw.append("<msg-loc-info>");
088                    sw.append("<loc-token/>");
089                    sw.append("<loc-message>");
090                    sw.append(msg.getMessage());
091                    sw.append("</loc-message>");
092                    sw.append("</msg-loc-info>");
093                    sw.append("</task-status-msg>");
094                }
095                // exception-info
096                if (msg.getException() != null) {
097                    sw.append("\n\t\t\t");
098                    sw.append("<exception-info>");
099                    sw.append("\n\t\t\t\t");
100                    sw.append("<nesting-level>1</nesting-level>");
101                    sw.append("\n\t\t\t\t");
102                    sw.append("<msg-loc-info>");
103                    sw.append("\n\t\t\t\t\t");
104                    sw.append("<loc-token />");
105                    sw.append("\n\t\t\t\t\t");
106                    sw.append("<loc-message>");
107                    sw.append(msg.getException().getMessage());
108                    sw.append("</loc-message>");
109                    sw.append("\n\t\t\t\t\t");
110                    sw.append("<stack-trace>");
111                    StringWriter sw2 = new StringWriter();
112                    PrintWriter pw = new PrintWriter(sw2);
113                    msg.getException().printStackTrace(pw);
114                    pw.close();
115                    sw.append("<![CDATA[");
116                    sw.append(sw2.toString());
117                    sw.append("]]>");
118                    sw.append("</stack-trace>");
119                    sw.append("\n\t\t\t\t");
120                    sw.append("</msg-loc-info>");
121                    sw.append("\n\t\t\t");
122                    sw.append("</exception-info>");
123                }
124                // end: task-result-details
125                sw.append("\n\t\t");
126                sw.append("</task-result-details>");
127                // end: component-task-result-details
128                sw.append("\n\t");
129                sw.append("</component-task-result-details>");
130                // end: component-task-result
131                sw.append("\n");
132                sw.append("</component-task-result>");
133                // return result
134                return sw.toString();
135            } catch (Exception e) {
136                logger.warn("Error generating component management message", e);
137                return null;
138            }
139        }
140        
141         public static class Message {
142             private String task;
143             private String component;
144             private String result;
145             private Throwable exception;
146             private String type;
147             private String message;
148             
149             public String getComponent() {
150                 return component;
151             }
152             public void setComponent(String component) {
153                 this.component = component;
154             }
155             public Throwable getException() {
156                 return exception;
157             }
158             public void setException(Throwable exception) {
159                 this.exception = exception;
160             }
161             public String getResult() {
162                 return result;
163             }
164             public void setResult(String result) {
165                 this.result = result;
166             }
167             public String getTask() {
168                 return task;
169             }
170             public void setTask(String task) {
171                 this.task = task;
172             }
173             public String getType() {
174                 return type;
175             }
176             public void setType(String type) {
177                 this.type = type;
178             }
179             public String getMessage() {
180                 return message;
181             }
182             public void setMessage(String message) {
183                 this.message = message;
184             }
185         }
186         
187    }