diff options
author | Gautam Shah <gautams@amdocs.com> | 2018-06-01 08:38:32 +0530 |
---|---|---|
committer | GAUTAMS <gautams@amdocs.com> | 2018-06-04 13:53:51 +0530 |
commit | 03205dad582ffa5db54187f451b9a29d3d0af386 (patch) | |
tree | 21e67f2a40764fe1b1904e58cd775c0041528352 /openecomp-be/tools/artifact-copy-plugin/src/main/java/org | |
parent | 06b1f35479a40194c6d84c38cc1215ae76b81ba6 (diff) |
Fixing Merge Build issue and Sonar issues.
fixing build issue and Sonar issues.
Change-Id: I4209b79ab2a3646839df50be4b58f8f230b6d710
Issue-ID: SDC-1189
Signed-off-by: GAUTAMS <gautams@amdocs.com>
Diffstat (limited to 'openecomp-be/tools/artifact-copy-plugin/src/main/java/org')
4 files changed, 203 insertions, 223 deletions
diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java index e7ad5dacb1..f51a8aae60 100644 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java @@ -1,21 +1,68 @@ +/* + * 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.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Scanner; +import java.util.Set; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; public class ArtifactHelper { private MavenProject project; + private MavenSession session; + private static Map<String, byte[]> store = new HashMap<>(); + private static Set<String> terminalModuleCoordinates = new HashSet<>(); + private String unicornRoot = null; + private static File unicornMetaLocation = null; + private File tempLocation = Paths.get(System.getProperties().getProperty("java.io.tmpdir")).toFile(); + private static int snapshotBuildNumber = 0; + + void init(String terminalModuleCoordinate) { + setUnicornMetaLocation(getUnicornRootFile(unicornRoot.substring(0, unicornRoot.indexOf('/')), project)); + setTerminalModuleCoordinates(session.getProjects().get(session.getProjects().size() - 1)); + terminalModuleCoordinates.add(terminalModuleCoordinate); + } + + private static void setUnicornMetaLocation(File file) { + unicornMetaLocation = file; + } + + static void setSnapshotBuildNumber(int number) { + snapshotBuildNumber = number; + } List<ArtifactRepository> getRepositories(boolean snapshotRepo) { List<ArtifactRepository> list = new ArrayList<>(); @@ -33,24 +80,75 @@ public class ArtifactHelper { return list; } + private void setTerminalModuleCoordinates(MavenProject project) { + terminalModuleCoordinates.add(getModuleCoordinate(project)); + } + + private boolean isModuleTerminal(MavenProject project) { + return terminalModuleCoordinates.contains(getModuleCoordinate(project)); + } + + File getUnicornMetaLocation() { + return unicornMetaLocation; + } + String getContents(URL path) throws IOException { try (InputStream is = path.openStream(); Scanner scnr = new Scanner(is).useDelimiter("\\A")) { return scnr.hasNext() ? scnr.next() : ""; } } - String getChecksum(String filePath, String hashType) throws NoSuchAlgorithmException, IOException { - MessageDigest md = MessageDigest.getInstance(hashType); - md.update(Files.readAllBytes(Paths.get(filePath))); - byte[] hashBytes = md.digest(); + void store(String artifactId, byte[] data) { + store.put(artifactId, data); + } - StringBuffer buffer = new StringBuffer(); - for (byte hashByte : hashBytes) { - buffer.append(Integer.toString((hashByte & 0xff) + 0x100, 16).substring(1)); + void deleteAll(File f) { + if (!f.exists() && !f.isDirectory()) { + return; } - return buffer.toString(); + for (File file : f.listFiles()) { + if (f.isFile()) { + file.delete(); + } + } + } + + String getModuleCoordinate(MavenProject project) { + return project.getGroupId() + ":" + project.getArtifactId(); + } + + private File getUnicornRootFile(String moduleCoordinate, MavenProject proj) { + return getStateFile(moduleCoordinate, proj, unicornRoot); + } + + private File getStateFile(String moduleCoordinate, MavenProject proj, String filePath) { + return new File(getTopParentProject(moduleCoordinate, proj).getBasedir(), + filePath.substring(filePath.indexOf('/') + 1)); } + MavenProject getTopParentProject(String moduleCoordinate, MavenProject proj) { + if (getModuleCoordinate(proj).equals(moduleCoordinate) || proj.getParent() == null) { + return proj; + } else { + return getTopParentProject(moduleCoordinate, proj.getParent()); + } + } + + void shutDown(MavenProject project) throws IOException, ClassNotFoundException { + File file = new File(unicornMetaLocation, "compileState.dat"); + Map dataStore = null; + if (isModuleTerminal(project) && file.exists()) { + try (InputStream is = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(is)) { + dataStore = HashMap.class.cast(ois.readObject()); + dataStore.put("shutdownTime", (System.currentTimeMillis() / 1000) * 1000 + snapshotBuildNumber); + } + try (OutputStream os = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(os)) { + oos.writeObject(dataStore); + } + Files.copy(file.toPath(), Paths.get(tempLocation.getAbsolutePath(), file.getName()), + StandardCopyOption.REPLACE_EXISTING); + } + } } diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java index 4838608835..96f29d0054 100644 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java @@ -2,10 +2,6 @@ package org.openecomp.sdc.onboarding.util; import java.io.File; import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -37,46 +33,26 @@ public class CalibrateArtifactPlugin extends AbstractMojo { @Parameter private String version; @Parameter - private String targetLocation; - @Parameter - private String name; - @Parameter private String excludePackaging; @Parameter private ArtifactHelper artifactHelper; + @Override public void execute() throws MojoExecutionException, MojoFailureException { if (project.getPackaging().equals(excludePackaging)) { return; } if (project.getProperties().containsKey(ARTIFACT_COPY_PATH) && project.getProperties().getProperty(ARTIFACT_COPY_PATH) != null) { - File f = null; - String artifactPath = project.getProperties().getProperty(ARTIFACT_COPY_PATH) - .startsWith(session.getLocalRepository().getBasedir()) ? - project.getProperties().getProperty(ARTIFACT_COPY_PATH) : - project.getProperties().getProperty(ARTIFACT_COPY_PATH) - .replace(groupId, groupId.replace('.', '/')); - if (artifactPath.startsWith(session.getLocalRepository().getBasedir())) { - f = new File(artifactPath); - } else { - f = new File(session.getLocalRepository().getBasedir(), artifactPath); - } + File f = new File(project.getProperties().getProperty(ARTIFACT_COPY_PATH)); if (f.exists()) { project.getArtifact().setFile(f); } } - File file = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".unicorn"); - if (file.exists()) { - try { - Files.copy(file.toPath(), Paths.get( - session.getLocalRepository().getBasedir() + File.separator + project.getGroupId().replace(".", - File.separator) + File.separator + project.getArtifactId() + File.separator - + project.getVersion(), project.getBuild().getFinalName() + ".unicorn"), - StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - throw new UncheckedIOException(e); - } + try { + artifactHelper.shutDown(project); + } catch (IOException | ClassNotFoundException e) { + throw new MojoExecutionException("Unexpected Error Occured during shutdown activities", e); } } } diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java deleted file mode 100644 index c89fdd2a4e..0000000000 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.nio.file.StandardOpenOption; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; -import java.util.List; -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; - -@Mojo(name = "copy-helper", threadSafe = true, defaultPhase = LifecyclePhase.CLEAN, - requiresDependencyResolution = ResolutionScope.NONE) -public class CopyArtifactPlugin extends AbstractMojo { - - @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 targetLocation; - @Parameter - private String name; - @Parameter - private ArtifactHelper artifactHelper; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - if (!project.getProperties().containsKey("resolvedVersion")) { - return; - } - boolean isSnapshot = version.contains("SNAPSHOT"); - List<ArtifactRepository> artRepoList = artifactHelper.getRepositories(isSnapshot); - String resolvedVersion = project.getProperties().getProperty("resolvedVersion"); - try { - if (!version.equals(resolvedVersion)) { - boolean result = copyResolvedArtifact(artRepoList, resolvedVersion); - if (result && getLog().isInfoEnabled()) { - getLog().info("Data Artifact Copied with " + resolvedVersion); - } - - } - File orgFile = new File( - session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) - + File.separator + artifactId + File.separator + version); - if (!orgFile.exists()) { - return; - } - File[] list = orgFile.listFiles(t -> t.getName().equals(artifactId + "-" + version + ".jar")); - if (list != null && list.length > 0) { - String directory = session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", - File.separator)) + File.separator + targetLocation + File.separator + version; - if (!Paths.get(directory, name).toFile().exists()) { - return; - } - Files.copy(list[0].toPath(), Paths.get(directory, name), StandardCopyOption.REPLACE_EXISTING); - copyTargetArtifact(directory, list[0]); - } - } catch (IOException | NoSuchAlgorithmException e) { - throw new MojoFailureException(e.getMessage(), e); - } - } - - private void copyTargetArtifact(String directory, File source) throws IOException, NoSuchAlgorithmException { - File[] files = new File(directory).listFiles( - f -> f.getName().endsWith(".jar") && !f.getName().equals(name) && f.getName().startsWith( - name.substring(0, name.lastIndexOf('-')))); - if (files == null || files.length == 0) { - return; - } - Arrays.sort(files, this::compare); - File tgtFile = files[files.length - 1]; - Files.copy(source.toPath(), tgtFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - for (String checksumType : Arrays.asList("sha1", "md5")) { - File potentialFile = new File(tgtFile.getAbsolutePath() + "." + checksumType); - if (potentialFile.exists()) { - Files.write(potentialFile.toPath(), - artifactHelper.getChecksum(source.getAbsolutePath(), checksumType).getBytes(), - StandardOpenOption.CREATE); - } - } - } - - - private boolean copyResolvedArtifact(List<ArtifactRepository> list, String resolvedVersion) { - for (ArtifactRepository repo : list) { - try { - writeContents( - new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/' - + artifactId + "-" + (version.equals(resolvedVersion) ? version : - version.replace("SNAPSHOT", resolvedVersion)) - + ".jar")); - return true; - } catch (IOException e) { - getLog().debug(e); - } - } - return false; - } - - - private void writeContents(URL path) throws IOException { - String directory = - session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) - + File.separator + artifactId + File.separator + version; - try (InputStream is = path.openStream()) { - Files.copy(is, Paths.get(directory, artifactId + "-" + version + ".jar"), - StandardCopyOption.REPLACE_EXISTING); - } - - } - - private int compare(File file1, File file2) { - if (file1.lastModified() > file2.lastModified()) { - return 1; - } - if (file1.lastModified() < file2.lastModified()) { - return -1; - } - return 0; - } - -} diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java index 22ba506c48..6fa9b0e365 100644 --- a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java @@ -18,7 +18,12 @@ 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; @@ -38,9 +43,11 @@ import org.apache.maven.settings.Proxy; requiresDependencyResolution = ResolutionScope.NONE) public class InitializationHelperMojo extends AbstractMojo { - private static final String SKIP_GET = "skipGet"; + 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; @@ -53,42 +60,36 @@ public class InitializationHelperMojo extends AbstractMojo { @Parameter private String version; @Parameter - private String targetLocation; - @Parameter - private String name; - @Parameter private String excludePackaging; @Parameter private ArtifactHelper artifactHelper; @Override public void execute() throws MojoExecutionException, MojoFailureException { - if (System.getProperties().containsKey(SKIP_GET)) { - project.getProperties() - .setProperty(SKIP_GET, Boolean.toString(System.getProperties().containsKey(SKIP_GET))); + if (System.getProperties().containsKey(UNICORN_INITIALIZED)) { return; - } else { - File orgFile = new File( - session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) - + File.separator + artifactId + File.separator + version); - String resolvedVersion = getResolvedVersion(artifactHelper.getRepositories(version.contains("SNAPSHOT"))); - project.getProperties().setProperty("resolvedVersion", resolvedVersion); - System.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString()); - if (resolvedVersion.equals(version) && !orgFile.exists()) { - project.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString()); - } } + 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) { + 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); @@ -99,16 +100,60 @@ public class InitializationHelperMojo extends AbstractMojo { 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); } - if (timestamp != null && buildNumber != null) { - return timestamp + "-" + buildNumber; - } } 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); @@ -121,10 +166,29 @@ public class InitializationHelperMojo extends AbstractMojo { String protocol) { for (Proxy proxy : session.getSettings().getProxies()) { if (proxy.isActive() && proxy.getProtocol().equalsIgnoreCase(protocol)) { - System.setProperty(proxyHostProperty, proxy.getHost()); - System.setProperty(proxyPortProperty, String.valueOf(proxy.getPort())); - System.setProperty(nonProxyHostsProperty, proxy.getNonProxyHosts()); + 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); + } + } + } |