001// SECTION-START[License Header]
002// <editor-fold defaultstate="collapsed" desc=" Generated License ">
003/*
004 *   Java Object Management and Configuration
005 *   Copyright (C) Christian Schulte, 2005-206
006 *   All rights reserved.
007 *
008 *   Redistribution and use in source and binary forms, with or without
009 *   modification, are permitted provided that the following conditions
010 *   are met:
011 *
012 *     o Redistributions of source code must retain the above copyright
013 *       notice, this list of conditions and the following disclaimer.
014 *
015 *     o Redistributions in binary form must reproduce the above copyright
016 *       notice, this list of conditions and the following disclaimer in
017 *       the documentation and/or other materials provided with the
018 *       distribution.
019 *
020 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
021 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
022 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
023 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
024 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
025 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
029 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 *
031 *   $JOMC$
032 *
033 */
034// </editor-fold>
035// SECTION-END
036package org.jomc.logging.ri.jdk;
037
038import java.util.logging.Level;
039
040// SECTION-START[Documentation]
041// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
042/**
043 * Object management and configuration logging system implementation backed by JDK Logging.
044 *
045 * <dl>
046 *   <dt><b>Identifier:</b></dt><dd>org.jomc.logging.ri.jdk.JdkLogger</dd>
047 *   <dt><b>Name:</b></dt><dd>JOMC Logging ⁑ JDK Logging</dd>
048 *   <dt><b>Specifications:</b></dt>
049 *     <dd>org.jomc.logging.Logger @ 1.1</dd>
050 *     <dd>org.jomc.spi.Listener @ 1.0</dd>
051 *   <dt><b>Abstract:</b></dt><dd>No</dd>
052 *   <dt><b>Final:</b></dt><dd>No</dd>
053 *   <dt><b>Stateless:</b></dt><dd>Yes</dd>
054 * </dl>
055 *
056 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
057 * @version 1.3
058 */
059// </editor-fold>
060// SECTION-END
061// SECTION-START[Annotations]
062// <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
063@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
064// </editor-fold>
065// SECTION-END
066public final class JdkLogger
067    implements
068    org.jomc.logging.Logger,
069    org.jomc.spi.Listener
070{
071    // SECTION-START[Constructors]
072    // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
073    /** Creates a new {@code JdkLogger} instance. */
074    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
075    public JdkLogger()
076    {
077        // SECTION-START[Default Constructor]
078        super();
079        // SECTION-END
080    }
081    // </editor-fold>
082    // SECTION-END
083    // SECTION-START[Logger]
084
085    public boolean isConfigEnabled()
086    {
087        return this.getLogger().isLoggable( Level.CONFIG );
088    }
089
090    public void config( final String message )
091    {
092        this.log( Level.CONFIG, message, null );
093    }
094
095    public void config( final Throwable throwable )
096    {
097        this.log( Level.CONFIG, throwable.getMessage(), throwable );
098    }
099
100    public void config( final String message, final Throwable throwable )
101    {
102        this.log( Level.CONFIG, message, throwable );
103    }
104
105    public boolean isDebugEnabled()
106    {
107        return this.getLogger().isLoggable( Level.FINE );
108    }
109
110    public void debug( final String message )
111    {
112        this.log( Level.FINE, message, null );
113    }
114
115    public void debug( final Throwable throwable )
116    {
117        this.log( Level.FINE, throwable.getMessage(), throwable );
118    }
119
120    public void debug( final String message, final Throwable throwable )
121    {
122        this.log( Level.FINE, message, throwable );
123    }
124
125    public boolean isErrorEnabled()
126    {
127        return this.getLogger().isLoggable( Level.SEVERE );
128    }
129
130    public void error( final String message )
131    {
132        this.log( Level.SEVERE, message, null );
133    }
134
135    public void error( final Throwable throwable )
136    {
137        this.log( Level.SEVERE, throwable.getMessage(), throwable );
138    }
139
140    public void error( final String message, final Throwable throwable )
141    {
142        this.log( Level.SEVERE, message, throwable );
143    }
144
145    public boolean isFatalEnabled()
146    {
147        return this.getLogger().isLoggable( Level.SEVERE );
148    }
149
150    public void fatal( final String message )
151    {
152        this.log( Level.SEVERE, message, null );
153    }
154
155    public void fatal( final Throwable throwable )
156    {
157        this.log( Level.SEVERE, throwable.getMessage(), throwable );
158    }
159
160    public void fatal( final String message, final Throwable throwable )
161    {
162        this.log( Level.SEVERE, message, throwable );
163    }
164
165    public boolean isInfoEnabled()
166    {
167        return this.getLogger().isLoggable( Level.INFO );
168    }
169
170    public void info( final String message )
171    {
172        this.log( Level.INFO, message, null );
173    }
174
175    public void info( final Throwable throwable )
176    {
177        this.log( Level.INFO, throwable.getMessage(), throwable );
178    }
179
180    public void info( final String message, final Throwable throwable )
181    {
182        this.log( Level.INFO, message, throwable );
183    }
184
185    public boolean isTraceEnabled()
186    {
187        return this.getLogger().isLoggable( Level.FINEST );
188    }
189
190    public void trace( final String message )
191    {
192        this.log( Level.FINEST, message, null );
193    }
194
195    public void trace( final Throwable throwable )
196    {
197        this.log( Level.FINEST, throwable.getMessage(), throwable );
198    }
199
200    public void trace( final String message, final Throwable throwable )
201    {
202        this.log( Level.FINEST, message, throwable );
203    }
204
205    public boolean isWarnEnabled()
206    {
207        return this.getLogger().isLoggable( Level.WARNING );
208    }
209
210    public void warn( final String message )
211    {
212        this.log( Level.WARNING, message, null );
213    }
214
215    public void warn( final Throwable throwable )
216    {
217        this.log( Level.WARNING, throwable.getMessage(), throwable );
218    }
219
220    public void warn( final String message, final Throwable throwable )
221    {
222        this.log( Level.WARNING, message, throwable );
223    }
224
225    // SECTION-END
226    // SECTION-START[Listener]
227    public void onLog( final Level level, final String message, final Throwable throwable )
228    {
229        if ( level != null )
230        {
231            if ( level.intValue() <= Level.FINEST.intValue() )
232            {
233                this.getObjectManagementLogger().trace( message, throwable );
234            }
235            else if ( level.intValue() <= Level.FINER.intValue() )
236            {
237                this.getObjectManagementLogger().debug( message, throwable );
238            }
239            else if ( level.intValue() <= Level.FINE.intValue() )
240            {
241                this.getObjectManagementLogger().debug( message, throwable );
242            }
243            else if ( level.intValue() <= Level.CONFIG.intValue() )
244            {
245                this.getObjectManagementLogger().config( message, throwable );
246            }
247            else if ( level.intValue() <= Level.INFO.intValue() )
248            {
249                this.getObjectManagementLogger().info( message, throwable );
250            }
251            else if ( level.intValue() <= Level.WARNING.intValue() )
252            {
253                this.getObjectManagementLogger().warn( message, throwable );
254            }
255            else if ( level.intValue() <= Level.SEVERE.intValue() )
256            {
257                this.getObjectManagementLogger().error( message, throwable );
258            }
259            else if ( level.intValue() < Level.OFF.intValue() )
260            {
261                this.getObjectManagementLogger().fatal( message, throwable );
262            }
263        }
264    }
265    // SECTION-END
266    // SECTION-START[JdkLogger]
267
268    /**
269     * Gets the JDK logger backing the instance.
270     *
271     * @return The JDK logger backing the instance.
272     *
273     * @see #getName()
274     * @see java.util.logging.Logger#getLogger(java.lang.String)
275     */
276    public java.util.logging.Logger getLogger()
277    {
278        return java.util.logging.Logger.getLogger( this.getName() );
279    }
280
281    private void log( final Level level, final String msg, final Throwable t )
282    {
283        if ( this.getLogger().isLoggable( level ) )
284        {
285            StackTraceElement caller;
286            final Throwable x = new Throwable();
287            final StackTraceElement[] elements = x.getStackTrace();
288
289            String cname = "unknown";
290            String method = "unknown";
291
292            if ( elements != null && elements.length >= this.getStackDepth() )
293            {
294                caller = elements[this.getStackDepth()];
295                cname = caller.getClassName();
296                method = caller.getMethodName();
297            }
298
299            if ( t == null )
300            {
301                this.getLogger().logp( level, cname, method, msg );
302            }
303            else
304            {
305                this.getLogger().logp( level, cname, method, msg, t );
306            }
307        }
308    }
309
310    // SECTION-END
311    // SECTION-START[Dependencies]
312    // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
313    /**
314     * Gets the {@code <ObjectManagementLogger>} dependency.
315     * <p>
316     *   This method returns the {@code <JOMC Logging ⁑ JDK Logging>} object of the {@code <org.jomc.logging.Logger>} specification at specification level 1.1.
317     *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
318     * </p>
319     * <p><strong>Properties:</strong>
320     *   <table border="1" width="100%" cellpadding="3" cellspacing="0">
321     *     <tr class="TableSubHeadingColor">
322     *       <th align="left" scope="col" nowrap><b>Name</b></th>
323     *       <th align="left" scope="col" nowrap><b>Type</b></th>
324     *       <th align="left" scope="col" nowrap><b>Documentation</b></th>
325     *     </tr>
326     *     <tr class="TableRow">
327     *       <td align="left" valign="top" nowrap>{@code <name>}</td>
328     *       <td align="left" valign="top" nowrap>{@code java.lang.String}</td>
329     *       <td align="left" valign="top">Name of the logger object management events are logged with.</td>
330     *     </tr>
331     *     <tr class="TableRow">
332     *       <td align="left" valign="top" nowrap>{@code <stackDepth>}</td>
333     *       <td align="left" valign="top" nowrap>{@code int}</td>
334     *       <td align="left" valign="top"></td>
335     *     </tr>
336     *   </table>
337     * </p>
338     * <dl>
339     *   <dt><b>Final:</b></dt><dd>No</dd>
340     * </dl>
341     * @return Logger object management events are logged with.
342     * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
343     */
344    @SuppressWarnings("unused")
345    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
346    private org.jomc.logging.Logger getObjectManagementLogger()
347    {
348        final org.jomc.logging.Logger _d = (org.jomc.logging.Logger) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "ObjectManagementLogger" );
349        assert _d != null : "'ObjectManagementLogger' dependency not found.";
350        return _d;
351    }
352    // </editor-fold>
353    // SECTION-END
354    // SECTION-START[Properties]
355    // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
356    /**
357     * Gets the value of the {@code <name>} property.
358     * <p><dl>
359     *   <dt><b>Final:</b></dt><dd>No</dd>
360     * </dl></p>
361     * @return Name of the component events are logged for.
362     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
363     */
364    @SuppressWarnings("unused")
365    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
366    public java.lang.String getName()
367    {
368        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "name" );
369        assert _p != null : "'name' property not found.";
370        return _p;
371    }
372    /**
373     * Gets the value of the {@code <stackDepth>} property.
374     * <p><dl>
375     *   <dt><b>Final:</b></dt><dd>No</dd>
376     * </dl></p>
377     * @return Number of frames between the logger and the frame of the caller.
378     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
379     */
380    @SuppressWarnings("unused")
381    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
382    private int getStackDepth()
383    {
384        final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "stackDepth" );
385        assert _p != null : "'stackDepth' property not found.";
386        return _p.intValue();
387    }
388    // </editor-fold>
389    // SECTION-END
390    // SECTION-START[Messages]
391    // SECTION-END
392}