1 | // SECTION-START[License Header] |
2 | // <editor-fold defaultstate="collapsed" desc=" Generated License "> |
3 | /* |
4 | * Java Object Management and Configuration |
5 | * Copyright (C) Christian Schulte, 2005-206 |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * |
12 | * o Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * |
15 | * o Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in |
17 | * the documentation and/or other materials provided with the |
18 | * distribution. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
21 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
22 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, |
24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | * |
31 | * $JOMC: DefaultListener.java 4881 2014-02-20 22:55:53Z schulte $ |
32 | * |
33 | */ |
34 | // </editor-fold> |
35 | // SECTION-END |
36 | package org.jomc.ri; |
37 | |
38 | import java.io.BufferedReader; |
39 | import java.io.IOException; |
40 | import java.io.PrintStream; |
41 | import java.io.PrintWriter; |
42 | import java.io.StringReader; |
43 | import java.io.StringWriter; |
44 | import java.util.logging.Level; |
45 | import org.jomc.spi.Listener; |
46 | |
47 | // SECTION-START[Documentation] |
48 | // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> |
49 | /** |
50 | * Default {@code Listener} implementation. |
51 | * |
52 | * <dl> |
53 | * <dt><b>Identifier:</b></dt><dd>org.jomc.ri.DefaultListener</dd> |
54 | * <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ DefaultListener</dd> |
55 | * <dt><b>Abstract:</b></dt><dd>No</dd> |
56 | * <dt><b>Final:</b></dt><dd>No</dd> |
57 | * <dt><b>Stateless:</b></dt><dd>No</dd> |
58 | * </dl> |
59 | * |
60 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.1 |
61 | * @version 1.1 |
62 | */ |
63 | // </editor-fold> |
64 | // SECTION-END |
65 | // SECTION-START[Annotations] |
66 | // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> |
67 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.6", comments = "See http://www.jomc.org/jomc/1.6/jomc-tools-1.6" ) |
68 | // </editor-fold> |
69 | // SECTION-END |
70 | public class DefaultListener implements Listener |
71 | { |
72 | // SECTION-START[DefaultListener] |
73 | |
74 | /** Constant holding the platforms line separator. */ |
75 | private static final String LINE_SEPARATOR = System.getProperty( "line.separator", "\n" ); |
76 | |
77 | /** Flag indicating the listener is enabled by default. */ |
78 | private static volatile Boolean defaultEnabled; |
79 | |
80 | /** Flag indicating the listener is enabled. */ |
81 | private Boolean enabled; |
82 | |
83 | /** |
84 | * Gets a flag indicating the listener is enabled by default. |
85 | * <p>The default enabled flag is controlled by system property |
86 | * {@code org.jomc.ri.DefaultListener.defaultEnabled} holding a value indicating the listener is enabled by default. |
87 | * If that property is not set, the {@code true} default is returned.</p> |
88 | * |
89 | * @return {@code true}, if the listener is enabled by default; {@code false}, if the listener is disabled by |
90 | * default. |
91 | * |
92 | * @see #setDefaultEnabled(java.lang.Boolean) |
93 | */ |
94 | public static boolean isDefaultEnabled() |
95 | { |
96 | if ( defaultEnabled == null ) |
97 | { |
98 | defaultEnabled = Boolean.valueOf( System.getProperty( |
99 | "org.jomc.ri.DefaultListener.defaultEnabled", Boolean.toString( true ) ) ); |
100 | |
101 | } |
102 | |
103 | return defaultEnabled; |
104 | } |
105 | |
106 | /** |
107 | * Sets the flag indicating the listener is enabled by default. |
108 | * |
109 | * @param value The new value of the flag indicating the listener is enabled by default or {@code null}. |
110 | * |
111 | * @see #isDefaultEnabled() |
112 | */ |
113 | public static void setDefaultEnabled( final Boolean value ) |
114 | { |
115 | defaultEnabled = value; |
116 | } |
117 | |
118 | /** |
119 | * Gets a flag indicating the listener is enabled. |
120 | * |
121 | * @return {@code true}, if the listener is enabled; {@code false}, if the listener is disabled. |
122 | * |
123 | * @see #isDefaultEnabled() |
124 | * @see #setEnabled(java.lang.Boolean) |
125 | */ |
126 | public final boolean isEnabled() |
127 | { |
128 | if ( this.enabled == null ) |
129 | { |
130 | this.enabled = isDefaultEnabled(); |
131 | } |
132 | |
133 | return this.enabled; |
134 | } |
135 | |
136 | /** |
137 | * Sets the flag indicating the listener is enabled. |
138 | * |
139 | * @param value The new value of the flag indicating the listener is enabled or {@code null}. |
140 | * |
141 | * @see #isEnabled() |
142 | */ |
143 | public final void setEnabled( final Boolean value ) |
144 | { |
145 | this.enabled = value; |
146 | } |
147 | |
148 | /** |
149 | * Gets called on logging. |
150 | * <p>This method prints messages to the "standard" system streams. Messages with a level greater than |
151 | * {@code WARNING} are printed to the system error stream. All other messages are printed to the system output |
152 | * stream.</p> |
153 | * |
154 | * @param level The level of the event. |
155 | * @param message The message of the event or {@code null}. |
156 | * @param throwable The throwable of the event or {@code null}. |
157 | * |
158 | * @throws NullPointerException if {@code level} is {@code null}. |
159 | * |
160 | * @see #isEnabled() |
161 | */ |
162 | public void onLog( final Level level, final String message, final Throwable throwable ) |
163 | { |
164 | if ( level == null ) |
165 | { |
166 | throw new NullPointerException( "level" ); |
167 | } |
168 | |
169 | if ( this.isEnabled() ) |
170 | { |
171 | // JDK: As of JDK 6, "System.console().writer()". |
172 | final PrintStream out = level.intValue() > Level.WARNING.intValue() ? System.err : System.out; |
173 | |
174 | try |
175 | { |
176 | if ( message != null ) |
177 | { |
178 | out.print( this.splitLines( level, message ) ); |
179 | out.flush(); |
180 | } |
181 | |
182 | if ( throwable != null ) |
183 | { |
184 | final StringWriter stringWriter = new StringWriter(); |
185 | final PrintWriter printWriter = new PrintWriter( stringWriter ); |
186 | throwable.printStackTrace( printWriter ); |
187 | printWriter.close(); |
188 | out.print( this.splitLines( level, stringWriter.toString() ) ); |
189 | out.flush(); |
190 | } |
191 | } |
192 | catch ( final IOException e ) |
193 | { |
194 | e.printStackTrace(); |
195 | } |
196 | } |
197 | } |
198 | |
199 | private StringBuilder splitLines( final Level level, final String message ) throws IOException |
200 | { |
201 | BufferedReader reader = null; |
202 | boolean suppressExceptionOnClose = true; |
203 | |
204 | try |
205 | { |
206 | final String linePrefix = "[" + level.getLocalizedName() + "] "; |
207 | final StringBuilder b = new StringBuilder( message.length() ); |
208 | reader = new BufferedReader( new StringReader( message ) ); |
209 | |
210 | String line; |
211 | while ( ( line = reader.readLine() ) != null ) |
212 | { |
213 | b.append( linePrefix ).append( line ).append( LINE_SEPARATOR ); |
214 | } |
215 | |
216 | suppressExceptionOnClose = false; |
217 | return b; |
218 | } |
219 | finally |
220 | { |
221 | try |
222 | { |
223 | if ( reader != null ) |
224 | { |
225 | reader.close(); |
226 | } |
227 | } |
228 | catch ( final IOException e ) |
229 | { |
230 | if ( !suppressExceptionOnClose ) |
231 | { |
232 | throw e; |
233 | } |
234 | } |
235 | } |
236 | } |
237 | |
238 | // SECTION-END |
239 | // SECTION-START[Constructors] |
240 | // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> |
241 | /** Creates a new {@code DefaultListener} instance. */ |
242 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.6", comments = "See http://www.jomc.org/jomc/1.6/jomc-tools-1.6" ) |
243 | public DefaultListener() |
244 | { |
245 | // SECTION-START[Default Constructor] |
246 | super(); |
247 | // SECTION-END |
248 | } |
249 | // </editor-fold> |
250 | // SECTION-END |
251 | // SECTION-START[Dependencies] |
252 | // SECTION-END |
253 | // SECTION-START[Properties] |
254 | // SECTION-END |
255 | // SECTION-START[Messages] |
256 | // SECTION-END |
257 | } |