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;
CdmStore.connect();
}
+ P2Util.setP2UpdateRepositories();
+
}
+ @Override
+ public void postWindowClose() {
+ P2Util.saveP2RepositoryPreferences();
+ }
}
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;
.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
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);
return null;
}
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+ */
/** {@inheritDoc} */
@Override
public void stop(BundleContext context) throws Exception {
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;
+ }
+
+
}
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;
/**
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 {
}
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);
+ }
}
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";
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;
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;
*/
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);
/**
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>
--- /dev/null
+/*******************************************************************************
+ * 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));
+ }
+ }
+}
--- /dev/null
+/*******************************************************************************
+ * 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();
+ }
+}