Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-43507] Make it easier to manage the scope of connections wit…
…h the scope of the request
  • Loading branch information
stephenc committed May 4, 2017
1 parent a1fa90b commit a55f35b
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/main/java/jenkins/scm/api/trait/SCMSourceRequest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +73,8 @@ public abstract class SCMSourceRequest implements Closeable {

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

0 comments on commit a55f35b

Please sign in to comment.