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$
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.1</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.3
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.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
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.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
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 isConfigEnabled()
86      {
87          return this.getLogger().isLoggable( Level.CONFIG );
88      }
89  
90      public void config( final String message )
91      {
92          this.log( Level.CONFIG, message, null );
93      }
94  
95      public void config( final Throwable throwable )
96      {
97          this.log( Level.CONFIG, throwable.getMessage(), throwable );
98      }
99  
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 }