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;
53 void init(String terminalModuleCoordinate) {
54 setUnicornMetaLocation(getUnicornRootFile(unicornRoot.substring(0, unicornRoot.indexOf('/')), project));
55 setTerminalModuleCoordinates(session.getProjects().get(session.getProjects().size() - 1));
56 terminalModuleCoordinates.add(terminalModuleCoordinate);
59 private static void setUnicornMetaLocation(File file) {
60 unicornMetaLocation = file;
63 static void setSnapshotBuildNumber(int number) {
64 snapshotBuildNumber = number;
67 List<ArtifactRepository> getRepositories(boolean snapshotRepo) {
68 List<ArtifactRepository> list = new ArrayList<>();
69 for (ArtifactRepository artRepo : project.getRemoteArtifactRepositories()) {
71 if (artRepo.getSnapshots().isEnabled()) {
75 if (artRepo.getReleases().isEnabled()) {
83 private void setTerminalModuleCoordinates(MavenProject project) {
84 terminalModuleCoordinates.add(getModuleCoordinate(project));
87 private boolean isModuleTerminal(MavenProject project) {
88 return terminalModuleCoordinates.contains(getModuleCoordinate(project));
91 File getUnicornMetaLocation() {
92 return unicornMetaLocation;
95 String getContents(URL path) throws IOException {
96 try (InputStream is = path.openStream(); Scanner scnr = new Scanner(is).useDelimiter("\\A")) {
97 return scnr.hasNext() ? scnr.next() : "";
101 void store(String artifactId, byte[] data) {
102 store.put(artifactId, data);
105 void deleteAll(File f) {
106 if (!f.exists() && !f.isDirectory()) {
109 for (File file : f.listFiles()) {
116 String getModuleCoordinate(MavenProject project) {
117 return project.getGroupId() + ":" + project.getArtifactId();
120 private File getUnicornRootFile(String moduleCoordinate, MavenProject proj) {
121 return getStateFile(moduleCoordinate, proj, unicornRoot);
124 private File getStateFile(String moduleCoordinate, MavenProject proj, String filePath) {
125 return new File(getTopParentProject(moduleCoordinate, proj).getBasedir(),
126 filePath.substring(filePath.indexOf('/') + 1));
129 MavenProject getTopParentProject(String moduleCoordinate, MavenProject proj) {
130 if (getModuleCoordinate(proj).equals(moduleCoordinate) || proj.getParent() == null) {
133 return getTopParentProject(moduleCoordinate, proj.getParent());
137 void shutDown(MavenProject project) throws IOException, ClassNotFoundException {
138 File file = new File(unicornMetaLocation, "compileState.dat");
139 Map dataStore = null;
140 if (isModuleTerminal(project) && file.exists()) {
141 try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) {
142 dataStore = HashMap.class.cast(ois.readObject());
143 dataStore.put("shutdownTime", (System.currentTimeMillis() / 1000) * 1000 + snapshotBuildNumber);
145 try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) {
146 oos.writeObject(dataStore);
148 Files.copy(file.toPath(), Paths.get(tempLocation.getAbsolutePath(), file.getName()),
149 StandardCopyOption.REPLACE_EXISTING);