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