2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on a "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.onboarding;
19 import static org.openecomp.sdc.onboarding.Constants.JAVA_EXT;
20 import static org.openecomp.sdc.onboarding.Constants.UNICORN;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.ObjectInputStream;
26 import java.io.UncheckedIOException;
28 import java.net.URISyntaxException;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.security.MessageDigest;
34 import java.security.NoSuchAlgorithmException;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
40 import java.util.Optional;
41 import java.util.concurrent.ForkJoinPool;
42 import java.util.concurrent.RecursiveTask;
43 import java.util.stream.Collectors;
44 import org.apache.maven.plugin.MojoFailureException;
45 import org.apache.maven.project.MavenProject;
49 private BuildHelper() {
53 static long getChecksum(File file, String fileType) {
55 return readSources(file, fileType).hashCode();
56 } catch (IOException e) {
57 throw new UncheckedIOException(e);
61 static String getSourceChecksum(String data, String hashType) throws NoSuchAlgorithmException {
62 MessageDigest md = MessageDigest.getInstance(hashType);
63 md.update(data.getBytes());
64 byte[] hashBytes = md.digest();
66 StringBuilder buffer = new StringBuilder();
67 for (byte hashByte : hashBytes) {
68 buffer.append(Integer.toString((hashByte & 0xff) + 0x100, 16).substring(1));
70 return buffer.toString();
74 private static Map<String, List<String>> readSources(File file, String fileType) throws IOException {
75 Map<String, List<String>> source = new HashMap<>();
77 List<File> list = Files.walk(Paths.get(file.getAbsolutePath()))
78 .filter(JAVA_EXT.equals(fileType) ? BuildHelper::isRegularJavaFile :
79 Files::isRegularFile).map(p -> p.toFile())
80 .collect(Collectors.toList());
81 source.putAll(ForkJoinPool.commonPool()
82 .invoke(new FileReadTask(list.toArray(new File[0]), file.getAbsolutePath())));
87 private static boolean isRegularJavaFile(Path path) {
88 File file = path.toFile();
89 return file.isFile() && file.getName().endsWith(JAVA_EXT);
92 private static class FileReadTask extends RecursiveTask<Map<String, List<String>>> {
94 private Map<String, List<String>> store = new HashMap<>();
97 private static final int MAX_FILES = 10;
99 FileReadTask(File[] files, String pathPrefix) {
101 this.pathPrefix = pathPrefix;
104 private static List<String> getData(File file) throws IOException {
105 List<String> coll = Files.readAllLines(file.toPath(), StandardCharsets.ISO_8859_1);
106 if (file.getAbsolutePath().contains(File.separator + "generated-sources" + File.separator)) {
107 Iterator<String> itr = coll.iterator();
108 while (itr.hasNext()) {
109 String s = itr.next();
110 if (s == null || s.trim().startsWith("/") || s.trim().startsWith("*")) {
120 protected Map<String, List<String>> compute() {
121 if (files.length > MAX_FILES) {
122 FileReadTask task1 = new FileReadTask(Arrays.copyOfRange(files, 0, files.length / 2), pathPrefix);
124 new FileReadTask(Arrays.copyOfRange(files, files.length / 2, files.length), pathPrefix);
127 store.putAll(task1.join());
128 store.putAll(task2.join());
130 for (File toRead : files) {
132 store.put(toRead.getAbsolutePath().substring(pathPrefix.length())
133 .replace(File.separatorChar, '.'), getData(toRead));
134 } catch (IOException e) {
135 throw new UncheckedIOException(e);
144 static Optional<String> getArtifactPathInLocalRepo(String repoPath, MavenProject project, byte[] sourceChecksum)
145 throws MojoFailureException {
149 uri = new URI(repoPath + (project.getGroupId().replace('.', '/')) + '/' + project.getArtifactId() + '/'
150 + project.getVersion());
151 } catch (URISyntaxException e) {
152 throw new MojoFailureException(e.getMessage(), e);
154 File f = new File(uri);
155 File[] list = f.listFiles(t -> t.getName().equals(project.getArtifactId() + "-" + project.getVersion() + "."
156 + project.getPackaging()));
157 if (list != null && list.length > 0) {
158 File checksumFile = new File(list[0].getParentFile(), project.getBuild().getFinalName() + "." + UNICORN);
160 if (checksumFile.exists() && Arrays.equals(sourceChecksum, Files.readAllBytes(checksumFile.toPath()))) {
161 return Optional.of(list[0].getAbsolutePath());
163 } catch (IOException e) {
164 throw new UncheckedIOException(e);
167 return Optional.empty();
170 static <T> Optional<T> readState(String fileName, Class<T> clazz) {
171 try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
172 ObjectInputStream ois = new ObjectInputStream(is)) {
173 return Optional.of(clazz.cast(ois.readObject()));
174 } catch (Exception ignored) {
175 //ignore. it is taken care.
176 return Optional.empty();