summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/compile-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/BuildHelper.java
blob: 42f3166ad6a7978aef9be5832c1e1c34035378df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright © 2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on a "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdc.onboarding;

import static org.openecomp.sdc.onboarding.Constants.JAVA_EXT;
import static org.openecomp.sdc.onboarding.Constants.UNICORN;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.stream.Collectors;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

class BuildHelper {

    private BuildHelper() {
        // donot remove.
    }

    static long getChecksum(File file, String fileType) {
        try {
            return readSources(file, fileType).hashCode();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    static String getSourceChecksum(String data, String hashType) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(hashType);
        md.update(data.getBytes());
        byte[] hashBytes = md.digest();

        StringBuffer buffer = new StringBuffer();
        for (byte hashByte : hashBytes) {
            buffer.append(Integer.toString((hashByte & 0xff) + 0x100, 16).substring(1));
        }
        return buffer.toString();
    }


    private static Map<String, String> readSources(File file, String fileType) throws IOException {
        Map<String, String> source = new HashMap<>();
        if (file.exists()) {
            List<File> list = Files.walk(Paths.get(file.getAbsolutePath()))
                                   .filter(JAVA_EXT.equals(fileType) ? BuildHelper::isRegularJavaFile :
                                                   Files::isRegularFile).map(p -> p.toFile())
                                   .collect(Collectors.toList());
            source.putAll(ForkJoinPool.commonPool()
                                      .invoke(new FileReadTask(list.toArray(new File[0]), file.getAbsolutePath())));
        }
        return source;
    }

    private static boolean isRegularJavaFile(Path path) {
        File file = path.toFile();
        return file.isFile() && file.getName().endsWith(JAVA_EXT);
    }

    private static String getData(File file, byte[] buffer) {
        try (FileInputStream fis = new FileInputStream(file);
             BufferedInputStream bis = new BufferedInputStream(fis, 64 * 1024)) {
            bis.read(buffer, 0, ((int) file.length()));
            if (file.getAbsolutePath().contains(File.separator + "generated-sources" + File.separator)) {
                StringBuffer sb = new StringBuffer();
                List<String> coll = Files.readAllLines(file.toPath());
                for (String s : coll) {
                    if (s != null && !s.trim().startsWith("/") && !s.trim().startsWith("*")) {
                        sb.append(s);
                    }
                }
                return sb.toString();
            }
        } catch (IOException ioe) {
            throw new UncheckedIOException(ioe);
        }
        return new String(buffer, 0, ((int) file.length()));
    }


    private static class FileReadTask extends RecursiveTask<Map<String, String>> {

        Map<String, String> store = new HashMap<>();
        private byte[] buffer = new byte[1024 * 1024];
        File[] files;
        String pathPrefix;
        private final int MAX_FILES = 10;

        FileReadTask(File[] files, String pathPrefix) {
            this.files = files;
            this.pathPrefix = pathPrefix;
        }

        @Override
        protected Map<String, String> compute() {
            if (files.length > MAX_FILES) {
                FileReadTask task1 = new FileReadTask(Arrays.copyOfRange(files, 0, files.length / 2), pathPrefix);
                FileReadTask task2 =
                        new FileReadTask(Arrays.copyOfRange(files, files.length / 2, files.length), pathPrefix);
                task1.fork();
                task2.fork();
                store.putAll(task1.join());
                store.putAll(task2.join());
            } else {
                for (File toRead : files) {
                    store.put(toRead.getAbsolutePath().substring(pathPrefix.length()), getData(toRead, buffer));
                }
            }

            return store;
        }
    }

    static Optional<String> getArtifactPathInLocalRepo(String repoPath, MavenProject project, byte[] sourceChecksum)
            throws MojoFailureException {

        URI uri = null;
        try {
            uri = new URI(repoPath + (project.getGroupId().replace('.', '/')) + '/' + project.getArtifactId() + '/'
                                  + project.getVersion());
        } catch (URISyntaxException e) {
            throw new MojoFailureException(e.getMessage());
        }
        File f = new File(uri);
        File[] list = f.listFiles(t -> t.getName().equals(project.getArtifactId() + "-" + project.getVersion() + "."
                                                                  + project.getPackaging()));
        if (list != null && list.length > 0) {
            File checksumFile = new File(list[0].getParentFile(), project.getBuild().getFinalName() + "." + UNICORN);
            try {
                if (checksumFile.exists() && Arrays.equals(sourceChecksum, Files.readAllBytes(checksumFile.toPath()))) {
                    return Optional.of(list[0].getAbsolutePath());
                }
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
        return Optional.empty();
    }

    static <T> Optional<T> readState(String fileName, Class<T> clazz) {
        try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
             ObjectInputStream ois = new ObjectInputStream(is)) {
            return Optional.of(clazz.cast(ois.readObject()));
        } catch (Exception ignored) {
            //ignore. it is taken care.
            return Optional.empty();
        }
    }

}