22ba506c48d930969c9d18ca1d4558c13e244442
[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.IOException;
21 import java.net.URL;
22 import java.util.List;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.execution.MavenSession;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.plugins.annotations.ResolutionScope;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.settings.Proxy;
36
37 @Mojo(name = "init-artifact-helper", threadSafe = true, defaultPhase = LifecyclePhase.PRE_CLEAN,
38         requiresDependencyResolution = ResolutionScope.NONE)
39 public class InitializationHelperMojo extends AbstractMojo {
40
41     private static final String SKIP_GET = "skipGet";
42     private static final String HTTP = "http";
43     private static final String HTTPS = "https";
44
45     @Parameter(defaultValue = "${session}")
46     private MavenSession session;
47     @Parameter(defaultValue = "${project}", readonly = true)
48     private MavenProject project;
49     @Parameter
50     private String groupId;
51     @Parameter
52     private String artifactId;
53     @Parameter
54     private String version;
55     @Parameter
56     private String targetLocation;
57     @Parameter
58     private String name;
59     @Parameter
60     private String excludePackaging;
61     @Parameter
62     private ArtifactHelper artifactHelper;
63
64     @Override
65     public void execute() throws MojoExecutionException, MojoFailureException {
66         if (System.getProperties().containsKey(SKIP_GET)) {
67             project.getProperties()
68                    .setProperty(SKIP_GET, Boolean.toString(System.getProperties().containsKey(SKIP_GET)));
69             return;
70         } else {
71             File orgFile = new File(
72                     session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator))
73                             + File.separator + artifactId + File.separator + version);
74             String resolvedVersion = getResolvedVersion(artifactHelper.getRepositories(version.contains("SNAPSHOT")));
75             project.getProperties().setProperty("resolvedVersion", resolvedVersion);
76             System.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString());
77             if (resolvedVersion.equals(version) && !orgFile.exists()) {
78                 project.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString());
79             }
80         }
81     }
82
83     private String getResolvedVersion(List<ArtifactRepository> list) {
84         Pattern timestampPattern = Pattern.compile(".*<timestamp>(.*)</timestamp>.*");
85         Pattern buildNumberPattern = Pattern.compile(".*<buildNumber>(.*)</buildNumber>.*");
86         String timestamp = null;
87         String buildNumber = null;
88         for (ArtifactRepository repo : list) {
89             try {
90                 URL url = new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version
91                                           + "/maven-metadata.xml");
92                 setProxy(url);
93                 String content = artifactHelper.getContents(url);
94                 Matcher m = timestampPattern.matcher(content);
95                 if (m.find()) {
96                     timestamp = m.group(1);
97                 }
98                 m = buildNumberPattern.matcher(content);
99                 if (m.find()) {
100                     buildNumber = m.group(1);
101                 }
102             } catch (IOException e) {
103                 getLog().debug(e);
104             }
105             if (timestamp != null && buildNumber != null) {
106                 return timestamp + "-" + buildNumber;
107             }
108         }
109         return version;
110     }
111
112     private void setProxy(URL url) {
113         if (url.getProtocol().equalsIgnoreCase(HTTP)) {
114             setProperties("http.proxyHost", "http.proxyPort", "http.nonProxyHosts", HTTP);
115         } else if (url.getProtocol().equalsIgnoreCase(HTTPS)) {
116             setProperties("https.proxyHost", "https.proxyPort", "https.nonProxyHosts", HTTPS);
117         }
118     }
119
120     private void setProperties(String proxyHostProperty, String proxyPortProperty, String nonProxyHostsProperty,
121             String protocol) {
122         for (Proxy proxy : session.getSettings().getProxies()) {
123             if (proxy.isActive() && proxy.getProtocol().equalsIgnoreCase(protocol)) {
124                 System.setProperty(proxyHostProperty, proxy.getHost());
125                 System.setProperty(proxyPortProperty, String.valueOf(proxy.getPort()));
126                 System.setProperty(nonProxyHostsProperty, proxy.getNonProxyHosts());
127             }
128         }
129     }
130 }