Revert "ref #7268, #6913 Remove p2 related code"
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 4 Jun 2018 14:14:33 +0000 (16:14 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 4 Jun 2018 14:14:33 +0000 (16:14 +0200)
This reverts commit 598845be674417e5a4d79cc533f3ff3b1a0b4cdb.

eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/ApplicationWorkbenchWindowAdvisor.java
eu.etaxonomy.taxeditor.application/src/main/java/eu/etaxonomy/taxeditor/TaxonomicEditorPlugin.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/update/P2Util.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/IPreferenceKeys.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/PreferencesUtil.java
eu.etaxonomy.taxeditor.workbench/src/main/java/eu/etaxonomy/taxeditor/workbench/update/InstallNewSoftwareHandler.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.workbench/src/main/java/eu/etaxonomy/taxeditor/workbench/update/PreloadingRepositoryHandler.java [new file with mode: 0644]

index 04ca7b60995411e5e20c3ab5a96642bd59be4a44..79b4a8fde90b26d08b4844a924b75ee74788a988 100644 (file)
@@ -15,6 +15,7 @@ import org.eclipse.ui.application.WorkbenchWindowAdvisor;
 import org.eclipse.ui.internal.WorkbenchWindow;
 
 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
+import eu.etaxonomy.taxeditor.handler.update.P2Util;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.util.ApplicationUtil;
@@ -72,7 +73,13 @@ public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
             CdmStore.connect();
         }
 
+               P2Util.setP2UpdateRepositories();
+
        }
 
+       @Override
+    public void postWindowClose() {
+        P2Util.saveP2RepositoryPreferences();
+    }
 }
 
index 5aab8ba505f864ffb7144935d69e0ecf99618c98..3d2a3330d82d3d416643d716e1bde9c1dbd5ff97 100644 (file)
@@ -1,10 +1,16 @@
 package eu.etaxonomy.taxeditor;
 
 import org.apache.log4j.Logger;
