|
Lines 8-23
Link Here
|
| 8 |
|
8 |
|
| 9 |
package org.eclipse.mylyn.internal.tasks.core.notifications; |
9 |
package org.eclipse.mylyn.internal.tasks.core.notifications; |
| 10 |
|
10 |
|
|
|
11 |
import java.io.IOException; |
| 11 |
import java.io.InputStream; |
12 |
import java.io.InputStream; |
| 12 |
import java.util.ArrayList; |
13 |
import java.util.ArrayList; |
| 13 |
import java.util.Collections; |
14 |
import java.util.Collections; |
| 14 |
import java.util.Date; |
|
|
| 15 |
import java.util.List; |
15 |
import java.util.List; |
| 16 |
import java.util.concurrent.CopyOnWriteArrayList; |
16 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 17 |
|
17 |
|
| 18 |
import javax.xml.parsers.SAXParser; |
|
|
| 19 |
import javax.xml.parsers.SAXParserFactory; |
| 20 |
|
| 21 |
import org.apache.commons.httpclient.Header; |
18 |
import org.apache.commons.httpclient.Header; |
| 22 |
import org.apache.commons.httpclient.HostConfiguration; |
19 |
import org.apache.commons.httpclient.HostConfiguration; |
| 23 |
import org.apache.commons.httpclient.HttpClient; |
20 |
import org.apache.commons.httpclient.HttpClient; |
|
Lines 45-55
Link Here
|
| 45 |
|
42 |
|
| 46 |
protected static final long START_DELAY = 30 * 1000; |
43 |
protected static final long START_DELAY = 30 * 1000; |
| 47 |
|
44 |
|
| 48 |
protected static final long RECHECK_DELAY = 24 * 60 * 60 * 1000; |
45 |
protected static final long RECHECK_DELAY = 14 * 24 * 60 * 60 * 1000; |
| 49 |
|
46 |
|
| 50 |
private String serviceMessageUrl; |
47 |
private String serviceMessageUrl; |
| 51 |
|
48 |
|
| 52 |
private volatile List<ServiceMessage> messages = Collections.emptyList(); |
49 |
private volatile List<? extends ServiceMessage> messages = Collections.emptyList(); |
| 53 |
|
50 |
|
| 54 |
private Job messageCheckJob; |
51 |
private Job messageCheckJob; |
| 55 |
|
52 |
|
|
Lines 63-73
Link Here
|
| 63 |
|
60 |
|
| 64 |
private final long checktime; |
61 |
private final long checktime; |
| 65 |
|
62 |
|
| 66 |
public ServiceMessageManager(String serviceMessageUrl, String lastModified, String eTag, long checktime) { |
63 |
private final Environment environment; |
|
|
64 |
|
| 65 |
public ServiceMessageManager(String serviceMessageUrl, String lastModified, String eTag, long checktime, |
| 66 |
Environment environment) { |
| 67 |
this.serviceMessageUrl = serviceMessageUrl; |
67 |
this.serviceMessageUrl = serviceMessageUrl; |
| 68 |
this.lastModified = lastModified; |
68 |
this.lastModified = lastModified; |
| 69 |
this.checktime = checktime; |
69 |
this.checktime = checktime; |
| 70 |
this.eTag = eTag; |
70 |
this.eTag = eTag; |
|
|
71 |
this.environment = environment; |
| 72 |
} |
| 73 |
|
| 74 |
public ServiceMessageManager(String serviceMessageUrl, String lastModified, String eTag, long checktime) { |
| 75 |
this(serviceMessageUrl, lastModified, eTag, checktime, new Environment()); |
| 71 |
} |
76 |
} |
| 72 |
|
77 |
|
| 73 |
public void start() { |
78 |
public void start() { |
|
Lines 75-82
Link Here
|
| 75 |
messageCheckJob = new Job("Checking for new service message") { //$NON-NLS-1$ |
80 |
messageCheckJob = new Job("Checking for new service message") { //$NON-NLS-1$ |
| 76 |
@Override |
81 |
@Override |
| 77 |
protected IStatus run(IProgressMonitor monitor) { |
82 |
protected IStatus run(IProgressMonitor monitor) { |
| 78 |
updateServiceMessage(monitor); |
83 |
try { |
| 79 |
return Status.OK_STATUS; |
84 |
updateServiceMessage(monitor); |
|
|
85 |
return Status.OK_STATUS; |
| 86 |
} catch (Throwable t) { |
| 87 |
// fail silently |
| 88 |
return Status.CANCEL_STATUS; |
| 89 |
} |
| 80 |
} |
90 |
} |
| 81 |
|
91 |
|
| 82 |
}; |
92 |
}; |
|
Lines 95-112
Link Here
|
| 95 |
messageCheckJob.schedule(START_DELAY); |
105 |
messageCheckJob.schedule(START_DELAY); |
| 96 |
} else { |
106 |
} else { |
| 97 |
long nextCheckTime = checktime + RECHECK_DELAY; |
107 |
long nextCheckTime = checktime + RECHECK_DELAY; |
| 98 |
long now = new Date().getTime(); |
108 |
long now = System.currentTimeMillis(); |
| 99 |
if (nextCheckTime < now) { |
109 |
if (nextCheckTime < now) { |
| 100 |
messageCheckJob.schedule(START_DELAY); |
110 |
messageCheckJob.schedule(START_DELAY); |
| 101 |
} else if (nextCheckTime > now) { |
111 |
} else if (nextCheckTime > now) { |
| 102 |
if ((nextCheckTime - now) < START_DELAY) { |
112 |
if (nextCheckTime - now < START_DELAY) { |
| 103 |
messageCheckJob.schedule(START_DELAY); |
113 |
messageCheckJob.schedule(START_DELAY); |
| 104 |
} else { |
114 |
} else { |
| 105 |
messageCheckJob.schedule(nextCheckTime - now); |
115 |
messageCheckJob.schedule(nextCheckTime - now); |
| 106 |
} |
116 |
} |
| 107 |
} |
117 |
} |
| 108 |
} |
118 |
} |
| 109 |
|
|
|
| 110 |
} |
119 |
} |
| 111 |
|
120 |
|
| 112 |
public void stop() { |
121 |
public void stop() { |
|
Lines 143-158
Link Here
|
| 143 |
listeners.remove(listener); |
152 |
listeners.remove(listener); |
| 144 |
} |
153 |
} |
| 145 |
|
154 |
|
| 146 |
private void notifyListeners(List<ServiceMessage> messages) { |
155 |
private void notifyListeners(List<? extends ServiceMessage> messages) { |
| 147 |
this.messages = messages; |
156 |
this.messages = messages; |
| 148 |
for (final ServiceMessage message : messages) { |
157 |
for (final ServiceMessage message : messages) { |
| 149 |
message.setETag(eTag); |
158 |
message.setETag(eTag); |
| 150 |
message.setLastModified(lastModified); |
159 |
message.setLastModified(lastModified); |
| 151 |
} |
160 |
} |
| 152 |
|
161 |
|
|
|
162 |
ArrayList<ServiceMessage> sortedMessages = new ArrayList<ServiceMessage>(messages); |
| 163 |
Collections.sort(messages); |
| 153 |
final ServiceMessageEvent event = new ServiceMessageEvent(this, ServiceMessageEvent.EVENT_KIND.MESSAGE_UPDATE, |
164 |
final ServiceMessageEvent event = new ServiceMessageEvent(this, ServiceMessageEvent.EVENT_KIND.MESSAGE_UPDATE, |
| 154 |
messages); |
165 |
sortedMessages); |
| 155 |
|
|
|
| 156 |
for (final IServiceMessageListener listener : listeners) { |
166 |
for (final IServiceMessageListener listener : listeners) { |
| 157 |
SafeRunner.run(new ISafeRunnable() { |
167 |
SafeRunner.run(new ISafeRunnable() { |
| 158 |
public void run() throws Exception { |
168 |
public void run() throws Exception { |
|
Lines 176-182
Link Here
|
| 176 |
*/ |
186 |
*/ |
| 177 |
public int updateServiceMessage(IProgressMonitor monitor) { |
187 |
public int updateServiceMessage(IProgressMonitor monitor) { |
| 178 |
int status = -1; |
188 |
int status = -1; |
| 179 |
List<ServiceMessage> messages = null; |
189 |
List<? extends ServiceMessage> messages = null; |
| 180 |
try { |
190 |
try { |
| 181 |
HttpClient httpClient = new HttpClient(WebUtil.getConnectionManager()); |
191 |
HttpClient httpClient = new HttpClient(WebUtil.getConnectionManager()); |
| 182 |
WebUtil.configureHttpClient(httpClient, null); |
192 |
WebUtil.configureHttpClient(httpClient, null); |
|
Lines 203-215
Link Here
|
| 203 |
|
213 |
|
| 204 |
InputStream in = WebUtil.getResponseBodyAsStream(method, monitor); |
214 |
InputStream in = WebUtil.getResponseBodyAsStream(method, monitor); |
| 205 |
try { |
215 |
try { |
| 206 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
216 |
messages = readMessages(in, monitor); |
| 207 |
factory.setValidating(false); |
|
|
| 208 |
SAXParser parser = factory.newSAXParser(); |
| 209 |
|
| 210 |
ServiceMessageXmlHandler handler = new ServiceMessageXmlHandler(); |
| 211 |
parser.parse(in, handler); |
| 212 |
messages = handler.getMessages(); |
| 213 |
} finally { |
217 |
} finally { |
| 214 |
in.close(); |
218 |
in.close(); |
| 215 |
} |
219 |
} |
|
Lines 218-238
Link Here
|
| 218 |
} else if (status == HttpStatus.SC_NOT_MODIFIED) { |
222 |
} else if (status == HttpStatus.SC_NOT_MODIFIED) { |
| 219 |
// no new messages |
223 |
// no new messages |
| 220 |
} else { |
224 |
} else { |
| 221 |
if (!statusLogged) { |
225 |
logStatus(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN, |
| 222 |
statusLogged = true; |
226 |
"Http error retrieving service message: " + HttpStatus.getStatusText(status))); //$NON-NLS-1$ |
| 223 |
StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN, |
|
|
| 224 |
"Http error retrieving service message: " + HttpStatus.getStatusText(status))); //$NON-NLS-1$ |
| 225 |
} |
| 226 |
} |
227 |
} |
| 227 |
} finally { |
228 |
} finally { |
| 228 |
WebUtil.releaseConnection(method, monitor); |
229 |
WebUtil.releaseConnection(method, monitor); |
| 229 |
} |
230 |
} |
| 230 |
} catch (Exception e) { |
231 |
} catch (Exception e) { |
| 231 |
if (!statusLogged) { |
232 |
logStatus(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN, |
| 232 |
statusLogged = true; |
233 |
"Http error retrieving service message.", e)); //$NON-NLS-1$ |
| 233 |
StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN, |
|
|
| 234 |
"Http error retrieving service message.", e)); //$NON-NLS-1$ |
| 235 |
} |
| 236 |
} |
234 |
} |
| 237 |
|
235 |
|
| 238 |
if (messages != null && messages.size() > 0) { |
236 |
if (messages != null && messages.size() > 0) { |
|
Lines 241-244
Link Here
|
| 241 |
return status; |
239 |
return status; |
| 242 |
} |
240 |
} |
| 243 |
|
241 |
|
|
|
242 |
private void logStatus(IStatus status) { |
| 243 |
if (!statusLogged) { |
| 244 |
statusLogged = true; |
| 245 |
//StatusHandler.log(status); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// private List<ServiceMessage> readMessages(InputStream in) throws ParserConfigurationException, SAXException, |
| 250 |
// IOException { |
| 251 |
// SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 252 |
// factory.setValidating(false); |
| 253 |
// SAXParser parser = factory.newSAXParser(); |
| 254 |
// |
| 255 |
// ServiceMessageXmlHandler handler = new ServiceMessageXmlHandler(); |
| 256 |
// parser.parse(in, handler); |
| 257 |
// return handler.getMessages(); |
| 258 |
// } |
| 259 |
|
| 260 |
private List<? extends ServiceMessage> readMessages(InputStream in, IProgressMonitor monitor) throws IOException { |
| 261 |
FeedReader reader = new FeedReader(environment); |
| 262 |
reader.parse(in, monitor); |
| 263 |
return reader.getEntries(); |
| 264 |
} |
| 265 |
|
| 244 |
} |
266 |
} |