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