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