Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Jun 16, 2024
1 parent ad3a0c6 commit 38819d8
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 84 deletions.
6 changes: 2 additions & 4 deletions src/main/java/org/codehaus/mojo/wagon/ListMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> files = wagonDownload.getFileList(wagon, this.getWagonFileSet(), this.getLog());
for (String file : files) {
getLog().info("\t" + file);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class DefaultWagonDownload implements WagonDownload {

@Override
public List getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException {
public List<String> getFileList(Wagon wagon, WagonFileSet fileSet, Log logger) throws WagonException {
logger.info("Scanning remote file system: " + wagon.getRepository().getUrl() + " ...");

WagonDirectoryScanner dirScan = new WagonDirectoryScanner();
Expand All @@ -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<String> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
47 changes: 23 additions & 24 deletions src/main/java/org/codehaus/mojo/wagon/shared/SelectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<String> patDirs = tokenizePath(pattern);
List<String> strDirs = tokenizePath(str);

int patIdxStart = 0;
int patIdxEnd = patDirs.size() - 1;
Expand All @@ -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++;
Expand Down Expand Up @@ -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<String> patDirs = tokenizePath(pattern);
List<String> strDirs = tokenizePath(str);

int patIdxStart = 0;
int patIdxEnd = patDirs.size() - 1;
Expand All @@ -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++;
Expand All @@ -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;
}
}
Expand All @@ -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--;
Expand All @@ -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;
}
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -399,11 +398,11 @@ private static boolean equals(char c1, char c2, boolean isCaseSensitive) {
* @param path Path to tokenize. Must not be <code>null</code>.
* @return a Vector of path elements from the tokenized path
*/
public static Vector tokenizePath(String path) {
Vector ret = new Vector();
public static List<String> tokenizePath(String path) {
List<String> ret = new ArrayList<>();
StringTokenizer st = new StringTokenizer(path, "/");
while (st.hasMoreTokens()) {
ret.addElement(st.nextToken());
ret.add(st.nextToken());
}
return ret;
}
Expand Down
Loading

0 comments on commit 38819d8

Please sign in to comment.