2 * Copyright (C) 2020 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.handler
.update
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import org
.apache
.logging
.log4j
.LogManager
;import org
.apache
.logging
.log4j
.Logger
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.IJobChangeEvent
;
20 import org
.eclipse
.core
.runtime
.jobs
.Job
;
21 import org
.eclipse
.core
.runtime
.jobs
.JobChangeAdapter
;
22 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
23 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
24 import org
.eclipse
.e4
.ui
.workbench
.IWorkbench
;
25 import org
.eclipse
.equinox
.internal
.p2
.metadata
.InstallableUnit
;
26 import org
.eclipse
.equinox
.p2
.core
.IProvisioningAgent
;
27 import org
.eclipse
.equinox
.p2
.metadata
.IInstallableUnit
;
28 import org
.eclipse
.equinox
.p2
.operations
.ProvisioningJob
;
29 import org
.eclipse
.equinox
.p2
.operations
.ProvisioningSession
;
30 import org
.eclipse
.equinox
.p2
.operations
.UpdateOperation
;
31 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
32 import org
.eclipse
.swt
.widgets
.Shell
;
34 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
40 public class SearchPluginHandler
{
41 private Logger logger
= LogManager
.getLogger(getClass());
43 private UpdateOperation operation
;
46 public void execute(final IProvisioningAgent agent
, final Shell shell
, final UISynchronize sync
,
47 final IWorkbench workbench
) {
48 Job checkUpdateJob
= new Job(Messages
.UpdateHandler_CHECK_UPDATE_JOB
) {
50 protected IStatus
run(final IProgressMonitor monitor
) {
51 return checkForPlugins(agent
, shell
, sync
, monitor
);
54 checkUpdateJob
.schedule();
57 checkUpdateJob
.addJobChangeListener(new JobChangeAdapter() {
59 public void done(IJobChangeEvent event
) {
60 if (event
.getResult().isOK()) {
61 sync
.syncExec(new Runnable() {
65 if(MessageDialog
.openConfirm(shell
, Messages
.UpdateHandler_UPDATES_FOUND_TITLE
, Messages
.UpdateHandler_UPDATES_FOUND_MESSAGE
)){
66 Job installUpdateJob
= new Job(Messages
.UpdateHandler_INSTALL_JOB
) {
68 protected IStatus
run(final IProgressMonitor monitor
) {
70 ProvisioningJob provisioningJob
= operation
.getProvisioningJob(monitor
);
72 // updates cannot run from within Eclipse IDE!!!
73 if (provisioningJob
== null) {
74 logger
.error("Trying to update from the Eclipse IDE? This won't work!"); //$NON-NLS-1$
75 return Status
.CANCEL_STATUS
;
77 configureProvisioningJob(provisioningJob
, shell
, sync
, workbench
);
78 provisioningJob
.schedule();
79 return Status
.OK_STATUS
;
82 installUpdateJob
.schedule();
91 private IStatus
checkForPlugins(final IProvisioningAgent agent
, final Shell shell
, final UISynchronize sync
,
92 IProgressMonitor monitor
) {
94 // configure update operation
95 final ProvisioningSession session
= new ProvisioningSession(agent
);
96 operation
= new UpdateOperation(session
);
97 configureUpdate(operation
);
99 // check for updates, this causes I/O
100 final IStatus status
= operation
.resolveModal(monitor
);
102 // failed to find updates (inform user and exit)
103 if (status
.getCode() == UpdateOperation
.STATUS_NOTHING_TO_UPDATE
) {
104 showMessage(shell
, sync
);
105 return Status
.CANCEL_STATUS
;
107 return Status
.OK_STATUS
;
110 private void configureProvisioningJob(ProvisioningJob provisioningJob
, final Shell shell
, final UISynchronize sync
,
111 final IWorkbench workbench
) {
113 // register a job change listener to track
114 // installation progress and notify user upon success
115 provisioningJob
.addJobChangeListener(new JobChangeAdapter() {
117 public void done(IJobChangeEvent event
) {
118 if (event
.getResult().isOK()) {
119 sync
.syncExec(new Runnable() {
123 boolean restart
= MessageDialog
.openQuestion(shell
, Messages
.UpdateHandler_UPDATE_INSTALLED_TITLE
,
124 Messages
.UpdateHandler_UPDATE_INSTALLED_TITLE_MESSAGE
);
136 private void showMessage(final Shell parent
, final UISynchronize sync
) {
138 MessageDialog
.openWarning(parent
, Messages
.UpdateHandler_NO_UPDATE_TITLE
,
139 Messages
.UpdateHandler_NO_UPDATE_MESSAGE
)
143 private UpdateOperation
configureUpdate(final UpdateOperation operation
) {
144 // create uri and check for validity
146 uri
= P2Util
.getP2UpdateRepository();
148 // set location of artifact and metadata repo
149 operation
.getProvisioningContext().setArtifactRepositories(new URI
[] { uri
});
150 operation
.getProvisioningContext().setMetadataRepositories(new URI
[] { uri
});
151 InstallableUnit ctxIU
= new InstallableUnit();
152 ctxIU
.setId("eu.etaxonomy.taxeditor.local");
153 List
<IInstallableUnit
> extraIUs
= new ArrayList
<>();
155 operation
.getProvisioningContext().setExtraInstallableUnits(extraIUs
);