]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/management/install/InstallUpdateProductOperation.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / install / InstallUpdateProductOperation.java
diff --git a/bundles/org.simantics.project/src/org/simantics/project/management/install/InstallUpdateProductOperation.java b/bundles/org.simantics.project/src/org/simantics/project/management/install/InstallUpdateProductOperation.java
new file mode 100644 (file)
index 0000000..6e8b45d
--- /dev/null
@@ -0,0 +1,280 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 IBM Corporation and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     IBM Corporation - initial API and implementation\r
+ *     Code 9 - ongoing development\r
+ *     Sonatype, Inc. - ongoing development\r
+ *******************************************************************************/\r
+package org.simantics.project.management.install;\r
+\r
+import java.net.URI;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.CoreException;\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.Status;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;\r
+import org.eclipse.equinox.internal.p2.director.ProfileChangeRequest;\r
+import org.eclipse.equinox.internal.provisional.p2.director.IDirector;\r
+import org.eclipse.equinox.internal.provisional.p2.installer.IInstallOperation;\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.ProvisionException;\r
+import org.eclipse.equinox.p2.engine.IProfile;\r
+import org.eclipse.equinox.p2.engine.IProfileRegistry;\r
+import org.eclipse.equinox.p2.metadata.IInstallableUnit;\r
+import org.eclipse.equinox.p2.metadata.IVersionedId;\r
+import org.eclipse.equinox.p2.metadata.Version;\r
+import org.eclipse.equinox.p2.metadata.VersionRange;\r
+import org.eclipse.equinox.p2.query.IQuery;\r
+import org.eclipse.equinox.p2.query.IQueryResult;\r
+import org.eclipse.equinox.p2.query.QueryUtil;\r
+import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;\r
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;\r
+import org.eclipse.osgi.service.environment.EnvironmentInfo;\r
+import org.eclipse.osgi.util.NLS;\r
+import org.simantics.project.internal.Activator;\r
+\r
+/**\r
+ * This operation performs installation or update of an Eclipse-based product.\r
+ */\r
+@SuppressWarnings("restriction")\r
+public class InstallUpdateProductOperation implements IInstallOperation {\r
+\r
+       private IArtifactRepositoryManager artifactRepoMan;\r
+       private IProvisioningAgent agent;\r
+       private IDirector director;\r
+       private final InstallDescription installDescription;\r
+       private boolean isInstall = true;\r
+       private IMetadataRepositoryManager metadataRepoMan;\r
+       private IProfileRegistry profileRegistry;\r
+       private IStatus result;\r
+\r
+       public InstallUpdateProductOperation(IProvisioningAgent agent, InstallDescription description) {\r
+               this.agent = agent;\r
+               this.installDescription = description;\r
+       }\r
+\r
+       /**\r
+        * Determine what top level installable units should be installed by the director\r
+        */\r
+       private Collection<IInstallableUnit> computeUnitsToInstall() throws CoreException {\r
+               ArrayList<IInstallableUnit> units = new ArrayList<IInstallableUnit>();\r
+               IVersionedId roots[] = installDescription.getRoots();\r
+               for (int i = 0; i < roots.length; i++) {\r
+                       IVersionedId root = roots[i];\r
+                       IInstallableUnit iu = findUnit(root);\r
+                       if (iu != null)\r
+                               units.add(iu);\r
+               }\r
+               return units;\r
+       }\r
+\r
+       /**\r
+        * This profile is being updated; return the units to uninstall from the profile.\r
+        */\r
+       private IQueryResult<IInstallableUnit> computeUnitsToUninstall(IProfile p) {\r
+               return p.query(QueryUtil.createIUAnyQuery(), null);\r
+       }\r
+\r
+       /**\r
+        * Create and return the profile into which units will be installed.\r
+        */\r
+       private IProfile createProfile() throws ProvisionException {\r
+               IProfile profile = getProfile();\r
+               if (profile == null) {\r
+                       Map<String, String> properties = new HashMap<String, String>();\r
+                       properties.put(IProfile.PROP_INSTALL_FOLDER, installDescription.getInstallLocation().toString());\r
+                       EnvironmentInfo info = (EnvironmentInfo) ServiceHelper.getService(Activator.getDefault().getBundle().getBundleContext(), EnvironmentInfo.class.getName());\r
+                       String env = "osgi.os=" + info.getOS() + ",osgi.ws=" + info.getWS() + ",osgi.arch=" + info.getOSArch(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$\r
+                       properties.put(IProfile.PROP_ENVIRONMENTS, env);\r
+                       properties.put(IProfile.PROP_NAME, installDescription.getProductName());\r
+                       properties.putAll(installDescription.getProfileProperties());\r
+                       IPath location = installDescription.getBundleLocation();\r
+                       if (location != null)\r
+                               properties.put(IProfile.PROP_CACHE, location.toOSString());\r
+                       profile = profileRegistry.addProfile(getProfileId(), properties);\r
+               }\r
+               return profile;\r
+       }\r
+\r
+       /**\r
+        * Performs the actual product install or update.\r
+        */\r
+       private void doInstall(SubMonitor monitor) throws CoreException {\r
+               prepareMetadataRepositories();\r
+               prepareArtifactRepositories();\r
+               IProfile p = createProfile();\r
+               Collection<IInstallableUnit> toInstall = computeUnitsToInstall();\r
+               monitor.worked(5);\r
+\r
+               IStatus s;\r
+               ProfileChangeRequest request = new ProfileChangeRequest(p);\r
+               if (isInstall) {\r
+                       monitor.setTaskName(NLS.bind("Installing", installDescription.getProductName()));\r
+                       request.addAll(toInstall);\r
+                       s = director.provision(request, null, monitor.newChild(90));\r
+               } else {\r
+                       monitor.setTaskName(NLS.bind("Updating", installDescription.getProductName()));\r
+                       IQueryResult<IInstallableUnit> toUninstall = computeUnitsToUninstall(p);\r
+                       request.removeAll(toUninstall.toUnmodifiableSet());\r
+                       request.addAll(toInstall);\r
+                       s = director.provision(request, null, monitor.newChild(90));\r
+               }\r
+               if (!s.isOK())\r
+                       throw new CoreException(s);\r
+       }\r
+\r
+       /**\r
+        * Returns an exception of severity error with the given error message.\r
+        */\r
+       private CoreException fail(String message) {\r
+               return fail(message, null);\r
+       }\r
+\r
+       /**\r
+        * Returns an exception of severity error with the given error message.\r
+        */\r
+       private CoreException fail(String message, Throwable throwable) {\r
+               return new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, message, throwable));\r
+       }\r
+\r
+       /**\r
+        * Finds and returns the installable unit with the given id, and optionally the\r
+        * given version.\r
+        */\r
+       private IInstallableUnit findUnit(IVersionedId spec) throws CoreException {\r
+               String id = spec.getId();\r
+               if (id == null)\r
+                       throw fail("No id");\r
+               Version version = spec.getVersion();\r
+               VersionRange range = VersionRange.emptyRange;\r
+               if (version != null && !version.equals(Version.emptyVersion))\r
+                       range = new VersionRange(version, true, version, true);\r
+               IQuery<IInstallableUnit> query = QueryUtil.createIUQuery(id, range);\r
+               Iterator<IInstallableUnit> matches = metadataRepoMan.query(query, null).iterator();\r
+               // pick the newest match\r
+               IInstallableUnit newest = null;\r
+               while (matches.hasNext()) {\r
+                       IInstallableUnit candidate = matches.next();\r
+                       if (newest == null || (newest.getVersion().compareTo(candidate.getVersion()) < 0))\r
+                               newest = candidate;\r
+               }\r
+               if (newest == null)\r
+                       throw fail("IU not found " + id);\r
+               return newest;\r
+       }\r
+\r
+       /**\r
+        * Returns the profile being installed into.\r
+        */\r
+       private IProfile getProfile() {\r
+               return profileRegistry.getProfile(getProfileId());\r
+       }\r
+\r
+       /**\r
+        * Returns the id of the profile to use for install/update based on this operation's install description.\r
+        */\r
+       private String getProfileId() {\r
+               IPath location = installDescription.getInstallLocation();\r
+               if (location != null)\r
+                       return location.toString();\r
+               return installDescription.getProductName();\r
+       }\r
+\r
+       /**\r
+        * Returns the result of the install operation, or <code>null</code> if\r
+        * no install operation has been run.\r
+        */\r
+       public IStatus getResult() {\r
+               return result;\r
+       }\r
+\r
+       private Object getService(String name) throws CoreException {\r
+               Object service = agent.getService(name);\r
+               if (service == null)\r
+                       throw fail("No service "  + name);\r
+               return service;\r
+       }\r
+\r
+       /* (non-Javadoc)\r
+        * @see org.eclipse.equinox.internal.provisional.p2.installer.IInstallOperation#install(org.eclipse.core.runtime.IProgressMonitor)\r
+        */\r
+       public IStatus install(IProgressMonitor pm) {\r
+               SubMonitor monitor = SubMonitor.convert(pm, "Preparing", 100);\r
+               try {\r
+                       try {\r
+                               preInstall();\r
+                               isInstall = getProfile() == null;\r
+                               doInstall(monitor);\r
+                               result = new Status(IStatus.OK, Activator.PLUGIN_ID, isInstall ? "Install Complete" : "Update Complete", null);\r
+                               monitor.setTaskName("Cleanup");\r
+                       } finally {\r
+                               postInstall();\r
+                       }\r
+               } catch (CoreException e) {\r
+                       result = e.getStatus();\r
+               } finally {\r
+                       monitor.done();\r
+               }\r
+               return result;\r
+       }\r
+\r
+       /**\r
+        * Returns whether this operation represents the product being installed\r
+        * for the first time, in a new profile.\r
+        */\r
+       public boolean isFirstInstall() {\r
+               return isInstall;\r
+       }\r
+\r
+       private void postInstall() {\r
+               //nothing to do\r
+       }\r
+\r
+       private void preInstall() throws CoreException {\r
+               //obtain required services\r
+               director = (IDirector) getService(IDirector.SERVICE_NAME);\r
+               metadataRepoMan = (IMetadataRepositoryManager) getService(IMetadataRepositoryManager.SERVICE_NAME);\r
+               artifactRepoMan = (IArtifactRepositoryManager) getService(IArtifactRepositoryManager.SERVICE_NAME);\r
+               profileRegistry = (IProfileRegistry) getService(IProfileRegistry.SERVICE_NAME);\r
+       }\r
+\r
+       private void prepareArtifactRepositories() throws ProvisionException {\r
+               URI[] repos = installDescription.getArtifactRepositories();\r
+               if (repos == null)\r
+                       return;\r
+\r
+               // Repositories must be registered before they are loaded\r
+               // This is to avoid them being possibly overridden with the configuration as a referenced repository\r
+               for (int i = 0; i < repos.length; i++) {\r
+                       artifactRepoMan.addRepository(repos[i]);\r
+                       artifactRepoMan.loadRepository(repos[i], null);\r
+               }\r
+       }\r
+\r
+       private void prepareMetadataRepositories() throws ProvisionException {\r
+               URI[] repos = installDescription.getMetadataRepositories();\r
+               if (repos == null)\r
+                       return;\r
+\r
+               // Repositories must be registered before they are loaded\r
+               // This is to avoid them being possibly overridden with the configuration as a referenced repository\r
+               for (int i = 0; i < repos.length; i++) {\r
+                       metadataRepoMan.addRepository(repos[i]);\r
+                       metadataRepoMan.loadRepository(repos[i], null);\r
+               }\r
+       }\r
+}\r