001/*
002 *  jDTAUS Core Utilities
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.text.util;
022
023import java.util.Locale;
024import org.jdtaus.core.container.ContainerFactory;
025import org.jdtaus.core.logging.spi.Logger;
026import org.jdtaus.core.text.MessageEvent;
027import org.jdtaus.core.text.MessageListener;
028
029/**
030 * {@code MessageListener} logging messages to a system logger.
031 *
032 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
033 * @version $JDTAUS: MessageLogger.java 8743 2012-10-07 03:06:20Z schulte $
034 *
035 * @see #onMessage(MessageEvent)
036 */
037public final class MessageLogger implements MessageListener
038{
039    //--MessageListener---------------------------------------------------------
040
041    /**
042     * {@inheritDoc}
043     * <p>This method logs all messages given by the event using the
044     * corresponding log level.</p>
045     *
046     * @param event the event holding messages.
047     */
048    public void onMessage( final MessageEvent event )
049    {
050        if ( event != null )
051        {
052            for ( int i = 0; i < event.getMessages().length; i++ )
053            {
054                switch ( event.getType() )
055                {
056                    case MessageEvent.ERROR:
057                        this.getLogger().error( event.getMessages()[i].getText(
058                            this.getLocale() ) );
059
060                        break;
061
062                    case MessageEvent.INFORMATION:
063                    case MessageEvent.NOTIFICATION:
064                        this.getLogger().info( event.getMessages()[i].getText(
065                            this.getLocale() ) );
066
067                        break;
068
069                    case MessageEvent.WARNING:
070                        this.getLogger().warn( event.getMessages()[i].getText(
071                            this.getLocale() ) );
072
073                        break;
074
075                    default:
076                        this.getLogger().warn(
077                            this.getUnknownMessageEventTypeMessage(
078                            this.getLocale(),
079                            new Integer( event.getType() ) ) );
080
081                }
082            }
083        }
084    }
085
086    //---------------------------------------------------------MessageListener--
087    //--MessageLogger-----------------------------------------------------------
088
089    /** Creates a new {@code MessageLogger} instance. */
090    public MessageLogger()
091    {
092        super();
093    }
094
095    //-----------------------------------------------------------MessageLogger--
096    //--Dependencies------------------------------------------------------------
097
098// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
099    // This section is managed by jdtaus-container-mojo.
100
101    /**
102     * Gets the configured <code>Logger</code> implementation.
103     *
104     * @return The configured <code>Logger</code> implementation.
105     */
106    private Logger getLogger()
107    {
108        return (Logger) ContainerFactory.getContainer().
109            getDependency( this, "Logger" );
110
111    }
112
113    /**
114     * Gets the configured <code>Locale</code> implementation.
115     *
116     * @return The configured <code>Locale</code> implementation.
117     */
118    private Locale getLocale()
119    {
120        return (Locale) ContainerFactory.getContainer().
121            getDependency( this, "Locale" );
122
123    }
124
125// </editor-fold>//GEN-END:jdtausDependencies
126
127    //------------------------------------------------------------Dependencies--
128    //--Messages----------------------------------------------------------------
129
130// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages
131    // This section is managed by jdtaus-container-mojo.
132
133    /**
134     * Gets the text of message <code>unknownMessageEventType</code>.
135     * <blockquote><pre>Meldung unbekannten Typs {0,number} ignoriert.</pre></blockquote>
136     * <blockquote><pre>Ignored message event of unknown type {0,number}.</pre></blockquote>
137     *
138     * @param locale The locale of the message instance to return.
139     * @param unknownEventType The unknown event type.
140     *
141     * @return Message stating that an unknown message event got ignored.
142     */
143    private String getUnknownMessageEventTypeMessage( final Locale locale,
144            final java.lang.Number unknownEventType )
145    {
146        return ContainerFactory.getContainer().
147            getMessage( this, "unknownMessageEventType", locale,
148                new Object[]
149                {
150                    unknownEventType
151                });
152
153    }
154
155// </editor-fold>//GEN-END:jdtausMessages
156
157    //----------------------------------------------------------------Messages--
158}