diff --git a/src/main/java/org/codehaus/mojo/wagon/ListMojo.java b/src/main/java/org/codehaus/mojo/wagon/ListMojo.java index 74b73f0..e5ad48a 100644 --- a/src/main/java/org/codehaus/mojo/wagon/ListMojo.java +++ b/src/main/java/org/codehaus/mojo/wagon/ListMojo.java @@ -14,10 +14,8 @@ public class ListMojo extends AbstractWagonListMojo { @Override protected void execute(Wagon wagon) throws WagonException { - List files = wagonDownload.getFileList(wagon, this.getWagonFileSet(), this.getLog()); - - for (Object file1 : files) { - String file = (String) file1; + List files = wagonDownload.getFileList(wagon, this.getWagonFileSet(), this.getLog()); + for (String file : files) { getLog().info("\t" + file); } } diff --git a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultMavenRepoMerger.java b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultMavenRepoMerger.java index 8197dd5..3e25242 100644 --- a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultMavenRepoMerger.java +++ b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultMavenRepoMerger.java @@ -20,13 +20,13 @@ */ import java.io.File; -import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.Writer; +import java.nio.file.Files; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -163,19 +163,13 @@ private void mergeMetadata(File existingMetadata, Log logger) throws IOException private String checksum(File file, String type) throws IOException, NoSuchAlgorithmException { MessageDigest md5 = MessageDigest.getInstance(type); - - InputStream is = new FileInputStream(file); - - byte[] buf = new byte[8192]; - - int i; - - while ((i = is.read(buf)) > 0) { - md5.update(buf, 0, i); + try (InputStream is = Files.newInputStream(file.toPath())) { + byte[] buf = new byte[8192]; + int i; + while ((i = is.read(buf)) > 0) { + md5.update(buf, 0, i); + } } - - IOUtil.close(is); - return encode(md5.digest()); } @@ -185,34 +179,27 @@ private String encode(byte[] binaryData) { throw new IllegalArgumentException("Unrecognised length for binary data: " + bitLength + " bits"); } - String retValue = ""; - + StringBuilder retValue = new StringBuilder(); for (byte aBinaryData : binaryData) { String t = Integer.toHexString(aBinaryData & 0xff); - if (t.length() == 1) { - retValue += ("0" + t); + retValue.append("0").append(t); } else { - retValue += t; + retValue.append(t); } } - return retValue.trim(); + return retValue.toString().trim(); } public static File createTempDirectory(String prefix) throws IOException { - final File temp; - - temp = File.createTempFile(prefix, Long.toString(System.nanoTime())); - - if (!(temp.delete())) { + File temp = File.createTempFile(prefix, Long.toString(System.nanoTime())); + if (!temp.delete()) { throw new IOException("Could not delete temp file: " + temp.getAbsolutePath()); } - - if (!(temp.mkdir())) { + if (!temp.mkdir()) { throw new IOException("Could not create temp directory: " + temp.getAbsolutePath()); } - - return (temp); + return temp; } } diff --git a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonDownload.java b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonDownload.java index a926ddd..35d5a94 100644 --- a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonDownload.java +++ b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonDownload.java @@ -32,7 +32,7 @@ public class DefaultWagonDownload implements WagonDownload { @Override - public List getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException { + public List getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException { logger.info("Scanning remote file system: " + wagon.getRepository().getUrl() + " ..."); WagonDirectoryScanner dirScan = new WagonDirectoryScanner(); @@ -53,18 +53,16 @@ public List getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws Wa @Override public void download(Wagon wagon, WagonFileSet remoteFileSet, Log logger) throws WagonException { - List fileList = this.getFileList(wagon, remoteFileSet, logger); + List fileList = this.getFileList(wagon, remoteFileSet, logger); String url = wagon.getRepository().getUrl() + "/"; - if (fileList.size() == 0) { + if (fileList.isEmpty()) { logger.info("Nothing to download."); return; } - for (Object aFileList : fileList) { - String remoteFile = (String) aFileList; - + for (String remoteFile : fileList) { File destination = new File(remoteFileSet.getDownloadDirectory() + "/" + remoteFile); destination.getParentFile().mkdirs(); diff --git a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonFactory.java b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonFactory.java index 23edb9a..ac6fb6e 100644 --- a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonFactory.java +++ b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonFactory.java @@ -186,7 +186,7 @@ private Wagon configureWagon(Wagon wagon, String repositoryId, Settings settings return wagon; } - private Wagon configureWagon(Wagon wagon, String repositoryId, Server server) throws TransferFailedException { + private void configureWagon(Wagon wagon, String repositoryId, Server server) throws TransferFailedException { final PlexusConfiguration plexusConf = new XmlPlexusConfiguration((Xpp3Dom) server.getConfiguration()); try { if (!(componentConfigurator instanceof BasicComponentConfigurator)) { @@ -196,9 +196,8 @@ private Wagon configureWagon(Wagon wagon, String repositoryId, Server server) th wagon, plexusConf, (ClassRealm) this.getClass().getClassLoader()); } catch (ComponentConfigurationException e) { throw new TransferFailedException( - "While configuring wagon for \'" + repositoryId + "\': Unable to apply wagon configuration.", e); + "While configuring wagon for '" + repositoryId + "': Unable to apply wagon configuration.", e); } - return wagon; } public AuthenticationInfo getAuthenticationInfo(String id, Settings settings) { diff --git a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonUpload.java b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonUpload.java index d27ccae..e7eb73d 100644 --- a/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonUpload.java +++ b/src/main/java/org/codehaus/mojo/wagon/shared/DefaultWagonUpload.java @@ -69,7 +69,7 @@ public void upload(Wagon wagon, FileSet fileset) throws WagonException { File source = new File(fileset.getDirectory(), file); - LOG.info("Uploading " + source + " to " + url + relativeDestPath + " ..."); + LOG.info("Uploading {} to {}{} ...", source, url, relativeDestPath); wagon.put(source, relativeDestPath); } @@ -87,11 +87,9 @@ public void upload(Wagon wagon, FileSet fileset, boolean optimize) throws WagonE "Wagon " + wagon.getRepository().getProtocol() + " does not support optimize upload"); } - LOG.info("Uploading " + fileset); - - File zipFile; - zipFile = File.createTempFile("wagon", ".zip"); + LOG.info("Uploading {}", fileset); + File zipFile = File.createTempFile("wagon", ".zip"); try { FileSetManager fileSetManager = new FileSetManager(LOG, LOG.isDebugEnabled()); String[] files = fileSetManager.getIncludedFiles(fileset); @@ -101,7 +99,7 @@ public void upload(Wagon wagon, FileSet fileset, boolean optimize) throws WagonE return; } - LOG.info("Creating " + zipFile + " ..."); + LOG.info("Creating {} ...", zipFile); createZip(files, zipFile, fileset.getDirectory()); String remoteFile = zipFile.getName(); @@ -110,7 +108,7 @@ public void upload(Wagon wagon, FileSet fileset, boolean optimize) throws WagonE remoteFile = remoteDir + "/" + remoteFile; } - LOG.info("Uploading " + zipFile + " to " + wagon.getRepository().getUrl() + "/" + remoteFile + " ..."); + LOG.info("Uploading {} to {}/{} ...", zipFile, wagon.getRepository().getUrl(), remoteFile); wagon.put(zipFile, remoteFile); // We use the super quiet option here as all the noise seems to kill/stall the connection @@ -120,11 +118,11 @@ public void upload(Wagon wagon, FileSet fileset, boolean optimize) throws WagonE } try { - LOG.info("Remote: " + command); + LOG.info("Remote: {}", command); ((CommandExecutor) wagon).executeCommand(command); } finally { command = "rm -f " + remoteFile; - LOG.info("Remote: " + command); + LOG.info("Remote: {}", command); ((CommandExecutor) wagon).executeCommand(command); } diff --git a/src/main/java/org/codehaus/mojo/wagon/shared/SelectorUtils.java b/src/main/java/org/codehaus/mojo/wagon/shared/SelectorUtils.java index 0d25a1c..7c83072 100644 --- a/src/main/java/org/codehaus/mojo/wagon/shared/SelectorUtils.java +++ b/src/main/java/org/codehaus/mojo/wagon/shared/SelectorUtils.java @@ -19,8 +19,9 @@ * under the License. */ +import java.util.ArrayList; +import java.util.List; import java.util.StringTokenizer; -import java.util.Vector; /** * A copy of plexus-util's SelectorUtils to deal with unix file separator only. @@ -61,8 +62,8 @@ public static boolean matchPatternStart(String pattern, String str, boolean isCa return false; } - Vector patDirs = tokenizePath(pattern); - Vector strDirs = tokenizePath(str); + List patDirs = tokenizePath(pattern); + List strDirs = tokenizePath(str); int patIdxStart = 0; int patIdxEnd = patDirs.size() - 1; @@ -71,11 +72,11 @@ public static boolean matchPatternStart(String pattern, String str, boolean isCa // up to first '**' while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { - String patDir = (String) patDirs.elementAt(patIdxStart); + String patDir = patDirs.get(patIdxStart); if (patDir.equals("**")) { break; } - if (!match(patDir, (String) strDirs.elementAt(strIdxStart), isCaseSensitive)) { + if (!match(patDir, strDirs.get(strIdxStart), isCaseSensitive)) { return false; } patIdxStart++; @@ -123,8 +124,8 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit return false; } - Vector patDirs = tokenizePath(pattern); - Vector strDirs = tokenizePath(str); + List patDirs = tokenizePath(pattern); + List strDirs = tokenizePath(str); int patIdxStart = 0; int patIdxEnd = patDirs.size() - 1; @@ -133,11 +134,11 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit // up to first '**' while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { - String patDir = (String) patDirs.elementAt(patIdxStart); + String patDir = patDirs.get(patIdxStart); if (patDir.equals("**")) { break; } - if (!match(patDir, (String) strDirs.elementAt(strIdxStart), isCaseSensitive)) { + if (!match(patDir, strDirs.get(strIdxStart), isCaseSensitive)) { return false; } patIdxStart++; @@ -146,7 +147,7 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit if (strIdxStart > strIdxEnd) { // String is exhausted for (int i = patIdxStart; i <= patIdxEnd; i++) { - if (!patDirs.elementAt(i).equals("**")) { + if (!patDirs.get(i).equals("**")) { return false; } } @@ -160,11 +161,11 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit // up to last '**' while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { - String patDir = (String) patDirs.elementAt(patIdxEnd); + String patDir = patDirs.get(patIdxEnd); if (patDir.equals("**")) { break; } - if (!match(patDir, (String) strDirs.elementAt(strIdxEnd), isCaseSensitive)) { + if (!match(patDir, strDirs.get(strIdxEnd), isCaseSensitive)) { return false; } patIdxEnd--; @@ -173,7 +174,7 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit if (strIdxStart > strIdxEnd) { // String is exhausted for (int i = patIdxStart; i <= patIdxEnd; i++) { - if (!patDirs.elementAt(i).equals("**")) { + if (!patDirs.get(i).equals("**")) { return false; } } @@ -183,7 +184,7 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { int patIdxTmp = -1; for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { - if (patDirs.elementAt(i).equals("**")) { + if (patDirs.get(i).equals("**")) { patIdxTmp = i; break; } @@ -201,8 +202,8 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit strLoop: for (int i = 0; i <= strLength - patLength; i++) { for (int j = 0; j < patLength; j++) { - String subPat = (String) patDirs.elementAt(patIdxStart + j + 1); - String subStr = (String) strDirs.elementAt(strIdxStart + i + j); + String subPat = patDirs.get(patIdxStart + j + 1); + String subStr = strDirs.get(strIdxStart + i + j); if (!match(subPat, subStr, isCaseSensitive)) { continue strLoop; } @@ -221,7 +222,7 @@ public static boolean matchPath(String pattern, String str, boolean isCaseSensit } for (int i = patIdxStart; i <= patIdxEnd; i++) { - if (!patDirs.elementAt(i).equals("**")) { + if (!patDirs.get(i).equals("**")) { return false; } } @@ -385,10 +386,8 @@ private static boolean equals(char c1, char c2, boolean isCaseSensitive) { } if (!isCaseSensitive) { // NOTE: Try both upper case and lower case as done by String.equalsIgnoreCase() - if (Character.toUpperCase(c1) == Character.toUpperCase(c2) - || Character.toLowerCase(c1) == Character.toLowerCase(c2)) { - return true; - } + return Character.toUpperCase(c1) == Character.toUpperCase(c2) + || Character.toLowerCase(c1) == Character.toLowerCase(c2); } return false; } @@ -399,11 +398,11 @@ private static boolean equals(char c1, char c2, boolean isCaseSensitive) { * @param path Path to tokenize. Must not be null. * @return a Vector of path elements from the tokenized path */ - public static Vector tokenizePath(String path) { - Vector ret = new Vector(); + public static List tokenizePath(String path) { + List ret = new ArrayList<>(); StringTokenizer st = new StringTokenizer(path, "/"); while (st.hasMoreTokens()) { - ret.addElement(st.nextToken()); + ret.add(st.nextToken()); } return ret; } diff --git a/src/main/java/org/codehaus/mojo/wagon/shared/WagonDirectoryScanner.java b/src/main/java/org/codehaus/mojo/wagon/shared/WagonDirectoryScanner.java index 7ffdd12..903cfd6 100644 --- a/src/main/java/org/codehaus/mojo/wagon/shared/WagonDirectoryScanner.java +++ b/src/main/java/org/codehaus/mojo/wagon/shared/WagonDirectoryScanner.java @@ -60,7 +60,7 @@ public class WagonDirectoryScanner { /** * The files which matched at least one include and at least one exclude and relative to directory */ - private List filesIncluded = new ArrayList(); + private List filesIncluded = new ArrayList<>(); private Log logger; @@ -207,7 +207,7 @@ public void scan() throws WagonException { excludes = new String[0]; } - filesIncluded = new ArrayList(); + filesIncluded = new ArrayList<>(); scandir(directory, ""); @@ -250,11 +250,7 @@ private boolean isRidiculousFile(String fileName) { */ private void scandir(String dir, String vpath) throws WagonException { logger.debug("scandir: dir: " + dir + " vpath: " + vpath); - List files = wagon.getFileList(dir); - - for (Object file1 : files) { - String fileName = (String) file1; - + for (String fileName : wagon.getFileList(dir)) { if (isRidiculousFile(fileName)) // including ".." { continue; @@ -313,7 +309,7 @@ private boolean isDirectory(String existedRemotePath) { private boolean canListPath(String remotePath) { try { - List resources = wagon.getFileList(remotePath); + List resources = wagon.getFileList(remotePath); return resources != null && !resources.isEmpty(); } catch (WagonException e) { return false; @@ -321,7 +317,7 @@ private boolean canListPath(String remotePath) { } // /////////////////////////////////////////////////////////////////////////////// - public List getFilesIncluded() { + public List getFilesIncluded() { return filesIncluded; } diff --git a/src/main/java/org/codehaus/mojo/wagon/shared/WagonDownload.java b/src/main/java/org/codehaus/mojo/wagon/shared/WagonDownload.java index 502bda9..376deb5 100644 --- a/src/main/java/org/codehaus/mojo/wagon/shared/WagonDownload.java +++ b/src/main/java/org/codehaus/mojo/wagon/shared/WagonDownload.java @@ -37,7 +37,7 @@ public interface WagonDownload { * @return a list of files at the remote host relative to RemoteFileSet's directory * @throws WagonException if any wagon error */ - List getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException; + List getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException; /** * @param wagon - a Wagon instance