1 11 package org.eclipse.update.internal.scheduler; 12 13 import java.lang.reflect.Constructor ; 14 import java.util.Calendar ; 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 25 public class SchedulerStartup implements IStartup { 26 public static final String P_ENABLED = "enabled"; 29 public static final String P_SCHEDULE = "schedule"; 31 public static final String VALUE_ON_STARTUP = "on-startup"; 33 public static final String VALUE_ON_SCHEDULE = "on-schedule"; 35 public static final String P_DOWNLOAD = "download"; 37 public static final String P_DAY = "day"; 40 public static final String P_HOUR = "hour"; 42 private Job job; 44 45 static final Object automaticJobFamily = new Object (); 46 47 private IJobChangeListener jobListener; 49 50 public static final String [] 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 [] 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 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 if (pref.getBoolean(P_ENABLED) == false) 102 return; 103 104 String schedule = pref.getString(P_SCHEDULE); 105 long delay = -1L; 106 if (schedule.equals(VALUE_ON_STARTUP)) 107 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 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 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 157 private long computeDelay(Preferences pref) { 158 159 int target_d = getDay(pref); 160 int target_h = getHour(pref); 161 162 Calendar calendar = Calendar.getInstance(); 163 int current_d = calendar.get(Calendar.DAY_OF_WEEK); 165 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; 173 if (target_d == -1) { 174 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 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 } 206 207 private void startSearch(long delay) { 208 if (job != null) { 209 if (jobListener != null) 213 Job.getJobManager().removeJobChangeListener(jobListener); 214 Job.getJobManager().cancel(automaticJobFamily); 215 } 216 if (jobListener == null) { 217 jobListener = createJobChangeAdapter(); 219 if (jobListener == null) 220 return; 221 } 222 Job.getJobManager().addJobChangeListener(jobListener); 223 String jobName = UpdateSchedulerMessages.AutomaticUpdatesJob_AutomaticUpdateSearch; 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 237 238 private Job createUpdateJob(String name, boolean download) { 239 try { 240 Class theClass = Class 241 .forName("org.eclipse.update.internal.scheduler.AutomaticUpdateJob"); Constructor constructor = theClass.getConstructor(new Class [] { 243 String .class, Boolean.TYPE, Boolean.TYPE }); 244 return (Job) constructor.newInstance(new Object [] { name, 245 Boolean.TRUE, new Boolean (download) }); 246 } catch (Exception e) { 247 UpdateSchedulerPlugin.logException(e, false); 248 return null; 249 } 250 } 251 252 256 private IJobChangeListener createJobChangeAdapter() { 257 try { 258 Class theClass = Class 259 .forName("org.eclipse.update.internal.scheduler.UpdateJobChangeAdapter"); Constructor constructor = theClass 261 .getConstructor(new Class [] { SchedulerStartup.class }); 262 return (IJobChangeListener) constructor 263 .newInstance(new Object [] { this }); 264 } catch (Exception e) { 265 UpdateSchedulerPlugin.logException(e, false); 266 return null; 267 } 268 } 269 270 Job getJob() { 271 return job; 272 } 273 } 274 | Popular Tags |