summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java
blob: 958505752491ef30938e3d950eef212e684bf01c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright © 2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on a "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdc.onboarding.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Proxy;

@Mojo(name = "init-artifact-helper", threadSafe = true, defaultPhase = LifecyclePhase.PRE_CLEAN,
        requiresDependencyResolution = ResolutionScope.NONE)
public class InitializationHelperMojo extends AbstractMojo {

    private static final String UNICORN_INITIALIZED = "unicorn_initialized";
    private static final String HTTP = "http";
    private static final String HTTPS = "https";
    private static final String SNAPSHOT = "SNAPSHOT";
    private static final String DOT = ".";

    @Parameter(defaultValue = "${session}")
    private MavenSession session;
    @Parameter(defaultValue = "${project}", readonly = true)
    private MavenProject project;
    @Parameter
    private String groupId;
    @Parameter
    private String artifactId;
    @Parameter
    private String version;
    @Parameter
    private String excludePackaging;
    @Parameter
    private ArtifactHelper artifactHelper;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (System.getProperties().containsKey(UNICORN_INITIALIZED)) {
            try {
                artifactHelper.shutDown(project);
            } catch (IOException | ClassNotFoundException e) {
                throw new MojoExecutionException("Unexpected Error Occured during shutdown activities", e);
            }
            return;
        }
        artifactHelper.init(groupId + ":" + artifactId);
        artifactHelper.deleteAll(artifactHelper.getUnicornMetaLocation());
        String resolvedVersion =
                getResolvedVersion(artifactHelper.getRepositories(version.contains(SNAPSHOT)), artifactId);
        getLog().info(resolvedVersion.equals(version) ? "Unicorn Initialization Failed!!!" :
                              "Unicorn Initialization Completed Successfully!!!");
        System.getProperties().setProperty(UNICORN_INITIALIZED, Boolean.TRUE.toString());
    }

    private String getResolvedVersion(List<ArtifactRepository> list, String artifactId) {
        Pattern timestampPattern = Pattern.compile(".*<timestamp>(.*)</timestamp>.*");
        Pattern buildNumberPattern = Pattern.compile(".*<buildNumber>(.*)</buildNumber>.*");

        String timestamp = null;
        String buildNumber = null;
        for (ArtifactRepository repo : list) {
            try {
                URL url = new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version
                                          + "/maven-metadata.xml");
                URL fallbackUrl =
                        new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/');
                setProxy(url);
                String content = artifactHelper.getContents(url);
                Matcher m = timestampPattern.matcher(content);
                if (m.find()) {
                    timestamp = m.group(1);
                }
                m = buildNumberPattern.matcher(content);
                if (m.find()) {
                    buildNumber = m.group(1);
                }
                timestamp = verifyBuildTimestamp(buildNumber, timestamp, fallbackUrl);
                if (timestamp != null && buildNumber != null) {
                    byte[] data = fetchContents(repo.getUrl(), artifactId, timestamp + "-" + buildNumber);
                    artifactHelper.store(artifactId, data);
                    getLog().info(artifactId + " Version to be copied is " + timestamp + "-" + buildNumber);
                    ArtifactHelper.setSnapshotBuildNumber(Integer.parseInt(buildNumber));
                    return timestamp + "-" + buildNumber;
                }
            } catch (IOException e) {
                getLog().debug(e);
            }
        }
        return version;
    }

    private String verifyBuildTimestamp(String buildNumber, String timestamp, URL fallbackUrl) throws IOException {
        if (buildNumber == null) {
            return timestamp;
        }
        String buildPage = artifactHelper.getContents(fallbackUrl);
        Pattern verifyPattern = Pattern.compile(
                ".*" + artifactId + "-" + version.replace(SNAPSHOT, "") + "(.*)" + "-" + buildNumber + ".jar</a>.*");
        Matcher m = verifyPattern.matcher(buildPage);
        if (m.find()) {
            String str = m.group(1);
            if (!str.equals(timestamp)) {
                return str;
            }
        }
        return timestamp;
    }

    private byte[] fetchContents(String repoUrl, String artifactId, String resolvedVersion) throws IOException {
        File location = Paths.get(project.getBuild().getDirectory(), "build-data").toFile();
        location.mkdirs();
        File file = new File(location, artifactId + "-" + (version.equals(resolvedVersion) ? version :
                                                                   version.replace(SNAPSHOT, resolvedVersion)) + DOT
                                               + "jar");
        URL path = new URL(repoUrl + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/' + artifactId
                                   + "-" + (version.equals(resolvedVersion) ? version :
                                                    version.replace(SNAPSHOT, resolvedVersion)) + DOT + "jar");
        try (InputStream is = path.openStream()) {
            Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
        byte[] data = Files.readAllBytes(file.toPath());
        try {
            addJarToClasspath(file);
        } catch (Exception e) {
            getLog().error("Error while feeding the build-data into system.", e);
        }

        return data;
    }

    private void setProxy(URL url) {
        if (url.getProtocol().equalsIgnoreCase(HTTP)) {
            setProperties("http.proxyHost", "http.proxyPort", "http.nonProxyHosts", HTTP);
        } else if (url.getProtocol().equalsIgnoreCase(HTTPS)) {
            setProperties("https.proxyHost", "https.proxyPort", "https.nonProxyHosts", HTTPS);
        }
    }

    private void setProperties(String proxyHostProperty, String proxyPortProperty, String nonProxyHostsProperty,
            String protocol) {
        for (Proxy proxy : session.getSettings().getProxies()) {
            if (proxy.isActive() && proxy.getProtocol().equalsIgnoreCase(protocol)) {
                if (proxy.getHost() != null && !proxy.getHost().trim().isEmpty()) {
                    System.setProperty(proxyHostProperty, proxy.getHost());
                    System.setProperty(proxyPortProperty, String.valueOf(proxy.getPort()));
                }
                if (proxy.getNonProxyHosts() != null && !proxy.getNonProxyHosts().trim().isEmpty()) {
                    System.setProperty(nonProxyHostsProperty, proxy.getNonProxyHosts());
                }
            }
        }
    }

    public void addJarToClasspath(File jar) throws MojoFailureException {
        try {
            ClassLoader cl = ClassLoader.getSystemClassLoader();
            Class<?> clazz = cl.getClass();

            Method method = clazz.getSuperclass().getDeclaredMethod("addURL", new Class[] {URL.class});

            method.setAccessible(true);
            method.invoke(cl, new Object[] {jar.toURI().toURL()});
        } catch (Exception e) {
            throw new MojoFailureException("Problem while loadig build-data", e);
        }
    }

}