1 package org.simantics.project.management.install;
\r
3 import java.io.IOException;
\r
5 import org.eclipse.core.runtime.IPath;
\r
6 import org.eclipse.core.runtime.IProgressMonitor;
\r
7 import org.eclipse.core.runtime.IStatus;
\r
8 import org.eclipse.core.runtime.SubMonitor;
\r
9 import org.eclipse.equinox.internal.provisional.p2.installer.InstallAdvisor;
\r
10 import org.eclipse.equinox.internal.provisional.p2.installer.InstallDescription;
\r
11 import org.eclipse.equinox.p2.core.IProvisioningAgent;
\r
12 import org.eclipse.equinox.p2.core.IProvisioningAgentProvider;
\r
13 import org.eclipse.equinox.p2.core.ProvisionException;
\r
14 import org.eclipse.equinox.p2.core.UIServices;
\r
15 import org.osgi.framework.BundleContext;
\r
16 import org.osgi.framework.ServiceReference;
\r
17 import org.simantics.project.management.P2Util;
\r
18 import org.simantics.project.management.TrustAllCertificates;
\r
21 * P2 installer util.
\r
23 * Heavily inspired by org.eclipse.equinox.p2.installer project.
\r
25 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
27 public class InstallerUtil {
\r
29 private static final String SYS_PROP_INSTALL_DESCRIPTION = "org.eclipse.equinox.p2.installDescription";
\r
31 public static IStatus install(InstallAdvisor advisor, InstallDescription description, IProgressMonitor monitor) throws Exception {
\r
32 if (advisor == null)
\r
33 advisor = getDefaultAdvisor();
\r
34 if (description == null)
\r
35 description = getDefaultDescription(SubMonitor.convert(monitor));
\r
37 advisor.prepareInstallDescription(description);
\r
38 IProvisioningAgent agent = startAgent(description);
\r
40 InstallUpdateProductOperation operation = new InstallUpdateProductOperation(agent, description);
\r
42 IStatus status = advisor.performInstall(operation);
\r
43 if (!status.isOK()) {
\r
44 // ShowMessage.showStatus(status);
\r
46 advisor.setResult(status);
\r
52 public static InstallDescription getDefaultDescription(SubMonitor monitor) throws Exception {
\r
53 String site = System.getProperty(SYS_PROP_INSTALL_DESCRIPTION);
\r
54 return InstallDescriptionParser.createDescription(site, monitor);
\r
58 public static InstallAdvisor getDefaultAdvisor() {
\r
59 InstallAdvisor advisor = new DefaultInstallAdvisor();
\r
64 public static IProvisioningAgent startAgent(InstallDescription description) throws Exception {
\r
65 IPath installLocation = description.getInstallLocation();
\r
66 if (installLocation == null)
\r
67 throw new Exception("No install location");
\r
71 IProvisioningAgentProvider provider = P2Util.getProvisioningAgentProvider();
\r
73 // Set agent location if specified. Contains P2 install data
\r
74 IPath agentLocation = description.getAgentLocation();
\r
76 // Recruit Agent - Permission to provision
\r
77 IProvisioningAgent agent = provider.createAgent(agentLocation == null ? null : agentLocation.toFile().toURI());
\r
79 // Omit "This bundle is not certified" dialog.
\r
80 UIServices serviceUI = new TrustAllCertificates();
\r
81 agent.registerService("org.eclipse.equinox.p2.core.UIServices", serviceUI);
\r
85 } catch (ProvisionException e) {
\r
86 throw new Exception("Cannot create agent", e);
\r
92 * Copied from ServiceHelper because we need to obtain services
\r
93 * before p2 has been started.
\r
95 public static Object getService(BundleContext context, String name) {
\r
96 if (context == null)
\r
98 ServiceReference reference = context.getServiceReference(name);
\r
99 if (reference == null)
\r
101 Object result = context.getService(reference);
\r
102 context.ungetService(reference);
\r
107 public static void launchProduct(InstallDescription description) throws Exception {
\r
108 IPath installLocation = description.getInstallLocation();
\r
109 IPath toRun = installLocation.append(description.getLauncherName());
\r
111 Runtime.getRuntime().exec(toRun.toString(), null, installLocation.toFile());
\r
112 } catch (IOException e) {
\r
113 throw new Exception("Lanuching an app failed", e);
\r