KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > controller > ControllerInstallLog


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.controller;
66
67 import com.jcorporate.expresso.kernel.InstallLog;
68 import org.apache.log4j.Logger;
69
70 import java.io.ByteArrayOutputStream JavaDoc;
71 import java.io.PrintStream JavaDoc;
72
73 /**
74  * This is an implementation of the <code>IntallLog</code> interface that is
75  * useful when running installation systems inside controllers. It renders the
76  * log messages as a series of outputs with the following rules:
77  * <p/>
78  * <ul>
79  * <li>For each output, if an exception was logged, there are two nested outputs:
80  * One named 'errorMessage' the other 'stackTrace' which contain the exception
81  * message and the exception stack trace</li>
82  * <li>For each output, a 'color' attribute is set. This attribute can be
83  * used at render time to render the font color of the output. Of course, a JSP
84  * can use different color schemes, but the concept is to provide highest
85  * performance possible</li>
86  * </ul>
87  * </p>
88  * first before using the log or
89  *
90  * @author Michael Rimov
91  * @see com.jcorporate.expresso.kernel.InstallLog
92  * @see com.jcorporate.expresso.kernel.ConsoleInstallLog
93  */

94 public class ControllerInstallLog implements InstallLog {
95
96     protected ControllerResponse response;
97     protected Block parentBlock;
98
99     private static final transient Logger log = Logger.getLogger(ControllerInstallLog.class);
100
101     /**
102      * Constructs a ControllerInstallLog and sets the ControllerResponse that
103      * we will be using to add the outputs to.
104      *
105      * @param theResponse
106      */

107     public ControllerInstallLog(ControllerResponse theResponse) {
108         response = theResponse;
109         parentBlock = null;
110     }
111
112     /**
113      * The point is that we don't want the default constructor used.
114      */

115     protected ControllerInstallLog() {
116
117     }
118
119     /**
120      * Optional method. If you want the outputs to be added to a block rather
121      * than the root of the ControllerResponse then call setParentBlock() before
122      * usage
123      *
124      * @param b the block you wish to use.
125      */

126     public void setParentBlock(Block b) {
127         parentBlock = b;
128     }
129
130     /**
131      * Helper method that adds the output to either the parent block
132      * or the controller response, depending on what was needed
133      *
134      * @param logMessage the output to add
135      */

136     protected void addOutput(Output logMessage) {
137         try {
138             if (parentBlock == null) {
139                 response.add(logMessage);
140             } else {
141                 parentBlock.add(logMessage);
142             }
143         } catch (ControllerException ex) {
144             log.warn("Error logging install message", ex);
145         }
146     }
147
148
149     /**
150      * Log a debug message
151      *
152      * @param message The message to log.
153      */

154     public void debug(String JavaDoc message) {
155         Output o = new Output("[DEBUG] " + message);
156         o.setAttribute("color", "green");
157         addOutput(o);
158     }
159
160
161     /**
162      * Log an info message
163      *
164      * @param message The message to log.
165      */

166     public void info(String JavaDoc message) {
167         Output o = new Output(message);
168         o.setAttribute("color", "black");
169         addOutput(o);
170     }
171
172     /**
173      * Log a warning message
174      *
175      * @param message The message to log.
176      */

177     public void warn(String JavaDoc message) {
178         Output o = new Output("[WARN] " + message);
179         o.setAttribute("color", "orange");
180         addOutput(o);
181     }
182
183     /**
184      * Log a warning exception
185      *
186      * @param message The message to log.
187      * @param error The <code>Throwable</code> object to log
188      */

189     public void warn(String JavaDoc message, Throwable JavaDoc error) {
190         Output o = new Output("[WARN] " + message);
191         o.setAttribute("color", "orange");
192         addNestedOutputs(o, error);
193         addOutput(o);
194     }
195
196     /**
197      * Log an error exception
198      *
199      * @param message The message to log.
200      * @param error The <code>Throwable</code> object to log
201      */

202     public void error(String JavaDoc message, Throwable JavaDoc error) {
203         Output o = new Output("[ERROR] " + message);
204         o.setAttribute("color", "red");
205         addNestedOutputs(o, error);
206         addOutput(o);
207     }
208
209     /**
210      * Log an error message
211      *
212      * @param message The message to log.
213      */

214     public void error(String JavaDoc message) {
215         Output o = new Output("[ERROR] " + message);
216         o.setAttribute("color", "red");
217         addOutput(o);
218     }
219
220
221     /**
222      * Adds the exceptions as nested outputs to the original log message
223      *
224      * @param o the original log message
225      * @param error the exception to log as well.
226      */

227     protected void addNestedOutputs(Output o, Throwable JavaDoc error) {
228         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
229         error.printStackTrace(new PrintStream JavaDoc(bos));
230         o.addNested(new Output("stackTrace", bos.toString()));
231         o.addNested(new Output("errorMessage", error.getMessage()));
232     }
233 }
Popular Tags