|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2011 Sonatype, Inc. and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Sonatype, Inc. - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.equinox.p2.operations; |
| 12 |
|
| 13 |
import java.net.URI; |
| 14 |
import java.util.*; |
| 15 |
import org.eclipse.core.runtime.Assert; |
| 16 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 17 |
import org.eclipse.equinox.internal.p2.operations.Activator; |
| 18 |
import org.eclipse.equinox.p2.core.IProvisioningAgent; |
| 19 |
import org.eclipse.equinox.p2.engine.*; |
| 20 |
import org.eclipse.equinox.p2.engine.query.UserVisibleRootQuery; |
| 21 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 22 |
import org.eclipse.equinox.p2.metadata.IVersionedId; |
| 23 |
import org.eclipse.equinox.p2.query.*; |
| 24 |
import org.osgi.framework.InvalidSyntaxException; |
| 25 |
import org.osgi.framework.ServiceReference; |
| 26 |
|
| 27 |
/** |
| 28 |
* OperationHelper provides a set of helpers to simplify dealing with the running installation. |
| 29 |
* Among other things, it simplifies the installation, un-installation and update. |
| 30 |
* If the system you are trying to modify is not the running one, you need to directly use the various subclass of {@link ProfileChangeOperation}. |
| 31 |
* @since 2.1 |
| 32 |
*/ |
| 33 |
public class OperationHelper { |
| 34 |
private IProvisioningAgent getAgent() { |
| 35 |
Collection<ServiceReference<IProvisioningAgent>> ref = null; |
| 36 |
try { |
| 37 |
ref = Activator.getContext().getServiceReferences(IProvisioningAgent.class, '(' + IProvisioningAgent.SERVICE_CURRENT + '=' + Boolean.TRUE.toString() + ')'); |
| 38 |
} catch (InvalidSyntaxException e) { |
| 39 |
//ignore can't happen since we write the filter ourselves |
| 40 |
} |
| 41 |
if (ref == null || ref.size() == 0) |
| 42 |
throw new IllegalStateException("p2 is not configured properly. No provisioning agent could be found."); |
| 43 |
IProvisioningAgent agent = Activator.getContext().getService(ref.iterator().next()); |
| 44 |
Activator.getContext().ungetService(ref.iterator().next()); |
| 45 |
return agent; |
| 46 |
} |
| 47 |
|
| 48 |
private Collection<IInstallableUnit> gatherIUs(ProvisioningContext context, Collection<IVersionedId> ius, IProgressMonitor monitor) { |
| 49 |
Collection<IInstallableUnit> unitsToInstall = new ArrayList<IInstallableUnit>(ius.size()); |
| 50 |
|
| 51 |
for (IVersionedId versionedId : ius) { |
| 52 |
if (ius instanceof IInstallableUnit) { |
| 53 |
unitsToInstall.add((IInstallableUnit) versionedId); |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
IQuery<IInstallableUnit> installableUnits = QueryUtil.createIUQuery(versionedId.getId(), versionedId.getVersion()); |
| 58 |
IQueryResult<IInstallableUnit> matches = context.getMetadata(monitor).query(installableUnits, monitor); |
| 59 |
// if (ius.isEmpty()) |
| 60 |
// Do something smart. like throw an exception |
| 61 |
//Add the first IU |
| 62 |
Iterator<IInstallableUnit> iuIt = matches.iterator(); |
| 63 |
unitsToInstall.add(iuIt.next()); |
| 64 |
|
| 65 |
if (iuIt.hasNext()) |
| 66 |
System.out.println("Log the fact that we have a problem"); |
| 67 |
} |
| 68 |
return unitsToInstall; |
| 69 |
} |
| 70 |
|
| 71 |
private ProvisioningContext createProvisioningContext(Collection<URI> repos, IProvisioningAgent agent) { |
| 72 |
ProvisioningContext ctx = new ProvisioningContext(agent); |
| 73 |
if (repos != null) { |
| 74 |
ctx.setMetadataRepositories(repos.toArray(new URI[repos.size()])); |
| 75 |
ctx.setArtifactRepositories(repos.toArray(new URI[repos.size()])); |
| 76 |
} |
| 77 |
return ctx; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* This factory method creates an {@link InstallOperation} to install all the elements listed from the specified repositories. |
| 82 |
* @param toInstall the elements to install. This can not be null. |
| 83 |
* @param repos the repositories to install the elements from. |
| 84 |
* @param monitor the progress monitor |
| 85 |
* @return an operation to install |
| 86 |
*/ |
| 87 |
public InstallOperation createInstallOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) { |
| 88 |
Assert.isNotNull(toInstall); |
| 89 |
IProvisioningAgent agent = getAgent(); |
| 90 |
//add the repos |
| 91 |
ProvisioningContext ctx = createProvisioningContext(repos, agent); |
| 92 |
|
| 93 |
//find the ius to install and create the operation |
| 94 |
InstallOperation resultingOperation = new InstallOperation(new ProvisioningSession(agent), gatherIUs(ctx, toInstall, monitor)); |
| 95 |
resultingOperation.setProvisioningContext(ctx); |
| 96 |
resultingOperation.setProfileId(IProfileRegistry.SELF); |
| 97 |
|
| 98 |
return resultingOperation; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Create an {@link UninstallOperation} that will uninstall the listed elements from the running instance. |
| 103 |
* @param toUninstall the elements to uninstall. This can not be null. |
| 104 |
* @param repos the repositories to install the elements from. Passing null will |
| 105 |
* @param monitor the progress monitor |
| 106 |
* @return an operation to uninstall |
| 107 |
*/ |
| 108 |
public UninstallOperation createUninstallOperation(Collection<IVersionedId> toUninstall, Collection<URI> repos, IProgressMonitor monitor) { |
| 109 |
Assert.isNotNull(toUninstall); |
| 110 |
IProvisioningAgent agent = getAgent(); |
| 111 |
ProvisioningContext ctx = createProvisioningContext(repos, agent); |
| 112 |
|
| 113 |
//find the ius to uninstall and create the operation |
| 114 |
UninstallOperation resultingOperation = new UninstallOperation(new ProvisioningSession(agent), gatherIUs(ctx, toUninstall, monitor)); |
| 115 |
resultingOperation.setProvisioningContext(ctx); |
| 116 |
resultingOperation.setProfileId(IProfileRegistry.SELF); |
| 117 |
|
| 118 |
return resultingOperation; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Return the {@link IInstallableUnit} that are installed in the running instance of eclipse. |
| 123 |
* @param rootsOnly set to true to return only the elements that have been explicitly installed (aka roots). |
| 124 |
* @param monitor the progress monitor |
| 125 |
* @return the installable units installed |
| 126 |
*/ |
| 127 |
public IQueryResult<IInstallableUnit> listInstalledElements(boolean rootsOnly, IProgressMonitor monitor) { |
| 128 |
IProfileRegistry registry = (IProfileRegistry) getAgent().getService(IProfileRegistry.SERVICE_NAME); |
| 129 |
IProfile profile = registry.getProfile(IProfileRegistry.SELF); |
| 130 |
if (rootsOnly) |
| 131 |
return profile.query(new UserVisibleRootQuery(), monitor); |
| 132 |
return profile.query(QueryUtil.ALL_UNITS, monitor); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Create an {@link UpdateOperation} that will update the elements specified. |
| 137 |
* @param toUpdate |
| 138 |
* @param repos |
| 139 |
* @param monitor |
| 140 |
* @return an instance of {@link UpdateOperation} |
| 141 |
*/ |
| 142 |
public UpdateOperation createUpdateOperation(Collection<IVersionedId> toUpdate, Collection<URI> repos, IProgressMonitor monitor) { |
| 143 |
IProvisioningAgent agent = getAgent(); |
| 144 |
ProvisioningContext ctx = createProvisioningContext(repos, agent); |
| 145 |
|
| 146 |
//find the ius to update and create the operation |
| 147 |
UpdateOperation resultingOperation = new UpdateOperation(new ProvisioningSession(agent), toUpdate == null ? null : gatherIUs(ctx, toUpdate, monitor)); |
| 148 |
resultingOperation.setProvisioningContext(ctx); |
| 149 |
resultingOperation.setProfileId(IProfileRegistry.SELF); |
| 150 |
|
| 151 |
return resultingOperation; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* This factory method creates an {@link AbsolutInstallOperation} that will cause the current installation to exclusively contain the elements listed once executed. |
| 156 |
* @param toInstall the elements to install. This can not be null. |
| 157 |
* @param repos the repositories to install the elements from. |
| 158 |
* @param monitor the progress monitor |
| 159 |
* @return an instance of {@link InstallOperation}. |
| 160 |
*/ |
| 161 |
public InstallOperation createAbsoluteInstallOperation(Collection<IVersionedId> toInstall, Collection<URI> repos, IProgressMonitor monitor) { |
| 162 |
IProvisioningAgent agent = getAgent(); |
| 163 |
ProvisioningContext ctx = createProvisioningContext(repos, agent); |
| 164 |
|
| 165 |
Collection<IInstallableUnit> iusToInstall; |
| 166 |
if (toInstall == null) |
| 167 |
iusToInstall = ctx.getMetadata(monitor).query(QueryUtil.createIUGroupQuery(), monitor).toUnmodifiableSet(); |
| 168 |
else |
| 169 |
iusToInstall = gatherIUs(ctx, toInstall, monitor); |
| 170 |
|
| 171 |
AbsoluteInstallOperation resultingOperation = new AbsoluteInstallOperation(new ProvisioningSession(agent), iusToInstall); |
| 172 |
resultingOperation.setProvisioningContext(ctx); |
| 173 |
resultingOperation.setProfileId(IProfileRegistry.SELF); |
| 174 |
|
| 175 |
return resultingOperation; |
| 176 |
} |
| 177 |
} |