merge
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / P2Util.java
index e666929acc0b96365cf295874c7fdef93a2c2738..a286718ff9854c4a23dc879cd655b6fdecd62087 100644 (file)
@@ -2,6 +2,8 @@ package eu.etaxonomy.taxeditor.update;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
@@ -10,22 +12,28 @@ import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
+import org.eclipse.equinox.internal.p2.ui.ProvUI;
 import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
 import org.eclipse.equinox.p2.core.IProvisioningAgent;
+import org.eclipse.equinox.p2.core.ProvisionException;
 import org.eclipse.equinox.p2.operations.ProvisioningJob;
 import org.eclipse.equinox.p2.operations.ProvisioningSession;
 import org.eclipse.equinox.p2.operations.Update;
 import org.eclipse.equinox.p2.operations.UpdateOperation;
+import org.eclipse.equinox.p2.repository.IRepository;
+import org.eclipse.equinox.p2.repository.IRepositoryManager;
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
 import org.eclipse.equinox.p2.ui.ProvisioningUI;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.PlatformUI;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 
-import eu.etaxonomy.taxeditor.ApplicationUtil;
 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.util.ApplicationUtil;
 
 /**
  * This class is a utility class for updating the editor from a p2 update site,
@@ -36,10 +44,7 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
  * - Allow configurable update sites
  *
  * Notes :
- * - tried to get rid of the popup dialog which warns about unsigned jars using
- *   -Declipse.p2.unsignedPolicy=allow but this does not work due to
- *   https://bugs.eclipse.org/bugs/show_bug.cgi?id=391399, so the only solution
- *   is to sign the jars with an official certificate.
+ *
  *
  * @see http://wiki.eclipse.org/Equinox/p2/Adding_Self-Update_to_an_RCP_Application
  * @see http://bugs.eclipse.org/281226
@@ -48,6 +53,7 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
 public class P2Util {
 
     //private static String LOCAL_UPDATE_SITE = "file:///path/.../to/Development/EDIT/taxeditor/eu.etaxonomy.taxeditor/target/repository/";
+
     private static String EDIT_NIGHTLY_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/nightly/";
     private static String EDIT_NIGHTLY_UPDATE_SITE_NAME = "Taxonomic Editor Nightly";
 
@@ -57,8 +63,12 @@ public class P2Util {
     private static String EDIT_STABLE_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/stable/";
     private static String EDIT_STABLE_UPDATE_SITE_NAME = "Taxonomic Editor Stable";
 
+    /**
+     * Retrieve and load the saved list of repositories from the preference store,
+     * making sure that at least the default repository is always loaded.
+     */
     @SuppressWarnings("restriction")
-    public static void setDefaultUpdateRepositories() {
+    public static void setP2UpdateRepositories() {
         String updateSite = EDIT_NIGHTLY_UPDATE_SITE;
         String updateSiteName = EDIT_NIGHTLY_UPDATE_SITE_NAME;
 
@@ -66,26 +76,74 @@ public class P2Util {
             updateSite = EDIT_STABLE_UPDATE_SITE;
             updateSiteName = EDIT_STABLE_UPDATE_SITE_NAME;
         }
-        try {
-          final MetadataRepositoryElement element = new MetadataRepositoryElement(null, new URI(updateSite), true);
-          element.setNickname(updateSiteName);
-          ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),new MetadataRepositoryElement[] {element}, null);
-        } catch (URISyntaxException e) {
-            MessagingUtils.errorDialog("Invalid update site URI",
-                    P2Util.class,
-                    "The update site URI has an invalid syntax",
-                    TaxonomicEditorPlugin.PLUGIN_ID,
-                    e,
-                    false);
+        List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
+        List<MetadataRepositoryElement> savedRepoElements = PreferencesUtil.getP2Repositories();
+        if(savedRepoElements.isEmpty()) {
+            // we always need an update site, so we add the default one
+            try {
+                MetadataRepositoryElement element = new MetadataRepositoryElement(null, new URI(updateSite), true);
+                element.setNickname(updateSiteName);
+                repoElements.add(element);
+            } catch (URISyntaxException e) {
+                MessagingUtils.errorDialog("Invalid update site URI",
+                        P2Util.class,
+                        "The update site URI has an invalid syntax",
+                        TaxonomicEditorPlugin.PLUGIN_ID,
+                        e,
+                        false);
+            }
+        }
+        repoElements.addAll(savedRepoElements);
+
+        ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
+                .toArray(new MetadataRepositoryElement[]{} ), null);
+
+    }
+
+    /**
+     * {@link org.eclipse.equinox.p2.ui.RepositoryManipulationPage} which handles the repsitory site list
+     * in preferences does not create a preference store and hence the changes are not saved. This means
+     * that we need to save it ourselves.
+     *
+     * This method saves the list of current repositories in the preference store as a string with
+     * specific delimiters.
+     */
+
+    public static void saveP2RepositoryPreferences() {
+
+        IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
+
+        URI[] currentlyEnabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
+        URI[] currentlyDisabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_DISABLED);
+
+        List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
+
+        for(URI repo : currentlyEnabled) {
+            boolean enabled = true;
+            String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
+            MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
+            element.setNickname(nickname);
+            element.setEnabled(enabled);
+            repoElements.add(element);
         }
-      }
+
+        for(URI repo : currentlyDisabled) {
+            boolean enabled = false;
+            String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
+            MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
+            element.setNickname(nickname);
+            element.setEnabled(enabled);
+            repoElements.add(element);
+        }
+        PreferencesUtil.setP2Repositories(repoElements);
+    }
 
     /**
      *
      *
      */
     public static void checkForUpdates() {
-        // the main job which performs the update
+
         Job updateJob = new Job("Update Job") {
             @Override
             public IStatus run(IProgressMonitor monitor) {
@@ -101,6 +159,21 @@ public class P2Util {
      */
     private static IStatus doCheckForUpdates(IProgressMonitor monitor) {
 
+     // force refresh all the caches before
+        IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
+        URI[] repos = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
+        for(URI repo : repos) {
+            try {
+                metaManager.refreshRepository(repo, monitor);
+            } catch (ProvisionException pe) {
+                IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
+                        "Error occured while reloading cache.", pe);
+
+            } catch (OperationCanceledException oce) {
+                IStatus errorStatus = new Status(IStatus.ERROR, TaxonomicEditorPlugin.PLUGIN_ID,
+                        "Error occured while reloading cache.", oce);
+            }
+        }
         BundleContext bundleContext = TaxonomicEditorPlugin.getContext();
         ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
         if (reference == null) {