View Javadoc

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: JdkLogger.java 4723 2013-01-03 08:50:05Z schulte $
32   *
33   */
34  // </editor-fold>
35  // SECTION-END
36  package org.jomc.logging.ri.jdk;
37  
38  import java.util.logging.Level;
39  
40  // SECTION-START[Documentation]
41  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
42  /**
43   * Object management and configuration logging system implementation backed by JDK Logging.
44   *
45   * <dl>
46   *   <dt><b>Identifier:</b></dt><dd>org.jomc.logging.ri.jdk.JdkLogger</dd>
47   *   <dt><b>Name:</b></dt><dd>JOMC Logging ⁑ JDK Logging</dd>
48   *   <dt><b>Specifications:</b></dt>
49   *     <dd>org.jomc.logging.Logger @ 1.0</dd>
50   *     <dd>org.jomc.spi.Listener @ 1.0</dd>
51   *   <dt><b>Abstract:</b></dt><dd>No</dd>
52   *   <dt><b>Final:</b></dt><dd>No</dd>
53   *   <dt><b>Stateless:</b></dt><dd>Yes</dd>
54   * </dl>
55   *
56   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
57   * @version 1.2
58   */
59  // </editor-fold>
60  // SECTION-END
61  // SECTION-START[Annotations]
62  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
63  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
64  // </editor-fold>
65  // SECTION-END
66  public final class JdkLogger
67      implements
68      org.jomc.logging.Logger,
69      org.jomc.spi.Listener
70  {
71      // SECTION-START[Constructors]
72      // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
73      /** Creates a new {@code JdkLogger} instance. */
74      @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
75      public JdkLogger()
76      {
77          // SECTION-START[Default Constructor]
78          super();
79          // SECTION-END
80      }
81      // </editor-fold>
82      // SECTION-END
83      // SECTION-START[Logger]
84  
85      public boolean isDebugEnabled()
86      {
87          return this.getLogger().isLoggable( Level.FINE );
88      }
89  
90      public void debug( final String message )
91      {
92          this.log( Level.FINE, message, null );
93      }
94  
95      public void debug( final Throwable throwable )
96      {
97          this.log( Level.FINE, throwable.getMessage(), throwable );
98      }
99  
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     @SuppressWarnings("unused")
317     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
318     private org.jomc.logging.Logger getObjectManagementLogger()
319     {
320         final org.jomc.logging.Logger _d = (org.jomc.logging.Logger) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "ObjectManagementLogger" );
321         assert _d != null : "'ObjectManagementLogger' dependency not found.";
322         return _d;
323     }
324     // </editor-fold>
325     // SECTION-END
326     // SECTION-START[Properties]
327     // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
328     /**
329      * Gets the value of the {@code <name>} property.
330      * <p><dl>
331      *   <dt><b>Final:</b></dt><dd>No</dd>
332      * </dl></p>
333      * @return Name of the component events are logged for.
334      * @throws org.jomc.ObjectManagementException if getting the property instance fails.
335      */
336     @SuppressWarnings("unused")
337     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
338     public java.lang.String getName()
339     {
340         final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "name" );
341         assert _p != null : "'name' property not found.";
342         return _p;
343     }
344     /**
345      * Gets the value of the {@code <stackDepth>} property.
346      * <p><dl>
347      *   <dt><b>Final:</b></dt><dd>No</dd>
348      * </dl></p>
349      * @return Number of frames between the logger and the frame of the caller.
350      * @throws org.jomc.ObjectManagementException if getting the property instance fails.
351      */
352     @SuppressWarnings("unused")
353     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
354     private int getStackDepth()
355     {
356         final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "stackDepth" );
357         assert _p != null : "'stackDepth' property not found.";
358         return _p.intValue();
359     }
360     // </editor-fold>
361     // SECTION-END
362     // SECTION-START[Messages]
363     // SECTION-END
364 }