| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2017 Code 9 and others. |
| 3 |
* |
| 4 |
* This |
| 5 |
* program and the accompanying materials are made available under the terms of |
| 6 |
* the Eclipse Public License 2.0 which accompanies this distribution, and is |
| 7 |
* available at |
| 8 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: EPL-2.0 |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* Code 9 - initial API and implementation |
| 14 |
* IBM - ongoing development |
| 15 |
******************************************************************************/ |
| 16 |
package org.eclipse.equinox.p2.publisher; |
| 17 |
|
| 18 |
import java.net.URI; |
| 19 |
import java.util.Collection; |
| 20 |
import org.eclipse.core.runtime.*; |
| 21 |
import org.eclipse.equinox.internal.p2.core.helpers.Tracing; |
| 22 |
import org.eclipse.equinox.internal.p2.publisher.Activator; |
| 23 |
import org.eclipse.equinox.p2.core.IProvisioningAgent; |
| 24 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 25 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 26 |
import org.eclipse.equinox.p2.repository.*; |
| 27 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; |
| 28 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; |
| 29 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; |
| 30 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
| 31 |
|
| 32 |
public class Publisher { |
| 33 |
static final public String PUBLISH_PACK_FILES_AS_SIBLINGS = "publishPackFilesAsSiblings"; //$NON-NLS-1$ |
| 34 |
private static final long SERVICE_TIMEOUT = 5000; |
| 35 |
|
| 36 |
private IPublisherInfo info; |
| 37 |
private IPublisherResult results; |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns a metadata repository that corresponds to the given settings. If a |
| 41 |
* repository at the given location already exists, it is updated with the |
| 42 |
* settings and returned. If no repository is found then a new Simple repository |
| 43 |
* is created, configured and returned |
| 44 |
* |
| 45 |
* @param agent the provisioning agent to use when creating the repository |
| 46 |
* @param location the URL location of the repository |
| 47 |
* @param name the name of the repository |
| 48 |
* @param append whether or not the repository should appended or cleared |
| 49 |
* @param compress whether or not to compress the repository index |
| 50 |
* @return the discovered or created repository |
| 51 |
* @throws ProvisionException |
| 52 |
*/ |
| 53 |
public static IMetadataRepository createMetadataRepository(IProvisioningAgent agent, URI location, String name, |
| 54 |
boolean append, boolean compress) throws ProvisionException { |
| 55 |
try { |
| 56 |
IMetadataRepository result = loadMetadataRepository(agent, location, true, true); |
| 57 |
if (result != null && result.isModifiable()) { |
| 58 |
result.setProperty(IRepository.PROP_COMPRESSED, compress ? "true" : "false"); //$NON-NLS-1$//$NON-NLS-2$ |
| 59 |
if (!append) |
| 60 |
result.removeAll(); |
| 61 |
return result; |
| 62 |
} |
| 63 |
} catch (ProvisionException e) { |
| 64 |
// fall through and create a new repository |
| 65 |
} |
| 66 |
|
| 67 |
// the given repo location is not an existing repo so we have to create |
| 68 |
// something |
| 69 |
IMetadataRepositoryManager manager = getService(agent, IMetadataRepositoryManager.SERVICE_NAME); |
| 70 |
String repositoryName = name == null ? location + " - metadata" : name; //$NON-NLS-1$ |
| 71 |
IMetadataRepository result = manager.createRepository(location, repositoryName, |
| 72 |
IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); |
| 73 |
if (result != null) { |
| 74 |
manager.removeRepository(result.getLocation()); |
| 75 |
result.setProperty(IRepository.PROP_COMPRESSED, compress ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 76 |
return result; |
| 77 |
} |
| 78 |
// I don't think we can really get here, but just in case, we better throw a |
| 79 |
// provisioning exception |
| 80 |
String msg = org.eclipse.equinox.internal.p2.metadata.repository.Messages.repoMan_internalError; |
| 81 |
throw new ProvisionException( |
| 82 |
new Status(IStatus.ERROR, Activator.ID, ProvisionException.INTERNAL_ERROR, msg, null)); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Load a metadata repository from the given location. |
| 87 |
* |
| 88 |
* @param location the URI location of the repository |
| 89 |
* @param modifiable whether to ask the manager for a modifiable |
| 90 |
* repository |
| 91 |
* @param removeFromManager remove the loaded repository from the manager if it |
| 92 |
* wasn't already loaded |
| 93 |
* @return the loaded repository |
| 94 |
* @throws ProvisionException |
| 95 |
*/ |
| 96 |
public static IMetadataRepository loadMetadataRepository(IProvisioningAgent agent, URI location, boolean modifiable, |
| 97 |
boolean removeFromManager) throws ProvisionException { |
| 98 |
return loadRepository(agent, IMetadataRepositoryManager.SERVICE_NAME, location, modifiable, removeFromManager); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns an artifact repository that corresponds to the given settings. If a |
| 103 |
* repository at the given location already exists, it is updated with the |
| 104 |
* settings and returned. If no repository is found then a new Simple repository |
| 105 |
* is created, configured and returned |
| 106 |
* |
| 107 |
* @param agent the provisioning agent to use when creating the repository |
| 108 |
* @param location the URL location of the repository |
| 109 |
* @param name the name of the repository |
| 110 |
* @param compress whether or not to compress the repository index |
| 111 |
* @return the discovered or created repository |
| 112 |
* @throws ProvisionException |
| 113 |
*/ |
| 114 |
@Deprecated(forRemoval = true, since = "2.3.0") |
| 115 |
public static IArtifactRepository createArtifactRepository(IProvisioningAgent agent, URI location, String name, |
| 116 |
boolean compress) throws ProvisionException { |
| 117 |
try { |
| 118 |
IArtifactRepository result = loadArtifactRepository(agent, location, true, true); |
| 119 |
if (result != null && result.isModifiable()) { |
| 120 |
result.setProperty(IRepository.PROP_COMPRESSED, compress ? "true" : "false"); //$NON-NLS-1$//$NON-NLS-2$ |
| 121 |
return result; |
| 122 |
} |
| 123 |
} catch (ProvisionException e) { |
| 124 |
// fall through and create a new repository |
| 125 |
} |
| 126 |
|
| 127 |
IArtifactRepositoryManager manager = getService(agent, IArtifactRepositoryManager.SERVICE_NAME); |
| 128 |
String repositoryName = name != null ? name : location + " - artifacts"; //$NON-NLS-1$ |
| 129 |
IArtifactRepository result = manager.createRepository(location, repositoryName, |
| 130 |
IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); |
| 131 |
if (result != null) { |
| 132 |
manager.removeRepository(result.getLocation()); |
| 133 |
result.setProperty(IRepository.PROP_COMPRESSED, compress ? "true" : "false"); //$NON-NLS-1$//$NON-NLS-2$ |
| 134 |
return result; |
| 135 |
} |
| 136 |
// I don't think we can really get here, but just in case, we better throw a |
| 137 |
// provisioning exception |
| 138 |
String msg = org.eclipse.equinox.internal.p2.artifact.repository.Messages.repoMan_internalError; |
| 139 |
throw new ProvisionException( |
| 140 |
new Status(IStatus.ERROR, Activator.ID, ProvisionException.INTERNAL_ERROR, msg, null)); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Load an artifact repository from the given location. |
| 145 |
* |
| 146 |
* @param location the URI location of the repository |
| 147 |
* @param modifiable whether to ask the manager for a modifiable |
| 148 |
* repository |
| 149 |
* @param removeFromManager remove the loaded repository from the manager if it |
| 150 |
* wasn't already loaded |
| 151 |
* @return the loaded repository |
| 152 |
* @throws ProvisionException |
| 153 |
*/ |
| 154 |
public static IArtifactRepository loadArtifactRepository(IProvisioningAgent agent, URI location, boolean modifiable, |
| 155 |
boolean removeFromManager) throws ProvisionException { |
| 156 |
return loadRepository(agent, IArtifactRepositoryManager.SERVICE_NAME, location, modifiable, removeFromManager); |
| 157 |
} |
| 158 |
|
| 159 |
private static <T, R extends IRepository<T>> R loadRepository(IProvisioningAgent agent, String serviceName, |
| 160 |
URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { |
| 161 |
IRepositoryManager<T> manager = getService(agent, serviceName); |
| 162 |
boolean existing = manager.contains(location); |
| 163 |
int flags = modifiable ? IRepositoryManager.REPOSITORY_HINT_MODIFIABLE : 0; |
| 164 |
@SuppressWarnings("unchecked") |
| 165 |
R result = (R) manager.loadRepository(location, flags, null); |
| 166 |
if (!existing && removeFromManager) { |
| 167 |
manager.removeRepository(location); |
| 168 |
} |
| 169 |
return result; |
| 170 |
} |
| 171 |
|
| 172 |
public Publisher(IPublisherInfo info) { |
| 173 |
this.info = info; |
| 174 |
results = new PublisherResult(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Obtains a service from the agent, waiting for a reasonable timeout period if |
| 179 |
* the service is not yet available. This method never returns |
| 180 |
* <code>null</code>; an exception is thrown if the service could not be |
| 181 |
* obtained. |
| 182 |
* |
| 183 |
* @param <T> The type of the service to return |
| 184 |
* @param agent The agent to obtain the service from |
| 185 |
* @param serviceName The name of the service to obtain |
| 186 |
* @return The service instance |
| 187 |
*/ |
| 188 |
@SuppressWarnings("unchecked") |
| 189 |
protected static <T> T getService(IProvisioningAgent agent, String serviceName) { |
| 190 |
T service = (T) agent.getService(serviceName); |
| 191 |
if (service != null) |
| 192 |
return service; |
| 193 |
long start = System.currentTimeMillis(); |
| 194 |
do { |
| 195 |
try { |
| 196 |
Thread.sleep(100); |
| 197 |
} catch (InterruptedException e) { |
| 198 |
// ignore and keep waiting |
| 199 |
} |
| 200 |
service = (T) agent.getService(serviceName); |
| 201 |
if (service != null) |
| 202 |
return service; |
| 203 |
} while ((System.currentTimeMillis() - start) < SERVICE_TIMEOUT); |
| 204 |
// could not obtain the service |
| 205 |
throw new IllegalStateException("Unable to obtain required service: " + serviceName); //$NON-NLS-1$ |
| 206 |
} |
| 207 |
|
| 208 |
public Publisher(IPublisherInfo info, IPublisherResult results) { |
| 209 |
this.info = info; |
| 210 |
this.results = results; |
| 211 |
} |
| 212 |
|
| 213 |
class ArtifactProcess implements IRunnableWithProgress { |
| 214 |
|
| 215 |
private final IPublisherAction[] actions; |
| 216 |
private final IPublisherInfo publisherInfo; |
| 217 |
private IStatus result = null; |
| 218 |
|
| 219 |
public ArtifactProcess(IPublisherAction[] actions, IPublisherInfo info) { |
| 220 |
this.publisherInfo = info; |
| 221 |
this.actions = actions; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void run(IProgressMonitor monitor) { |
| 226 |
MultiStatus finalStatus = new MultiStatus("this", 0, "publishing result", null); //$NON-NLS-1$//$NON-NLS-2$ |
| 227 |
for (IPublisherAction action : actions) { |
| 228 |
if (monitor.isCanceled()) { |
| 229 |
result = Status.CANCEL_STATUS; |
| 230 |
return; |
| 231 |
} |
| 232 |
IStatus status = action.perform(publisherInfo, results, monitor); |
| 233 |
finalStatus.merge(status); |
| 234 |
monitor.worked(1); |
| 235 |
} |
| 236 |
result = finalStatus; |
| 237 |
} |
| 238 |
|
| 239 |
public IStatus getStatus() { |
| 240 |
return result; |
| 241 |
} |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
public IStatus publish(IPublisherAction[] actions, IProgressMonitor monitor) { |
| 246 |
if (monitor == null) |
| 247 |
monitor = new NullProgressMonitor(); |
| 248 |
SubMonitor sub = SubMonitor.convert(monitor, actions.length); |
| 249 |
if (Tracing.DEBUG_PUBLISHING) |
| 250 |
Tracing.debug("Invoking publisher"); //$NON-NLS-1$ |
| 251 |
try { |
| 252 |
ArtifactProcess artifactProcess = new ArtifactProcess(actions, info); |
| 253 |
|
| 254 |
IStatus finalStatus = null; |
| 255 |
if (info.getArtifactRepository() != null) { |
| 256 |
finalStatus = info.getArtifactRepository().executeBatch(artifactProcess, sub); |
| 257 |
if (!finalStatus.matches(IStatus.ERROR | IStatus.CANCEL)) |
| 258 |
// If the batch process didn't report any errors, then |
| 259 |
// Use the status from our actions |
| 260 |
finalStatus = artifactProcess.getStatus(); |
| 261 |
} else { |
| 262 |
artifactProcess.run(sub); |
| 263 |
finalStatus = artifactProcess.getStatus(); |
| 264 |
} |
| 265 |
|
| 266 |
if (Tracing.DEBUG_PUBLISHING) |
| 267 |
Tracing.debug("Publishing complete. Result=" + finalStatus); //$NON-NLS-1$ |
| 268 |
|
| 269 |
savePublishedIUs(); |
| 270 |
|
| 271 |
if (!finalStatus.isOK()) |
| 272 |
return finalStatus; |
| 273 |
return Status.OK_STATUS; |
| 274 |
} finally { |
| 275 |
sub.done(); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
protected void savePublishedIUs() { |
| 280 |
IMetadataRepository metadataRepository = info.getMetadataRepository(); |
| 281 |
if (metadataRepository != null) { |
| 282 |
Collection<IInstallableUnit> ius = results.getIUs(null, null); |
| 283 |
metadataRepository.addInstallableUnits(ius); |
| 284 |
} |
| 285 |
} |
| 286 |
} |