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;
38 import org.apache.maven.artifact.repository.ArtifactRepository;
39 import org.apache.maven.execution.MavenSession;
40 import org.apache.maven.project.MavenProject;
42 public class ArtifactHelper {
44 private MavenProject project;
45 private MavenSession session;
46 private static Map<String, byte[]> store = new HashMap<>();
47 private static Set<String> terminalModuleCoordinates = new HashSet<>();
48 private String unicornRoot = null;
49 private static File unicornMetaLocation = null;
50 private File tempLocation = Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile();
51 private static int snapshotBuildNumber = 0;
52 private static final String HYPHEN = "-";
54 void init(String terminalModuleCoordinate) {
55 setUnicornMetaLocation(getUnicornRootFile(unicornRoot.substring(0, unicornRoot.indexOf('/')), project));
56 setTerminalModuleCoordinates(session.getProjects().get(session.getProjects().size() - 1));
57 terminalModuleCoordinates.add(terminalModuleCoordinate);
60 private static void setUnicornMetaLocation(File file) {
61 unicornMetaLocation = file;
64 static void setSnapshotBuildNumber(int number) {
65 snapshotBuildNumber = number;
68 List<ArtifactRepository> getRepositories(boolean snapshotRepo) {
69 List<ArtifactRepository> list = new ArrayList<>();
70 for (ArtifactRepository artRepo : project.getRemoteArtifactRepositories()) {
72 if (artRepo.getSnapshots().isEnabled()) {
76 if (artRepo.getReleases().isEnabled()) {
84 private void setTerminalModuleCoordinates(MavenProject project) {
85 terminalModuleCoordinates.add(getModuleCoordinate(project));
88 private boolean isModuleTerminal(MavenProject project) {
89 return terminalModuleCoordinates.contains(getModuleCoordinate(project));
92 File getUnicornMetaLocation() {
93 return unicornMetaLocation;
96 String getContents(URL path) throws IOException {
97 try (InputStream is = path.openStream(); Scanner scnr = new Scanner(is).useDelimiter("\\A")) {
98 return scnr.hasNext() ? scnr.next() : "";
102 void store(String artifactId, byte[] data) {
103 store.put(artifactId, data);
106 void deleteAll(File f) {
107 if (!f.exists() && !f.isDirectory()) {
110 for (File file : f.listFiles()) {
117 String getModuleCoordinate(MavenProject project) {
118 return project.getGroupId() + ":" + project.getArtifactId();
121 private File getUnicornRootFile(String moduleCoordinate, MavenProject proj) {
122 return getStateFile(moduleCoordinate, proj, unicornRoot);
125 private File getStateFile(String moduleCoordinate, MavenProject proj, String filePath) {
126 return new File(getTopParentProject(moduleCoordinate, proj).getBasedir(),
127 filePath.substring(filePath.indexOf('/') + 1));
130 MavenProject getTopParentProject(String moduleCoordinate, MavenProject proj) {
131 if (getModuleCoordinate(proj).equals(moduleCoordinate) || proj.getParent() == null) {
134 return getTopParentProject(moduleCoordinate, proj.getParent());
138 void shutDown(MavenProject project) throws IOException, ClassNotFoundException {
139 File file = new File(unicornMetaLocation, "compileState.dat");
140 Map dataStore = null;
141 if (isModuleTerminal(project) && file.exists()) {
142 try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) {
143 dataStore = HashMap.class.cast(ois.readObject());
144 dataStore.put("shutdownTime", (System.currentTimeMillis() / 1000) * 1000 + snapshotBuildNumber);
145 dataStore.put("version", project.getVersion());
147 try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) {
148 oos.writeObject(dataStore);
150 Files.copy(file.toPath(),
151 Paths.get(tempLocation.getAbsolutePath(), file.getName() + HYPHEN + project.getVersion()),
152 StandardCopyOption.REPLACE_EXISTING);