]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/install/InstallerUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / install / InstallerUtil.java
1 package org.simantics.project.management.install;
2
3 import java.io.IOException;
4
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;
19
20 /**
21  * P2 installer util.
22  * 
23  * Heavily inspired by org.eclipse.equinox.p2.installer project.
24  * 
25  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
26  */
27 public class InstallerUtil {
28
29         private static final String SYS_PROP_INSTALL_DESCRIPTION = "org.eclipse.equinox.p2.installDescription";
30         
31         public static IStatus install(InstallAdvisor advisor, InstallDescription description, IProgressMonitor monitor) throws Exception {
32                 if (advisor == null)
33                         advisor = getDefaultAdvisor();
34                 if (description == null)
35                         description = getDefaultDescription(SubMonitor.convert(monitor));
36
37                 advisor.prepareInstallDescription(description);
38                 IProvisioningAgent agent = startAgent(description);             
39                 
40                 InstallUpdateProductOperation operation = new InstallUpdateProductOperation(agent, description);
41                 
42                 IStatus status = advisor.performInstall(operation);
43                 if (!status.isOK()) {
44 //                      ShowMessage.showStatus(status);
45                 }
46                 advisor.setResult(status);
47                 advisor.stop();
48                 agent.stop();
49                 return status;
50         }
51         
52         public static InstallDescription getDefaultDescription(SubMonitor monitor) throws Exception {
53                 String site = System.getProperty(SYS_PROP_INSTALL_DESCRIPTION);
54                 return InstallDescriptionParser.createDescription(site, monitor);
55                 
56         }
57         
58         public static InstallAdvisor getDefaultAdvisor() {
59                 InstallAdvisor advisor = new DefaultInstallAdvisor();
60                 advisor.start();
61                 return advisor;
62         }
63         
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");
68                 
69                 try {
70                         // Create recruiter
71                         IProvisioningAgentProvider provider = P2Util.getProvisioningAgentProvider();
72                         
73                         // Set agent location if specified. Contains P2 install data
74                         IPath agentLocation = description.getAgentLocation();
75                         
76                         // Recruit Agent - Permission to provision
77                         IProvisioningAgent agent = provider.createAgent(agentLocation == null ? null : agentLocation.toFile().toURI());
78                         
79                         // Omit "This bundle is not certified" dialog.                  
80                         UIServices serviceUI = new TrustAllCertificates();
81                         agent.registerService("org.eclipse.equinox.p2.core.UIServices", serviceUI);
82                         
83                         
84                         return agent;
85                 } catch (ProvisionException e) {
86                         throw new Exception("Cannot create agent", e);
87                 }
88         }
89
90         
91         /**
92          * Copied from ServiceHelper because we need to obtain services
93          * before p2 has been started.
94          */
95         public static Object getService(BundleContext context, String name) {
96                 if (context == null)
97                         return null;
98                 ServiceReference reference = context.getServiceReference(name);
99                 if (reference == null)
100                         return null;
101                 Object result = context.getService(reference);
102                 context.ungetService(reference);
103                 return result;
104         }
105         
106         
107         public static void launchProduct(InstallDescription description) throws Exception {
108                 IPath installLocation = description.getInstallLocation();
109                 IPath toRun = installLocation.append(description.getLauncherName());
110                 try {
111                         Runtime.getRuntime().exec(toRun.toString(), null, installLocation.toFile());
112                 } catch (IOException e) {
113                         throw new Exception("Lanuching an app failed", e);
114                 }
115
116         }
117 }