View Javadoc

1   /*
2   Copyright (c) 2005, CodeSmarts
3    All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8   	* 	Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
10  	* 	Redistributions in binary form must reproduce the above
11  copyright notice, this list of conditions and the following disclaimer
12  in the documentation and/or other materials provided with the
13  distribution.
14  	* 	Neither the name of the CodeSmarts nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17  
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30  
31  package net.codesmarts.log4j;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  import org.apache.commons.httpclient.HttpClient;
36  import org.apache.commons.httpclient.methods.PostMethod;
37  import org.apache.log4j.spi.LoggingEvent;
38                  
39  /***
40   * Appender to file Bug Reports into the Bugs Genie bug tracking system (http://bugs-bug-genie.sourceforge.net/)
41   * @author Fred McCann
42   */
43  public class BugsGenieBugReportAppender extends AbstractHttpBugReportAppender {
44  
45      protected String product = null;
46      protected String edition = null;
47      protected String build = null;
48      protected String category = null;
49      protected String application = null;
50      protected String status = null;
51      protected String assignTo = null;
52      protected String bugsUsername = null;
53      protected String bugsPassword = null;
54      protected String url = null;
55      
56      /***
57       * Set the Bugs Genie application for this appender
58       * @param application The application to set.
59       */
60      public void setApplication(String application) {
61          this.application = application;
62      }
63      
64      /***
65       * Set the Bugs Genie user to whom to assign bugs. Defaults to the bugsUsername
66       * @param assignTo The assignTo to set.
67       */
68      public void setAssignTo(String assignTo) {
69          this.assignTo = assignTo;
70      }
71      
72      /***
73       * Set the Bugs Genie build. 
74       * @param build The build to set.
75       */
76      public void setBuild(String build) {
77          this.build = build;
78      }
79      
80      /***
81       * Url of the direct_bug_post.php file
82       * @param url The url to set.
83       */
84      public void setUrl(String url) {
85          this.url = url;
86      }
87      
88      /***
89       * Set the Bugs Genie password for this appender
90       * @param bugsPassword The bugsPassword to set.
91       */
92      public void setBugsPassword(String bugsPassword) {
93          this.bugsPassword = bugsPassword;
94      }
95      /***
96       * Set the Bugs Genie username for this appender
97       * @param bugsUsername The bugsUsername to set.
98       */
99      public void setBugsUsername(String bugsUsername) {
100         this.bugsUsername = bugsUsername;
101     }
102     
103     /***
104      * Set the Bugs Genie category for this appender
105      * @param category The category to set.
106      */
107     public void setCategory(String category) {
108         this.category = category;
109     }
110     
111     /***
112      * Set the Bugs Genie edition for this appender
113      * @param edition The edition to set.
114      */
115     public void setEdition(String edition) {
116         this.edition = edition;
117     }
118     
119     /***
120      * Set the Bugs Genie product for this appender
121      * @param product The product to set.
122      */
123     public void setProduct(String product) {
124         this.product = product;
125     }
126     
127     /***
128      * Set the Bugs Genie status for this appender
129      * @param status The status to set.
130      */
131     public void setStatus(String status) {
132         this.status = status;
133     }
134         
135     /***
136      * initialize appender
137      */
138     public void init() {
139         super.init();
140 
141         boolean complete = true;
142         
143         if (product == null) {
144             getErrorHandler().error("Must set product for "+this.getClass().getName());
145             complete = false;
146         }
147 
148         if (edition == null) {
149             getErrorHandler().error("Must set edition for "+this.getClass().getName());
150             complete = false;
151         }
152 
153         if (build == null) {
154             getErrorHandler().error("Must set build for "+this.getClass().getName());
155             complete = false;
156         }
157 
158         if (category == null) {
159             getErrorHandler().error("Must set category for "+this.getClass().getName());
160             complete = false;
161         }
162 
163         if (application == null) {
164             getErrorHandler().error("Must set application for "+this.getClass().getName());
165             complete = false;
166         }
167 
168         if (status == null) {
169             getErrorHandler().error("Must set status for "+this.getClass().getName());
170             complete = false;
171         }
172 
173         if (bugsUsername == null) {
174             getErrorHandler().error("Must set bugsUsername for "+this.getClass().getName());
175             complete = false;
176         }
177 
178         if (bugsPassword == null) {
179             getErrorHandler().error("Must set bugsPassword for "+this.getClass().getName());
180             complete = false;
181         }
182         
183         if (url == null) {
184             getErrorHandler().error("Must set url for "+this.getClass().getName());
185             complete = false;
186         }        
187         
188         if (!complete) {
189             close();
190             return;
191         }
192 
193         if (assignTo == null)
194             assignTo = bugsUsername;
195     }
196     
197     /***
198      * @see net.codesmarts.log4j.BugReportAppender#append(net.codesmarts.log4j.BugReport)
199      */
200     public synchronized void append(BugReport report) {
201         // Get a client
202         HttpClient httpClient = getHttpClient();
203         
204         // Create new post action
205         PostMethod post = new PostMethod(url);
206         
207         // Add configuration parameters
208         post.addParameter("pak",product);
209         post.addParameter("edition",edition);
210         post.addParameter("build",build);
211         post.addParameter("category",category);
212         post.addParameter("application",application);
213         post.addParameter("status",status);
214         post.addParameter("assignto",assignTo);
215         post.addParameter("username",bugsUsername);        
216         post.addParameter("password",bugsPassword);
217                
218         // Get title and description
219         String title = null;
220         StringBuffer description = new StringBuffer();
221         
222         List events = report.getEvents();
223         LoggingEvent event = null;
224         
225         Iterator i = events.iterator();
226         
227         // Write formatted events to bug report description
228         while (i.hasNext()) {
229             event = (LoggingEvent)i.next();
230             description.append(getLayout().format(event));
231             
232             if (event.getLevel().isGreaterOrEqual(this.getThresholdPriority()) && title==null)
233                 title = event.getMessage().toString();
234         }
235         
236         // add bug report specific parameters
237         post.addParameter("hash",report.getKey());        
238         post.addParameter("title",title);
239         post.addParameter("long_desc",description.toString());
240         
241         // File Report
242         try {
243             httpClient.executeMethod(post);
244             String response = post.getResponseBodyAsString();
245             if (!response.toLowerCase().startsWith("bug filed")) 
246                 getErrorHandler().error("Could not post report to "+url+". "+response);
247         }
248         catch (Exception e) {
249             getErrorHandler().error("Could not post report to "+url+". "+e.getMessage());
250         }
251         finally {
252             post.releaseConnection();
253         }
254         
255     }
256 
257 }