1 | /* |
2 | * Copyright 2005-2006 The RbUtils Project |
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 | */ |
17 | |
18 | // $Id: AbstractDefaultLogger.java,v 1.5 2007/07/10 00:10:36 ktcguru Exp $ |
19 | |
20 | package org.ktc.rbutils.api.audit; |
21 | |
22 | import java.io.OutputStream; |
23 | import java.io.PrintWriter; |
24 | import org.apache.commons.io.IOUtils; |
25 | import org.ktc.rbutils.RbUtilsHelper; |
26 | import org.ktc.rbutils.api.audit.event.ProcessEndEvent; |
27 | import org.ktc.rbutils.api.audit.event.ProcessEvent; |
28 | import org.ktc.rbutils.api.audit.event.ProcessFileEndEvent; |
29 | import org.ktc.rbutils.api.audit.event.ProcessFileStartEvent; |
30 | import org.ktc.rbutils.api.audit.event.ProcessStartEvent; |
31 | import org.ktc.rbutils.api.i18n.AbstractLocalized; |
32 | import org.ktc.rbutils.api.i18n.Message; |
33 | |
34 | /** |
35 | * Abstract class for file logging (plain text). |
36 | * @since RbUtils 0.8.2 |
37 | * @version $Revision: 1.5 $ |
38 | * @author moishi |
39 | */ |
40 | public abstract class AbstractDefaultLogger extends AbstractLocalized implements Logger { |
41 | |
42 | /** Where to write info messages. */ |
43 | protected PrintWriter infoWriter; |
44 | /** Close info stream after use. */ |
45 | protected boolean closeInfo; |
46 | /** Where to write error messages. */ |
47 | protected PrintWriter errorWriter; |
48 | /** Close error stream after use. */ |
49 | protected boolean closeError; |
50 | /** Name of the task that manages the process. This <b>MUST</b> be set by subclasses. */ |
51 | // TODO Code - refactoring: should be refactor with ant task (use the tasktype) |
52 | protected String taskname; |
53 | |
54 | /** Relative path (from the root directory of the process) to the file being processed. |
55 | * @since RbUtils 0.11 |
56 | */ |
57 | protected String currentFilePath; |
58 | |
59 | /** |
60 | * Creates a new <code>DefaultLogger</code> instance. |
61 | * TODO Javadoc. |
62 | * @param infoStream the <code>OutputStream</code> for info messages |
63 | * @param closeInfoAfterUse auditFinished should close aInfoStream |
64 | * @param errorStream the <code>OutputStream</code> for error messages |
65 | * @param closeErrorAfterUse auditFinished should close aErrorStream |
66 | */ |
67 | public AbstractDefaultLogger(final OutputStream infoStream, final boolean closeInfoAfterUse, |
68 | final OutputStream errorStream, final boolean closeErrorAfterUse) |
69 | { |
70 | closeInfo = closeInfoAfterUse; |
71 | closeError = closeErrorAfterUse; |
72 | infoWriter = new PrintWriter(infoStream); |
73 | if (infoStream == errorStream) { |
74 | errorWriter = infoWriter; |
75 | } |
76 | else { |
77 | errorWriter = new PrintWriter(errorStream); |
78 | } |
79 | |
80 | // Set the resource class for messages |
81 | classResource = defaultlogger.class.getName(); |
82 | } |
83 | |
84 | /** |
85 | * Flushes and closes (if asked) the info and error streams. |
86 | */ |
87 | protected void flushAndCloseStreams() { |
88 | infoWriter.flush(); |
89 | if (closeInfo) { |
90 | IOUtils.closeQuietly(infoWriter); |
91 | } |
92 | |
93 | errorWriter.flush(); |
94 | if (closeError) { |
95 | IOUtils.closeQuietly(errorWriter); |
96 | } |
97 | } |
98 | |
99 | /** {@inheritDoc} */ |
100 | public void addException(final Exception exception) { |
101 | // We synchronized on errorWriter to be sured that the stack in written in one shot |
102 | // No outside messages will be found in the stack |
103 | synchronized (errorWriter) { |
104 | final String message = exception.getMessage(); |
105 | errorWriter.println(getMsgWithFilePathPrefix(message)); |
106 | exception.printStackTrace(errorWriter); |
107 | } |
108 | } |
109 | |
110 | /** {@inheritDoc} */ |
111 | public void processStarted(final ProcessStartEvent event) { |
112 | //TODO Code: should use CommonProperties |
113 | final String version = RbUtilsHelper.getVersion(); |
114 | final String rootMsg = taskname + " (" + version + ")"; |
115 | |
116 | final Object[] args = {rootMsg, event.getRootDirectory().getAbsolutePath()}; |
117 | final Message message = getMessage(defaultlogger.PROCESS_START, args); |
118 | final String displayMsg = message.getMessage(); |
119 | |
120 | infoWriter.println(displayMsg); |
121 | } |
122 | |
123 | /** {@inheritDoc} */ |
124 | public void processEnded(final ProcessEndEvent event) { |
125 | final Object[] args = {event.getRootDirectory().getAbsolutePath()}; |
126 | final Message message = getMessage(defaultlogger.PROCESS_END, args); |
127 | final String displayMsg = message.getMessage(); |
128 | |
129 | infoWriter.println(displayMsg); |
130 | flushAndCloseStreams(); |
131 | } |
132 | |
133 | /** {@inheritDoc} */ |
134 | public void processFileStarted(final ProcessFileStartEvent event) { |
135 | // RBUTILS-027, prefix error messages with the rel path to the current file |
136 | currentFilePath = event.getFileName(); |
137 | |
138 | // final Object[] args = {event.getFileName(), event.getClassName(), event.getLocale()}; |
139 | // final Message message = getMessage(defaultlogger.PROCESS_FILE_START, args); |
140 | // final String displayMsg = message.getMessage(); |
141 | // infoWriter.println(displayMsg); |
142 | } |
143 | |
144 | /** {@inheritDoc} */ |
145 | public void processFileEnded(final ProcessFileEndEvent event) { |
146 | // RBUTILS-027, prefix error messages with the rel path to the current file |
147 | currentFilePath = null; |
148 | |
149 | // final Object[] args = {event.getFileName()}; |
150 | // final Message message = getMessage(defaultlogger.PROCESS_FILE_END, args); |
151 | // final String displayMsg = message.getMessage(); |
152 | // infoWriter.println(displayMsg); |
153 | } |
154 | |
155 | /** |
156 | * Does nothing. Lets subclasses implements this method if needed. {@inheritDoc} |
157 | */ |
158 | public void addError(final ProcessEvent event) { |
159 | // Do Nothing |
160 | } |
161 | |
162 | /** |
163 | * Does nothing. Lets subclasses implements this method if needed. {@inheritDoc} |
164 | */ |
165 | public void addInfo(final ProcessEvent event) { |
166 | // Do Nothing |
167 | } |
168 | |
169 | /** |
170 | * Add the current file path to the given message. |
171 | * @param message the message to be prefixed. |
172 | * @return the message with the path prefix. |
173 | * @since RbUtils 0.11 |
174 | */ |
175 | protected String getMsgWithFilePathPrefix(final String message) { |
176 | return currentFilePath + ": " + message; |
177 | } |
178 | } |