001    // SECTION-START[License Header]
002    /*
003     *   Copyright (c) 2009 The JOMC Project
004     *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
005     *   All rights reserved.
006     *
007     *   Redistribution and use in source and binary forms, with or without
008     *   modification, are permitted provided that the following conditions
009     *   are met:
010     *
011     *     o Redistributions of source code must retain the above copyright
012     *       notice, this list of conditions and the following disclaimer.
013     *
014     *     o Redistributions in binary form must reproduce the above copyright
015     *       notice, this list of conditions and the following disclaimer in
016     *       the documentation and/or other materials provided with the
017     *       distribution.
018     *
019     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
020     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
022     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
023     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
024     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
025     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
026     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
027     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
028     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030     *
031     *   $Id: JdkLogger.java 699 2009-10-04 02:05:15Z schulte2005 $
032     *
033     */
034    // SECTION-END
035    package org.jomc.logging.ri.jdk;
036    
037    import java.util.logging.Level;
038    
039    // SECTION-START[Documentation]
040    /**
041     * JDK logging system implementation.
042     * <p><b>Specifications</b><ul>
043     * <li>{@code org.jomc.logging.Logger} {@code 1.0} {@code Multiton}</li>
044     * </ul></p>
045     * <p><b>Properties</b><ul>
046     * <li>"{@link #getName name}"
047     * <blockquote>Property of type {@code java.lang.String}.
048     * <p>Name of the component events are logged for.</p>
049     * </blockquote></li>
050     * </ul></p>
051     *
052     * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0
053     * @version $Id: JdkLogger.java 699 2009-10-04 02:05:15Z schulte2005 $
054     */
055    // SECTION-END
056    // SECTION-START[Annotations]
057    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
058                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-4/jomc-tools" )
059    // SECTION-END
060    public class JdkLogger
061        implements
062        org.jomc.logging.Logger
063    {
064        // SECTION-START[Constructors]
065    
066        /** Creates a new {@code JdkLogger} instance. */
067        @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
068                                     comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-4/jomc-tools" )
069        public JdkLogger()
070        {
071            // SECTION-START[Default Constructor]
072            super();
073            // SECTION-END
074        }
075        // SECTION-END
076        // SECTION-START[JdkLogger]
077    
078        public boolean isDebugEnabled()
079        {
080            return this.getLogger().isLoggable( Level.FINE );
081        }
082    
083        public void debug( final String message )
084        {
085            this.log( Level.FINE, message, null );
086        }
087    
088        public void debug( final Throwable throwable )
089        {
090            this.log( Level.FINE, throwable.getMessage(), throwable );
091        }
092    
093        public void debug( final String message, final Throwable throwable )
094        {
095            this.log( Level.FINE, message, throwable );
096        }
097    
098        public boolean isErrorEnabled()
099        {
100            return this.getLogger().isLoggable( Level.SEVERE );
101        }
102    
103        public void error( final String message )
104        {
105            this.log( Level.SEVERE, message, null );
106        }
107    
108        public void error( final Throwable throwable )
109        {
110            this.log( Level.SEVERE, throwable.getMessage(), throwable );
111        }
112    
113        public void error( final String message, final Throwable throwable )
114        {
115            this.log( Level.SEVERE, message, throwable );
116        }
117    
118        public boolean isFatalEnabled()
119        {
120            return this.getLogger().isLoggable( Level.SEVERE );
121        }
122    
123        public void fatal( final String message )
124        {
125            this.log( Level.SEVERE, message, null );
126        }
127    
128        public void fatal( final Throwable throwable )
129        {
130            this.log( Level.SEVERE, throwable.getMessage(), throwable );
131        }
132    
133        public void fatal( final String message, final Throwable throwable )
134        {
135            this.log( Level.SEVERE, message, throwable );
136        }
137    
138        public boolean isInfoEnabled()
139        {
140            return this.getLogger().isLoggable( Level.INFO );
141        }
142    
143        public void info( final String message )
144        {
145            this.log( Level.INFO, message, null );
146        }
147    
148        public void info( final Throwable throwable )
149        {
150            this.log( Level.INFO, throwable.getMessage(), throwable );
151        }
152    
153        public void info( final String message, final Throwable throwable )
154        {
155            this.log( Level.INFO, message, throwable );
156        }
157    
158        public boolean isTraceEnabled()
159        {
160            return this.getLogger().isLoggable( Level.FINEST );
161        }
162    
163        public void trace( final String message )
164        {
165            this.log( Level.FINEST, message, null );
166        }
167    
168        public void trace( final Throwable throwable )
169        {
170            this.log( Level.FINEST, throwable.getMessage(), throwable );
171        }
172    
173        public void trace( final String message, final Throwable throwable )
174        {
175            this.log( Level.FINEST, message, throwable );
176        }
177    
178        public boolean isWarnEnabled()
179        {
180            return this.getLogger().isLoggable( Level.WARNING );
181        }
182    
183        public void warn( final String message )
184        {
185            this.log( Level.WARNING, message, null );
186        }
187    
188        public void warn( final Throwable throwable )
189        {
190            this.log( Level.WARNING, throwable.getMessage(), throwable );
191        }
192    
193        public void warn( final String message, final Throwable throwable )
194        {
195            this.log( Level.WARNING, message, throwable );
196        }
197    
198        /**
199         * Requests the JDK logger for the name given by property {@code name}.
200         *
201         * @return the JDK logger for the name given by property {@code name}.
202         */
203        public java.util.logging.Logger getLogger()
204        {
205            return java.util.logging.Logger.getLogger( this.getName() );
206        }
207    
208        private void log( final Level level, final String msg, final Throwable t )
209        {
210            if ( this.getLogger().isLoggable( level ) )
211            {
212                StackTraceElement caller;
213                final Throwable x = new Throwable();
214                final StackTraceElement[] elements = x.getStackTrace();
215    
216                String cname = "unknown";
217                String method = "unknown";
218    
219                if ( elements != null && elements.length > 2 )
220                {
221                    caller = elements[2];
222                    cname = caller.getClassName();
223                    method = caller.getMethodName();
224                }
225    
226                if ( t == null )
227                {
228                    this.getLogger().logp( level, cname, method, msg );
229                }
230                else
231                {
232                    this.getLogger().logp( level, cname, method, msg, t );
233                }
234            }
235        }
236    
237        // SECTION-END
238        // SECTION-START[Dependencies]
239        // SECTION-END
240        // SECTION-START[Properties]
241    
242        /**
243         * Gets the value of the {@code name} property.
244         * @return Name of the component events are logged for.
245         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
246         */
247        @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
248                                     comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-4/jomc-tools" )
249        public java.lang.String getName()
250        {
251            final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager().getProperty( this, "name" );
252            assert _p != null : "'name' property not found.";
253            return _p;
254        }
255        // SECTION-END
256        // SECTION-START[Messages]
257        // SECTION-END
258    }