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.internal.transformation;
20  
21  import java.io.File;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.security.MessageDigest;
26  import java.util.function.BiConsumer;
27  import java.util.function.Supplier;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.DefaultArtifact;
31  import org.apache.maven.artifact.handler.ArtifactHandler;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * Transformed artifact is derived with some transformation from source artifact.
37   *
38   * @since TBD
39   */
40  abstract class TransformedArtifact extends DefaultArtifact {
41  
42      private final OnChangeTransformer onChangeTransformer;
43  
44      TransformedArtifact(
45              Artifact source,
46              Supplier<Path> sourcePathProvider,
47              String classifier,
48              String extension,
49              Path targetPath,
50              BiConsumer<Path, Path> transformerConsumer) {
51          super(
52                  source.getGroupId(),
53                  source.getArtifactId(),
54                  source.getVersionRange(),
55                  source.getScope(),
56                  extension,
57                  classifier,
58                  new TransformedArtifactHandler(
59                          classifier, extension, source.getArtifactHandler().getPackaging()));
60          this.onChangeTransformer =
61                  new OnChangeTransformer(sourcePathProvider, targetPath, TransformedArtifact::sha1, transformerConsumer);
62      }
63  
64      @Override
65      public boolean isResolved() {
66          return getFile() != null;
67      }
68  
69      @Override
70      public void setFile(File file) {
71          throw new IllegalStateException("transformed artifact file cannot be set");
72      }
73  
74      @Override
75      public File getFile() {
76          Path result = onChangeTransformer.get();
77          if (result == null) {
78              return null;
79          }
80          return result.toFile();
81      }
82  
83      private static final int BUFFER_SIZE = 8192;
84  
85      private static String sha1(Path path) {
86          try {
87              MessageDigest md = MessageDigest.getInstance("SHA-1");
88              try (InputStream fis = Files.newInputStream(path)) {
89                  byte[] buffer = new byte[BUFFER_SIZE];
90                  int read;
91                  while ((read = fis.read(buffer)) != -1) {
92                      md.update(buffer, 0, read);
93                  }
94              }
95              StringBuilder result = new StringBuilder();
96              for (byte b : md.digest()) {
97                  result.append(String.format("%02x", b));
98              }
99              return result.toString();
100         } catch (Exception e) {
101             throw new RuntimeException(e);
102         }
103     }
104 
105     private static class TransformedArtifactHandler implements ArtifactHandler {
106         private final String classifier;
107 
108         private final String extension;
109 
110         private final String packaging;
111 
112         private TransformedArtifactHandler(String classifier, String extension, String packaging) {
113             this.classifier = classifier;
114             this.extension = requireNonNull(extension);
115             this.packaging = requireNonNull(packaging);
116         }
117 
118         public String getClassifier() {
119             return classifier;
120         }
121 
122         public String getDirectory() {
123             return null;
124         }
125 
126         public String getExtension() {
127             return extension;
128         }
129 
130         public String getLanguage() {
131             return "none";
132         }
133 
134         public String getPackaging() {
135             return packaging;
136         }
137 
138         public boolean isAddedToClasspath() {
139             return false;
140         }
141 
142         public boolean isIncludesDependencies() {
143             return false;
144         }
145     }
146 }