| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2018 IBM Corporation 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 |
* IBM Corporation - initial API and implementation |
| 14 |
* Band XI - add more commands |
| 15 |
* Composent, Inc. - command additions |
| 16 |
* SAP - bug fixes |
| 17 |
* Red Hat Inc. - Bug 460967 |
| 18 |
******************************************************************************/ |
| 19 |
package org.eclipse.equinox.internal.p2.console; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.net.URI; |
| 23 |
import java.net.URISyntaxException; |
| 24 |
import java.util.*; |
| 25 |
import java.util.Map.Entry; |
| 26 |
import org.eclipse.core.runtime.IStatus; |
| 27 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 28 |
import org.eclipse.equinox.p2.core.IProvisioningAgent; |
| 29 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 30 |
import org.eclipse.equinox.p2.engine.IProfile; |
| 31 |
import org.eclipse.equinox.p2.engine.IProfileRegistry; |
| 32 |
import org.eclipse.equinox.p2.metadata.*; |
| 33 |
import org.eclipse.equinox.p2.query.*; |
| 34 |
import org.eclipse.equinox.p2.repository.artifact.*; |
| 35 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
| 36 |
import org.eclipse.osgi.framework.console.CommandInterpreter; |
| 37 |
import org.eclipse.osgi.framework.console.CommandProvider; |
| 38 |
|
| 39 |
/** |
| 40 |
* An OSGi console command provider that adds various commands for interacting |
| 41 |
* with the provisioning system. |
| 42 |
*/ |
| 43 |
public class ProvCommandProvider implements CommandProvider { |
| 44 |
private static final String WILDCARD_ANY = "*"; //$NON-NLS-1$ |
| 45 |
public static final String NEW_LINE = "\r\n"; //$NON-NLS-1$ |
| 46 |
public static final String TAB = "\t"; //$NON-NLS-1$ |
| 47 |
|
| 48 |
// holds the mapping between command name and command description |
| 49 |
private Map<String, String> commandsHelp = null; |
| 50 |
// hold the mappings between command groups and the command names of the |
| 51 |
// commands in the group |
| 52 |
private Map<String, String[]> commandGroups = null; |
| 53 |
|
| 54 |
private final IProvisioningAgent agent; |
| 55 |
|
| 56 |
// private Profile profile; |
| 57 |
|
| 58 |
public ProvCommandProvider(String profileId, IProvisioningAgent agent) { |
| 59 |
this.agent = agent; |
| 60 |
// look up the profile we are currently running and use it as the |
| 61 |
// default. |
| 62 |
// TODO define a way to spec the default profile to manage |
| 63 |
// if (profileId != null) { |
| 64 |
// profile = registry.getProfile(profileId); |
| 65 |
// if (profile != null) |
| 66 |
// return; |
| 67 |
// } |
| 68 |
// // A default was not defined so manage the first profile we can find |
| 69 |
// Profile[] profiles = registry.getProfiles(); |
| 70 |
// if (profiles.length > 0) |
| 71 |
// profile = profiles[0]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Adds both a metadata repository and artifact repository |
| 76 |
*/ |
| 77 |
public void _provaddrepo(CommandInterpreter interpreter) { |
| 78 |
String urlString = interpreter.nextArgument(); |
| 79 |
if (urlString == null) { |
| 80 |
interpreter.println("Repository location must be provided"); |
| 81 |
return; |
| 82 |
} |
| 83 |
URI repoURI = toURI(interpreter, urlString); |
| 84 |
if (repoURI == null) |
| 85 |
return; |
| 86 |
// add metadata repo |
| 87 |
if (ProvisioningHelper.addMetadataRepository(agent, repoURI) == null) { |
| 88 |
interpreter.println("Unable to add metadata repository: " + repoURI); |
| 89 |
} else // add artifact repo at same URL |
| 90 |
if (ProvisioningHelper.addArtifactRepository(agent, repoURI) == null) { |
| 91 |
interpreter.println("Unable to add artifact repository: " + repoURI); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public void _provdelrepo(CommandInterpreter interpreter) { |
| 96 |
String urlString = interpreter.nextArgument(); |
| 97 |
if (urlString == null) { |
| 98 |
interpreter.println("Repository location must be provided"); |
| 99 |
return; |
| 100 |
} |
| 101 |
URI repoURI = toURI(interpreter, urlString); |
| 102 |
if (repoURI == null) |
| 103 |
return; |
| 104 |
ProvisioningHelper.removeMetadataRepository(agent, repoURI); |
| 105 |
ProvisioningHelper.removeArtifactRepository(agent, repoURI); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Adds a metadata repository. |
| 110 |
*/ |
| 111 |
public void _provaddmetadatarepo(CommandInterpreter interpreter) { |
| 112 |
String urlString = interpreter.nextArgument(); |
| 113 |
if (urlString == null) { |
| 114 |
interpreter.println("Repository location must be provided"); |
| 115 |
return; |
| 116 |
} |
| 117 |
URI repoURI = toURI(interpreter, urlString); |
| 118 |
if (repoURI == null) |
| 119 |
return; |
| 120 |
if (ProvisioningHelper.addMetadataRepository(agent, repoURI) == null) |
| 121 |
interpreter.println("Unable to add repository: " + repoURI); |
| 122 |
} |
| 123 |
|
| 124 |
public void _provdelmetadatarepo(CommandInterpreter interpreter) { |
| 125 |
String urlString = interpreter.nextArgument(); |
| 126 |
if (urlString == null) { |
| 127 |
interpreter.println("Repository location must be provided"); |
| 128 |
return; |
| 129 |
} |
| 130 |
URI repoURI = toURI(interpreter, urlString); |
| 131 |
if (repoURI == null) |
| 132 |
return; |
| 133 |
ProvisioningHelper.removeMetadataRepository(agent, repoURI); |
| 134 |
} |
| 135 |
|
| 136 |
public void _provaddartifactrepo(CommandInterpreter interpreter) { |
| 137 |
String urlString = interpreter.nextArgument(); |
| 138 |
if (urlString == null) { |
| 139 |
interpreter.println("Repository location must be provided"); |
| 140 |
return; |
| 141 |
} |
| 142 |
URI repoURI = toURI(interpreter, urlString); |
| 143 |
if (repoURI == null) |
| 144 |
return; |
| 145 |
if (ProvisioningHelper.addArtifactRepository(agent, repoURI) == null) |
| 146 |
interpreter.println("Unable to add repository " + repoURI); |
| 147 |
} |
| 148 |
|
| 149 |
public void _provdelartifactrepo(CommandInterpreter interpreter) { |
| 150 |
String urlString = interpreter.nextArgument(); |
| 151 |
if (urlString == null) { |
| 152 |
interpreter.println("Repository location must be provided"); |
| 153 |
return; |
| 154 |
} |
| 155 |
URI repoURI = toURI(interpreter, urlString); |
| 156 |
if (repoURI == null) |
| 157 |
return; |
| 158 |
ProvisioningHelper.removeArtifactRepository(agent, repoURI); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Install a given IU to a given profile location. |
| 163 |
*/ |
| 164 |
public void _provinstall(CommandInterpreter interpreter) { |
| 165 |
String iu = interpreter.nextArgument(); |
| 166 |
String version = interpreter.nextArgument(); |
| 167 |
String profileId = interpreter.nextArgument(); |
| 168 |
if (profileId == null || profileId.equals("this")) //$NON-NLS-1$ |
| 169 |
profileId = IProfileRegistry.SELF; |
| 170 |
if (iu == null || version == null || profileId == null) { |
| 171 |
interpreter.println("Installable unit id, version, and profileid must be provided"); |
| 172 |
return; |
| 173 |
} |
| 174 |
IProfile profile = ProvisioningHelper.getProfile(agent, profileId); |
| 175 |
if (profile == null) { |
| 176 |
if (profileId.equals(IProfileRegistry.SELF)) { |
| 177 |
interpreter.println("No profile found"); |
| 178 |
} else { |
| 179 |
interpreter.println("Profile " + profileId + " not found"); |
| 180 |
} |
| 181 |
return; |
| 182 |
} |
| 183 |
IStatus s = null; |
| 184 |
try { |
| 185 |
s = ProvisioningHelper.install(agent, iu, version, profile, new NullProgressMonitor()); |
| 186 |
} catch (ProvisionException e) { |
| 187 |
interpreter.println("Installation failed with ProvisionException for " + iu + " " + version); |
| 188 |
interpreter.printStackTrace(e); |
| 189 |
return; |
| 190 |
} |
| 191 |
if (s.isOK()) |
| 192 |
interpreter.println("Installation complete for " + iu + " " + version); |
| 193 |
else { |
| 194 |
interpreter.println("Installation failed for " + iu + " " + version); |
| 195 |
interpreter.println(flattenStatus(s.getChildren(), " ")); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private String flattenStatus(IStatus[] childs, String indent) { |
| 200 |
StringBuilder sb = new StringBuilder(); |
| 201 |
|
| 202 |
for (int i = 0; (childs != null) && (i < childs.length); i++) { |
| 203 |
sb.append(indent).append(childs[i].getMessage()).append(NEW_LINE); |
| 204 |
sb.append(flattenStatus(childs[i].getChildren(), indent + " ")); |
| 205 |
} |
| 206 |
return sb.toString(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Creates a profile given an id, location, and flavor |
| 211 |
*/ |
| 212 |
public void _provaddprofile(CommandInterpreter interpreter) { |
| 213 |
String profileId = interpreter.nextArgument(); |
| 214 |
String location = interpreter.nextArgument(); |
| 215 |
if (profileId == null || location == null) { |
| 216 |
interpreter.println("Id and location must be provided"); |
| 217 |
return; |
| 218 |
} |
| 219 |
String environments = interpreter.nextArgument(); |
| 220 |
Map<String, String> props = new HashMap<>(); |
| 221 |
props.put(IProfile.PROP_INSTALL_FOLDER, location); |
| 222 |
if (environments != null) |
| 223 |
props.put(IProfile.PROP_ENVIRONMENTS, environments); |
| 224 |
|
| 225 |
try { |
| 226 |
ProvisioningHelper.addProfile(agent, profileId, props); |
| 227 |
} catch (ProvisionException e) { |
| 228 |
interpreter.println("Add profile failed. " + e.getMessage()); |
| 229 |
interpreter.printStackTrace(e); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Deletes a profile given an id, location, and flavor |
| 235 |
*/ |
| 236 |
public void _provdelprofile(CommandInterpreter interpreter) { |
| 237 |
String profileId = interpreter.nextArgument(); |
| 238 |
if (profileId == null) { |
| 239 |
interpreter.println("profileid must be provided"); |
| 240 |
return; |
| 241 |
} |
| 242 |
ProvisioningHelper.removeProfile(agent, profileId); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Lists the installable units that match the given URL, id, and/or version. |
| 247 |
* |
| 248 |
* @param interpreter |
| 249 |
*/ |
| 250 |
public void _provliu(CommandInterpreter interpreter) { |
| 251 |
String urlString = processArgument(interpreter.nextArgument()); |
| 252 |
String id = processArgument(interpreter.nextArgument()); |
| 253 |
String version = processArgument(interpreter.nextArgument()); |
| 254 |
URI repoURL = null; |
| 255 |
if (urlString != null && !urlString.equals(WILDCARD_ANY)) |
| 256 |
repoURL = toURI(interpreter, urlString); |
| 257 |
IInstallableUnit[] units = sort(ProvisioningHelper.getInstallableUnits(agent, repoURL, |
| 258 |
QueryUtil.createIUQuery(id, VersionRange.create(version)), null)); |
| 259 |
for (IInstallableUnit unit : units) { |
| 260 |
println(interpreter, unit); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Lists the installable units that match the given URL and query. A third |
| 266 |
* boolean argument can be provided where <code>true</code> means "full |
| 267 |
* query" and <code>false</code> means "e;match query"e;. The |
| 268 |
* default is <code>false</code>. |
| 269 |
* |
| 270 |
* @param interpreter |
| 271 |
*/ |
| 272 |
public void _provlquery(CommandInterpreter interpreter) { |
| 273 |
String urlString = processArgument(interpreter.nextArgument()); |
| 274 |
String expression = processArgument(interpreter.nextArgument()); |
| 275 |
if (expression == null) { |
| 276 |
interpreter.println("Please enter a query"); |
| 277 |
return; |
| 278 |
} |
| 279 |
boolean useFull = Boolean.parseBoolean(processArgument(interpreter.nextArgument())); |
| 280 |
URI repoURL = null; |
| 281 |
if (urlString != null && !urlString.equals(WILDCARD_ANY)) |
| 282 |
repoURL = toURI(interpreter, urlString); |
| 283 |
|
| 284 |
IQuery<IInstallableUnit> query = useFull ? QueryUtil.createQuery(expression) |
| 285 |
: QueryUtil.createMatchQuery(expression); |
| 286 |
IInstallableUnit[] units = sort(ProvisioningHelper.getInstallableUnits(agent, repoURL, query, null)); |
| 287 |
// Now print out results |
| 288 |
if (units.length == 0) |
| 289 |
interpreter.println("No units found"); |
| 290 |
else { |
| 291 |
for (IInstallableUnit unit : units) { |
| 292 |
println(interpreter, unit); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Lists the known metadata repositories, or the contents of a given metadata |
| 299 |
* repository. |
| 300 |
* |
| 301 |
* @param interpreter |
| 302 |
*/ |
| 303 |
public void _provlr(CommandInterpreter interpreter) { |
| 304 |
String urlString = processArgument(interpreter.nextArgument()); |
| 305 |
String id = processArgument(interpreter.nextArgument()); |
| 306 |
String version = processArgument(interpreter.nextArgument()); |
| 307 |
if (urlString == null) { |
| 308 |
for (URI repository : ProvisioningHelper.getMetadataRepositories(agent)) { |
| 309 |
interpreter.println(repository); |
| 310 |
} |
| 311 |
return; |
| 312 |
} |
| 313 |
URI repoLocation = toURI(interpreter, urlString); |
| 314 |
if (repoLocation == null) |
| 315 |
return; |
| 316 |
IInstallableUnit[] units = sort(ProvisioningHelper.getInstallableUnits(agent, repoLocation, |
| 317 |
QueryUtil.createIUQuery(id, VersionRange.create(version)), null)); |
| 318 |
for (IInstallableUnit unit : units) { |
| 319 |
println(interpreter, unit); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Lists the group IUs in all known metadata repositories, or in the given |
| 325 |
* metadata repository. |
| 326 |
* |
| 327 |
* @param interpreter |
| 328 |
*/ |
| 329 |
public void _provlg(CommandInterpreter interpreter) { |
| 330 |
String urlString = processArgument(interpreter.nextArgument()); |
| 331 |
IQueryable<IInstallableUnit> queryable = null; |
| 332 |
if (urlString == null) { |
| 333 |
queryable = agent.getService(IMetadataRepositoryManager.class); |
| 334 |
if (queryable == null) |
| 335 |
return; |
| 336 |
} else { |
| 337 |
URI repoURL = toURI(interpreter, urlString); |
| 338 |
if (repoURL == null) |
| 339 |
return; |
| 340 |
queryable = ProvisioningHelper.getMetadataRepository(agent, repoURL); |
| 341 |
if (queryable == null) |
| 342 |
return; |
| 343 |
} |
| 344 |
IInstallableUnit[] units = sort(queryable.query(QueryUtil.createIUGroupQuery(), null)); |
| 345 |
for (IInstallableUnit unit : units) { |
| 346 |
println(interpreter, unit); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Lists the known artifact repositories, or the contents of a given artifact |
| 352 |
* repository. |
| 353 |
* |
| 354 |
* @param interpreter |
| 355 |
*/ |
| 356 |
public void _provlar(CommandInterpreter interpreter) { |
| 357 |
String urlString = processArgument(interpreter.nextArgument()); |
| 358 |
if (urlString == null) { |
| 359 |
for (URI repository : ProvisioningHelper.getArtifactRepositories(agent)) { |
| 360 |
interpreter.println(repository); |
| 361 |
} |
| 362 |
return; |
| 363 |
} |
| 364 |
URI repoURL = toURI(interpreter, urlString); |
| 365 |
if (repoURL == null) |
| 366 |
return; |
| 367 |
IArtifactRepository repo = ProvisioningHelper.getArtifactRepository(agent, repoURL); |
| 368 |
IQueryResult<IArtifactKey> keys = null; |
| 369 |
try { |
| 370 |
keys = (repo != null) ? repo.query(ArtifactKeyQuery.ALL_KEYS, null) : null; |
| 371 |
} catch (UnsupportedOperationException e) { |
| 372 |
interpreter.println("Repository does not support queries."); |
| 373 |
return; |
| 374 |
} |
| 375 |
if (keys == null || keys.isEmpty()) { |
| 376 |
interpreter.println("Repository has no artifacts"); |
| 377 |
return; |
| 378 |
} |
| 379 |
IFileArtifactRepository fileRepo = repo.getAdapter(IFileArtifactRepository.class); |
| 380 |
for (IArtifactKey key : keys) { |
| 381 |
IArtifactDescriptor[] descriptors = repo.getArtifactDescriptors(key); |
| 382 |
for (IArtifactDescriptor descriptor : descriptors) { |
| 383 |
File location = null; |
| 384 |
if (fileRepo != null) |
| 385 |
location = fileRepo.getArtifactFile(descriptor); |
| 386 |
println(interpreter, key, location); |
| 387 |
} |
| 388 |
|
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Returns the given string as an URL, or <code>null</code> if the string could |
| 394 |
* not be interpreted as an URL. |
| 395 |
*/ |
| 396 |
private URI toURI(CommandInterpreter interpreter, String urlString) { |
| 397 |
try { |
| 398 |
return new URI(urlString); |
| 399 |
} catch (URISyntaxException e) { |
| 400 |
interpreter.print(e.getMessage()); |
| 401 |
interpreter.println(); |
| 402 |
return null; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
private String processArgument(String arg) { |
| 407 |
if (arg == null || arg.equals(WILDCARD_ANY)) |
| 408 |
return null; |
| 409 |
return arg; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Lists the known profiles, or the contents of a given profile. |
| 414 |
* |
| 415 |
* @param interpreter |
| 416 |
*/ |
| 417 |
public void _provlp(CommandInterpreter interpreter) { |
| 418 |
String profileId = processArgument(interpreter.nextArgument()); |
| 419 |
String id = processArgument(interpreter.nextArgument()); |
| 420 |
String range = processArgument(interpreter.nextArgument()); |
| 421 |
if (profileId == null) { |
| 422 |
IProfile[] profiles = ProvisioningHelper.getProfiles(agent); |
| 423 |
if (profiles == null) { |
| 424 |
interpreter.println("No profile found"); |
| 425 |
return; |
| 426 |
} |
| 427 |
for (IProfile profile : profiles) { |
| 428 |
interpreter.println(profile.getProfileId()); |
| 429 |
} |
| 430 |
return; |
| 431 |
} |
| 432 |
// determine which profile is to be listed |
| 433 |
IProfile target = null; |
| 434 |
if (profileId.equals("this")) //$NON-NLS-1$ |
| 435 |
profileId = IProfileRegistry.SELF; |
| 436 |
target = ProvisioningHelper.getProfile(agent, profileId); |
| 437 |
if (target == null) { |
| 438 |
if (profileId.equals(IProfileRegistry.SELF)) { |
| 439 |
interpreter.println("No profile found"); |
| 440 |
} else { |
| 441 |
interpreter.println("Profile " + profileId + " not found"); |
| 442 |
} |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
// list the profile contents |
| 447 |
IInstallableUnit[] result = sort(target.query(QueryUtil.createIUQuery(id, VersionRange.create(range)), null)); |
| 448 |
for (IInstallableUnit unit : result) { |
| 449 |
interpreter.println(unit); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Lists the profile timestamps for a given profile id, if no profile id, the |
| 455 |
* default profile is used. |
| 456 |
* |
| 457 |
* @param interpreter |
| 458 |
*/ |
| 459 |
public void _provlpts(CommandInterpreter interpreter) { |
| 460 |
String profileId = processArgument(interpreter.nextArgument()); |
| 461 |
if (profileId == null || profileId.equals("this")) { //$NON-NLS-1$ |
| 462 |
profileId = IProfileRegistry.SELF; |
| 463 |
} |
| 464 |
long[] profileTimestamps = ProvisioningHelper.getProfileTimestamps(agent, profileId); |
| 465 |
// if no profile timestamps for given id, print that out and done |
| 466 |
if (profileTimestamps == null || profileTimestamps.length == 0) { |
| 467 |
if (profileId.equals(IProfileRegistry.SELF)) { |
| 468 |
interpreter.println("No profile found"); |
| 469 |
} else { |
| 470 |
interpreter.print("No timestamps found for profile "); |
| 471 |
interpreter.println(profileId); |
| 472 |
} |
| 473 |
return; |
| 474 |
} |
| 475 |
// else if there are some timestamps then print them out on separate line |
| 476 |
interpreter.print("Timestamps for profile "); |
| 477 |
interpreter.println(profileId); |
| 478 |
for (long profileTimestamp : profileTimestamps) { |
| 479 |
interpreter.print("\t"); //$NON-NLS-1$ |
| 480 |
interpreter.println(Long.valueOf(profileTimestamp)); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Revert a profile to a given timestamp |
| 486 |
*/ |
| 487 |
public void _provrevert(CommandInterpreter interpreter) { |
| 488 |
String timestamp = interpreter.nextArgument(); |
| 489 |
if (timestamp == null) { |
| 490 |
interpreter |
| 491 |
.println("Valid timestamp must be provided. Timestamps can be retrieved via 'provlpts' command."); |
| 492 |
return; |
| 493 |
} |
| 494 |
Long ts = null; |
| 495 |
try { |
| 496 |
ts = Long.valueOf(timestamp); |
| 497 |
} catch (NumberFormatException e) { |
| 498 |
interpreter.println( |
| 499 |
"Timestamp " + timestamp + " not valid. Timestamps can be retrieved via 'provlpts' command."); |
| 500 |
return; |
| 501 |
} |
| 502 |
String profileId = interpreter.nextArgument(); |
| 503 |
if (profileId == null || profileId.equals("this")) |
| 504 |
profileId = IProfileRegistry.SELF; |
| 505 |
|
| 506 |
IProfile profile = ProvisioningHelper.getProfile(agent, profileId); |
| 507 |
if (profile == null) { |
| 508 |
if (profileId.equals(IProfileRegistry.SELF)) { |
| 509 |
interpreter.println("No profile found"); |
| 510 |
} else { |
| 511 |
interpreter.println("Profile " + profileId + " not found"); |
| 512 |
} |
| 513 |
return; |
| 514 |
} |
| 515 |
IStatus s = null; |
| 516 |
try { |
| 517 |
s = ProvisioningHelper.revertToPreviousState(agent, profile, ts.longValue()); |
| 518 |
} catch (ProvisionException e) { |
| 519 |
interpreter.println("revert failed "); |
| 520 |
interpreter.printStackTrace(e); |
| 521 |
return; |
| 522 |
} |
| 523 |
if (s.isOK()) |
| 524 |
interpreter.println("revert completed"); |
| 525 |
else { |
| 526 |
interpreter.println("revert failed "); |
| 527 |
printErrorStatus(interpreter, s); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
private IInstallableUnit[] sort(IQueryResult<IInstallableUnit> queryResult) { |
| 532 |
IInstallableUnit[] units = queryResult.toArray(IInstallableUnit.class); |
| 533 |
Arrays.sort(units, (arg0, arg1) -> arg0.toString().compareTo(arg1.toString())); |
| 534 |
return units; |
| 535 |
} |
| 536 |
|
| 537 |
public void _provlgp(CommandInterpreter interpreter) { |
| 538 |
String profileId = processArgument(interpreter.nextArgument()); |
| 539 |
if (profileId == null || profileId.equals("this")) { |
| 540 |
profileId = IProfileRegistry.SELF; |
| 541 |
} |
| 542 |
IProfile profile = ProvisioningHelper.getProfile(agent, profileId); |
| 543 |
if (profile == null) { |
| 544 |
if (profileId.equals(IProfileRegistry.SELF)) { |
| 545 |
interpreter.println("No profile found"); |
| 546 |
} else { |
| 547 |
interpreter.println("Profile " + profileId + " not found"); |
| 548 |
} |
| 549 |
return; |
| 550 |
} |
| 551 |
IInstallableUnit[] units = sort(profile.query(QueryUtil.createIUGroupQuery(), new NullProgressMonitor())); |
| 552 |
// Now print out results |
| 553 |
for (IInstallableUnit unit : units) { |
| 554 |
println(interpreter, unit); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Lists the installable units that match the given profile id and query. The id |
| 560 |
* can be "this" to denote the self profile. A third boolean argument |
| 561 |
* can be provided where <code>true</code> means "full query" and |
| 562 |
* <code>false</code> means "e;match query"e;. The default is |
| 563 |
* <code>false</code>. |
| 564 |
* |
| 565 |
* @param interpreter |
| 566 |
*/ |
| 567 |
public void _provlpquery(CommandInterpreter interpreter) { |
| 568 |
String profileId = processArgument(interpreter.nextArgument()); |
| 569 |
if (profileId == null || profileId.equals("this")) { |
| 570 |
profileId = IProfileRegistry.SELF; |
| 571 |
} |
| 572 |
|
| 573 |
String expression = processArgument(interpreter.nextArgument()); |
| 574 |
if (expression == null) { |
| 575 |
interpreter.println("Please enter a query"); |
| 576 |
return; |
| 577 |
} |
| 578 |
|
| 579 |
boolean useFull = Boolean.parseBoolean(processArgument(interpreter.nextArgument())); |
| 580 |
IQuery<IInstallableUnit> query = useFull ? QueryUtil.createQuery(expression) |
| 581 |
: QueryUtil.createMatchQuery(expression); |
| 582 |
|
| 583 |
IProfile profile = ProvisioningHelper.getProfile(agent, profileId); |
| 584 |
if (profile == null) { |
| 585 |
if (profileId.equals(IProfileRegistry.SELF)) { |
| 586 |
interpreter.println("No profile found"); |
| 587 |
} else { |
| 588 |
interpreter.println("Profile " + profileId + " not found"); |
| 589 |
} |
| 590 |
return; |
| 591 |
} |
| 592 |
IInstallableUnit[] units = sort(profile.query(query, new NullProgressMonitor())); |
| 593 |
// Now print out results |
| 594 |
if (units.length == 0) |
| 595 |
interpreter.println("No units found"); |
| 596 |
else { |
| 597 |
for (IInstallableUnit unit : units) { |
| 598 |
println(interpreter, unit); |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
public void _provremove(CommandInterpreter interpreter) { |
| 604 |
String iu = interpreter.nextArgument(); |
| 605 |
String version = interpreter.nextArgument(); |
| 606 |
String profileId = interpreter.nextArgument(); |
| 607 |
if (profileId == null || profileId.equals("this")) |
| 608 |
profileId = IProfileRegistry.SELF; |
| 609 |
if (version == null) { |
| 610 |
version = Version.emptyVersion.toString(); |
| 611 |
} |
| 612 |
if (iu == null) { |
| 613 |
interpreter.println("Installable unit id must be provided"); |
| 614 |
return; |
| 615 |
} |
| 616 |
IProfile profile = ProvisioningHelper.getProfile(agent, profileId); |
| 617 |
if (profile == null) { |
| 618 |
if (profileId.equals(IProfileRegistry.SELF)) { |
| 619 |
interpreter.println("No profile found"); |
| 620 |
} else { |
| 621 |
interpreter.println("Profile " + profileId + " not found"); |
| 622 |
} |
| 623 |
return; |
| 624 |
} |
| 625 |
IStatus s = null; |
| 626 |
try { |
| 627 |
s = ProvisioningHelper.uninstall(agent, iu, version, profile, new NullProgressMonitor()); |
| 628 |
} catch (ProvisionException e) { |
| 629 |
interpreter.println("Remove failed with ProvisionException for " + iu + " " + version); |
| 630 |
interpreter.printStackTrace(e); |
| 631 |
return; |
| 632 |
} |
| 633 |
if (s.isOK()) |
| 634 |
interpreter.println("Remove complete for " + iu + " " + version); |
| 635 |
else { |
| 636 |
interpreter.println("Remove failed for " + iu + " " + version); |
| 637 |
printErrorStatus(interpreter, s); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Handles the help command |
| 643 |
* |
| 644 |
* @param intp |
| 645 |
* @return description for a particular command or false if there is no command |
| 646 |
* with the specified name |
| 647 |
*/ |
| 648 |
public Object _help(CommandInterpreter intp) { |
| 649 |
String commandName = intp.nextArgument(); |
| 650 |
if (commandName == null) { |
| 651 |
return false; |
| 652 |
} |
| 653 |
String help = getHelp(commandName); |
| 654 |
|
| 655 |
return help.length() > 0 ? help : false; |
| 656 |
} |
| 657 |
|
| 658 |
private void printErrorStatus(CommandInterpreter interpreter, IStatus status) { |
| 659 |
interpreter.print("--Error status "); |
| 660 |
interpreter.print("message=" + status.getMessage()); |
| 661 |
interpreter.print(",code=" + status.getCode()); |
| 662 |
String severityString = null; |
| 663 |
switch (status.getSeverity()) { |
| 664 |
case IStatus.INFO: |
| 665 |
severityString = "INFO"; |
| 666 |
break; |
| 667 |
case IStatus.CANCEL: |
| 668 |
severityString = "CANCEL"; |
| 669 |
break; |
| 670 |
case IStatus.WARNING: |
| 671 |
severityString = "WARNING"; |
| 672 |
break; |
| 673 |
case IStatus.ERROR: |
| 674 |
severityString = "ERROR"; |
| 675 |
break; |
| 676 |
} |
| 677 |
interpreter.print(",severity=" + severityString); |
| 678 |
interpreter.print(",bundle=" + status.getPlugin()); |
| 679 |
interpreter.println("--"); |
| 680 |
Throwable t = status.getException(); |
| 681 |
if (t != null) |
| 682 |
interpreter.printStackTrace(t); |
| 683 |
IStatus[] children = status.getChildren(); |
| 684 |
if (children != null && children.length > 0) { |
| 685 |
interpreter.println("Error status children:"); |
| 686 |
for (IStatus child : children) { |
| 687 |
printErrorStatus(interpreter, child); |
| 688 |
} |
| 689 |
} |
| 690 |
interpreter.println("--End Error Status--"); |
| 691 |
} |
| 692 |
|
| 693 |
@Override |
| 694 |
public String getHelp() { |
| 695 |
return getHelp(null); |
| 696 |
} |
| 697 |
|
| 698 |
/* |
| 699 |
* Returns either the help message for a particular command, or returns the help |
| 700 |
* messages for all commands (if command is not specified) |
| 701 |
*/ |
| 702 |
private String getHelp(String commandName) { |
| 703 |
StringBuilder help = new StringBuilder(); |
| 704 |
|
| 705 |
if (commandsHelp == null) { |
| 706 |
initializeCommandsHelp(); |
| 707 |
} |
| 708 |
|
| 709 |
if (commandGroups == null) { |
| 710 |
initializeCommandGroups(); |
| 711 |
} |
| 712 |
|
| 713 |
if (commandName != null) { |
| 714 |
if (commandsHelp.containsKey(commandName)) { |
| 715 |
addCommand(commandName, commandsHelp.get(commandName), help); |
| 716 |
} |
| 717 |
return help.toString(); |
| 718 |
} |
| 719 |
|
| 720 |
addHeader(Messages.Console_help_header, help); |
| 721 |
for (Entry<String, String[]> groupEntry : commandGroups.entrySet()) { |
| 722 |
addHeader(groupEntry.getKey(), help); |
| 723 |
for (String command : groupEntry.getValue()) { |
| 724 |
addCommand(command, commandsHelp.get(command), help); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
return help.toString(); |
| 729 |
} |
| 730 |
|
| 731 |
private void addHeader(String header, StringBuilder help) { |
| 732 |
help.append("---"); //$NON-NLS-1$ |
| 733 |
help.append(header); |
| 734 |
help.append("---"); //$NON-NLS-1$ |
| 735 |
help.append(NEW_LINE); |
| 736 |
} |
| 737 |
|
| 738 |
private void addCommand(String command, String description, StringBuilder help) { |
| 739 |
help.append(TAB); |
| 740 |
help.append(command); |
| 741 |
help.append(" "); //$NON-NLS-1$ |
| 742 |
help.append(description); |
| 743 |
help.append(NEW_LINE); |
| 744 |
} |
| 745 |
|
| 746 |
private void initializeCommandsHelp() { |
| 747 |
commandsHelp = new HashMap<>(); |
| 748 |
|
| 749 |
// add commands for repository |
| 750 |
commandsHelp.put("provaddrepo", Messages.Console_help_provaddrepo_description); //$NON-NLS-1$ |
| 751 |
commandsHelp.put("provdelrepo", Messages.Console_help_provdelrepo_description); //$NON-NLS-1$ |
| 752 |
commandsHelp.put("provaddmetadatarepo", Messages.Console_help_provaddmetadatarepo_description); //$NON-NLS-1$ |
| 753 |
commandsHelp.put("provdelmetadatarepo", Messages.Console_help_provdelmetadatarepo_description); //$NON-NLS-1$ |
| 754 |
commandsHelp.put("provaddartifactrepo", Messages.Console_help_provaddartifactrepo_description); //$NON-NLS-1$ |
| 755 |
commandsHelp.put("provdelartifactrepo", Messages.Console_help_provdelartifactrepo_description); //$NON-NLS-1$ |
| 756 |
commandsHelp.put("provlg", Messages.Console_help_provlg_description); //$NON-NLS-1$ |
| 757 |
commandsHelp.put("provlr", Messages.Console_help_provlr_description); //$NON-NLS-1$ |
| 758 |
commandsHelp.put("provlar", Messages.Console_help_provlar_description); //$NON-NLS-1$ |
| 759 |
commandsHelp.put("provliu", Messages.Console_help_provliu_description); //$NON-NLS-1$ |
| 760 |
commandsHelp.put("provlquery", Messages.Console_help_provlquery_description); //$NON-NLS-1$ |
| 761 |
|
| 762 |
// add commands for profiles |
| 763 |
commandsHelp.put("provaddprofile", Messages.Console_help_provaddprofile_description); //$NON-NLS-1$ |
| 764 |
commandsHelp.put("provdelprofile", Messages.Console_help_provdelprofile_description); //$NON-NLS-1$ |
| 765 |
commandsHelp.put("provlp", Messages.Console_help_provlp_description); //$NON-NLS-1$ |
| 766 |
commandsHelp.put("provlgp", Messages.Console_help_provlgp_description); //$NON-NLS-1$ |
| 767 |
commandsHelp.put("provlpts", Messages.Console_help_provlpts_description); //$NON-NLS-1$ |
| 768 |
commandsHelp.put("provlpquery", Messages.Console_help_provlpquery_description); //$NON-NLS-1$ |
| 769 |
|
| 770 |
// add commands for install/uninstall |
| 771 |
commandsHelp.put("provinstall", Messages.Console_help_provinstall_description); //$NON-NLS-1$ |
| 772 |
commandsHelp.put("provremove", Messages.Console_help_provremove_description); //$NON-NLS-1$ |
| 773 |
commandsHelp.put("provrevert", Messages.Console_help_provrevert_description); //$NON-NLS-1$ |
| 774 |
} |
| 775 |
|
| 776 |
private void initializeCommandGroups() { |
| 777 |
commandGroups = new LinkedHashMap<>(); |
| 778 |
commandGroups.put(Messages.Console_help_repository_header, |
| 779 |
new String[] { "provaddrepo", "provdelrepo", "provaddmetadatarepo", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 780 |
"provdelmetadatarepo", "provaddartifactrepo", "provdelartifactrepo", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 781 |
"provlg", "provlr", "provlar", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 782 |
"provliu", "provlquery" }); //$NON-NLS-1$ //$NON-NLS-2$ |
| 783 |
|
| 784 |
commandGroups.put(Messages.Console_help_profile_registry_header, |
| 785 |
new String[] { "provaddprofile", "provdelprofile", //$NON-NLS-1$ //$NON-NLS-2$ |
| 786 |
"provlp", "provlgp", "provlpts", "provlpquery" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
| 787 |
|
| 788 |
commandGroups.put(Messages.Console_help_install_header, |
| 789 |
new String[] { "provinstall", "provremove", "provrevert" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* Prints a string representation of an {@link IInstallableUnit} to the |
| 794 |
* iterpreter's output stream. |
| 795 |
*/ |
| 796 |
public void print(CommandInterpreter interpreter, IInstallableUnit unit) { |
| 797 |
interpreter.print(unit.getId() + ' ' + unit.getVersion()); |
| 798 |
} |
| 799 |
|
| 800 |
/** |
| 801 |
* Prints a string representation of an {@link IInstallableUnit} to the |
| 802 |
* iterpreter's output stream, following by a line terminator |
| 803 |
*/ |
| 804 |
public void println(CommandInterpreter interpreter, IInstallableUnit unit) { |
| 805 |
print(interpreter, unit); |
| 806 |
interpreter.println(); |
| 807 |
} |
| 808 |
|
| 809 |
private void println(CommandInterpreter interpreter, IArtifactKey artifactKey, File location) { |
| 810 |
interpreter.print(artifactKey.toString() + ' ' + location); |
| 811 |
interpreter.println(); |
| 812 |
} |
| 813 |
} |