Skip to content

Commit

Permalink
Modernize source code up to Java 8 (#362)
Browse files Browse the repository at this point in the history
* Modernize source code up to Java 8

This performs some automated refactoring of code to use language and library updates.

Signed-off-by: Matt Sicker <[email protected]>

* Remove unnecessary casts

Co-Authored-By: Oleg Nenashev <[email protected]>

Co-authored-by: Oleg Nenashev <[email protected]>
Co-authored-by: Jeff Thompson <[email protected]>
  • Loading branch information
3 people committed Dec 27, 2019
1 parent 946602b commit ab8a2c2
Show file tree
Hide file tree
Showing 38 changed files with 318 additions and 309 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package hudson.remoting;

import org.jenkinsci.remoting.util.AnonymousClassWarnings;

import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import org.jenkinsci.remoting.util.AnonymousClassWarnings;

/**
* {@link CommandTransport} that works with {@code byte[]} instead of command object.
Expand Down Expand Up @@ -38,7 +39,7 @@ public abstract class AbstractByteArrayCommandTransport extends CommandTransport
*/
public abstract void setup(@Nonnull ByteArrayReceiver receiver);

public static interface ByteArrayReceiver {
public interface ByteArrayReceiver {
/**
* Notifies the {@link Channel} that the transport has received a new block.
*
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/hudson/remoting/BinarySafeStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
*/
package hudson.remoting;

import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FilterOutputStream;
import java.io.UnsupportedEncodingException;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
Expand Down Expand Up @@ -315,11 +315,7 @@ public void flush() throws IOException {
private static final int[] DECODING_TABLE = new int[128];

static {
try {
ENCODING_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
ENCODING_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(StandardCharsets.US_ASCII);

Arrays.fill(DECODING_TABLE,-2);
for (int i = 0; i < ENCODING_TABLE.length; i++)
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/hudson/remoting/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@
import org.jenkinsci.remoting.CallableDecorator;
import org.jenkinsci.remoting.RoleChecker;
import org.jenkinsci.remoting.nio.NioChannelHub;
import org.jenkinsci.remoting.util.LoggingChannelListener;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Date;
import java.util.Hashtable;
Expand All @@ -63,10 +67,6 @@
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import org.jenkinsci.remoting.util.LoggingChannelListener;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* Represents a communication channel to the remote peer.
Expand Down Expand Up @@ -389,11 +389,7 @@ public enum Mode {
/*package*/ final byte[] preamble;

Mode(String preamble) {
try {
this.preamble = preamble.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
this.preamble = preamble.getBytes(StandardCharsets.US_ASCII);
}

Mode(byte[] preamble) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/CommandTransport.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package hudson.remoting;

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import javax.annotation.CheckForNull;

/**
* Lower level abstraction under {@link Channel} for sending and receiving commands
Expand Down Expand Up @@ -58,7 +58,7 @@ protected CommandTransport() {
* SPI implemented by {@link Channel} so that the transport can pass the received command
* to {@link Channel} for processing.
*/
protected static interface CommandReceiver {
protected interface CommandReceiver {
/**
* Notifies the channel that a new {@link Command} was received from the other side.
*
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/remoting/FastPipedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/
package hudson.remoting;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;

/**
Expand Down Expand Up @@ -78,7 +78,7 @@ public FastPipedOutputStream(FastPipedInputStream sink, int bufferSize) throws I

private FastPipedInputStream sink() throws IOException {
FastPipedInputStream s = sink.get();
if (s==null) throw (IOException)new IOException("Reader side has already been abandoned").initCause(allocatedAt);
if (s==null) throw new IOException("Reader side has already been abandoned", allocatedAt);
return s;
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public void write(byte[] b, int off, int len) throws IOException {
FastPipedInputStream s = sink(); // make sure the sink is still trying to read, or else fail the write.

if(s.closed!=null) {
throw (IOException)new IOException("Pipe is already closed").initCause(s.closed);
throw new IOException("Pipe is already closed", s.closed);
}

synchronized(s.buffer) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/FileSystemJarCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public FileSystemJarCache(@Nonnull File rootDir, boolean touch) {

@Override
public String toString() {
return String.format("FileSystem JAR Cache: path=%s, touch=%s", rootDir, Boolean.toString(touch));
return String.format("FileSystem JAR Cache: path=%s, touch=%s", rootDir, touch);
}

@Override
Expand Down Expand Up @@ -149,7 +149,7 @@ protected URL retrieve(Channel channel, long sum1, long sum2) throws IOException
Files.deleteIfExists(PathUtils.fileToPath(tmp));
}
} catch (IOException e) {
throw (IOException)new IOException("Failed to write to "+target).initCause(e);
throw new IOException("Failed to write to "+target, e);
}
}

Expand Down
97 changes: 45 additions & 52 deletions src/main/java/hudson/remoting/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,76 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.remoting.Channel.Mode;

import java.io.Console;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchProviderException;
import java.security.PrivilegedActionException;
import java.security.cert.CertificateFactory;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import org.jenkinsci.remoting.engine.WorkDirManager;
import org.jenkinsci.remoting.util.IOUtils;
import org.jenkinsci.remoting.util.PathUtils;
import org.jenkinsci.remoting.util.https.NoCheckHostnameVerifier;
import org.jenkinsci.remoting.util.https.NoCheckTrustManager;
import org.jenkinsci.remoting.util.PathUtils;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.CmdLineException;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.InetSocketAddress;
import java.net.HttpURLConnection;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivilegedActionException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.security.cert.CertificateException;
import java.security.NoSuchAlgorithmException;
import java.security.KeyManagementException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
* Entry point for running a {@link Channel}. This is the main method of the agent JVM.
Expand Down Expand Up @@ -461,11 +459,7 @@ private SSLSocketFactory getSSLSocketFactory()
continue;
}
} else {
try {
cert = certOrAtFilename.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("US-ASCII support is mandated by the JLS", e);
}
cert = certOrAtFilename.getBytes(StandardCharsets.US_ASCII);
}
try {
keyStore.setCertificateEntry(String.format("alias-%d", i++),
Expand Down Expand Up @@ -508,11 +502,11 @@ public List<String> parseJnlpArguments() throws ParserConfigurationException, SA
HttpURLConnection http = (HttpURLConnection) con;
if (slaveJnlpCredentials != null) {
String userPassword = slaveJnlpCredentials;
String encoding = Base64.getEncoder().encodeToString(userPassword.getBytes("UTF-8"));
String encoding = Base64.getEncoder().encodeToString(userPassword.getBytes(StandardCharsets.UTF_8));
http.setRequestProperty("Authorization", "Basic " + encoding);
}
if (System.getProperty("proxyCredentials", proxyCredentials) != null) {
String encoding = Base64.getEncoder().encodeToString(System.getProperty("proxyCredentials", proxyCredentials).getBytes("UTF-8"));
String encoding = Base64.getEncoder().encodeToString(System.getProperty("proxyCredentials", proxyCredentials).getBytes(StandardCharsets.UTF_8));
http.setRequestProperty("Proxy-Authorization", "Basic " + encoding);
}
}
Expand Down Expand Up @@ -543,7 +537,7 @@ public List<String> parseJnlpArguments() throws ParserConfigurationException, SA
byte[] decrypted = cipher.doFinal(payload,16,payload.length-16);
input = new ByteArrayInputStream(decrypted);
} catch (GeneralSecurityException x) {
throw (IOException)new IOException("Failed to decrypt the JNLP file. Invalid secret key?").initCause(x);
throw new IOException("Failed to decrypt the JNLP file. Invalid secret key?", x);
}
}
if(contentType==null || !contentType.startsWith(expectedContentType)) {
Expand Down Expand Up @@ -574,14 +568,13 @@ public List<String> parseJnlpArguments() throws ParserConfigurationException, SA
} catch (SSLHandshakeException e) {
if(e.getMessage().contains("PKIX path building failed")) {
// invalid SSL certificate. One reason this happens is when the certificate is self-signed
IOException x = new IOException("Failed to validate a server certificate. If you are using a self-signed certificate, you can use the -noCertificateCheck option to bypass this check.");
x.initCause(e);
IOException x = new IOException("Failed to validate a server certificate. If you are using a self-signed certificate, you can use the -noCertificateCheck option to bypass this check.", e);
throw x;
} else
throw e;
} catch (IOException e) {
if (this.noReconnect)
throw (IOException)new IOException("Failing to obtain "+slaveJnlpURL).initCause(e);
throw new IOException("Failing to obtain " + slaveJnlpURL, e);

System.err.println("Failing to obtain "+slaveJnlpURL);
e.printStackTrace(System.err);
Expand Down
Loading

0 comments on commit ab8a2c2

Please sign in to comment.