f5bbbd605a4f4e008ee0be35fa88e97038863eb3
[sdc.git] /
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 import org.apache.maven.artifact.repository.ArtifactRepository;
39 import org.apache.maven.execution.MavenSession;
40 import org.apache.maven.project.MavenProject;
41
42 public class ArtifactHelper {
43
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 = "-";
53
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);
58     }
59
60     private static void setUnicornMetaLocation(File file) {
61         unicornMetaLocation = file;
62     }
63
64     static void setSnapshotBuildNumber(int number) {
65         snapshotBuildNumber = number;
66     }
67
68     List<ArtifactRepository> getRepositories(boolean snapshotRepo) {
69         List<ArtifactRepository> list = new ArrayList<>();
70         for (ArtifactRepository artRepo : project.getRemoteArtifactRepositories()) {
71             if (snapshotRepo) {
72                 if (artRepo.getSnapshots().isEnabled()) {
73                     list.add(artRepo);
74                 }
75             } else {
76                 if (artRepo.getReleases().isEnabled()) {
77                     list.add(artRepo);
78                 }
79             }
80         }
81         return list;
82     }
83
84     private void setTerminalModuleCoordinates(MavenProject project) {
85         terminalModuleCoordinates.add(getModuleCoordinate(project));
86     }
87
88     private boolean isModuleTerminal(MavenProject project) {
89         return terminalModuleCoordinates.contains(getModuleCoordinate(project));
90     }
91
92     File getUnicornMetaLocation() {
93         return unicornMetaLocation;
94     }
95
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() : "";
99         }
100     }
101
102     void store(String artifactId, byte[] data) {
103         store.put(artifactId, data);
104     }
105
106     void deleteAll(File f) {
107         if (!f.exists() && !f.isDirectory()) {
108             return;
109         }
110         for (File file : f.listFiles()) {
111             if (f.isFile()) {
112                 file.delete();
113             }
114         }
115     }
116
117     String getModuleCoordinate(MavenProject project) {
118         return project.getGroupId() + ":" + project.getArtifactId();
119     }
120
121     private File getUnicornRootFile(String moduleCoordinate, MavenProject proj) {
122         return getStateFile(moduleCoordinate, proj, unicornRoot);
123     }
124
125     private File getStateFile(String moduleCoordinate, MavenProject proj, String filePath) {
126         return new File(getTopParentProject(moduleCoordinate, proj).getBasedir(),
127                 filePath.substring(filePath.indexOf('/') + 1));
128     }
129
130     MavenProject getTopParentProject(String moduleCoordinate, MavenProject proj) {
131         if (getModuleCoordinate(proj).equals(moduleCoordinate) || proj.getParent() == null) {
132             return proj;
133         } else {
134             return getTopParentProject(moduleCoordinate, proj.getParent());
135         }
136     }
137
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());
146             }
147             try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) {
148                 oos.writeObject(dataStore);
149             }
150             Files.copy(file.toPath(),
151                     Paths.get(tempLocation.getAbsolutePath(), file.getName() + HYPHEN + project.getVersion()),
152                     StandardCopyOption.REPLACE_EXISTING);
153         }
154     }
155 }
156
157