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 8641 2012-09-27 06:45:17Z 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    //--Dependencies------------------------------------------------------------
088
089// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
090    // This section is managed by jdtaus-container-mojo.
091
092    /**
093     * Gets the configured <code>Logger</code> implementation.
094     *
095     * @return The configured <code>Logger</code> implementation.
096     */
097    private Logger getLogger()
098    {
099        return (Logger) ContainerFactory.getContainer().
100            getDependency( this, "Logger" );
101
102    }
103
104    /**
105     * Gets the configured <code>Locale</code> implementation.
106     *
107     * @return The configured <code>Locale</code> implementation.
108     */
109    private Locale getLocale()
110    {
111        return (Locale) ContainerFactory.getContainer().
112            getDependency( this, "Locale" );
113
114    }
115
116// </editor-fold>//GEN-END:jdtausDependencies
117
118    //------------------------------------------------------------Dependencies--
119    //--Messages----------------------------------------------------------------
120
121// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages
122    // This section is managed by jdtaus-container-mojo.
123
124    /**
125     * Gets the text of message <code>unknownMessageEventType</code>.
126     * <blockquote><pre>Meldung unbekannten Typs {0,number} ignoriert.</pre></blockquote>
127     * <blockquote><pre>Ignored message event of unknown type {0,number}.</pre></blockquote>
128     *
129     * @param locale The locale of the message instance to return.
130     * @param unknownEventType The unknown event type.
131     *
132     * @return Message stating that an unknown message event got ignored.
133     */
134    private String getUnknownMessageEventTypeMessage( final Locale locale,
135            final java.lang.Number unknownEventType )
136    {
137        return ContainerFactory.getContainer().
138            getMessage( this, "unknownMessageEventType", locale,
139                new Object[]
140                {
141                    unknownEventType
142                });
143
144    }
145
146// </editor-fold>//GEN-END:jdtausMessages
147
148    //----------------------------------------------------------------Messages--
149}