use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.
the class MavenMetadataGenerator method writeVersionMetadata.
private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
ArtifactPathInfo samplePomInfo = null;
logger.debug("writeVersionMetadata, firstLevelFiles:{}, store:{}", firstLevelFiles, store.getKey());
// first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including
// as a valid version
final List<SingleVersion> versions = new ArrayList<>();
nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
final String topPath = topResource.getPath();
if (topPath.endsWith("/")) {
final List<StoreResource> secondLevelListing = fileManager.listRaw(store, topPath);
for (final StoreResource fileResource : secondLevelListing) {
if (fileResource.getPath().endsWith(".pom")) {
ArtifactPathInfo filePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
// check if the pom is valid for the path
if (filePomInfo != null) {
versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
if (samplePomInfo == null) {
samplePomInfo = filePomInfo;
}
continue nextTopResource;
}
}
}
}
}
if (versions.isEmpty()) {
logger.debug("writeVersionMetadata, versions is empty, store:{}", store.getKey());
return false;
}
logger.debug("writeVersionMetadata, versions: {}, store:{}", versions, store.getKey());
Collections.sort(versions);
final Transfer metadataFile = fileManager.getTransfer(store, path);
OutputStream stream = null;
try {
final Document doc = xml.newDocumentBuilder().newDocument();
final Map<String, String> coordMap = new HashMap<>();
coordMap.put(ARTIFACT_ID, samplePomInfo == null ? null : samplePomInfo.getArtifactId());
coordMap.put(GROUP_ID, samplePomInfo == null ? null : samplePomInfo.getGroupId());
final String lastUpdated = generateUpdateTimestamp(getCurrentTimestamp());
doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
xml.createElement(doc.getDocumentElement(), null, coordMap);
final Map<String, String> versioningMap = new HashMap<>();
versioningMap.put(LAST_UPDATED, lastUpdated);
final SingleVersion latest = versions.get(versions.size() - 1);
versioningMap.put(LATEST, latest.renderStandard());
SingleVersion release = null;
for (int i = versions.size() - 1; i >= 0; i--) {
final SingleVersion r = versions.get(i);
if (r.isRelease()) {
release = r;
break;
}
}
if (release != null) {
versioningMap.put(RELEASE, release.renderStandard());
}
xml.createElement(doc, "versioning", versioningMap);
final Element versionsElem = xml.createElement(doc, "versioning/versions", Collections.emptyMap());
for (final SingleVersion version : versions) {
final Element vElem = doc.createElement(VERSION);
vElem.setTextContent(version.renderStandard());
versionsElem.appendChild(vElem);
}
final String xmlStr = xml.toXML(doc, true);
logger.debug("writeVersionMetadata, xmlStr: {}", xmlStr);
stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
stream.write(xmlStr.getBytes(UTF_8));
} catch (final GalleyMavenXMLException e) {
throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path, e.getMessage());
} catch (final IOException e) {
throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e, metadataFile, e.getMessage());
} finally {
closeQuietly(stream);
}
logger.debug("writeVersionMetadata, DONE, store: {}", store.getKey());
return true;
}
use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.
the class MavenMetadataGeneratorTest method generateFileContent_VersionsMetadataWith2Versions.
@Test
public void generateFileContent_VersionsMetadataWith2Versions() throws Exception {
final StoreResource resource = setupVersionsStructureWith2Versions();
final ConcreteResource metadataFile = resource.getChild("maven-metadata.xml");
final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), metadataFile.getPath(), new EventMetadata());
assertThat(transfer, notNullValue());
final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), new EventMetadata());
assertThat(metadata, notNullValue());
final VersioningView versioning = metadata.getVersioning();
final List<SingleVersion> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.get(0).renderStandard(), equalTo("1.0"));
assertThat(versions.get(1).renderStandard(), equalTo("1.1"));
assertThat(versioning.getReleaseVersion().renderStandard(), equalTo("1.1"));
assertThat(versioning.getLatestVersion().renderStandard(), equalTo("1.1"));
}
use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.
the class MavenMetadataGeneratorTest method generateFileContent_SnapshotMetadataWith2Versions.
@Test
public void generateFileContent_SnapshotMetadataWith2Versions() throws Exception {
final StoreResource resource = setupSnapshotDirWith2Snapshots();
final EventMetadata emd = new EventMetadata();
final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), resource.getChild("maven-metadata.xml").getPath(), emd);
assertThat(transfer, notNullValue());
final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), emd);
assertThat(metadata, notNullValue());
final VersioningView versioning = metadata.getVersioning();
final LatestSnapshotView latestSnapshot = versioning.getLatestSnapshot();
assertThat(latestSnapshot.isLocalCopy(), equalTo(false));
assertThat(latestSnapshot.getTimestamp(), equalTo(SnapshotUtils.parseSnapshotTimestamp("20140828.225800")));
assertThat(latestSnapshot.getBuildNumber(), equalTo(1));
final List<SnapshotArtifactView> snapshots = versioning.getSnapshotArtifacts();
assertThat(snapshots.size(), equalTo(4));
for (final SnapshotArtifactView snap : snapshots) {
final String extension = snap.getExtension();
assertThat(extension.equals("jar") || extension.equals("pom"), equalTo(true));
final String version = snap.getVersion();
System.out.println(version);
final SingleVersion parsed = VersionUtils.createSingleVersion(version);
assertThat(parsed.isSnapshot(), equalTo(true));
assertThat(parsed.isLocalSnapshot(), equalTo(false));
final SnapshotPart part = parsed.getSnapshotPart();
final String tstamp = SnapshotUtils.generateSnapshotTimestamp(part.getTimestamp());
assertThat(tstamp.equals("20140828.225800") || tstamp.equals("20140828.221400"), equalTo(true));
}
}
use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.
the class KojiMavenMetadataProvider method scanArchive.
private ArchiveScan scanArchive(final KojiArchiveInfo archive, final Set<Integer> seenBuilds) throws KojiClientException {
Logger logger = LoggerFactory.getLogger(getClass());
ArchiveScan scan = new ArchiveScan();
if (!archive.getFilename().endsWith(".pom")) {
logger.debug("Skipping non-POM: {}", archive.getFilename());
scan.setDisqualified(true);
return scan;
}
if (!isVerSignedAllowed(archive.getVersion())) {
logger.debug("version filter pattern not matched: {}", archive.getVersion());
scan.setDisqualified(true);
return scan;
}
SingleVersion singleVersion = null;
try {
singleVersion = VersionUtils.createSingleVersion(archive.getVersion());
scan.setSingleVersion(singleVersion);
} catch (InvalidVersionSpecificationException ivse) {
logger.warn("Skipping mal-formatted version: {}, relPath: {}, buildId: {}", archive.getVersion(), archive.getRelPath(), archive.getBuildId());
scan.setDisqualified(true);
return scan;
}
KojiBuildInfo build = null;
if (seenBuilds.contains(archive.getBuildId())) {
logger.debug("Skipping already seen build: {}", archive.getBuildId());
scan.setDisqualified(true);
return scan;
} else {
build = kojiContentProvider.getBuildInfo(archive.getBuildId(), null);
seenBuilds.add(archive.getBuildId());
scan.setBuild(build);
}
if (build == null) {
logger.debug("Cannot retrieve build info: {}. Skipping: {}", archive.getBuildId(), archive.getFilename());
scan.setDisqualified(true);
return scan;
}
if (build.getBuildState() != KojiBuildState.COMPLETE) {
logger.debug("Build: {} is not completed. The state is {}. Skipping.", build.getNvr(), build.getBuildState());
scan.setDisqualified(true);
return scan;
}
if (build.getTaskId() == null) {
logger.debug("Build: {} is not a real build. It looks like a binary import. Skipping.", build.getNvr());
// This is not a real build, it's a binary import.
scan.setDisqualified(true);
return scan;
}
return scan;
}
use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.
the class PackageMetadata method merge.
public boolean merge(PackageMetadata source, boolean isForGroup) {
Logger logger = LoggerFactory.getLogger(getClass());
boolean changed = false;
if (source.getName() != null) {
this.setName(source.getName());
changed = true;
}
if (source.getDescription() != null) {
this.setDescription(source.getDescription());
changed = true;
}
if (source.getAuthor() != null) {
this.setAuthor(source.getAuthor());
changed = true;
}
if (source.getRepository() != null) {
this.setRepository(source.getRepository());
changed = true;
}
if (source.getReadme() != null) {
this.setReadme(source.getReadme());
changed = true;
}
if (source.getReadmeFilename() != null) {
this.setReadmeFilename(source.getReadmeFilename());
changed = true;
}
if (source.getHomepage() != null) {
this.setHomepage(source.getHomepage());
changed = true;
}
if (source.getBugs() != null) {
this.setBugs(source.getBugs());
changed = true;
}
if (source.getLicense() != null) {
this.setLicense(source.getLicense());
changed = true;
}
// merge maintainers list
for (UserInfo m : source.getMaintainers()) {
if (m.getName() != null && !maintainers.contains(m)) {
this.addMaintainers(new UserInfo(m.getName(), m.getEmail(), m.getUrl()));
changed = true;
}
}
// merge keywords list
for (String key : source.getKeywords()) {
if (!keywords.contains(key)) {
this.addKeywords(key);
changed = true;
}
}
// merge users map
Map<String, Boolean> sourceUsers = source.getUsers();
for (final String user : sourceUsers.keySet()) {
if (users.containsKey(user) && users.get(user).equals(sourceUsers.get(user))) {
continue;
}
users.put(user, sourceUsers.get(user));
changed = true;
}
// merge time map
SimpleDateFormat sdf = new SimpleDateFormat(TIMEOUT_FORMAT);
Map<String, Date> clone = new LinkedHashMap<>();
for (final String key : time.keySet()) {
String value = time.get(key);
Date date = null;
try {
date = sdf.parse(value);
} catch (ParseException e) {
logger.error(String.format("Cannot parse date: %s. Reason: %s", value, e));
}
clone.put(key, date);
}
Map<String, String> sourceTimes = source.getTime();
boolean added = false;
for (final String key : sourceTimes.keySet()) {
String value = sourceTimes.get(key);
Date date;
try {
date = sdf.parse(value);
} catch (ParseException e) {
logger.error(String.format("Cannot parse date: %s. Reason: %s", value, e));
continue;
}
// if source's version update time is more recent(sort as the letter order), will update it into the original map.
if (clone.containsKey(key) && date.compareTo(clone.get(key)) <= 0) {
continue;
}
clone.put(key, date);
added = true;
changed = true;
}
// only sorting the map when update occurred
if (added) {
// sort as the time value in map
List<Map.Entry<String, Date>> timeList = new ArrayList<>(clone.entrySet());
// sort the time as value (update time) asc
timeList.sort(Map.Entry.comparingByValue());
Map<String, String> result = new LinkedHashMap<>();
// make the 'modified' and 'created' value as the first two keys in final map
if (clone.get(MODIFIED) != null) {
result.put(MODIFIED, sdf.format(clone.get(MODIFIED)));
}
if (clone.get(CREATED) != null) {
result.put(CREATED, sdf.format(clone.get(CREATED)));
}
for (final Map.Entry<String, Date> entry : timeList) {
if (MODIFIED.equals(entry.getKey()) || CREATED.equals(entry.getKey())) {
continue;
}
result.put(entry.getKey(), sdf.format(entry.getValue()));
}
time = result;
}
// merge dist-tag object
DistTag sourceDist = source.getDistTags();
Map<String, String> sourceDistMap = sourceDist.fetchTagsMap();
Map<String, String> thisDistMap = distTags.fetchTagsMap();
if (thisDistMap.isEmpty() && !sourceDistMap.isEmpty()) {
this.setDistTags(sourceDist);
changed = true;
} else {
for (final String tag : sourceDistMap.keySet()) {
SingleVersion sourceVersion = VersionUtils.createSingleVersion(sourceDistMap.get(tag));
SingleVersion thisVersion = VersionUtils.createSingleVersion(thisDistMap.get(tag));
if (thisDistMap.containsKey(tag) && sourceVersion.compareTo(thisVersion) <= 0) {
continue;
}
distTags.putTag(tag, sourceDistMap.get(tag));
changed = true;
}
}
// merge versions, keep the first version metadata for a given version that coming across
Map<String, VersionMetadata> sourceVersions = source.getVersions();
for (final String v : sourceVersions.keySet()) {
VersionMetadata value = sourceVersions.get(v);
// for publish merging, it always accepts the latest updated version meta
if (!isForGroup || !versions.containsKey(v)) {
versions.put(v, value);
changed = true;
}
}
return changed;
}
Aggregations