Skip to content
Permalink
Browse files

[JENKINS-43507] Make it easier to manage the scope of connections wit…

…h the scope of the request
  • Loading branch information...
stephenc committed Apr 26, 2017
1 parent a1fa90b commit a55f35bbc5e3226b18940aae8ebc88341a6df1f7
Showing with 40 additions and 1 deletion.
  1. +40 −1 src/main/java/jenkins/scm/api/trait/SCMSourceRequest.java
@@ -31,6 +31,8 @@
import hudson.model.TaskListener;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -71,6 +73,8 @@

private final Set<SCMHead> observerIncludes;

private final List<Closeable> managedClosables = new ArrayList<Closeable>();

protected SCMSourceRequest(SCMSourceRequestBuilder<?, ?> builder, TaskListener listener) {
this.source = builder.source();
this.filters = Collections.unmodifiableList(new ArrayList<SCMHeadFilter>(builder.filters()));
@@ -225,12 +229,47 @@ public TaskListener listener() {
return listener;
}

/**
* Adds managing a {@link Closeable} into the scope of the {@link SCMSourceRequest}
*
* @param closeable the {@link Closeable} to manage.
*/
public void manage(@CheckForNull Closeable closeable) {
if (closeable != null) {
managedClosables.add(closeable);
}
}

/**
* {@inheritDoc}
*/
@Override
public void close() throws IOException {
// default to no-op but allow subclasses to store persistent connections in the request and clean up after
IOException ioe = null;
for (Closeable c : managedClosables) {
try {
c.close();
} catch (IOException e) {
if (ioe == null) {
ioe = e;
} else {
// TODO replace with direct call to addSuppressed once baseline Java is 1.7
try {
Method addSuppressed = Throwable.class.getMethod("addSuppressed", Throwable.class);
addSuppressed.invoke(ioe, e);
} catch (NoSuchMethodException e1) {
// ignore, best effort
} catch (IllegalAccessException e1) {
// ignore, best effort
} catch (InvocationTargetException e1) {
// ignore, best effort
}
}
}
}
if (ioe != null) {
throw ioe;
}
}

public SCMSource source() {

0 comments on commit a55f35b

Please sign in to comment.
You can’t perform that action at this time.