Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrelves2010-05-25 16:03:26 -0400
committerrelves2010-05-25 16:03:26 -0400
commit881b6c37cfae275e990e06dd3616b7e3a20e3012 (patch)
treea6e43f6a3e099d2a44efa80a9df91e9f9dde9704 /org.eclipse.mylyn.tasks.core
parentae7d66e3f5e2f5f622013d283e420a28bb0b994f (diff)
downloadorg.eclipse.mylyn.tasks-881b6c37cfae275e990e06dd3616b7e3a20e3012.tar.gz
org.eclipse.mylyn.tasks-881b6c37cfae275e990e06dd3616b7e3a20e3012.tar.xz
org.eclipse.mylyn.tasks-881b6c37cfae275e990e06dd3616b7e3a20e3012.zip
NEW - bug 263528: display a repository service message to warn users of problems, updates, and other relevant information
https://bugs.eclipse.org/bugs/show_bug.cgi?id=263528
Diffstat (limited to 'org.eclipse.mylyn.tasks.core')
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/IServiceMessageListener.java21
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessage.java107
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageEvent.java52
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageManager.java225
-rw-r--r--org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageXmlHandler.java92
5 files changed, 497 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/IServiceMessageListener.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/IServiceMessageListener.java
new file mode 100644
index 000000000..eca6e93c3
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/IServiceMessageListener.java
@@ -0,0 +1,21 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.tasks.core.notifications;
+
+/**
+ * @author Robert Elves
+ */
+public interface IServiceMessageListener {
+
+ public void handleEvent(ServiceMessageEvent event);
+
+}
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessage.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessage.java
new file mode 100644
index 000000000..4b96603bd
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessage.java
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.tasks.core.notifications;
+
+/**
+ * @author Robert Elves
+ */
+public class ServiceMessage {
+
+ public enum Element {
+ ID, TITLE, DESCRIPTION, URL, IMAGE, VERSION
+ };
+
+ private String id;
+
+ private String title;
+
+ private String description;
+
+ private String url;
+
+ private String image;
+
+ private String version;
+
+ private String eTag;
+
+ private String lastModified;
+
+ public String getId() {
+ return id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public String getImage() {
+ return image;
+ }
+
+ public void setETag(String eTag) {
+ this.eTag = eTag;
+ }
+
+ public void setLastModified(String lastModified) {
+ this.lastModified = lastModified;
+ }
+
+ public String getETag() {
+ return eTag;
+ }
+
+ public String getLastModified() {
+ return lastModified;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public void setImage(String image) {
+ this.image = image;
+ }
+
+ public void setVersion(String version) {
+ this.version = version;
+ }
+
+ public boolean isValid() {
+ return id != null && title != null && description != null && image != null;
+ }
+
+}
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageEvent.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageEvent.java
new file mode 100644
index 000000000..8483cd5c7
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageEvent.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.tasks.core.notifications;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.EventObject;
+import java.util.List;
+
+/**
+ * @author Steffen Pingel
+ */
+public class ServiceMessageEvent extends EventObject {
+
+ public enum EVENT_KIND {
+ MESSAGE_UPDATE, STOP
+ };
+
+ private static final long serialVersionUID = 1L;
+
+ private final List<ServiceMessage> messages;
+
+ private final EVENT_KIND eventKind;
+
+ public ServiceMessageEvent(ServiceMessageManager manager, EVENT_KIND kind) {
+ this(manager, kind, new ArrayList<ServiceMessage>());
+ }
+
+ public ServiceMessageEvent(ServiceMessageManager manager, EVENT_KIND eventKind, List<ServiceMessage> messages) {
+ super(manager);
+ this.eventKind = eventKind;
+ this.messages = Collections.unmodifiableList(new ArrayList<ServiceMessage>(messages));
+ }
+
+ public List<ServiceMessage> getMessages() {
+ return messages;
+ }
+
+ public EVENT_KIND getEventKind() {
+ return eventKind;
+ }
+
+}
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageManager.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageManager.java
new file mode 100644
index 000000000..9e204de43
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageManager.java
@@ -0,0 +1,225 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2008 Tasktop Technologies.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Tasktop EULA
+ * which accompanies this distribution, and is available at
+ * http://tasktop.com/legal
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.tasks.core.notifications;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.ISafeRunnable;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.SafeRunner;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
+import org.eclipse.mylyn.commons.core.StatusHandler;
+import org.eclipse.mylyn.commons.net.WebLocation;
+import org.eclipse.mylyn.commons.net.WebUtil;
+import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
+
+/**
+ * @author Robert Elves
+ * @author Steffen Pingel
+ */
+public class ServiceMessageManager {
+
+ protected static final long START_DELAY = 30 * 1000;
+
+ protected static final long RECHECK_DELAY = 2 * 60 * 60 * 1000;
+
+ private String serviceMessageUrl;
+
+ private volatile List<ServiceMessage> messages = Collections.emptyList();
+
+ private Job messageCheckJob;
+
+ private final List<IServiceMessageListener> listeners = new CopyOnWriteArrayList<IServiceMessageListener>();
+
+ private String lastModified;
+
+ private String eTag;
+
+ private boolean statusLogged;
+
+ public ServiceMessageManager(String serviceMessageUrl, String lastModified, String eTag) {
+ this.serviceMessageUrl = serviceMessageUrl;
+ this.lastModified = lastModified;
+ this.eTag = eTag;
+ }
+
+ public void start() {
+ if (messageCheckJob == null) {
+ messageCheckJob = new Job("Checking for new service message") { //$NON-NLS-1$
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ updateServiceMessage(monitor);
+ return Status.OK_STATUS;
+ }
+
+ };
+ messageCheckJob.setSystem(true);
+ messageCheckJob.setPriority(Job.DECORATE);
+ messageCheckJob.addJobChangeListener(new JobChangeAdapter() {
+ @Override
+ public void done(IJobChangeEvent event) {
+ if (messageCheckJob != null) {
+ messageCheckJob.schedule(RECHECK_DELAY);
+ }
+ }
+ });
+ }
+ messageCheckJob.schedule(START_DELAY);
+ }
+
+ public void stop() {
+ if (messageCheckJob != null) {
+ messageCheckJob.cancel();
+ messageCheckJob = null;
+ }
+
+ final ServiceMessageEvent event = new ServiceMessageEvent(this, ServiceMessageEvent.EVENT_KIND.STOP);
+
+ for (final IServiceMessageListener listener : listeners) {
+ SafeRunner.run(new ISafeRunnable() {
+ public void run() throws Exception {
+ listener.handleEvent(event);
+ }
+
+ public void handleException(Throwable e) {
+ StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN, "Listener failed: " //$NON-NLS-1$
+ + listener.getClass(), e));
+ }
+ });
+ }
+ }
+
+ public void setServiceMessageUrl(String url) {
+ this.serviceMessageUrl = url;
+ }
+
+ public void addServiceMessageListener(IServiceMessageListener listener) {
+ listeners.add(listener);
+ }
+
+ public void removeServiceMessageListener(IServiceMessageListener listener) {
+ listeners.remove(listener);
+ }
+
+ private void notifyListeners(List<ServiceMessage> messages) {
+ this.messages = messages;
+ for (final ServiceMessage message : messages) {
+ message.setETag(eTag);
+ message.setLastModified(lastModified);
+ }
+
+ final ServiceMessageEvent event = new ServiceMessageEvent(this, ServiceMessageEvent.EVENT_KIND.MESSAGE_UPDATE,
+ messages);
+
+ for (final IServiceMessageListener listener : listeners) {
+ SafeRunner.run(new ISafeRunnable() {
+ public void run() throws Exception {
+ listener.handleEvent(event);
+ }
+
+ public void handleException(Throwable e) {
+ StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN, "Listener failed: " //$NON-NLS-1$
+ + listener.getClass(), e));
+ }
+ });
+ }
+ }
+
+ public List<ServiceMessage> getServiceMessages() {
+ return new ArrayList<ServiceMessage>(this.messages);
+ }
+
+ /**
+ * Public for testing
+ */
+ public int updateServiceMessage(IProgressMonitor monitor) {
+ int status = -1;
+ List<ServiceMessage> messages = null;
+ try {
+ HttpClient httpClient = new HttpClient(WebUtil.getConnectionManager());
+ WebUtil.configureHttpClient(httpClient, null);
+
+ WebLocation location = new WebLocation(serviceMessageUrl);
+ HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, location,
+ new SubProgressMonitor(monitor, 1));
+
+ GetMethod method = new GetMethod(serviceMessageUrl);
+ method.setRequestHeader("If-Modified-Since", lastModified); //$NON-NLS-1$
+ method.setRequestHeader("If-None-Match", eTag); //$NON-NLS-1$
+
+ try {
+ status = WebUtil.execute(httpClient, hostConfiguration, method, monitor);
+ if (status == HttpStatus.SC_OK && messageCheckJob != null) {
+ Header lastModifiedHeader = method.getResponseHeader("Last-Modified"); //$NON-NLS-1$
+ if (lastModifiedHeader != null) {
+ lastModified = lastModifiedHeader.getValue();
+ }
+ Header eTagHeader = method.getResponseHeader("ETag"); //$NON-NLS-1$
+ if (eTagHeader != null) {
+ eTag = eTagHeader.getValue();
+ }
+
+ InputStream in = WebUtil.getResponseBodyAsStream(method, monitor);
+ try {
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ factory.setValidating(false);
+ SAXParser parser = factory.newSAXParser();
+
+ ServiceMessageXmlHandler handler = new ServiceMessageXmlHandler();
+ parser.parse(in, handler);
+ messages = handler.getMessages();
+ } finally {
+ in.close();
+ }
+ } else if (status == HttpStatus.SC_NOT_FOUND) {
+ // no messages
+ } else if (status == HttpStatus.SC_NOT_MODIFIED) {
+ // no new messages
+ } else {
+ if (!statusLogged) {
+ statusLogged = true;
+ StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN,
+ "Http error retrieving service message: " + HttpStatus.getStatusText(status))); //$NON-NLS-1$
+ }
+ }
+ } finally {
+ WebUtil.releaseConnection(method, monitor);
+ }
+ } catch (Exception e) {
+ if (!statusLogged) {
+ statusLogged = true;
+ StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN,
+ "Http error retrieving service message.", e)); //$NON-NLS-1$
+ }
+ }
+
+ if (messages != null && messages.size() > 0) {
+ notifyListeners(messages);
+ }
+ return status;
+ }
+
+}
diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageXmlHandler.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageXmlHandler.java
new file mode 100644
index 000000000..3ba14985c
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/notifications/ServiceMessageXmlHandler.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.tasks.core.notifications;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * @author Robert Elves
+ * @author Steffen Pingel
+ */
+public class ServiceMessageXmlHandler extends DefaultHandler {
+
+ private static final String TAG_MESSAGE = "ServiceMessage"; //$NON-NLS-1$
+
+ private StringBuilder characters;
+
+ private final List<ServiceMessage> messages = new ArrayList<ServiceMessage>();
+
+ private ServiceMessage message;
+
+ @Override
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ characters.append(ch, start, length);
+ }
+
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
+ if (TAG_MESSAGE.equals(qName.trim())) {
+ message = new ServiceMessage();
+ }
+ characters = new StringBuilder();
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String qName) throws SAXException {
+ if (message == null) {
+ return;
+ }
+ if (TAG_MESSAGE.equals(qName.trim())) {
+ if (message.isValid()) {
+ messages.add(message);
+ }
+ } else {
+ String parsedText = characters.toString();
+ ServiceMessage.Element element;
+ try {
+ element = ServiceMessage.Element.valueOf(qName.trim().toUpperCase());
+ switch (element) {
+ case ID:
+ message.setId(parsedText);
+ break;
+ case DESCRIPTION:
+ message.setDescription(parsedText);
+ break;
+ case TITLE:
+ message.setTitle(parsedText);
+ break;
+ case URL:
+ message.setUrl(parsedText);
+ break;
+ case IMAGE:
+ message.setImage(parsedText);
+ break;
+ case VERSION:
+ message.setVersion(parsedText);
+ break;
+ }
+ } catch (IllegalArgumentException e) {
+ // ignore unrecognized elements
+ }
+ }
+ }
+
+ public List<ServiceMessage> getMessages() {
+ return new ArrayList<ServiceMessage>(messages);
+ }
+
+}

Back to the top