KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > util > PublicationLog


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl.util;
17
18 import org.outerj.daisy.books.store.BookInstance;
19 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout;
20 import org.apache.commons.lang.exception.ExceptionUtils;
21
22 import java.io.OutputStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Date JavaDoc;
28
29 /**
30  * A logger to which progress and errors happening during the book publication are logged.
31  */

32 public class PublicationLog {
33     private PrintWriter JavaDoc pw;
34     private DateFormat JavaDoc dateFormat;
35
36     public PublicationLog(BookInstance bookInstance) throws Exception JavaDoc {
37         dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.US);
38         OutputStream JavaDoc os = bookInstance.getResourceOutputStream(BookInstanceLayout.getPublicationLogPath());
39         pw = new PrintWriter JavaDoc(os);
40     }
41
42     public void dispose() {
43         if (pw != null)
44             pw.close();
45     }
46
47     public void info(String JavaDoc message) {
48         output(message, "INFO ");
49         pw.flush();
50     }
51
52     public void error(String JavaDoc message) {
53         error(message, null);
54     }
55
56     public void error(String JavaDoc message, Throwable JavaDoc t) {
57         output(message, "ERROR");
58         if (t != null)
59             pw.println(getFullStackTrace(t));
60         pw.flush();
61     }
62
63     /**
64      * Modified version of ExceptionUtils.getFullStackTrace, which always
65      * prints the stack trace of all exceptions (this might lead to some
66      * duplicate stacktraces, but at least we see the whole exception chain then).
67      */

68     private String JavaDoc getFullStackTrace(Throwable JavaDoc throwable) {
69         StringWriter JavaDoc sw = new StringWriter JavaDoc();
70         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw, true);
71         Throwable JavaDoc[] ts = ExceptionUtils.getThrowables(throwable);
72         for (int i = 0; i < ts.length; i++) {
73             ts[i].printStackTrace(pw);
74         }
75         return sw.getBuffer().toString();
76     }
77
78     private void output(String JavaDoc message, String JavaDoc type) {
79         String JavaDoc date = dateFormat.format(new Date JavaDoc());
80         pw.println("[" + type + "] <" + date + "> " + message);
81     }
82 }
83
Popular Tags