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: JdkLogger.java 4274 2012-02-17 04:07:29Z schulte2005 $
032     *
033     */
034    // </editor-fold>
035    // SECTION-END
036    package org.jomc.logging.ri.jdk;
037    
038    import 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.0</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:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
057     * @version 1.1
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.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
064    // </editor-fold>
065    // SECTION-END
066    public 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.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
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 isDebugEnabled()
086        {
087            return this.getLogger().isLoggable( Level.FINE );
088        }
089    
090        public void debug( final String message )
091        {
092            this.log( Level.FINE, message, null );
093        }
094    
095        public void debug( final Throwable throwable )
096        {
097            this.log( Level.FINE, throwable.getMessage(), throwable );
098        }
099    
100        public void debug( final String message, final Throwable throwable )
101        {
102            this.log( Level.FINE, message, throwable );
103        }
104    
105        public boolean isErrorEnabled()
106        {
107            return this.getLogger().isLoggable( Level.SEVERE );
108        }
109    
110        public void error( final String message )
111        {
112            this.log( Level.SEVERE, message, null );
113        }
114    
115        public void error( final Throwable throwable )
116        {
117            this.log( Level.SEVERE, throwable.getMessage(), throwable );
118        }
119    
120        public void error( final String message, final Throwable throwable )
121        {
122            this.log( Level.SEVERE, message, throwable );
123        }
124    
125        public boolean isFatalEnabled()
126        {
127            return this.getLogger().isLoggable( Level.SEVERE );
128        }
129    
130        public void fatal( final String message )
131        {
132            this.log( Level.SEVERE, message, null );
133        }
134    
135        public void fatal( final Throwable throwable )
136        {
137            this.log( Level.SEVERE, throwable.getMessage(), throwable );
138        }
139    
140        public void fatal( final String message, final Throwable throwable )
141        {
142            this.log( Level.SEVERE, message, throwable );
143        }
144    
145        public boolean isInfoEnabled()
146        {
147            return this.getLogger().isLoggable( Level.INFO );
148        }
149    
150        public void info( final String message )
151        {
152            this.log( Level.INFO, message, null );
153        }
154    
155        public void info( final Throwable throwable )
156        {
157            this.log( Level.INFO, throwable.getMessage(), throwable );
158        }
159    
160        public void info( final String message, final Throwable throwable )
161        {
162            this.log( Level.INFO, message, throwable );
163        }
164    
165        public boolean isTraceEnabled()
166        {
167            return this.getLogger().isLoggable( Level.FINEST );
168        }
169    
170        public void trace( final String message )
171        {
172            this.log( Level.FINEST, message, null );
173        }
174    
175        public void trace( final Throwable throwable )
176        {
177            this.log( Level.FINEST, throwable.getMessage(), throwable );
178        }
179    
180        public void trace( final String message, final Throwable throwable )
181        {
182            this.log( Level.FINEST, message, throwable );
183        }
184    
185        public boolean isWarnEnabled()
186        {
187            return this.getLogger().isLoggable( Level.WARNING );
188        }
189    
190        public void warn( final String message )
191        {
192            this.log( Level.WARNING, message, null );
193        }
194    
195        public void warn( final Throwable throwable )
196        {
197            this.log( Level.WARNING, throwable.getMessage(), throwable );
198        }
199    
200        public void warn( final String message, final Throwable throwable )
201        {
202            this.log( Level.WARNING, message, throwable );
203        }
204    
205        // SECTION-END
206        // SECTION-START[Listener]
207        public void onLog( final Level level, final String message, final Throwable throwable )
208        {
209            if ( level != null )
210            {
211                if ( level.equals( Level.CONFIG ) || level.equals( Level.FINE ) )
212                {
213                    this.getObjectManagementLogger().debug( message, throwable );
214                }
215                else if ( level.equals( Level.FINER ) || level.equals( Level.FINEST ) )
216                {
217                    this.getObjectManagementLogger().trace( message, throwable );
218                }
219                else if ( level.equals( Level.INFO ) )
220                {
221                    this.getObjectManagementLogger().info( message, throwable );
222                }
223                else if ( level.equals( Level.SEVERE ) )
224                {
225                    this.getObjectManagementLogger().error( message, throwable );
226                }
227                else if ( level.equals( Level.WARNING ) )
228                {
229                    this.getObjectManagementLogger().warn( message, throwable );
230                }
231                else
232                {
233                    this.getObjectManagementLogger().trace( message, throwable );
234                }
235            }
236        }
237    
238        // SECTION-END
239        // SECTION-START[JdkLogger]
240        /**
241         * Gets the JDK logger backing the instance.
242         *
243         * @return The JDK logger backing the instance.
244         *
245         * @see #getName()
246         * @see java.util.logging.Logger#getLogger(java.lang.String)
247         */
248        public java.util.logging.Logger getLogger()
249        {
250            return java.util.logging.Logger.getLogger( this.getName() );
251        }
252    
253        private void log( final Level level, final String msg, final Throwable t )
254        {
255            if ( this.getLogger().isLoggable( level ) )
256            {
257                StackTraceElement caller;
258                final Throwable x = new Throwable();
259                final StackTraceElement[] elements = x.getStackTrace();
260    
261                String cname = "unknown";
262                String method = "unknown";
263    
264                if ( elements != null && elements.length >= this.getStackDepth() )
265                {
266                    caller = elements[this.getStackDepth()];
267                    cname = caller.getClassName();
268                    method = caller.getMethodName();
269                }
270    
271                if ( t == null )
272                {
273                    this.getLogger().logp( level, cname, method, msg );
274                }
275                else
276                {
277                    this.getLogger().logp( level, cname, method, msg, t );
278                }
279            }
280        }
281    
282        // SECTION-END
283        // SECTION-START[Dependencies]
284        // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
285        /**
286         * Gets the {@code <ObjectManagementLogger>} dependency.
287         * <p>
288         *   This method returns the {@code <JOMC Logging JDK Logging>} object of the {@code <org.jomc.logging.Logger>} specification at specification level 1.0.
289         *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
290         * </p>
291         * <p><strong>Properties:</strong>
292         *   <table border="1" width="100%" cellpadding="3" cellspacing="0">
293         *     <tr class="TableSubHeadingColor">
294         *       <th align="left" scope="col" nowrap><b>Name</b></th>
295         *       <th align="left" scope="col" nowrap><b>Type</b></th>
296         *       <th align="left" scope="col" nowrap><b>Documentation</b></th>
297         *     </tr>
298         *     <tr class="TableRow">
299         *       <td align="left" valign="top" nowrap>{@code <name>}</td>
300         *       <td align="left" valign="top" nowrap>{@code java.lang.String}</td>
301         *       <td align="left" valign="top">Name of the logger object management events are logged with.</td>
302         *     </tr>
303         *     <tr class="TableRow">
304         *       <td align="left" valign="top" nowrap>{@code <stackDepth>}</td>
305         *       <td align="left" valign="top" nowrap>{@code int}</td>
306         *       <td align="left" valign="top"></td>
307         *     </tr>
308         *   </table>
309         * </p>
310         * <dl>
311         *   <dt><b>Final:</b></dt><dd>No</dd>
312         * </dl>
313         * @return Logger object management events are logged with.
314         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
315         */
316        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
317        private org.jomc.logging.Logger getObjectManagementLogger()
318        {
319            final org.jomc.logging.Logger _d = (org.jomc.logging.Logger) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "ObjectManagementLogger" );
320            assert _d != null : "'ObjectManagementLogger' dependency not found.";
321            return _d;
322        }
323        // </editor-fold>
324        // SECTION-END
325        // SECTION-START[Properties]
326        // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
327        /**
328         * Gets the value of the {@code <name>} property.
329         * <p><dl>
330         *   <dt><b>Final:</b></dt><dd>No</dd>
331         * </dl></p>
332         * @return Name of the component events are logged for.
333         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
334         */
335        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
336        public java.lang.String getName()
337        {
338            final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "name" );
339            assert _p != null : "'name' property not found.";
340            return _p;
341        }
342        /**
343         * Gets the value of the {@code <stackDepth>} property.
344         * <p><dl>
345         *   <dt><b>Final:</b></dt><dd>No</dd>
346         * </dl></p>
347         * @return Number of frames between the logger and the frame of the caller.
348         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
349         */
350        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
351        private int getStackDepth()
352        {
353            final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "stackDepth" );
354            assert _p != null : "'stackDepth' property not found.";
355            return _p.intValue();
356        }
357        // </editor-fold>
358        // SECTION-END
359        // SECTION-START[Messages]
360        // SECTION-END
361    }