1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.buildcache.xml;
20
21 import javax.annotation.Nonnull;
22
23 import java.util.List;
24
25 import org.apache.commons.lang3.ArrayUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.buildcache.CacheUtils;
28 import org.apache.maven.buildcache.xml.build.Artifact;
29 import org.apache.maven.buildcache.xml.build.CompletedExecution;
30 import org.apache.maven.buildcache.xml.build.DigestItem;
31 import org.apache.maven.buildcache.xml.build.PropertyValue;
32 import org.apache.maven.buildcache.xml.config.TrackedProperty;
33 import org.apache.maven.model.Dependency;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import static org.apache.maven.buildcache.checksum.KeyUtils.getArtifactKey;
38
39
40
41
42 public class DtoUtils {
43
44 private static final Logger LOGGER = LoggerFactory.getLogger(DtoUtils.class);
45
46 public static String findPropertyValue(String propertyName, CompletedExecution completedExecution) {
47 final List<PropertyValue> properties = completedExecution.getProperties();
48 if (properties == null) {
49 return null;
50 }
51 for (PropertyValue property : properties) {
52 if (StringUtils.equals(propertyName, property.getName())) {
53 return property.getValue();
54 }
55 }
56 return null;
57 }
58
59 public static Artifact createDto(org.apache.maven.artifact.Artifact artifact) {
60 Artifact dto = new Artifact();
61 dto.setArtifactId(artifact.getArtifactId());
62 dto.setGroupId(artifact.getGroupId());
63 dto.setVersion(artifact.getVersion());
64 dto.setClassifier(artifact.getClassifier());
65 dto.setType(artifact.getType());
66 dto.setScope(artifact.getScope());
67 dto.setFileName(CacheUtils.normalizedName(artifact));
68 return dto;
69 }
70
71 public static DigestItem createdDigestedByProjectChecksum(Artifact artifact, String projectChecksum) {
72 DigestItem dit = new DigestItem();
73 dit.setType("module");
74 dit.setHash(projectChecksum);
75 dit.setFileChecksum(artifact.getFileHash());
76 dit.setValue(getArtifactKey(artifact));
77 return dit;
78 }
79
80 public static DigestItem createDigestedFile(org.apache.maven.artifact.Artifact artifact, String fileHash) {
81 DigestItem dit = new DigestItem();
82 dit.setType("artifact");
83 dit.setHash(fileHash);
84 dit.setFileChecksum(fileHash);
85 dit.setValue(getArtifactKey(artifact));
86 return dit;
87 }
88
89 public static Dependency createDependency(org.apache.maven.artifact.Artifact artifact) {
90 final Dependency dependency = new Dependency();
91 dependency.setArtifactId(artifact.getArtifactId());
92 dependency.setGroupId(artifact.getGroupId());
93 dependency.setVersion(artifact.getVersion());
94 dependency.setClassifier(artifact.getClassifier());
95 dependency.setType(artifact.getType());
96 dependency.setScope(artifact.getScope());
97 return dependency;
98 }
99
100 public static Dependency createDependency(Artifact artifact) {
101 final Dependency dependency = new Dependency();
102 dependency.setArtifactId(artifact.getArtifactId());
103 dependency.setGroupId(artifact.getGroupId());
104 dependency.setVersion(artifact.getVersion());
105 dependency.setType(artifact.getType());
106 dependency.setScope(artifact.getScope());
107 dependency.setClassifier(artifact.getClassifier());
108 return dependency;
109 }
110
111 public static void addProperty(
112 CompletedExecution execution, String propertyName, Object value, String baseDirPath, boolean tracked) {
113 final PropertyValue valueType = new PropertyValue();
114 valueType.setName(propertyName);
115 if (value != null && value.getClass().isArray()) {
116 value = ArrayUtils.toString(value);
117 }
118 final String valueText = String.valueOf(value);
119 valueType.setValue(StringUtils.remove(valueText, baseDirPath));
120 valueType.setTracked(tracked);
121 execution.addProperty(valueType);
122 }
123
124
125
126
127
128
129
130
131 public static boolean containsAllProperties(
132 @Nonnull CompletedExecution cachedExecution, List<TrackedProperty> trackedProperties) {
133 if (trackedProperties == null || trackedProperties.isEmpty()) {
134 return true;
135 }
136
137 if (cachedExecution.getProperties() == null) {
138 return false;
139 }
140
141 final List<PropertyValue> executionProperties = cachedExecution.getProperties();
142 for (TrackedProperty trackedProperty : trackedProperties) {
143 if (!contains(executionProperties, trackedProperty.getPropertyName())) {
144 LOGGER.warn(
145 "Tracked property `{}` not found in cached build. Execution: {}",
146 trackedProperty.getPropertyName(),
147 cachedExecution.getExecutionKey());
148 return false;
149 }
150 }
151 return true;
152 }
153
154 public static boolean contains(List<PropertyValue> executionProperties, String propertyName) {
155 for (PropertyValue executionProperty : executionProperties) {
156 if (StringUtils.equals(executionProperty.getName(), propertyName)) {
157 return true;
158 }
159 }
160 return false;
161 }
162
163 public static Artifact copy(Artifact artifact) {
164 Artifact copy = new Artifact();
165 copy.setArtifactId(artifact.getArtifactId());
166 copy.setGroupId(artifact.getGroupId());
167 copy.setVersion(artifact.getVersion());
168 copy.setType(artifact.getType());
169 copy.setClassifier(artifact.getClassifier());
170 copy.setScope(artifact.getScope());
171 copy.setFileName(artifact.getFileName());
172 copy.setFileHash(artifact.getFileHash());
173 copy.setFileSize(artifact.getFileSize());
174 return copy;
175 }
176 }