+import org.eclipse.equinox.p2.core.IProvisioningAgent;
+import org.eclipse.equinox.p2.engine.IProfileRegistry;
+import org.eclipse.equinox.p2.operations.ProvisioningSession;
+import org.eclipse.equinox.p2.ui.Policy;
+import org.eclipse.equinox.p2.ui.ProvisioningUI;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.packageadmin.PackageAdmin;
 
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
@@ -20,6 +26,7 @@ public class TaxonomicEditorPlugin extends AbstractUIPlugin {
                        .getLogger(TaxonomicEditorPlugin.class);
 
        // The plug-in ID
+       /** Constant <code>PLUGIN_ID="eu.etaxonomy.taxeditor.application"</code> */
        public static final String PLUGIN_ID = "eu.etaxonomy.taxeditor.application";
 
        // The shared instance
@@ -29,12 +36,28 @@ public class TaxonomicEditorPlugin extends AbstractUIPlugin {
 
        private ServiceReference packageAdminRef;
 
+       /** Constant <code>bundleContext</code> */
        protected static BundleContext bundleContext;
 
 
+       private ServiceRegistration policyRegistration;
+
+       private ProvisioningSession session;
+       private ProvisioningUI ui;
+
+//     private CloudPolicy policy;
+
+       /**
+        * The constructor
+        */
        public TaxonomicEditorPlugin() {
        }
 
+       /*
+        * (non-Javadoc)
+        * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+        */
+       /** {@inheritDoc} */
        @Override
     public void start(BundleContext context) throws Exception {
                super.start(context);
@@ -65,6 +88,10 @@ public class TaxonomicEditorPlugin extends AbstractUIPlugin {
                return null;
        }
 
+       /*
+        * (non-Javadoc)
+        * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+        */
        /** {@inheritDoc} */
        @Override
        public void stop(BundleContext context) throws Exception {
@@ -86,4 +113,20 @@ public class TaxonomicEditorPlugin extends AbstractUIPlugin {
            return bundleContext;
        }
 
+       public ProvisioningUI getProvisioningUI() {
+
+           if (ui == null) {
+               ServiceReference reference = bundleContext.getServiceReference(IProvisioningAgent.SERVICE_NAME);
+               IProvisioningAgent agent = (IProvisioningAgent) bundleContext.getService(reference);
+               session = new ProvisioningSession(agent);
+               Policy policy = null;// = (Policy) ServiceHelper.getService(ProvUIActivator.getContext(), Policy.class.getName());
+               if (policy == null) {
+                   policy = new Policy();
+               }
+               ui = new ProvisioningUI(session, IProfileRegistry.SELF, policy);
+           }
+           return ui;
+       }
+
+
 }
index 08f1e79f1f6324cbb54ee860a03e1a1b3b3e5245..5d99d19658317a3d3b8ed261312748c83466a25e 100644 (file)
@@ -2,8 +2,19 @@ package eu.etaxonomy.taxeditor.handler.update;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+
+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.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 eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.util.ApplicationUtil;
 
 /**
@@ -29,6 +40,29 @@ 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 setP2UpdateRepositories() {
+        List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
+        List<MetadataRepositoryElement> savedRepoElements = new ArrayList<>();
+
+        MetadataRepositoryElement mre = new MetadataRepositoryElement(null, getP2UpdateRepository(), true);
+        mre.setNickname(getP2UpdateRepositoryName());
+        savedRepoElements.add(mre);
+
+        repoElements.addAll(savedRepoElements);
+
+        ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
+                .toArray(new MetadataRepositoryElement[]{} ));
+
+    }
+
+    public static String getP2UpdateRepositoryName(){
+        return ApplicationUtil.isStable()?EDIT_STABLE_UPDATE_SITE_NAME:EDIT_NIGHTLY_UPDATE_SITE_NAME;
+    }
 
     public static URI getP2UpdateRepository(){
         try {
@@ -38,4 +72,42 @@ public class P2Util {
         }
         return 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);
+    }
 }
index b6fc6f3e3de1f5bdfdaebe399f54bf7ec89b4ca9..2e847ebb4725039293e5b65b1daa903757e9b433 100644 (file)
@@ -173,6 +173,10 @@ public interface IPreferenceKeys {
 
     public static final String IS_SHOW_UP_WIDGET_IS_DISPOSED = "eu.etaxonomy.taxeditor.isShowUpWidgetIsDisposed";
 
+    /**
+     * Key for the saved P2 repositories
+     */
+    public static final String P2_REPOSITORY_LIST = "eu.etaxonomy.taxeditor.p2.repositories";
 //    public static final String ALLOW_OVERRIDE_NAMEDETAILS = "eu.etaxonomy.taxeditor.details.allow_override";
     public static final String ALLOW_OVERRIDE_RL = "eu.etaxonomy.taxeditor.isRL.allow_override";
     public static final String ALLOW_OVERRIDE_SPECIMEN_PREF = "eu.etaxonomy.taxeditor.specimen.allowOverride";
index 0965704794059096b40e0cc5a93ff73afcb0dff7..041d40eede2da3caad48f4b2c38a43c3ae9197d0 100644 (file)
@@ -13,6 +13,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -21,10 +23,12 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.StringTokenizer;
 import java.util.UUID;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
+import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.window.Window;
 import org.eclipse.swt.widgets.Composite;
@@ -76,8 +80,14 @@ import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
  */
 public class PreferencesUtil implements IPreferenceKeys {
 
+       /**
+        *
+        */
        public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
 
+       public static final String P2_REPOSITORIES_DELIM = ",";
+       public static final String P2_REPOSITORY_FIELDS_DELIM = ";";
+
        private final static Logger logger = Logger.getLogger(PreferencesUtil.class);
 
        /**
@@ -868,6 +878,62 @@ public class PreferencesUtil implements IPreferenceKeys {
         fos.close();
     }
 
+    /**
+     * Saves a list of P2 Metadata Repositories as string with specified delimiters
+     *
+     * @param p2Repos
+     */
+    public static void setP2Repositories(List<MetadataRepositoryElement> p2Repos) {
+        StringBuilder sb = new StringBuilder();
+        for(MetadataRepositoryElement p2Repo : p2Repos) {
+            sb.append(P2_REPOSITORIES_DELIM);
+            if(p2Repo.getName() == null || p2Repo.getName().isEmpty()) {
+                sb.append("-");
+            } else {
+                sb.append(p2Repo.getName());
+            }
+            sb.append(P2_REPOSITORY_FIELDS_DELIM);
+            sb.append(p2Repo.getLocation().toString());
+            sb.append(P2_REPOSITORY_FIELDS_DELIM);
+            sb.append(String.valueOf(p2Repo.isEnabled()));
+        }
+        getPreferenceStore().setValue(P2_REPOSITORY_LIST, sb.toString());
+    }
+
+
+    /**
+     * Retrieves a list of previously saved P2 repositories
+     *
+     * @return
+     */
+    public static List<MetadataRepositoryElement> getP2Repositories() {
+        List<MetadataRepositoryElement> p2Repos = new ArrayList<MetadataRepositoryElement>();
+        String p2ReposPref =  getPreferenceStore().getString(P2_REPOSITORY_LIST);
+        if(p2ReposPref != null && !p2ReposPref.isEmpty()) {
+            StringTokenizer p2ReposPrefST = new StringTokenizer(p2ReposPref,P2_REPOSITORIES_DELIM);
+
+            while(p2ReposPrefST.hasMoreTokens()) {
+                String p2RepoStr = p2ReposPrefST.nextToken();
+                StringTokenizer p2ReposStrST = new StringTokenizer(p2RepoStr,P2_REPOSITORY_FIELDS_DELIM);
+                if(p2ReposStrST.countTokens()==3) {
+                    String nickname = p2ReposStrST.nextToken();
+                    URI uri = null;
+                    try {
+                        uri = new URI(p2ReposStrST.nextToken());
+                    } catch (URISyntaxException e) {
+                        continue;
+                    }
+                    boolean enabled = Boolean.parseBoolean(p2ReposStrST.nextToken());
+                    MetadataRepositoryElement mre = new MetadataRepositoryElement(null, uri, true);
+                    mre.setNickname(nickname);
+                    mre.setEnabled(enabled);
+                    p2Repos.add(mre);
+                }
+            }
+        }
+
+        return p2Repos;
+    }
 
     /**
      * enables/disables nested composite. <br>
diff --git a/eu.etaxonomy.taxeditor.workbench/src/main/java/eu/etaxonomy/taxeditor/workbench/update/InstallNewSoftwareHandler.java b/eu.etaxonomy.taxeditor.workbench/src/main/java/eu/etaxonomy/taxeditor/workbench/update/InstallNewSoftwareHandler.java
new file mode 100644 (file)
index 0000000..9d81fad
--- /dev/null
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ *  Copyright (c) 2008, 2009 IBM Corporation 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:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package eu.etaxonomy.taxeditor.workbench.update;
+
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
+
+/**
+ * InstallNewSoftwareHandler invokes the install wizard
+ *
+ * @since 3.5
+ */
+public class InstallNewSoftwareHandler extends PreloadingRepositoryHandler {
+
+       /**
+        * The constructor.
+        */
+       public InstallNewSoftwareHandler() {
+               super();
+       }
+
+       @Override
+    protected void doExecute(LoadMetadataRepositoryJob job) {
+               getProvisioningUI().openInstallWizard(null, null, job);
+       }
+
+       @Override
+    protected boolean waitForPreload() {
+               // If the user cannot see repositories, then we may as well wait
+               // for existing repos to load so that content is available.
+               // If the user can manipulate the repositories, then we don't wait,
+               // because we don't know which ones they want to work with.
+               return !getProvisioningUI().getPolicy().getRepositoriesVisible();
+       }
+
+       @Override
+    protected void setLoadJobProperties(Job loadJob) {
+               super.setLoadJobProperties(loadJob);
+               // If we are doing a background load, we do not wish to authenticate, as the
+               // user is unaware that loading was needed
+               if (!waitForPreload()) {
+                       loadJob.setProperty(LoadMetadataRepositoryJob.SUPPRESS_AUTHENTICATION_JOB_MARKER, Boolean.toString(true));
+                       loadJob.setProperty(LoadMetadataRepositoryJob.SUPPRESS_REPOSITORY_EVENTS, Boolean.toString(true));
+               }
+       }
+}
diff --git a/eu.etaxonomy.taxeditor.workbench/src/main/java/eu/etaxonomy/taxeditor/workbench/update/PreloadingRepositoryHandler.java b/eu.etaxonomy.taxeditor.workbench/src/main/java/eu/etaxonomy/taxeditor/workbench/update/PreloadingRepositoryHandler.java
new file mode 100644 (file)
index 0000000..895e576
--- /dev/null
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2009 IBM Corporation 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:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package eu.etaxonomy.taxeditor.workbench.update;
+
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.equinox.p2.ui.LoadMetadataRepositoryJob;
+import org.eclipse.equinox.p2.ui.ProvisioningUI;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * PreloadingRepositoryHandler provides background loading of
+ * repositories before executing the provisioning handler.
+ *
+ * @since 3.5
+ */
+abstract class PreloadingRepositoryHandler{
+
+       /**
+        * The constructor.
+        */
+       public PreloadingRepositoryHandler() {
+               // constructor
+       }
+
+       /**
+        * Execute the command.
+        */
+       @Execute
+       public Object execute() {
+               doExecuteAndLoad();
+               return null;
+       }
+
+       private void doExecuteAndLoad() {
+               if (preloadRepositories()) {
+                       //cancel any load that is already running
+                       Job.getJobManager().cancel(LoadMetadataRepositoryJob.LOAD_FAMILY);
+                       final LoadMetadataRepositoryJob loadJob = new LoadMetadataRepositoryJob(getProvisioningUI());
+                       setLoadJobProperties(loadJob);
+                       if (waitForPreload()) {
+                               loadJob.addJobChangeListener(new JobChangeAdapter() {
+                                       @Override
+                    public void done(IJobChangeEvent event) {
+                                               if (PlatformUI.isWorkbenchRunning()) {
+                            if (event.getResult().isOK()) {
+                                                               PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
+                                                                       @Override
+                                    public void run() {
+                                                                               doExecute(loadJob);
+                                                                       }
+                                                               });
+                                                       }
+                        }
+                                       }
+                               });
+                               loadJob.setUser(true);
+                               loadJob.schedule();
+
+                       } else {
+                               loadJob.setSystem(true);
+                               loadJob.setUser(false);
+                               loadJob.schedule();
+                               doExecute(null);
+                       }
+               } else {
+                       doExecute(null);
+               }
+       }
+
+       protected abstract void doExecute(LoadMetadataRepositoryJob job);
+
+       protected boolean preloadRepositories() {
+               return true;
+       }
+
+       protected boolean waitForPreload() {
+               return true;
+       }
+
+       protected void setLoadJobProperties(Job loadJob) {
+               loadJob.setProperty(LoadMetadataRepositoryJob.ACCUMULATE_LOAD_ERRORS, Boolean.toString(true));
+       }
+
+       protected ProvisioningUI getProvisioningUI() {
+               return ProvisioningUI.getDefaultUI();
+       }
+
+       /**
+        * Return a shell appropriate for parenting dialogs of this handler.
+        * @return a Shell
+        */
+       protected Shell getShell() {
+               return PlatformUI.getWorkbench().getModalDialogShellProvider().getShell();
+       }
+}