KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > scheduler > SchedulerStartup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.scheduler;
12
13 import java.lang.reflect.Constructor JavaDoc;
14 import java.util.Calendar JavaDoc;
15
16 import org.eclipse.core.runtime.Preferences;
17 import org.eclipse.core.runtime.jobs.IJobChangeListener;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.ui.IStartup;
20
21 /**
22  * This plug-in is loaded on startup to fork a job that searches for new
23  * plug-ins.
24  */

25 public class SchedulerStartup implements IStartup {
26     // Preferences
27
public static final String JavaDoc P_ENABLED = "enabled"; //$NON-NLS-1$
28

29     public static final String JavaDoc P_SCHEDULE = "schedule"; //$NON-NLS-1$
30

31     public static final String JavaDoc VALUE_ON_STARTUP = "on-startup"; //$NON-NLS-1$
32

33     public static final String JavaDoc VALUE_ON_SCHEDULE = "on-schedule"; //$NON-NLS-1$
34

35     public static final String JavaDoc P_DOWNLOAD = "download"; // value is true or false, default isfalse //$NON-NLS-1$
36

37     // values are to be picked up from the arryas DAYS and HOURS
38
public static final String JavaDoc P_DAY = "day"; //$NON-NLS-1$
39

40     public static final String JavaDoc P_HOUR = "hour"; //$NON-NLS-1$
41

42     // Keeps track of the running job
43
private Job job;
44
45     static final Object JavaDoc automaticJobFamily = new Object JavaDoc();
46
47     // Listener for job changes
48
private IJobChangeListener jobListener;
49
50     public static final String JavaDoc[] DAYS = {
51             UpdateSchedulerMessages.SchedulerStartup_day,
52             UpdateSchedulerMessages.SchedulerStartup_Monday,
53             UpdateSchedulerMessages.SchedulerStartup_Tuesday,
54             UpdateSchedulerMessages.SchedulerStartup_Wednesday,
55             UpdateSchedulerMessages.SchedulerStartup_Thursday,
56             UpdateSchedulerMessages.SchedulerStartup_Friday,
57             UpdateSchedulerMessages.SchedulerStartup_Saturday,
58             UpdateSchedulerMessages.SchedulerStartup_Sunday };
59
60     public static final String JavaDoc[] HOURS = {
61             UpdateSchedulerMessages.SchedulerStartup_1AM,
62             UpdateSchedulerMessages.SchedulerStartup_2AM,
63             UpdateSchedulerMessages.SchedulerStartup_3AM,
64             UpdateSchedulerMessages.SchedulerStartup_4AM,
65             UpdateSchedulerMessages.SchedulerStartup_5AM,
66             UpdateSchedulerMessages.SchedulerStartup_6AM,
67             UpdateSchedulerMessages.SchedulerStartup_7AM,
68             UpdateSchedulerMessages.SchedulerStartup_8AM,
69             UpdateSchedulerMessages.SchedulerStartup_9AM,
70             UpdateSchedulerMessages.SchedulerStartup_10AM,
71             UpdateSchedulerMessages.SchedulerStartup_11AM,
72             UpdateSchedulerMessages.SchedulerStartup_12PM,
73             UpdateSchedulerMessages.SchedulerStartup_1PM,
74             UpdateSchedulerMessages.SchedulerStartup_2PM,
75             UpdateSchedulerMessages.SchedulerStartup_3PM,
76             UpdateSchedulerMessages.SchedulerStartup_4PM,
77             UpdateSchedulerMessages.SchedulerStartup_5PM,
78             UpdateSchedulerMessages.SchedulerStartup_6PM,
79             UpdateSchedulerMessages.SchedulerStartup_7PM,
80             UpdateSchedulerMessages.SchedulerStartup_8PM,
81             UpdateSchedulerMessages.SchedulerStartup_9PM,
82             UpdateSchedulerMessages.SchedulerStartup_10PM,
83             UpdateSchedulerMessages.SchedulerStartup_11PM,
84             UpdateSchedulerMessages.SchedulerStartup_12AM, };
85
86     /**
87      * The constructor.
88      */

89     public SchedulerStartup() {
90         UpdateSchedulerPlugin.setScheduler(this);
91     }
92
93     public void earlyStartup() {
94         scheduleUpdateJob();
95     }
96
97     public void scheduleUpdateJob() {
98         Preferences pref = UpdateSchedulerPlugin.getDefault()
99                 .getPluginPreferences();
100         // See if automatic search is enabled at all
101
if (pref.getBoolean(P_ENABLED) == false)
102             return;
103
104         String JavaDoc schedule = pref.getString(P_SCHEDULE);
105         long delay = -1L;
106         if (schedule.equals(VALUE_ON_STARTUP))
107             // have we already started a job ?
108
if (job == null)
109                 delay = 0L;
110             else
111                 delay = -1L;
112         else
113             delay = computeDelay(pref);
114         if (delay == -1L)
115             return;
116         startSearch(delay);
117     }
118
119     private int getDay(Preferences pref) {
120         String JavaDoc day = pref.getString(P_DAY);
121         for (int d = 0; d < DAYS.length; d++)
122             if (DAYS[d].equals(day))
123                 switch (d) {
124                 case 0:
125                     return -1;
126                 case 1:
127                     return Calendar.MONDAY;
128                 case 2:
129                     return Calendar.TUESDAY;
130                 case 3:
131                     return Calendar.WEDNESDAY;
132                 case 4:
133                     return Calendar.THURSDAY;
134                 case 5:
135                     return Calendar.FRIDAY;
136                 case 6:
137                     return Calendar.SATURDAY;
138                 case 7:
139                     return Calendar.SUNDAY;
140                 }
141         return -1;
142     }
143
144     private int getHour(Preferences pref) {
145         String JavaDoc hour = pref.getString(P_HOUR);
146         for (int h = 0; h < HOURS.length; h++)
147             if (HOURS[h].equals(hour))
148                 return h + 1;
149         return 1;
150     }
151
152     /*
153      * Computes the number of milliseconds from this moment to the next
154      * scheduled search. If that moment has already passed, returns 0L (start
155      * immediately).
156      */

157     private long computeDelay(Preferences pref) {
158
159         int target_d = getDay(pref);
160         int target_h = getHour(pref);
161
162         Calendar JavaDoc calendar = Calendar.getInstance();
163         // may need to use the BootLoader locale
164
int current_d = calendar.get(Calendar.DAY_OF_WEEK);
165         // starts with SUNDAY
166
int current_h = calendar.get(Calendar.HOUR_OF_DAY);
167         int current_m = calendar.get(Calendar.MINUTE);
168         int current_s = calendar.get(Calendar.SECOND);
169         int current_ms = calendar.get(Calendar.MILLISECOND);
170
171         long delay = 0L; // milliseconds
172

173         if (target_d == -1) {
174             // Compute the delay for "every day at x o'clock"
175
// Is it now ?
176
if (target_h == current_h && current_m == 0 && current_s == 0)
177                 return delay;
178
179             int delta_h = target_h - current_h;
180             if (target_h <= current_h)
181                 delta_h += 24;
182             delay = ((delta_h * 60 - current_m) * 60 - current_s) * 1000
183                     - current_ms;
184             return delay;
185         } else {
186             // Compute the delay for "every Xday at x o'clock"
187
// Is it now ?
188
if (target_d == current_d && target_h == current_h
189                     && current_m == 0 && current_s == 0)
190                 return delay;
191
192             int delta_d = target_d - current_d;
193             if (target_d < current_d
194                     || target_d == current_d
195                     && (target_h < current_h || target_h == current_h
196                             && current_m > 0))
197                 delta_d += 7;
198
199             delay = (((delta_d * 24 + target_h - current_h) * 60 - current_m) * 60 - current_s)
200                     * 1000 - current_ms;
201
202             return delay;
203         }
204         // return -1L;
205
}
206
207     private void startSearch(long delay) {
208         if (job != null) {
209             // cancel old job.
210
// We need to deregister the listener first,so we won't
211
// automatically start another job
212
if (jobListener != null)
213                 Job.getJobManager().removeJobChangeListener(jobListener);
214             Job.getJobManager().cancel(automaticJobFamily);
215         }
216         if (jobListener == null) {
217             // using reflection to avoid premature class loading
218
jobListener = createJobChangeAdapter();
219             if (jobListener == null)
220                 return;
221         }
222         Job.getJobManager().addJobChangeListener(jobListener);
223         String JavaDoc jobName = UpdateSchedulerMessages.AutomaticUpdatesJob_AutomaticUpdateSearch; //);
224
boolean download = UpdateSchedulerPlugin.getDefault()
225                 .getPluginPreferences().getBoolean(
226                         UpdateSchedulerPlugin.P_DOWNLOAD);
227         job = createUpdateJob(jobName, download);
228         if (job != null)
229             job.schedule(delay);
230
231     }
232
233     /*
234      * Loads the update job using reflection to avoid premature startup of the
235      * Update UI plug-in.
236      */

237
238     private Job createUpdateJob(String JavaDoc name, boolean download) {
239         try {
240             Class JavaDoc theClass = Class
241                     .forName("org.eclipse.update.internal.scheduler.AutomaticUpdateJob"); //$NON-NLS-1$
242
Constructor JavaDoc constructor = theClass.getConstructor(new Class JavaDoc[] {
243                     String JavaDoc.class, Boolean.TYPE, Boolean.TYPE });
244             return (Job) constructor.newInstance(new Object JavaDoc[] { name,
245                     Boolean.TRUE, new Boolean JavaDoc(download) });
246         } catch (Exception JavaDoc e) {
247             UpdateSchedulerPlugin.logException(e, false);
248             return null;
249         }
250     }
251
252     /*
253      * Loads the job listener using reflection to avoid premature startup of the
254      * Update UI plug-in.
255      */

256     private IJobChangeListener createJobChangeAdapter() {
257         try {
258             Class JavaDoc theClass = Class
259                     .forName("org.eclipse.update.internal.scheduler.UpdateJobChangeAdapter"); //$NON-NLS-1$
260
Constructor JavaDoc constructor = theClass
261                     .getConstructor(new Class JavaDoc[] { SchedulerStartup.class });
262             return (IJobChangeListener) constructor
263                     .newInstance(new Object JavaDoc[] { this });
264         } catch (Exception JavaDoc e) {
265             UpdateSchedulerPlugin.logException(e, false);
266             return null;
267         }
268     }
269
270     Job getJob() {
271         return job;
272     }
273 }
274
Popular Tags