ce63a8d46693c85967cba9bf55f227907ee94978
[sdc.git] / openecomp-be / tools / artifact-copy-plugin / src / main / java / org / openecomp / sdc / onboarding / util / ArtifactHelper.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.onboarding.util;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.OutputStream;
27 import java.net.URL;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.nio.file.StandardCopyOption;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Scanner;
37 import java.util.Set;
38
39 import org.apache.maven.artifact.repository.ArtifactRepository;
40 import org.apache.maven.execution.MavenSession;
41 import org.apache.maven.project.MavenProject;
42
43 public class ArtifactHelper {
44
45     private MavenProject project;
46     private MavenSession session;
47     private static final Map<String, byte[]> store = new HashMap<>();
48     private static final Set<String> terminalModuleCoordinates = new HashSet<>();
49     private String unicornRoot = null;
50     private static File unicornMetaLocation = null;
51     private final File tempLocation = Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile();
52     private static int snapshotBuildNumber = 0;
53     private static final String HYPHEN = "-";
54
55     void init(String terminalModuleCoordinate) {
56         setUnicornMetaLocation(getUnicornRootFile(unicornRoot.substring(0, unicornRoot.indexOf('/')), project));
57         setTerminalModuleCoordinates(session.getProjects().get(session.getProjects().size() - 1));
58         terminalModuleCoordinates.add(terminalModuleCoordinate);
59     }
60
61     private static void setUnicornMetaLocation(File file) {
62         unicornMetaLocation = file;
63     }
64
65     static void setSnapshotBuildNumber(int number) {
66         snapshotBuildNumber = number;
67     }
68
69     List<ArtifactRepository> getRepositories(boolean snapshotRepo) {
70         List<ArtifactRepository> list = new ArrayList<>();
71         for (ArtifactRepository artRepo : project.getRemoteArtifactRepositories()) {
72             if (snapshotRepo) {
73                 if (artRepo.getSnapshots().isEnabled()) {
74                     list.add(artRepo);
75                 }
76             } else {
77                 if (artRepo.getReleases().isEnabled()) {
78                     list.add(artRepo);
79                 }
80             }
81         }
82         return list;
83     }
84
85     private void setTerminalModuleCoordinates(MavenProject project) {
86         terminalModuleCoordinates.add(getModuleCoordinate(project));
87     }
88
89     private boolean isModuleTerminal(MavenProject project) {
90         return terminalModuleCoordinates.contains(getModuleCoordinate(project));
91     }
92
93     File getUnicornMetaLocation() {
94         return unicornMetaLocation;
95     }
96
97     String getContents(URL path) throws IOException {
98         try (InputStream is = path.openStream(); Scanner scanner = new Scanner(is).useDelimiter("\\A")) {
99             return scanner.hasNext() ? scanner.next() : "";
100         }
101     }
102
103     void store(String artifactId, byte[] data) {
104         store.put(artifactId, data);
105     }
106
107     void deleteAll(File f) {
108
109         if (!f.exists() || !f.isDirectory()) {
110             return;
111         }
112
113         File[] fileList = f.listFiles();
114         if (fileList == null) {
115             return;
116         }
117
118         for (File file : fileList) {
119
120             if (file.isFile()) {
121                 file.delete();
122             }
123         }
124     }
125
126     String getModuleCoordinate(MavenProject project) {
127         return project.getGroupId() + ":" + project.getArtifactId();
128     }
129
130     private File getUnicornRootFile(String moduleCoordinate, MavenProject mavenProject) {
131         return getStateFile(moduleCoordinate, mavenProject, unicornRoot);
132     }
133
134     private File getStateFile(String moduleCoordinate, MavenProject mavenProject, String filePath) {
135         return new File(getTopParentProject(moduleCoordinate, mavenProject).getBasedir(),
136                 filePath.substring(filePath.indexOf('/') + 1));
137     }
138
139     MavenProject getTopParentProject(String moduleCoordinate, MavenProject mavenProject) {
140         if (getModuleCoordinate(mavenProject).equals(moduleCoordinate) || mavenProject.getParent() == null) {
141             return mavenProject;
142         } else {
143             return getTopParentProject(moduleCoordinate, mavenProject.getParent());
144         }
145     }
146
147     void shutDown(MavenProject project) throws IOException, ClassNotFoundException {
148         File file = new File(unicornMetaLocation, "compileState.dat");
149         HashMap dataStore;
150         if (isModuleTerminal(project) && file.exists()) {
151             try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) {
152                 dataStore = (HashMap) ois.readObject();
153                 //noinspection unchecked
154                 dataStore.put("shutdownTime", (System.currentTimeMillis() / 1000) * 1000 + snapshotBuildNumber);
155                 //noinspection unchecked
156                 dataStore.put("version", project.getVersion());
157             }
158             try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) {
159                 oos.writeObject(dataStore);
160             }
161             Files.copy(file.toPath(),
162                     Paths.get(tempLocation.getAbsolutePath(), file.getName() + HYPHEN + project.getVersion()),
163                     StandardCopyOption.REPLACE_EXISTING);
164         }
165     }
166 }
167
168