use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class MavenMetadataMerger method merge.
@Override
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Generating merged metadata in: {}:{}", group.getKey(), path);
final Metadata master = new Metadata();
master.setVersioning(new Versioning());
final MetadataXpp3Reader reader = new MetadataXpp3Reader();
final FileReader fr = null;
InputStream stream = null;
boolean merged = false;
Transfer snapshotProvider = null;
for (final Transfer src : sources) {
if (!src.exists()) {
continue;
}
try {
stream = src.openInputStream();
String content = IOUtils.toString(stream);
logger.debug("Adding in metadata content from: {}\n\n{}\n\n", src, content);
// there is a lot of junk in here to make up for Metadata's anemic merge() method.
final Metadata md = reader.read(new StringReader(content), false);
if (md.getGroupId() != null) {
master.setGroupId(md.getGroupId());
}
if (md.getArtifactId() != null) {
master.setArtifactId(md.getArtifactId());
}
if (md.getVersion() != null) {
master.setVersion(md.getVersion());
}
master.merge(md);
Versioning versioning = master.getVersioning();
Versioning mdVersioning = md.getVersioning();
// FIXME: Should we try to merge snapshot lists instead of using the first one we encounter??
if (versioning.getSnapshot() == null && mdVersioning != null) {
logger.info("INCLUDING snapshot information from: {} in: {}:{}", src, group.getKey(), path);
snapshotProvider = src;
versioning.setSnapshot(mdVersioning.getSnapshot());
final List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
boolean added = false;
for (final SnapshotVersion snap : mdVersioning.getSnapshotVersions()) {
if (!snapshotVersions.contains(snap)) {
snapshotVersions.add(snap);
added = true;
}
}
if (added) {
Collections.sort(snapshotVersions, new SnapshotVersionComparator());
}
} else {
logger.warn("SKIPPING snapshot information from: {} in: {}:{} (obscured by: {})", src, group.getKey(), path, snapshotProvider);
}
merged = true;
} catch (final IOException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot read metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} catch (final XmlPullParserException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot parse metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} finally {
closeQuietly(fr);
closeQuietly(stream);
}
}
Versioning versioning = master.getVersioning();
if (versioning != null && versioning.getVersions() != null) {
if (metadataProviders != null) {
for (MavenMetadataProvider provider : metadataProviders) {
try {
Metadata toMerge = provider.getMetadata(group.getKey(), path);
if (toMerge != null) {
merged = master.merge(toMerge) || merged;
}
} catch (IndyWorkflowException e) {
logger.error(String.format("Cannot read metadata: %s from metadata provider: %s. Reason: %s", path, provider.getClass().getSimpleName(), e.getMessage()), e);
}
}
}
List<SingleVersion> versionObjects = versioning.getVersions().stream().map(VersionUtils::createSingleVersion).collect(Collectors.toList());
Collections.sort(versionObjects);
versioning.setVersions(versionObjects.stream().map(SingleVersion::renderStandard).collect(Collectors.toList()));
if (versionObjects.size() > 0) {
String latest = versionObjects.get(versionObjects.size() - 1).renderStandard();
versioning.setLatest(latest);
versioning.setRelease(latest);
}
}
if (merged) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
new MetadataXpp3Writer().write(baos, master);
return baos.toByteArray();
} catch (final IOException e) {
logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
}
}
return null;
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class MavenMetadataMergerTest method mergeOneTransferWithProviderContent.
@Test
public void mergeOneTransferWithProviderContent() throws Exception {
String path = "org/foo/bar/maven-metadata.xml";
HostedRepository h1 = new HostedRepository("test-hosted-1");
Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
initTestData(t1, VERSION_META + "simple-1.xml");
Group g = new Group("test-group", h1.getKey());
List<Transfer> sources = Arrays.asList(t1);
Versioning providedVersioning = new Versioning();
providedVersioning.setLatest("1.1");
providedVersioning.setRelease("1.1");
providedVersioning.addVersion("1.1");
Metadata provided = new Metadata();
provided.setGroupId("org.foo");
provided.setArtifactId("bar");
provided.setVersioning(providedVersioning);
TestMavenMetadataProvider testProvider = new TestMavenMetadataProvider(provided);
byte[] output = new MavenMetadataMerger(Collections.singletonList(testProvider)).merge(sources, g, path);
Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
assertThat(merged.getGroupId(), equalTo("org.foo"));
assertThat(merged.getArtifactId(), equalTo("bar"));
Versioning versioning = merged.getVersioning();
assertThat(versioning, notNullValue());
List<String> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.size(), equalTo(2));
assertThat(versioning.getRelease(), equalTo("1.1"));
assertThat(versioning.getLatest(), equalTo("1.1"));
int idx = 0;
assertThat(versions.get(idx), equalTo("1.0"));
idx++;
assertThat(versions.get(idx), equalTo("1.1"));
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class PromotionManager method rollbackPathsPromote.
/**
* Attempt to rollbackPathsPromote a previously failing {@link PathsPromoteResult}. This is meant to handle cases where an unrecoverable error
* occurs on the server side, and promotion can NOT proceed afterward. All paths in the completed paths set are deleted from the target, if
* possible. The output {@link PathsPromoteResult} contains the previous content, with any successfully removed target paths moved back from the
* completed-paths list to the pending-paths list. If an error occurs during rollbackPathsPromote, the error field will be set...otherwise, it will be null.
*
* @param result The result to rollbackPathsPromote
*
* @return The same result, with any successful deletions moved from the completed to pending paths list, and the error cleared (or set to a
* new error)
*
* @throws PromotionException
* @throws IndyWorkflowException
*/
public PathsPromoteResult rollbackPathsPromote(final PathsPromoteResult result) throws PromotionException, IndyWorkflowException {
StoreKey targetKey = result.getRequest().getTarget();
ReentrantLock lock;
synchronized (byPathTargetLocks) {
lock = byPathTargetLocks.get(targetKey);
if (lock == null) {
lock = new ReentrantLock();
byPathTargetLocks.put(targetKey, lock);
}
}
final List<Transfer> contents = getTransfersForPaths(targetKey, result.getCompletedPaths());
final Set<String> completed = result.getCompletedPaths();
final Set<String> skipped = result.getSkippedPaths();
if (completed == null || completed.isEmpty()) {
result.setError(null);
return result;
}
Set<String> pending = result.getPendingPaths();
pending = pending == null ? new HashSet<String>() : new HashSet<String>(pending);
String error = null;
final boolean copyToSource = result.getRequest().isPurgeSource();
ArtifactStore source = null;
try {
source = storeManager.getArtifactStore(result.getRequest().getSource());
} catch (final IndyDataException e) {
error = String.format("Failed to retrieve artifact store: %s. Reason: %s", result.getRequest().getSource(), e.getMessage());
logger.error(error, e);
}
boolean locked = false;
try {
if (error == null) {
locked = lock.tryLock(config.getLockTimeoutSeconds(), TimeUnit.SECONDS);
if (!locked) {
error = String.format("Failed to acquire promotion lock on target: %s in %d seconds.", targetKey, config.getLockTimeoutSeconds());
logger.warn(error);
}
}
if (error == null) {
for (final Transfer transfer : contents) {
if (transfer != null && transfer.exists()) {
InputStream stream = null;
try {
if (copyToSource) {
stream = transfer.openInputStream(true);
final String path = transfer.getPath();
contentManager.store(source, path, stream, TransferOperation.UPLOAD, new EventMetadata());
stream.close();
}
transfer.delete(true);
completed.remove(transfer.getPath());
pending.add(transfer.getPath());
} catch (final IOException e) {
error = String.format("Failed to rollback path promotion of: %s from: %s. Reason: %s", transfer, result.getRequest().getSource(), e.getMessage());
logger.error(error, e);
} finally {
closeQuietly(stream);
}
}
}
}
} catch (InterruptedException e) {
error = String.format("Interrupted waiting for promotion lock on target: %s", targetKey);
logger.warn(error);
} finally {
if (locked) {
lock.unlock();
}
}
return new PathsPromoteResult(result.getRequest(), pending, completed, skipped, error, new ValidationResult());
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class PromotionValidationTools method getTransfer.
public Transfer getTransfer(final StoreKey storeKey, final String path) throws IndyWorkflowException {
Logger logger = LoggerFactory.getLogger(getClass());
logger.info("Retrieving transfer for: {} in {}", path, storeKey);
Transfer result = readOnlyWrapper(contentManager.getTransfer(storeKey, path, TransferOperation.DOWNLOAD));
logger.info("Result: {}", result);
return result;
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class PromotionValidationTools method getRelationshipsForPom.
public Set<ProjectRelationship<?, ?>> getRelationshipsForPom(final String path, final ModelProcessorConfig config, final ValidationRequest request, final StoreKey... extraLocations) throws IndyWorkflowException, GalleyMavenException, IndyDataException {
Logger logger = LoggerFactory.getLogger(getClass());
logger.trace("Retrieving relationships for POM: {} (using extra locations: {})", path, Arrays.asList(extraLocations));
ArtifactRef artifactRef = getArtifact(path);
if (artifactRef == null) {
logger.trace("{} is not a valid artifact reference. Skipping.", path);
return null;
}
StoreKey key = request.getSourceRepository().getKey();
Transfer transfer = retrieve(request.getSourceRepository(), path);
if (transfer == null) {
logger.trace("Could not retrieve Transfer instance for: {} (path: {}, extra locations: {})", key, path, Arrays.asList(extraLocations));
return null;
}
List<Location> locations = new ArrayList<>(extraLocations.length + 1);
locations.add(transfer.getLocation());
addLocations(locations, extraLocations);
MavenPomView pomView = pomReader.read(artifactRef.asProjectVersionRef(), transfer, locations, MavenPomView.ALL_PROFILES);
try {
URI source = new URI("indy:" + key.getType().name() + ":" + key.getName());
return modelProcessor.readRelationships(pomView, source, config).getAllRelationships();
} catch (final URISyntaxException e) {
throw new IllegalStateException("Failed to construct URI for ArtifactStore: " + key + ". Reason: " + e.getMessage(), e);
}
}
Aggregations