Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public Artifact deriveArtifact() {
final Artifact derivedArtifact = new Artifact();

derivedArtifact.setId(componentPatternData.get(COMPONENT_PART));
derivedArtifact.setName(componentPatternData.get(COMPONENT_PART_NAME));

derivedArtifact.setComponent(componentPatternData.get(COMPONENT_NAME));
derivedArtifact.setVersion(componentPatternData.get(COMPONENT_VERSION));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import static org.metaeffekt.core.inventory.processor.filescan.FileSystemScanConstants.ATTRIBUTE_KEY_ARTIFACT_PATH;
import static org.metaeffekt.core.inventory.processor.filescan.FileSystemScanConstants.ATTRIBUTE_KEY_ASSET_ID_CHAIN;
import static org.metaeffekt.core.inventory.processor.model.Artifact.Attribute.ROOT_PATHS;
import static org.metaeffekt.core.inventory.processor.model.Artifact.Attribute.*;
import static org.metaeffekt.core.inventory.processor.model.Constants.KEY_PATH_IN_ASSET;
import static org.metaeffekt.core.util.FileUtils.asRelativePath;

Expand Down Expand Up @@ -73,6 +73,8 @@ public void process(FileSystemScanContext fileSystemScanContext) throws IOExcept
final String relativePath = asRelativePath(fileSystemScanContext.getBaseDir().getPath(), filePath);
artifact.set(ATTRIBUTE_KEY_ARTIFACT_PATH, relativePath);
artifact.set(KEY_PATH_IN_ASSET, relativePath);
artifact.set(FILE_NAME, fileName);
artifact.set(CLASSIFIER, artifact.inferClassifierFromFileNameAndVersion());

// evaluate conditions for unwrapping the file (file is not yet probed; any file may be subject to unwrapping)
final boolean unwrap = fileSystemScanContext.getScanParam().isImplicitUnwrap() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public Artifact getArtifactFromPomProperties(Artifact artifact, InputStream inpu
String packaging = pomProperties.getProperty("packaging");
dummyArtifact.setGroupId(groupId);
dummyArtifact.setVersion(version);
dummyArtifact.setName(artifactId);

// very little change about how its written

dummyArtifact.set(Constants.KEY_TYPE, Constants.ARTIFACT_TYPE_MODULE);
dummyArtifact.set(Constants.KEY_COMPONENT_SOURCE_TYPE, "jar-module");
Expand Down Expand Up @@ -557,10 +560,16 @@ public void run(Inventory inventory, Properties properties) {
artifact.setArtifactId(null);

deriveVersionIfNotSet(artifact);

deriveTypeIfNotSet(artifact);


artifact.deriveArtifactId();

artifact.set(Artifact.Attribute.CLASSIFIER, artifact.inferClassifierFromFileNameAndVersion());

deriveNameIfNotSet(artifact);

addPurlIfMissing(artifact);

} catch (Exception e) {
Expand Down Expand Up @@ -590,6 +599,40 @@ private void deriveTypeIfNotSet(Artifact artifact) {
}
}

private void deriveNameIfNotSet(Artifact artifact) {
if (StringUtils.isBlank(artifact.get(Constants.KEY_NAME))) {
String fileName = artifact.get(Artifact.Attribute.FILE_NAME);
String version = artifact.getVersion();
if (fileName != null && fileName.toLowerCase(Locale.US).endsWith(".jar")) {
String name = fileName.substring(0, fileName.lastIndexOf("."));
String classifier = artifact.get(Artifact.Attribute.CLASSIFIER);
if (StringUtils.isNotBlank(classifier) && name.endsWith("-" + classifier)) {
name += "-" + classifier;
}
if (name.endsWith("-" + version)) {

}
artifact.set(Constants.KEY_NAME, name);
artifact.set(Constants.KEY_COMPONENT_SOURCE_TYPE, "jar-module");
}
}
else if (StringUtils.isBlank(artifact.get(Constants.KEY_NAME)) && (Artifact.Attribute.ID != null)) {
String id = artifact.getId();
if (id != null && id.toLowerCase(Locale.US).endsWith(".jar")) {
String name = id.substring(0, id.lastIndexOf("."));
String classifier = artifact.get(Artifact.Attribute.CLASSIFIER);
if (StringUtils.isNotBlank(classifier) && name.endsWith("-" + classifier)) {
name += "-" + classifier;
}
artifact.set(Constants.KEY_NAME, name);
artifact.set(Constants.KEY_COMPONENT_SOURCE_TYPE, "jar-module");
}

}


}

private void addPurlIfMissing(Artifact artifact) {
if (StringUtils.isEmpty(artifact.get(Artifact.Attribute.PURL.getKey()))) {
final int suffixIndex = artifact.getId().lastIndexOf(".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ public class Artifact extends AbstractModelBase {
*/
public enum Attribute implements AbstractModelBase.Attribute {
ID("Id"),
NAME("Name"),
VERSION("Version"),
COMPONENT("Component"),
COMPONENT_TYPE("Component Type"),

CHECKSUM("Checksum"),
VERSION("Version"),

RELEASE("Release"),
CLASSIFIER("Classifier"),
ARCHITECTURE("Architecture"),
DISTRO("Distro"),
FILE_NAME("File Name"),
FILE_TYPE("File Type"),
SPECIFIC_FILE_TYPE("Specific File Type"),
PACKAGING("Packaging"),

// latest available version
LATEST_VERSION("Latest Version"),
Expand Down Expand Up @@ -73,6 +85,9 @@ public enum Attribute implements AbstractModelBase.Attribute {
HASH_SHA512("Hash (SHA-512)"),
PATH_IN_ASSET("Path in Asset"),

// is a mix of many information in one
QUALIFIER("Qualifier"),

/**
* An artifact Root Path is the topmost path in which parts of a logical artifact can be aggregated. In this
* case the path points to a folder. In case of an artifact being represented by a single file, the Artifact
Expand Down Expand Up @@ -117,6 +132,44 @@ public String getKey() {
}
}

/**
* Defines a default order.
*/
public static Artifact.Attribute[] ARTIFACT_ATTRIBUTE_LIST = new Artifact.Attribute[] {
Attribute.QUALIFIER,
Attribute.GROUPID,
Attribute.FILE_NAME,
Attribute.NAME,
Attribute.VERSION,
Attribute.CLASSIFIER,
Attribute.PACKAGING,
Attribute.RELEASE,
Attribute.ARCHITECTURE,
Attribute.DISTRO,
Attribute.PURL,
Attribute.CHECKSUM,
Attribute.HASH_SHA1,
Attribute.HASH_SHA256,
Attribute.HASH_SHA512,
Attribute.FILE_TYPE,
Attribute.SPECIFIC_FILE_TYPE,
Attribute.COMPONENT,
Attribute.COMPONENT_TYPE,
Attribute.ROOT_PATHS,
Attribute.PATH_IN_ASSET,
Attribute.LATEST_VERSION,
Attribute.LICENSE,
Attribute.CLASSIFICATION,
Attribute.SECURITY_RELEVANT,
Attribute.SECURITY_CATEGORY,
Attribute.VULNERABILITY,
Attribute.COMMENT,
Attribute.URL,
Attribute.VERIFIED
};

public static List<String> ARTIFACT_COLUMN_ORDER_LIST =
Arrays.stream(ARTIFACT_ATTRIBUTE_LIST).map(a -> a.key).collect(Collectors.toList());

// artifact id (derived from id and version)
private transient String artifactId;
Expand Down Expand Up @@ -180,6 +233,10 @@ public void setId(String id) {
set(Attribute.ID, id);
}

public String getName() {return get(Attribute.NAME);}

public void setName(String name) {set(Attribute.NAME, name);}

public String getVersion() {
return get(Attribute.VERSION);
}
Expand Down Expand Up @@ -220,6 +277,14 @@ public void setPathInAsset(String pathInAsset) {
set(Attribute.PATH_IN_ASSET, pathInAsset);
}

public String getQualifier() {return get(Attribute.QUALIFIER);}

public void setQualifier(String qualifier) {set(Attribute.QUALIFIER, qualifier);}

public String getRelease() {return get(Attribute.RELEASE);}

public void setRelease(String release) {set(Attribute.RELEASE, release);}

@Deprecated
public boolean isVerified() {
// FIXME: remove this concept
Expand Down Expand Up @@ -277,6 +342,7 @@ public void merge(Artifact a) {
super.merge(a);

deriveArtifactId();
set(Attribute.CLASSIFIER, inferClassifierFromFileNameAndVersion());
}

private void mergeRootPaths(Artifact a) {
Expand Down Expand Up @@ -381,9 +447,9 @@ public String createStringRepresentation() {
if (getVersion() != null) {
artifactRepresentation.append(getVersion());
}
if (getClassifier() != null) {
if (inferClassifierFromFileNameAndVersion() != null) {
artifactRepresentation.append(DELIMITER_COLON);
artifactRepresentation.append(getClassifier());
artifactRepresentation.append(inferClassifierFromFileNameAndVersion());
}
artifactRepresentation.append(DELIMITER_COLON);
// skip type if no information was derived
Expand All @@ -397,7 +463,7 @@ private String inferTypeFromId() {
String type = null;
String id = getId();
if (id != null) {
String classifier = inferClassifierFromId();
String classifier = inferClassifierFromFileNameAndVersion();
String version = getVersion();

if (version == null) {
Expand Down Expand Up @@ -502,10 +568,41 @@ private String inferClassifierFromId() {
return null;
}

public String inferClassifierFromFileNameAndVersion() {
final String fileName = get(Attribute.FILE_NAME);
final String version = getVersion();
if (StringUtils.isNotBlank(fileName) && StringUtils.isNotBlank(version)) {
// get rid of anything right to version
final String queryString = DELIMITER_DASH + version + DELIMITER_DASH;
final int versionIndex = fileName.indexOf(queryString);
if (versionIndex < 0) {
// no '-<version>-' part, no classifier
return null;
}
final int beginIndex = versionIndex + queryString.length();
final String classifierAndType = fileName.substring(beginIndex);
// get rid of trailing .{type}
final int index = classifierAndType.indexOf(DELIMITER_DOT);
if (index != -1) {
final String classifier = classifierAndType.substring(0, index).trim();
if (StringUtils.isNotBlank(classifier)) {
return classifier;
}
}
}
return null;
}

public String createCompareStringRepresentation() {
StringBuffer artifactRepresentation = new StringBuffer();
artifactRepresentation.append(normalize(getId()));
artifactRepresentation.append(DELIMITER_COLON);
artifactRepresentation.append(normalize(get(Attribute.NAME)));
artifactRepresentation.append(DELIMITER_COLON);
artifactRepresentation.append(normalize(get(Attribute.FILE_NAME)));
artifactRepresentation.append(DELIMITER_COLON);
artifactRepresentation.append(normalize(get(Attribute.CLASSIFIER)));
artifactRepresentation.append(DELIMITER_COLON);
artifactRepresentation.append(normalize(getChecksum()));
artifactRepresentation.append(DELIMITER_COLON);
artifactRepresentation.append(normalize(getGroupId()));
Expand Down Expand Up @@ -552,8 +649,9 @@ public String getType() {
return inferTypeFromId();
}

public String getClassifier() {
return inferClassifierFromId();
@Deprecated
public String getClassifierxxx() {
return get(Attribute.CLASSIFIER);
}

public boolean isEnabledForDistribution() {
Expand Down Expand Up @@ -757,4 +855,30 @@ public boolean isComponentOrComponentPart() {
return false;
}

public static List<String> orderAttributes(Collection<String> attributes, List<String> contextColumnList, List<String> artifactColumnOrder) {
// impose context or default order
final List<String> ordered = new ArrayList<>(attributes);
Collections.sort(ordered);
int insertIndex = 0;
if (contextColumnList != null) {
for (String key : contextColumnList) {
insertIndex = reinsert(insertIndex, key, ordered, attributes);
}
} else {
for (String key : artifactColumnOrder) {
insertIndex = reinsert(insertIndex, key, ordered, attributes);
}
}
return ordered;
}

private static int reinsert(int insertIndex, String key, List<String> orderedAttributesList, Collection<String> attributesSet) {
if (attributesSet.contains(key)) {
orderedAttributesList.remove(key);
orderedAttributesList.add(Math.min(insertIndex, orderedAttributesList.size()), key);
insertIndex++;
}
return insertIndex;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public enum Attribute implements AbstractModelBase.Attribute {

COMPONENT_NAME("Component Name"),
COMPONENT_PART("Component Part"),
COMPONENT_PART_NAME("Component Part - Name"),
COMPONENT_VERSION("Component Version"),

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public final class Constants {

// Artifact and Asset base keys
public static final String KEY_ID = "Id";
public static final String KEY_NAME = "Name";
public static final String KEY_ASSET_ID = "Asset Id";
public static final String KEY_URL = "URL";
public static final String KEY_VERSION = "Version";
Expand Down
Loading