]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/management/install/InstallerUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / install / InstallerUtil.java
diff --git a/bundles/org.simantics.project/src/org/simantics/project/management/install/InstallerUtil.java b/bundles/org.simantics.project/src/org/simantics/project/management/install/InstallerUtil.java
new file mode 100644 (file)
index 0000000..029a0dc
--- /dev/null
@@ -0,0 +1,117 @@
+package org.simantics.project.management.install;\r
+\r
+import java.io.IOException;\r
+\r
+import org.eclipse.core.runtime.IPath;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.eclipse.equinox.internal.provisional.p2.installer.InstallAdvisor;\r
+import org.eclipse.equinox.internal.provisional.p2.installer.InstallDescription;\r
+import org.eclipse.equinox.p2.core.IProvisioningAgent;\r
+import org.eclipse.equinox.p2.core.IProvisioningAgentProvider;\r
+import org.eclipse.equinox.p2.core.ProvisionException;\r
+import org.eclipse.equinox.p2.core.UIServices;\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.ServiceReference;\r
+import org.simantics.project.management.P2Util;\r
+import org.simantics.project.management.TrustAllCertificates;\r
+\r
+/**\r
+ * P2 installer util.\r
+ * \r
+ * Heavily inspired by org.eclipse.equinox.p2.installer project.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ */\r
+public class InstallerUtil {\r
+\r
+       private static final String SYS_PROP_INSTALL_DESCRIPTION = "org.eclipse.equinox.p2.installDescription";\r
+       \r
+       public static IStatus install(InstallAdvisor advisor, InstallDescription description, IProgressMonitor monitor) throws Exception {\r
+               if (advisor == null)\r
+                       advisor = getDefaultAdvisor();\r
+               if (description == null)\r
+                       description = getDefaultDescription(SubMonitor.convert(monitor));\r
+\r
+               advisor.prepareInstallDescription(description);\r
+               IProvisioningAgent agent = startAgent(description);             \r
+               \r
+               InstallUpdateProductOperation operation = new InstallUpdateProductOperation(agent, description);\r
+               \r
+               IStatus status = advisor.performInstall(operation);\r
+               if (!status.isOK()) {\r
+//                     ShowMessage.showStatus(status);\r
+               }\r
+               advisor.setResult(status);\r
+               advisor.stop();\r
+               agent.stop();\r
+               return status;\r
+       }\r
+       \r
+       public static InstallDescription getDefaultDescription(SubMonitor monitor) throws Exception {\r
+               String site = System.getProperty(SYS_PROP_INSTALL_DESCRIPTION);\r
+               return InstallDescriptionParser.createDescription(site, monitor);\r
+               \r
+       }\r
+       \r
+       public static InstallAdvisor getDefaultAdvisor() {\r
+               InstallAdvisor advisor = new DefaultInstallAdvisor();\r
+               advisor.start();\r
+               return advisor;\r
+       }\r
+       \r
+       public static IProvisioningAgent startAgent(InstallDescription description) throws Exception {\r
+               IPath installLocation = description.getInstallLocation();\r
+               if (installLocation == null)\r
+                       throw new Exception("No install location");\r
+               \r
+               try {\r
+                       // Create recruiter\r
+                       IProvisioningAgentProvider provider = P2Util.getProvisioningAgentProvider();\r
+                       \r
+                       // Set agent location if specified. Contains P2 install data\r
+                       IPath agentLocation = description.getAgentLocation();\r
+                       \r
+                       // Recruit Agent - Permission to provision\r
+                       IProvisioningAgent agent = provider.createAgent(agentLocation == null ? null : agentLocation.toFile().toURI());\r
+                       \r
+                       // Omit "This bundle is not certified" dialog.                  \r
+                       UIServices serviceUI = new TrustAllCertificates();\r
+                       agent.registerService("org.eclipse.equinox.p2.core.UIServices", serviceUI);\r
+                       \r
+                       \r
+                       return agent;\r
+               } catch (ProvisionException e) {\r
+                       throw new Exception("Cannot create agent", e);\r
+               }\r
+       }\r
+\r
+       \r
+       /**\r
+        * Copied from ServiceHelper because we need to obtain services\r
+        * before p2 has been started.\r
+        */\r
+       public static Object getService(BundleContext context, String name) {\r
+               if (context == null)\r
+                       return null;\r
+               ServiceReference reference = context.getServiceReference(name);\r
+               if (reference == null)\r
+                       return null;\r
+               Object result = context.getService(reference);\r
+               context.ungetService(reference);\r
+               return result;\r
+       }\r
+       \r
+       \r
+       public static void launchProduct(InstallDescription description) throws Exception {\r
+               IPath installLocation = description.getInstallLocation();\r
+               IPath toRun = installLocation.append(description.getLauncherName());\r
+               try {\r
+                       Runtime.getRuntime().exec(toRun.toString(), null, installLocation.toFile());\r
+               } catch (IOException e) {\r
+                       throw new Exception("Lanuching an app failed", e);\r
+               }\r
+\r
+       }\r
+}\r