View Javadoc

1   /*
2    * Created by IntelliJ IDEA.
3    * User: Mike
4    * Date: Feb 13, 2004
5    * Time: 7:47:06 PM
6    */
7   package com.atlassian.core.task.longrunning;
8   
9   import com.atlassian.core.util.DateUtils;
10  import com.atlassian.core.util.ProgressMeter;
11  
12  import java.util.MissingResourceException;
13  import java.util.ResourceBundle;
14  
15  import org.apache.log4j.Category;
16  
17  public abstract class AbstractLongRunningTask implements LongRunningTask
18  {
19      public static final Category log = Category.getInstance(AbstractLongRunningTask.class);
20      long startTime = System.currentTimeMillis();
21      long stopTime = 0;
22      protected ProgressMeter progress;
23  
24      protected AbstractLongRunningTask()
25      {
26          progress = new ProgressMeter();
27          progress.setStatus("Initializing... ");
28      }
29  
30      public void run()
31      {
32          progress.setStatus("Starting... ");
33          startTime = System.currentTimeMillis();
34      }
35  
36      /**
37       * Internationalisation key for the name of the task, so that the task's name
38       * can be displayed in the User Interface.  Default implementation returns null,
39       * since some uses of LongRunningTask may not require internationalisation of
40       * the name (eg if the name of the task is never displayed in the UI).
41       *
42       * @return I18n key as a string, or null if no key defined.  Null if not overridden.
43       */
44      public String getNameKey()
45      {
46          return null;
47      }
48  
49  
50      public int getPercentageComplete()
51      {
52          return progress.getPercentageComplete();
53      }
54  
55      public String getCurrentStatus()
56      {
57          return progress.getStatus();
58      }
59  
60      public long getElapsedTime()
61      {
62          return (stopTime == 0 ? System.currentTimeMillis() : stopTime) - startTime;
63      }
64  
65      public long getEstimatedTimeRemaining()
66      {
67          long elapsedTime = getElapsedTime();
68  
69          if (getPercentageComplete() == 0)
70              return 0;
71  
72          long totalTimeEstimate = 100 * elapsedTime / getPercentageComplete();
73          return totalTimeEstimate - elapsedTime;
74      }
75  
76      public boolean isComplete()
77      {
78          return getPercentageComplete() == 100;
79      }
80  
81      public String getPrettyElapsedTime()
82      {
83          return prettyTime(getElapsedTime());
84      }
85  
86      protected abstract ResourceBundle getResourceBundle();
87  
88      private String prettyTime(long time)
89      {
90          if (time < 1000)
91          {
92              return "Less than a second";
93          }
94          else if (time / DateUtils.SECOND_MILLIS < 60)
95          {
96              return time / DateUtils.SECOND_MILLIS + " seconds";
97          }
98  
99          String minutesAndAbove = null;
100 
101         try
102         {
103             minutesAndAbove = DateUtils.getDurationPretty(time / DateUtils.SECOND_MILLIS, getResourceBundle());
104         }
105         catch (MissingResourceException e)
106         {
107             log.error("Could not load resourcebundle for 'minute'!'", e);
108         }
109 
110         long secondsRemainder = (time / DateUtils.SECOND_MILLIS) % 60;
111 
112         if (secondsRemainder > 0)
113         {
114             minutesAndAbove += ", " + secondsRemainder + " second" + (secondsRemainder == 1 ? "" : "s");
115         }
116 
117         return minutesAndAbove;
118     }
119 
120     public String getPrettyTimeRemaining()
121     {
122         long estimatedTimeRemaining = getEstimatedTimeRemaining();
123 
124         if (estimatedTimeRemaining == 0)
125             return "Unknown";
126 
127         return prettyTime(estimatedTimeRemaining);
128     }
129 
130     public boolean isSuccessful()
131     {
132         return progress.isCompletedSuccessfully();
133     }
134 
135     protected void stopTimer()
136     {
137         stopTime = System.currentTimeMillis();
138     }
139 }