| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2017 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
* EclipseSource - ongoing development |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.p2.publisher; |
| 16 |
|
| 17 |
import java.io.File; |
| 18 |
import java.net.URI; |
| 19 |
import java.net.URISyntaxException; |
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
import org.eclipse.core.runtime.IStatus; |
| 24 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 25 |
import org.eclipse.core.runtime.Status; |
| 26 |
import org.eclipse.core.runtime.URIUtil; |
| 27 |
import org.eclipse.equinox.app.IApplication; |
| 28 |
import org.eclipse.equinox.app.IApplicationContext; |
| 29 |
import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository; |
| 30 |
import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository; |
| 31 |
import org.eclipse.equinox.internal.p2.publisher.Activator; |
| 32 |
import org.eclipse.equinox.internal.p2.publisher.Messages; |
| 33 |
import org.eclipse.equinox.p2.core.IProvisioningAgent; |
| 34 |
import org.eclipse.equinox.p2.core.IProvisioningAgentProvider; |
| 35 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 36 |
import org.eclipse.equinox.p2.metadata.IArtifactKey; |
| 37 |
import org.eclipse.equinox.p2.query.IQueryResult; |
| 38 |
import org.eclipse.equinox.p2.query.QueryUtil; |
| 39 |
import org.eclipse.equinox.p2.repository.artifact.ArtifactKeyQuery; |
| 40 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; |
| 41 |
import org.eclipse.osgi.util.NLS; |
| 42 |
import org.osgi.framework.ServiceReference; |
| 43 |
|
| 44 |
public abstract class AbstractPublisherApplication implements IApplication { |
| 45 |
|
| 46 |
// The mapping rules for in-place generation need to construct paths into the |
| 47 |
// structure |
| 48 |
// of an eclipse installation; in the future the default artifact mapping |
| 49 |
// declared in |
| 50 |
// SimpleArtifactRepository may change, for example, to not have a 'bundles' |
| 51 |
// directory |
| 52 |
// instead of a 'plugins' directory, so a separate constant is defined and used |
| 53 |
// here. |
| 54 |
protected static final String[][] INPLACE_MAPPING_RULES = { |
| 55 |
{ "(& (classifier=osgi.bundle))", "${repoUrl}/plugins/${id}_${version}.jar" }, //$NON-NLS-1$//$NON-NLS-2$ |
| 56 |
{ "(& (classifier=binary))", "${repoUrl}/binary/${id}_${version}" }, //$NON-NLS-1$//$NON-NLS-2$ |
| 57 |
{ "(& (classifier=org.eclipse.update.feature))", "${repoUrl}/features/${id}_${version}.jar" } }; //$NON-NLS-1$//$NON-NLS-2$ |
| 58 |
|
| 59 |
protected PublisherInfo info; |
| 60 |
protected String source; |
| 61 |
protected URI metadataLocation; |
| 62 |
protected String metadataRepoName; |
| 63 |
protected URI artifactLocation; |
| 64 |
protected String artifactRepoName; |
| 65 |
protected URI[] contextMetadataRepositories; |
| 66 |
protected URI[] contextArtifactRepositories; |
| 67 |
// whether repository xml files should be compressed |
| 68 |
protected boolean compress = false; |
| 69 |
protected boolean inplace = false; |
| 70 |
protected boolean append = false; |
| 71 |
protected String[] configurations; |
| 72 |
private IStatus status; |
| 73 |
|
| 74 |
private ServiceReference<IProvisioningAgent> agentRef; |
| 75 |
|
| 76 |
protected IProvisioningAgent agent; |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns the error message for this application, or the empty string if the |
| 80 |
* application terminated successfully. |
| 81 |
*/ |
| 82 |
public IStatus getStatus() { |
| 83 |
return status; |
| 84 |
} |
| 85 |
|
| 86 |
protected void initialize(PublisherInfo publisherInfo) throws ProvisionException { |
| 87 |
if (inplace) { |
| 88 |
File location = new File(source); |
| 89 |
if (metadataLocation == null) |
| 90 |
metadataLocation = location.toURI(); |
| 91 |
if (artifactLocation == null) |
| 92 |
artifactLocation = location.toURI(); |
| 93 |
publisherInfo.setArtifactOptions( |
| 94 |
publisherInfo.getArtifactOptions() | IPublisherInfo.A_INDEX | IPublisherInfo.A_PUBLISH); |
| 95 |
} |
| 96 |
initializeRepositories(publisherInfo); |
| 97 |
} |
| 98 |
|
| 99 |
protected IStatus createConfigurationEror(String message) { |
| 100 |
return new Status(IStatus.ERROR, "org.eclipse.equinox.p2.publisher", message); //$NON-NLS-1$ |
| 101 |
} |
| 102 |
|
| 103 |
private boolean isEmpty(IArtifactRepository repo) { |
| 104 |
IQueryResult<IArtifactKey> result = repo.query(QueryUtil.createLimitQuery(ArtifactKeyQuery.ALL_KEYS, 1), null); |
| 105 |
return result.isEmpty(); |
| 106 |
} |
| 107 |
|
| 108 |
protected void initializeRepositories(PublisherInfo publisherInfo) throws ProvisionException { |
| 109 |
if (artifactLocation != null) { |
| 110 |
@SuppressWarnings("removal") |
| 111 |
IArtifactRepository repo = Publisher.createArtifactRepository(agent, artifactLocation, artifactRepoName, |
| 112 |
compress); |
| 113 |
if (!append && !isEmpty(repo)) { |
| 114 |
File repoLocation = URIUtil.toFile(artifactLocation); |
| 115 |
if (repoLocation != null && source != null) { |
| 116 |
if (repoLocation.isFile()) |
| 117 |
repoLocation = repoLocation.getParentFile(); |
| 118 |
if (repoLocation.equals(new File(source))) |
| 119 |
throw new IllegalArgumentException( |
| 120 |
NLS.bind(Messages.exception_artifactRepoNoAppendDestroysInput, |
| 121 |
URIUtil.toUnencodedString(artifactLocation))); |
| 122 |
} |
| 123 |
repo.removeAll(new NullProgressMonitor()); |
| 124 |
} |
| 125 |
publisherInfo.setArtifactRepository(repo); |
| 126 |
} else if ((publisherInfo.getArtifactOptions() & IPublisherInfo.A_PUBLISH) > 0) |
| 127 |
throw new ProvisionException(createConfigurationEror(Messages.exception_noArtifactRepo)); |
| 128 |
if (metadataLocation == null) |
| 129 |
throw new ProvisionException(createConfigurationEror(Messages.exception_noMetadataRepo)); |
| 130 |
publisherInfo.setMetadataRepository( |
| 131 |
Publisher.createMetadataRepository(agent, metadataLocation, metadataRepoName, append, compress)); |
| 132 |
|
| 133 |
if (contextMetadataRepositories != null && contextMetadataRepositories.length > 0) { |
| 134 |
CompositeMetadataRepository contextMetadata = CompositeMetadataRepository.createMemoryComposite(agent); |
| 135 |
if (contextMetadata != null) { |
| 136 |
for (URI repositoryuri : contextMetadataRepositories) { |
| 137 |
contextMetadata.addChild(repositoryuri); |
| 138 |
} |
| 139 |
if (contextMetadata.getChildren().size() > 0) |
| 140 |
publisherInfo.setContextMetadataRepository(contextMetadata); |
| 141 |
} |
| 142 |
} |
| 143 |
if (contextArtifactRepositories != null && contextArtifactRepositories.length > 0) { |
| 144 |
CompositeArtifactRepository contextArtifact = CompositeArtifactRepository.createMemoryComposite(agent); |
| 145 |
if (contextArtifact != null) { |
| 146 |
for (URI repositoryuri : contextArtifactRepositories) { |
| 147 |
contextArtifact.addChild(repositoryuri); |
| 148 |
} |
| 149 |
|
| 150 |
if (contextArtifact.getChildren().size() > 0) |
| 151 |
publisherInfo.setContextArtifactRepository(contextArtifact); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
protected void processCommandLineArguments(String[] args, PublisherInfo publisherInfo) throws Exception { |
| 157 |
if (args == null) |
| 158 |
return; |
| 159 |
for (int i = 0; i < args.length; i++) { |
| 160 |
// check for args without parameters (i.e., a flag arg) |
| 161 |
processFlag(args[i], publisherInfo); |
| 162 |
|
| 163 |
// check for args with parameters. If we are at the last argument or if the next |
| 164 |
// one |
| 165 |
// has a '-' as the first character, then we can't have an arg with a parm so |
| 166 |
// continue. |
| 167 |
if (i == args.length - 1 || args[i + 1].startsWith("-")) //$NON-NLS-1$ |
| 168 |
continue; |
| 169 |
processParameter(args[i], args[++i], publisherInfo); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Process application arguments. |
| 175 |
* |
| 176 |
* @throws URISyntaxException thrown by subclasses |
| 177 |
*/ |
| 178 |
protected void processParameter(String arg, String parameter, PublisherInfo publisherInfo) |
| 179 |
throws URISyntaxException { |
| 180 |
try { |
| 181 |
if (arg.equalsIgnoreCase("-metadataRepository") || arg.equalsIgnoreCase("-mr")) //$NON-NLS-1$ //$NON-NLS-2$ |
| 182 |
metadataLocation = URIUtil.fromString(parameter); |
| 183 |
|
| 184 |
if (arg.equalsIgnoreCase("-artifactRepository") || arg.equalsIgnoreCase("-ar")) //$NON-NLS-1$ //$NON-NLS-2$ |
| 185 |
artifactLocation = URIUtil.fromString(parameter); |
| 186 |
} catch (URISyntaxException e) { |
| 187 |
throw new IllegalArgumentException(NLS.bind(Messages.exception_repoMustBeURL, parameter)); |
| 188 |
} |
| 189 |
|
| 190 |
if (arg.equalsIgnoreCase("-metadataRepositoryName")) //$NON-NLS-1$ |
| 191 |
metadataRepoName = parameter; |
| 192 |
|
| 193 |
if (arg.equalsIgnoreCase("-source")) { //$NON-NLS-1$ |
| 194 |
// check here to see if the location actually exists so we can fail gracefully |
| 195 |
// now rather than unpredictably later |
| 196 |
// see bug 272956 where we would fail with an NPE if someone gave us a URL |
| 197 |
// instead of a file-system path |
| 198 |
if (!new File(parameter).exists()) |
| 199 |
throw new IllegalArgumentException(NLS.bind(Messages.exception_sourcePath, parameter)); |
| 200 |
source = parameter; |
| 201 |
} |
| 202 |
|
| 203 |
if (arg.equalsIgnoreCase("-artifactRepositoryName")) //$NON-NLS-1$ |
| 204 |
artifactRepoName = parameter; |
| 205 |
|
| 206 |
if (arg.equalsIgnoreCase("-configs")) //$NON-NLS-1$ |
| 207 |
publisherInfo.setConfigurations(AbstractPublisherAction.getArrayFromString(parameter, ",")); //$NON-NLS-1$ |
| 208 |
|
| 209 |
if (arg.equalsIgnoreCase("-contextMetadata")) //$NON-NLS-1$ |
| 210 |
setContextRepositories(processRepositoryList(parameter), contextArtifactRepositories); |
| 211 |
|
| 212 |
if (arg.equalsIgnoreCase("-contextArtifacts")) //$NON-NLS-1$ |
| 213 |
setContextRepositories(contextMetadataRepositories, processRepositoryList(parameter)); |
| 214 |
} |
| 215 |
|
| 216 |
private URI[] processRepositoryList(String parameter) { |
| 217 |
String[] list = AbstractPublisherAction.getArrayFromString(parameter, ","); //$NON-NLS-1$ |
| 218 |
if (list == null || list.length == 0) |
| 219 |
return null; |
| 220 |
|
| 221 |
List<URI> result = new ArrayList<>(list.length); |
| 222 |
if (result != null) { |
| 223 |
for (String uri : list) { |
| 224 |
try { |
| 225 |
result.add(URIUtil.fromString(uri)); |
| 226 |
} catch (URISyntaxException e) { |
| 227 |
// TODO Auto-generated catch block |
| 228 |
e.printStackTrace(); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
return result.toArray(new URI[result.size()]); |
| 233 |
} |
| 234 |
|
| 235 |
protected void processFlag(String arg, PublisherInfo publisherInfo) { |
| 236 |
if (arg.equalsIgnoreCase("-publishArtifacts") || arg.equalsIgnoreCase("-pa")) //$NON-NLS-1$ //$NON-NLS-2$ |
| 237 |
publisherInfo.setArtifactOptions(publisherInfo.getArtifactOptions() | IPublisherInfo.A_PUBLISH); |
| 238 |
|
| 239 |
if (arg.equalsIgnoreCase("-publishArtifactRepository") || arg.equalsIgnoreCase("-par")) //$NON-NLS-1$ //$NON-NLS-2$ |
| 240 |
publisherInfo.setArtifactOptions(publisherInfo.getArtifactOptions() | IPublisherInfo.A_INDEX); |
| 241 |
|
| 242 |
if (arg.equalsIgnoreCase("-overwriteArtifacts")) //$NON-NLS-1$ |
| 243 |
publisherInfo.setArtifactOptions(publisherInfo.getArtifactOptions() | IPublisherInfo.A_OVERWRITE); |
| 244 |
|
| 245 |
if (arg.equalsIgnoreCase("-append")) //$NON-NLS-1$ |
| 246 |
append = true; |
| 247 |
|
| 248 |
if (arg.equalsIgnoreCase("-compress")) //$NON-NLS-1$ |
| 249 |
compress = true; |
| 250 |
|
| 251 |
if (arg.equalsIgnoreCase("-inplace")) //$NON-NLS-1$ |
| 252 |
inplace = true; |
| 253 |
} |
| 254 |
|
| 255 |
protected void setupAgent() throws ProvisionException { |
| 256 |
agentRef = Activator.getContext().getServiceReference(IProvisioningAgent.class); |
| 257 |
if (agentRef != null) { |
| 258 |
agent = Activator.getContext().getService(agentRef); |
| 259 |
if (agent != null) |
| 260 |
return; |
| 261 |
} |
| 262 |
ServiceReference<IProvisioningAgentProvider> providerRef = Activator.getContext() |
| 263 |
.getServiceReference(IProvisioningAgentProvider.class); |
| 264 |
if (providerRef == null) |
| 265 |
throw new RuntimeException("No provisioning agent provider is available"); //$NON-NLS-1$ |
| 266 |
IProvisioningAgentProvider provider = Activator.getContext().getService(providerRef); |
| 267 |
if (provider == null) |
| 268 |
throw new RuntimeException("No provisioning agent provider is available"); //$NON-NLS-1$ |
| 269 |
// obtain agent for currently running system |
| 270 |
agent = provider.createAgent(null); |
| 271 |
Activator.getContext().ungetService(providerRef); |
| 272 |
} |
| 273 |
|
| 274 |
public Object run(String args[]) throws Exception { |
| 275 |
try { |
| 276 |
info = createPublisherInfo(); |
| 277 |
processCommandLineArguments(args, info); |
| 278 |
Object result = run(info); |
| 279 |
if (result != IApplication.EXIT_OK) { |
| 280 |
System.out.println(NLS.bind(Messages.message_publisherArguments, null)); |
| 281 |
for (String arg : args) { |
| 282 |
System.out.println(arg); |
| 283 |
} |
| 284 |
} |
| 285 |
return result; |
| 286 |
} catch (Exception e) { |
| 287 |
if (e.getMessage() != null) |
| 288 |
System.err.println(e.getMessage()); |
| 289 |
else |
| 290 |
e.printStackTrace(System.err); |
| 291 |
throw e; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
protected PublisherInfo createPublisherInfo() { |
| 296 |
return new PublisherInfo(); |
| 297 |
} |
| 298 |
|
| 299 |
public Object run(PublisherInfo publisherInfo) throws Exception { |
| 300 |
try { |
| 301 |
this.info = publisherInfo; |
| 302 |
setupAgent(); |
| 303 |
initialize(publisherInfo); |
| 304 |
System.out.println(NLS.bind(Messages.message_generatingMetadata, publisherInfo.getSummary())); |
| 305 |
|
| 306 |
long before = System.currentTimeMillis(); |
| 307 |
IPublisherAction[] actions = createActions(); |
| 308 |
Publisher publisher = createPublisher(publisherInfo); |
| 309 |
IStatus result = publisher.publish(actions, new NullProgressMonitor()); |
| 310 |
long after = System.currentTimeMillis(); |
| 311 |
|
| 312 |
if (!result.isOK()) { |
| 313 |
// TODO: improve the string representation of the result |
| 314 |
System.out.println(result); |
| 315 |
Throwable th = result.getException(); |
| 316 |
if (th != null) { |
| 317 |
System.out.println(); |
| 318 |
System.out.println(NLS.bind(Messages.message_resultException, null)); |
| 319 |
th.printStackTrace(System.out); |
| 320 |
System.out.println(); |
| 321 |
} |
| 322 |
} |
| 323 |
if (!result.matches(IStatus.ERROR | IStatus.CANCEL)) { |
| 324 |
System.out.println( |
| 325 |
NLS.bind(Messages.message_generationCompleted, String.valueOf((after - before) / 1000))); |
| 326 |
return IApplication.EXIT_OK; |
| 327 |
} |
| 328 |
} catch (ProvisionException e) { |
| 329 |
status = e.getStatus(); |
| 330 |
if (status.getSeverity() == IStatus.ERROR && status.getMessage() != null) { |
| 331 |
System.out.println(status.getMessage()); |
| 332 |
} |
| 333 |
} |
| 334 |
return Integer.valueOf(1); |
| 335 |
} |
| 336 |
|
| 337 |
protected abstract IPublisherAction[] createActions(); |
| 338 |
|
| 339 |
protected Publisher createPublisher(PublisherInfo publisherInfo) { |
| 340 |
return new Publisher(publisherInfo); |
| 341 |
} |
| 342 |
|
| 343 |
@Override |
| 344 |
public Object start(IApplicationContext context) throws Exception { |
| 345 |
return run((String[]) context.getArguments().get("application.args")); //$NON-NLS-1$ |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
public void stop() { |
| 350 |
if (agentRef != null) { |
| 351 |
Activator.getContext().ungetService(agentRef); |
| 352 |
agentRef = null; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
public void setArtifactLocation(URI location) { |
| 357 |
this.artifactLocation = location; |
| 358 |
} |
| 359 |
|
| 360 |
public void setMetadataLocation(URI location) { |
| 361 |
this.metadataLocation = location; |
| 362 |
} |
| 363 |
|
| 364 |
public void setCompress(boolean value) { |
| 365 |
compress = value; |
| 366 |
} |
| 367 |
|
| 368 |
public void setContextRepositories(URI[] metadata, URI[] artifacts) { |
| 369 |
this.contextMetadataRepositories = metadata; |
| 370 |
this.contextArtifactRepositories = artifacts; |
| 371 |
} |
| 372 |
} |