001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Copyright (c) 2010 The JOMC Project
005     *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
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 BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     *
032     *   $Id$
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.logging.ri.log4j;
038    
039    import java.util.logging.Level;
040    
041    // SECTION-START[Documentation]
042    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
043    /**
044     * Log4J logging system implementation.
045     * <p><b>Specifications</b><ul>
046     * <li>{@code org.jomc.logging.Logger} {@code 1.0} {@code Multiton}</li>
047     * <li>{@code org.jomc.spi.Listener} {@code 1.0} {@code Multiton}</li>
048     * </ul></p>
049     * <p><b>Properties</b><ul>
050     * <li>"{@link #getName name}"
051     * <blockquote>Property of type {@code java.lang.String}.
052     * <p>Name of the component events are logged for.</p>
053     * </blockquote></li>
054     * </ul></p>
055     * <p><b>Dependencies</b><ul>
056     * <li>"{@link #getObjectManagementLogger ObjectManagementLogger}"<blockquote>
057     * Dependency on {@code org.jomc.logging.Logger} at specification level 1.0 bound to an instance.</blockquote></li>
058     * </ul></p>
059     *
060     * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0
061     * @version $Id$
062     */
063    // </editor-fold>
064    // SECTION-END
065    // SECTION-START[Annotations]
066    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
067    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
068                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-16/jomc-tools" )
069    // </editor-fold>
070    // SECTION-END
071    public class Log4JLogger
072        implements org.jomc.logging.Logger,
073                   org.jomc.spi.Listener
074    {
075        // SECTION-START[Constructors]
076        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
077    
078        /** Creates a new {@code Log4JLogger} instance. */
079        @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
080                                     comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-16/jomc-tools" )
081        public Log4JLogger()
082        {
083            // SECTION-START[Default Constructor]
084            super();
085            // SECTION-END
086        }
087        // </editor-fold>
088        // SECTION-END
089        // SECTION-START[Dependencies]
090        // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
091    
092        /**
093         * Gets the {@code ObjectManagementLogger} dependency.
094         * <p>This method returns the "{@code JOMC Logging Log4J Logging}" object of the {@code org.jomc.logging.Logger} specification at specification level 1.0.</p>
095         * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
096         * <p><b>Properties</b><dl>
097         * <dt>"{@code name}"</dt>
098         * <dd>Property of type {@code java.lang.String}.
099         * </dd>
100         * </dl>
101         * @return The {@code ObjectManagementLogger} dependency.
102         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
103         */
104        @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
105                                     comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-16/jomc-tools" )
106        private org.jomc.logging.Logger getObjectManagementLogger()
107        {
108            final org.jomc.logging.Logger _d = (org.jomc.logging.Logger) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "ObjectManagementLogger" );
109            assert _d != null : "'ObjectManagementLogger' dependency not found.";
110            return _d;
111        }
112        // </editor-fold>
113        // SECTION-END
114        // SECTION-START[Properties]
115        // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
116    
117        /**
118         * Gets the value of the {@code name} property.
119         * @return Name of the component events are logged for.
120         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
121         */
122        @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
123                                     comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-16/jomc-tools" )
124        public java.lang.String getName()
125        {
126            final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "name" );
127            assert _p != null : "'name' property not found.";
128            return _p;
129        }
130        // </editor-fold>
131        // SECTION-END
132        // SECTION-START[Log4JLogger]
133    
134        public boolean isDebugEnabled()
135        {
136            return this.getLogger().isEnabledFor( org.apache.log4j.Level.DEBUG );
137        }
138    
139        public void debug( final String string )
140        {
141            this.getLogger().debug( string );
142        }
143    
144        public void debug( final Throwable throwable )
145        {
146            this.getLogger().debug( throwable.getMessage(), throwable );
147        }
148    
149        public void debug( final String message, final Throwable throwable )
150        {
151            this.getLogger().debug( message, throwable );
152        }
153    
154        public boolean isErrorEnabled()
155        {
156            return this.getLogger().isEnabledFor( org.apache.log4j.Level.ERROR );
157        }
158    
159        public void error( final String string )
160        {
161            this.getLogger().error( string );
162        }
163    
164        public void error( final Throwable throwable )
165        {
166            this.getLogger().error( throwable.getMessage(), throwable );
167        }
168    
169        public void error( final String message, final Throwable throwable )
170        {
171            this.getLogger().error( message, throwable );
172        }
173    
174        public boolean isFatalEnabled()
175        {
176            return this.getLogger().isEnabledFor( org.apache.log4j.Level.FATAL );
177        }
178    
179        public void fatal( final String string )
180        {
181            this.getLogger().fatal( string );
182        }
183    
184        public void fatal( final Throwable throwable )
185        {
186            this.getLogger().fatal( throwable.getMessage(), throwable );
187        }
188    
189        public void fatal( final String message, final Throwable throwable )
190        {
191            this.getLogger().fatal( message, throwable );
192        }
193    
194        public boolean isInfoEnabled()
195        {
196            return this.getLogger().isEnabledFor( org.apache.log4j.Level.INFO );
197        }
198    
199        public void info( final String string )
200        {
201            this.getLogger().info( string );
202        }
203    
204        public void info( final Throwable throwable )
205        {
206            this.getLogger().info( throwable.getMessage(), throwable );
207        }
208    
209        public void info( final String message, final Throwable throwable )
210        {
211            this.getLogger().info( message, throwable );
212        }
213    
214        public boolean isTraceEnabled()
215        {
216            return this.getLogger().isEnabledFor( org.apache.log4j.Level.TRACE );
217        }
218    
219        public void trace( final String string )
220        {
221            this.getLogger().trace( string );
222        }
223    
224        public void trace( final Throwable throwable )
225        {
226            this.getLogger().trace( throwable.getMessage(), throwable );
227        }
228    
229        public void trace( final String message, final Throwable throwable )
230        {
231            this.getLogger().trace( message, throwable );
232        }
233    
234        public boolean isWarnEnabled()
235        {
236            return this.getLogger().isEnabledFor( org.apache.log4j.Level.WARN );
237        }
238    
239        public void warn( final String string )
240        {
241            this.getLogger().warn( string );
242        }
243    
244        public void warn( final Throwable throwable )
245        {
246            this.getLogger().warn( throwable.getMessage(), throwable );
247        }
248    
249        public void warn( final String message, final Throwable throwable )
250        {
251            this.getLogger().warn( message, throwable );
252        }
253    
254        /**
255         * Requests a Log4J logger for the name given by property {@code name}.
256         *
257         * @return The Log4J logger for the name given by property {@code name}.
258         */
259        public org.apache.log4j.Logger getLogger()
260        {
261            return org.apache.log4j.Logger.getLogger( this.getName() );
262        }
263    
264        public void onLog( final Level level, final String message, final Throwable throwable )
265        {
266            if ( level != null )
267            {
268                if ( level.equals( Level.CONFIG ) || level.equals( Level.FINE ) )
269                {
270                    this.getObjectManagementLogger().debug( message, throwable );
271                }
272                else if ( level.equals( Level.FINER ) || level.equals( Level.FINEST ) )
273                {
274                    this.getObjectManagementLogger().trace( message, throwable );
275                }
276                else if ( level.equals( Level.INFO ) )
277                {
278                    this.getObjectManagementLogger().info( message, throwable );
279                }
280                else if ( level.equals( Level.SEVERE ) )
281                {
282                    this.getObjectManagementLogger().error( message, throwable );
283                }
284                else if ( level.equals( Level.WARNING ) )
285                {
286                    this.getObjectManagementLogger().warn( message, throwable );
287                }
288                else
289                {
290                    this.getObjectManagementLogger().trace( message, throwable );
291                }
292            }
293        }
294    
295        // SECTION-END
296        // SECTION-START[Messages]
297        // SECTION-END
298    }