View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * DtoUtils
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      * Checks that all tracked (for reconciliation purposes) properties present in cached build record
126      *
127      * @param  cachedExecution   mojo execution record (from cache)
128      * @param  trackedProperties list of tracked properties (from config)
129      * @return                   true if all tracked properties are listed in the cache record
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 }