Skip to content

Commit

Permalink
[JENKINS-72113] Relax serialization of BoundObjectTable.Table (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe authored Oct 9, 2023
1 parent 892bddc commit d0fb746
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions core/src/main/java/org/kohsuke/stapler/bind/BoundObjectTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -112,7 +113,7 @@ public Table getTable() {
/**
* Per-session table that remembers all the bound instances.
*/
public static class Table {
public static class Table implements Serializable {
private final Map<String,Ref> entries = new HashMap<>();
private boolean logging;

Expand Down Expand Up @@ -224,7 +225,7 @@ public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object nod
/**
* Reference that resolves to an object.
*/
interface Ref {
interface Ref extends Serializable {
Object get();
}

Expand All @@ -237,12 +238,29 @@ private static class StrongRef implements Ref {
public Object get() {
return o;
}
private Object writeReplace() {
if (o instanceof Serializable) {
return this;
} else {
LOGGER.fine(() -> "Refusing to serialize " + o);
return new StrongRef(null);
}
}
}

private static class WeakRef extends WeakReference implements Ref {
private WeakRef(Object referent) {
super(referent);
}
private Object writeReplace() {
Object o = get();
if (o instanceof Serializable) {
return this;
} else {
LOGGER.fine(() -> "Refusing to serialize " + o);
return new WeakRef(null);
}
}
}

public static final String PREFIX = "/$stapler/bound/";
Expand Down

0 comments on commit d0fb746

Please sign in to comment.