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.util;
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;
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;
36 import java.util.Scanner;
39 import org.apache.maven.artifact.repository.ArtifactRepository;
40 import org.apache.maven.execution.MavenSession;
41 import org.apache.maven.project.MavenProject;
43 public class ArtifactHelper {
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 = "-";
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);
61 private static void setUnicornMetaLocation(File file) {
62 unicornMetaLocation = file;
65 static void setSnapshotBuildNumber(int number) {
66 snapshotBuildNumber = number;
69 List<ArtifactRepository> getRepositories(boolean snapshotRepo) {
70 List<ArtifactRepository> list = new ArrayList<>();
71 for (ArtifactRepository artRepo : project.getRemoteArtifactRepositories()) {
73 if (artRepo.getSnapshots().isEnabled()) {
77 if (artRepo.getReleases().isEnabled()) {
85 private void setTerminalModuleCoordinates(MavenProject project) {
86 terminalModuleCoordinates.add(getModuleCoordinate(project));
89 private boolean isModuleTerminal(MavenProject project) {
90 return terminalModuleCoordinates.contains(getModuleCoordinate(project));
93 File getUnicornMetaLocation() {
94 return unicornMetaLocation;
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() : "";
103 void store(String artifactId, byte[] data) {
104 store.put(artifactId, data);
107 void deleteAll(File f) {
109 if (!f.exists() || !f.isDirectory()) {
113 File[] fileList = f.listFiles();
114 if (fileList == null) {
118 for (File file : fileList) {
126 String getModuleCoordinate(MavenProject project) {
127 return project.getGroupId() + ":" + project.getArtifactId();
130 private File getUnicornRootFile(String moduleCoordinate, MavenProject mavenProject) {
131 return getStateFile(moduleCoordinate, mavenProject, unicornRoot);
134 private File getStateFile(String moduleCoordinate, MavenProject mavenProject, String filePath) {
135 return new File(getTopParentProject(moduleCoordinate, mavenProject).getBasedir(),
136 filePath.substring(filePath.indexOf('/') + 1));
139 MavenProject getTopParentProject(String moduleCoordinate, MavenProject mavenProject) {
140 if (getModuleCoordinate(mavenProject).equals(moduleCoordinate) || mavenProject.getParent() == null) {
143 return getTopParentProject(moduleCoordinate, mavenProject.getParent());
147 void shutDown(MavenProject project) throws IOException, ClassNotFoundException {
148 File file = new File(unicornMetaLocation, "compileState.dat");
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());
158 try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) {
159 oos.writeObject(dataStore);
161 Files.copy(file.toPath(),
162 Paths.get(tempLocation.getAbsolutePath(), file.getName() + HYPHEN + project.getVersion()),
163 StandardCopyOption.REPLACE_EXISTING);