aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java
blob: ce63a8d46693c85967cba9bf55f227907ee94978 (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
/*
 * 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.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;

public class ArtifactHelper {

    private MavenProject project;
    private MavenSession session;
    private static final Map<String, byte[]> store = new HashMap<>();
    private static final Set<String> terminalModuleCoordinates = new HashSet<>();
    private String unicornRoot = null;
    private static File unicornMetaLocation = null;
    private final File tempLocation = Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile();
    private static int snapshotBuildNumber = 0;
    private static final String HYPHEN = "-";

    void init(String terminalModuleCoordinate) {
        setUnicornMetaLocation(getUnicornRootFile(unicornRoot.substring(0, unicornRoot.indexOf('/')), project));
        setTerminalModuleCoordinates(session.getProjects().get(session.getProjects().size() - 1));
        terminalModuleCoordinates.add(terminalModuleCoordinate);
    }

    private static void setUnicornMetaLocation(File file) {
        unicornMetaLocation = file;
    }

    static void setSnapshotBuildNumber(int number) {
        snapshotBuildNumber = number;
    }

    List<ArtifactRepository> getRepositories(boolean snapshotRepo) {
        List<ArtifactRepository> list = new ArrayList<>();
        for (ArtifactRepository artRepo : project.getRemoteArtifactRepositories()) {
            if (snapshotRepo) {
                if (artRepo.getSnapshots().isEnabled()) {
                    list.add(artRepo);
                }
            } else {
                if (artRepo.getReleases().isEnabled()) {
                    list.add(artRepo);
                }
            }
        }
        return list;
    }

    private void setTerminalModuleCoordinates(MavenProject project) {
        terminalModuleCoordinates.add(getModuleCoordinate(project));
    }

    private boolean isModuleTerminal(MavenProject project) {
        return terminalModuleCoordinates.contains(getModuleCoordinate(project));
    }

    File getUnicornMetaLocation() {
        return unicornMetaLocation;
    }

    String getContents(URL path) throws IOException {
        try (InputStream is = path.openStream(); Scanner scanner = new Scanner(is).useDelimiter("\\A")) {
            return scanner.hasNext() ? scanner.next() : "";
        }
    }

    void store(String artifactId, byte[] data) {
        store.put(artifactId, data);
    }

    void deleteAll(File f) {

        if (!f.exists() || !f.isDirectory()) {
            return;
        }

        File[] fileList = f.listFiles();
        if (fileList == null) {
            return;
        }

        for (File file : fileList) {

            if (file.isFile()) {
                file.delete();
            }
        }
    }

    String getModuleCoordinate(MavenProject project) {
        return project.getGroupId() + ":" + project.getArtifactId();
    }

    private File getUnicornRootFile(String moduleCoordinate, MavenProject mavenProject) {
        return getStateFile(moduleCoordinate, mavenProject, unicornRoot);
    }

    private File getStateFile(String moduleCoordinate, MavenProject mavenProject, String filePath) {
        return new File(getTopParentProject(moduleCoordinate, mavenProject).getBasedir(),
                filePath.substring(filePath.indexOf('/') + 1));
    }

    MavenProject getTopParentProject(String moduleCoordinate, MavenProject mavenProject) {
        if (getModuleCoordinate(mavenProject).equals(moduleCoordinate) || mavenProject.getParent() == null) {
            return mavenProject;
        } else {
            return getTopParentProject(moduleCoordinate, mavenProject.getParent());
        }
    }

    void shutDown(MavenProject project) throws IOException, ClassNotFoundException {
        File file = new File(unicornMetaLocation, "compileState.dat");
        HashMap dataStore;
        if (isModuleTerminal(project) && file.exists()) {
            try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) {
                dataStore = (HashMap) ois.readObject();
                //noinspection unchecked
                dataStore.put("shutdownTime", (System.currentTimeMillis() / 1000) * 1000 + snapshotBuildNumber);
                //noinspection unchecked
                dataStore.put("version", project.getVersion());
            }
            try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) {
                oos.writeObject(dataStore);
            }
            Files.copy(file.toPath(),
                    Paths.get(tempLocation.getAbsolutePath(), file.getName() + HYPHEN + project.getVersion()),
                    StandardCopyOption.REPLACE_EXISTING);
        }
    }
}