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.monitor.util;
022
023import java.util.Date;
024import java.util.Locale;
025import org.jdtaus.core.container.ContainerFactory;
026import org.jdtaus.core.logging.spi.Logger;
027import org.jdtaus.core.monitor.TaskEvent;
028import org.jdtaus.core.monitor.TaskListener;
029
030/**
031 * {@code TaskListener} logging the duration of an operation performed by
032 * a {@code Task}.
033 *
034 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
035 * @version $JDTAUS: TaskDurationLogger.java 8641 2012-09-27 06:45:17Z schulte $
036 *
037 * @see #onTaskEvent(TaskEvent)
038 */
039public final class TaskDurationLogger implements TaskListener
040{
041    //--Dependencies------------------------------------------------------------
042
043// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
044    // This section is managed by jdtaus-container-mojo.
045
046    /**
047     * Gets the configured <code>Logger</code> implementation.
048     *
049     * @return The configured <code>Logger</code> implementation.
050     */
051    private Logger getLogger()
052    {
053        return (Logger) ContainerFactory.getContainer().
054            getDependency( this, "Logger" );
055
056    }
057
058    /**
059     * Gets the configured <code>Locale</code> implementation.
060     *
061     * @return The configured <code>Locale</code> implementation.
062     */
063    private Locale getLocale()
064    {
065        return (Locale) ContainerFactory.getContainer().
066            getDependency( this, "Locale" );
067
068    }
069
070// </editor-fold>//GEN-END:jdtausDependencies
071
072    //------------------------------------------------------------Dependencies--
073    //--Properties--------------------------------------------------------------
074
075// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
076    // This section is managed by jdtaus-container-mojo.
077
078    /**
079     * Gets the value of property <code>defaultLoggingThresholdMillis</code>.
080     *
081     * @return Default number of milliseconds a task at least needs to run to trigger a message when finished.
082     */
083    private java.lang.Long getDefaultLoggingThresholdMillis()
084    {
085        return (java.lang.Long) ContainerFactory.getContainer().
086            getProperty( this, "defaultLoggingThresholdMillis" );
087
088    }
089
090// </editor-fold>//GEN-END:jdtausProperties
091
092    //--------------------------------------------------------------Properties--
093    //--TaskListener------------------------------------------------------------
094
095    /**
096     * {@inheritDoc}
097     * <p>This method measures the time a task is running and logs
098     * information for tasks running longer than specified by configuration
099     * property {@code loggingThresholdMillis} (defaults to 60000).</p>
100     *
101     * @param event the event send by a {@code Task}.
102     */
103    public void onTaskEvent( final TaskEvent event )
104    {
105        if ( event != null )
106        {
107            final long now = System.currentTimeMillis();
108            final long start = event.getTask().getTimestamp();
109
110            if ( TaskEvent.ENDED == event.getType() &&
111                 now - start > this.getLoggingThresholdMillis() )
112            {
113                this.getLogger().info(
114                    this.getDurationInfoMessage(
115                    this.getLocale(),
116                    event.getTask().getDescription().
117                    getText( this.getLocale() ),
118                    new Date( start ),
119                    new Date( now ),
120                    new Long( now - start ) ) );
121
122            }
123        }
124    }
125
126    //------------------------------------------------------------TaskListener--
127    //--TaskDurationLogger------------------------------------------------------
128
129    /**
130     * The number of milliseconds a task at least needs to run to trigger a
131     * message when finished.
132     */
133    private Long loggingTresholdMillis;
134
135    /** Creates a new {@code TaskDurationLogger} instance. */
136    public TaskDurationLogger()
137    {
138        super();
139    }
140
141    /**
142     * Creates a new {@code TaskDurationLogger} instance taking the number of
143     * milliseconds a task at least needs to run to trigger a message when
144     * finished.
145     *
146     * @param loggingThresholdMillis the number of milliseconds a task at least
147     * needs to run to trigger a message when finished.
148     */
149    public TaskDurationLogger( final long loggingThresholdMillis )
150    {
151        super();
152
153        if ( loggingThresholdMillis > 0 )
154        {
155            this.loggingTresholdMillis = new Long( loggingThresholdMillis );
156        }
157    }
158
159    /**
160     * Gets the number of milliseconds a task at least needs to run to trigger
161     * a message when finished.
162     *
163     * @return the number of milliseconds a task at least needs to run to
164     * trigger a message when finished.
165     */
166    public long getLoggingThresholdMillis()
167    {
168        if ( this.loggingTresholdMillis == null )
169        {
170            this.loggingTresholdMillis =
171                this.getDefaultLoggingThresholdMillis();
172
173        }
174
175        return this.loggingTresholdMillis.intValue();
176    }
177
178    //------------------------------------------------------TaskDurationLogger--
179    //--Messages----------------------------------------------------------------
180
181// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages
182    // This section is managed by jdtaus-container-mojo.
183
184    /**
185     * Gets the text of message <code>durationInfo</code>.
186     * <blockquote><pre>Taskinformation:
187     *Beschreibung ... : {0}
188     *Start .......... : {1,date,long} um {1,time,long}
189     *Ende ........... : {2,date,long} um {2,time,long}
190     *Laufzeit ....... : {3}ms</pre></blockquote>
191     * <blockquote><pre>Taskinformation:
192     *Description ... : {0}
193     *Start ......... : {1,date,long} at {1,time,long}
194     *End ........... : {2,date,long} at {2,time,long}
195     *Duration ...... : {3}ms</pre></blockquote>
196     *
197     * @param locale The locale of the message instance to return.
198     * @param taskDescription The description of the task.
199     * @param startDate The start date of the task.
200     * @param endDate The end date of the task.
201     * @param durationMillis The number of milliseconds the task ran.
202     *
203     * @return Information about a task.
204     */
205    private String getDurationInfoMessage( final Locale locale,
206            final java.lang.String taskDescription,
207            final java.util.Date startDate,
208            final java.util.Date endDate,
209            final java.lang.Number durationMillis )
210    {
211        return ContainerFactory.getContainer().
212            getMessage( this, "durationInfo", locale,
213                new Object[]
214                {
215                    taskDescription,
216                    startDate,
217                    endDate,
218                    durationMillis
219                });
220
221    }
222
223// </editor-fold>//GEN-END:jdtausMessages
224
225    //----------------------------------------------------------------Messages--
226}