Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup tasks #415

Merged
merged 7 commits into from
Oct 21, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.jenkinsci.remoting.util.AnonymousClassWarnings;

import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
Expand Down Expand Up @@ -37,7 +36,7 @@ public abstract class AbstractByteArrayCommandTransport extends CommandTransport
*
* In this subtype, we pass in {@link ByteArrayReceiver} that uses byte[] instead of {@link Command}
*/
public abstract void setup(@Nonnull ByteArrayReceiver receiver);
public abstract void setup(ByteArrayReceiver receiver);

public interface ByteArrayReceiver {
/**
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/hudson/remoting/CallableDecoratorList.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ <V> java.util.concurrent.Callable<V> wrapCallable(java.util.concurrent.Callable<
}

private <V> java.util.concurrent.Callable<V> applyDecorator(final java.util.concurrent.Callable<V> inner, final CallableDecorator filter) {
return new java.util.concurrent.Callable<V>() {
@Override
public V call() throws Exception {
return filter.call(inner);
}
};
return () -> filter.call(inner);
}

<V,T extends Throwable> Callable<V,T> wrapUserRequest(final Callable<V,T> c) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/ChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public boolean isRemoteClassLoadingAllowed() {
* @since 3.12 {@code null} parameter value is deprecated.
* {@link #withoutJarCache()} or {@link #withJarCacheOrDefault(JarCache)} should be used instead.
*/
public ChannelBuilder withJarCache(@Nonnull JarCache jarCache) {
public ChannelBuilder withJarCache(JarCache jarCache) {
this.jarCache = jarCache;
return this;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public ChannelBuilder withRoles(Role... roles) {
public ChannelBuilder withRoles(final Collection<? extends Role> actual) {
return withRoleChecker(new RoleChecker() {
@Override
public void check(RoleSensitive subject, @Nonnull Collection<Role> expected) {
public void check(@Nonnull RoleSensitive subject, @Nonnull Collection<Role> expected) {
if (!actual.containsAll(expected)) {
Collection<Role> c = new ArrayList<>(expected);
c.removeAll(actual);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/hudson/remoting/Checksum.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
Expand Down Expand Up @@ -93,11 +94,11 @@ public void write(int b) {
}

@Override
public void write(byte[] b) {
public void write(@Nonnull byte[] b) {
}

@Override
public void write(byte[] b, int off, int len) {
public void write(@Nonnull byte[] b, int off, int len) {
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/hudson/remoting/ChunkedInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.remoting;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -38,7 +39,7 @@ public int read() throws IOException {
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
public int read(@Nonnull byte[] b, int off, int len) throws IOException {
if (nextPayload()) return -1;

len = Math.min(remaining,len);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/hudson/remoting/ChunkedOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.remoting;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.OutputStream;

Expand Down Expand Up @@ -44,7 +45,7 @@ public void write(int b) throws IOException {
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
public void write(@Nonnull byte[] b, int off, int len) throws IOException {
while (len>0) {
int s = Math.min(capacity(),len);
System.arraycopy(b,off,buf,size,s);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/hudson/remoting/ClassicCommandTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ public final Command read() throws IOException, ClassNotFoundException {
if (rawIn!=null)
rawIn.clear();
return cmd;
} catch (RuntimeException e) {// see JENKINS-19046
throw diagnoseStreamCorruption(e);
} catch (StreamCorruptedException e) {
} catch (RuntimeException | StreamCorruptedException e) {// see JENKINS-19046
throw diagnoseStreamCorruption(e);
}
}
Expand All @@ -87,7 +85,7 @@ public final Command read() throws IOException, ClassNotFoundException {
* To diagnose stream corruption, we'll try to read ahead the data.
* This operation can block, so we'll use another thread to do this.
*/
private StreamCorruptedException diagnoseStreamCorruption(Exception e) throws StreamCorruptedException {
private StreamCorruptedException diagnoseStreamCorruption(Exception e) {
if (rawIn==null) {// no source of diagnostics information. can't diagnose.
if (e instanceof StreamCorruptedException)
return (StreamCorruptedException)e;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/hudson/remoting/DaemonThreadFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.remoting;

import javax.annotation.Nonnull;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -11,7 +12,7 @@ public class DaemonThreadFactory implements ThreadFactory {
private static final Logger LOGGER = Logger.getLogger(DaemonThreadFactory.class.getName());

@Override
public Thread newThread(Runnable r) {
public Thread newThread(@Nonnull Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setUncaughtExceptionHandler((t, e) -> LOGGER.log(Level.SEVERE, "Unhandled exception in thread " + t, e));
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/hudson/remoting/DelegatingExecutorService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.remoting;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
Expand All @@ -24,6 +25,7 @@ public void shutdown() {
}

@Override
@Nonnull
public List<Runnable> shutdownNow() {
return base.shutdownNow();
}
Expand All @@ -39,36 +41,42 @@ public boolean isTerminated() {
}

@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
public boolean awaitTermination(long timeout, @Nonnull TimeUnit unit) throws InterruptedException {
return base.awaitTermination(timeout, unit);
}

@Override
@Nonnull
public <T> Future<T> submit(Callable<T> task) {
return base.submit(task);
}

@Override
@Nonnull
public <T> Future<T> submit(Runnable task, T result) {
return base.submit(task, result);
}

@Override
@Nonnull
public Future<?> submit(Runnable task) {
return base.submit(task);
}

@Override
@Nonnull
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return base.invokeAll(tasks);
}

@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
@Nonnull
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, @Nonnull TimeUnit unit) throws InterruptedException {
return base.invokeAll(tasks, timeout, unit);
}

@Override
@Nonnull
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return base.invokeAny(tasks);
}
Expand Down
48 changes: 18 additions & 30 deletions src/main/java/hudson/remoting/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -112,7 +113,7 @@ public class Engine extends Thread {
private final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
@Override
public Thread newThread(final Runnable r) {
public Thread newThread(@Nonnull final Runnable r) {
Thread thread = defaultFactory.newThread(() -> {
CURRENT.set(Engine.this);
r.run();
Expand Down Expand Up @@ -223,7 +224,6 @@ public Thread newThread(final Runnable r) {
* (e.g. if a filesystem mount gets disconnected).
* @since 3.8
*/
@Nonnull
public boolean failIfWorkDirIsMissing = WorkDirManager.DEFAULT_FAIL_IF_WORKDIR_IS_MISSING;

private DelegatingX509ExtendedTrustManager agentTrustManager = new DelegatingX509ExtendedTrustManager(new BlindTrustX509ExtendedTrustManager());
Expand Down Expand Up @@ -502,11 +502,7 @@ public void run() {
}
try {
kmf.init(store, password);
} catch (KeyStoreException e) {
throw new IllegalStateException(e);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (UnrecoverableKeyException e) {
} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) {
throw new IllegalStateException(e);
}
try {
Expand Down Expand Up @@ -873,18 +869,15 @@ static KeyStore getCacertsKeyStore()
throws PrivilegedActionException, KeyStoreException, NoSuchProviderException, CertificateException,
NoSuchAlgorithmException, IOException {
Map<String, String> properties = AccessController.doPrivileged(
new PrivilegedExceptionAction<Map<String, String>>() {
@Override
public Map<String, String> run() throws Exception {
Map<String, String> result = new HashMap<>();
result.put("trustStore", System.getProperty("javax.net.ssl.trustStore"));
result.put("javaHome", System.getProperty("java.home"));
result.put("trustStoreType",
System.getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType()));
result.put("trustStoreProvider", System.getProperty("javax.net.ssl.trustStoreProvider", ""));
result.put("trustStorePasswd", System.getProperty("javax.net.ssl.trustStorePassword", ""));
return result;
}
(PrivilegedExceptionAction<Map<String, String>>) () -> {
Map<String, String> result = new HashMap<>();
result.put("trustStore", System.getProperty("javax.net.ssl.trustStore"));
result.put("javaHome", System.getProperty("java.home"));
result.put("trustStoreType",
System.getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType()));
result.put("trustStoreProvider", System.getProperty("javax.net.ssl.trustStoreProvider", ""));
result.put("trustStorePasswd", System.getProperty("javax.net.ssl.trustStorePassword", ""));
return result;
});
KeyStore keystore = null;

Expand Down Expand Up @@ -939,9 +932,7 @@ public Map<String, String> run() throws Exception {

keystore.load(trustStoreStream, trustStorePasswdChars);
if (trustStorePasswdChars != null) {
for (int i = 0; i < trustStorePasswdChars.length; ++i) {
trustStorePasswdChars[i] = 0;
}
Arrays.fill(trustStorePasswdChars, (char) 0);
}
}
} finally {
Expand All @@ -955,14 +946,11 @@ public Map<String, String> run() throws Exception {

@CheckForNull
private static FileInputStream getFileInputStream(final File file) throws PrivilegedActionException {
return AccessController.doPrivileged(new PrivilegedExceptionAction<FileInputStream>() {
@Override
public FileInputStream run() throws Exception {
try {
return file.exists() ? new FileInputStream(file) : null;
} catch (FileNotFoundException e) {
return null;
}
return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) () -> {
try {
return file.exists() ? new FileInputStream(file) : null;
} catch (FileNotFoundException e) {
return null;
}
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/hudson/remoting/FastPipedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package hudson.remoting;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -148,15 +149,15 @@ public int read() throws IOException {
}

@Override
public int read(byte[] b) throws IOException {
public int read(@Nonnull byte[] b) throws IOException {
return read(b, 0, b.length);
}

/**
* @exception IOException The pipe is not connected.
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
public int read(@Nonnull byte[] b, int off, int len) throws IOException {
if(source == null) {
throw new IOException("Unconnected pipe");
}
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/hudson/remoting/FastPipedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package hudson.remoting;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
Expand All @@ -40,12 +41,6 @@ public class FastPipedOutputStream extends OutputStream implements ErrorPropagat

WeakReference<FastPipedInputStream> sink;

/**
* Keeps track of the total # of bytes written via this output stream.
* Helps with debugging, and serves no other purpose.
*/
private long written=0;

private final Throwable allocatedAt = new Throwable();

/**
Expand Down Expand Up @@ -136,15 +131,15 @@ public void write(int b) throws IOException {
}

@Override
public void write(byte[] b) throws IOException {
public void write(@Nonnull byte[] b) throws IOException {
write(b, 0, b.length);
}

/**
* @exception IOException The pipe is not connected or a reader has closed it.
*/
@Override
public void write(byte[] b, int off, int len) throws IOException {
public void write(@Nonnull byte[] b, int off, int len) throws IOException {
if(sink == null) {
throw new IOException("Unconnected pipe");
}
Expand All @@ -164,7 +159,6 @@ public void write(byte[] b, int off, int len) throws IOException {
// release a reference to 's' during the wait so that if the reader has abandoned the pipe
// we can tell.
byte[] buf = s.buffer;
s = null;

Thread t = Thread.currentThread();
String oldName = t.getName();
Expand Down Expand Up @@ -195,7 +189,6 @@ public void write(byte[] b, int off, int len) throws IOException {

off += amount;
len -= amount;
written += amount;

s.buffer.notifyAll();
}
Expand Down
Loading