Skip to content

Commit

Permalink
fix: do lazy initialization in a thread-safe way (#10456)
Browse files Browse the repository at this point in the history
fixes #9323
  • Loading branch information
Denis authored and vaadin-bot committed Mar 29, 2021
1 parent 573fec3 commit a6801c4
Showing 1 changed file with 12 additions and 14 deletions.
Expand Up @@ -20,6 +20,7 @@
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -66,15 +67,13 @@
public class UidlRequestHandler extends SynchronizedRequestHandler
implements SessionExpiredHandler {

private AtomicReference<ServerRpcHandler> rpcHandler = new AtomicReference<>();

private ServerRpcHandler rpcHandler;

public static final Pattern HASH_PATTERN = Pattern.compile("window.location.hash ?= ?'(.*?)'");
public static final Pattern HASH_PATTERN = Pattern
.compile("window.location.hash ?= ?'(.*?)'");
public static final Pattern URL_PATTERN = Pattern.compile("^(.*)#(.+)$");
public static final String PUSH_STATE_HASH =
"setTimeout(() => history.pushState(null, null, location.pathname + location.search + '#%s'));";
public static final String PUSH_STATE_LOCATION =
"setTimeout(() => history.pushState(null, null, '%s'));";
public static final String PUSH_STATE_HASH = "setTimeout(() => history.pushState(null, null, location.pathname + location.search + '#%s'));";
public static final String PUSH_STATE_LOCATION = "setTimeout(() => history.pushState(null, null, '%s'));";

private static final String SYNC_ID = '"' + SERVER_SYNC_ID + '"';
private static final String RPC = RPC_INVOCATIONS;
Expand Down Expand Up @@ -141,8 +140,7 @@ private void writeRefresh(VaadinResponse response) throws IOException {
commitJsonResponse(response, json);
}

void writeUidl(UI ui, Writer writer, boolean resync)
throws IOException {
void writeUidl(UI ui, Writer writer, boolean resync) throws IOException {
JsonObject uidl = createUidl(ui, resync);

if (ui instanceof JavaScriptBootstrapUI) {
Expand Down Expand Up @@ -184,11 +182,12 @@ public boolean handleSessionExpired(VaadinRequest request,
}

private ServerRpcHandler getRpcHandler(VaadinSession session) {
session.checkHasLock();
if (rpcHandler == null) {
rpcHandler = createRpcHandler();
ServerRpcHandler handler = rpcHandler.get();
if (handler == null) {
rpcHandler.compareAndSet(null, createRpcHandler());
handler = rpcHandler.get();
}
return rpcHandler;
return handler;
}

/**
Expand Down Expand Up @@ -330,4 +329,3 @@ private String removeHashInRpc(JsonArray rpc) {
return null;
}
}

0 comments on commit a6801c4

Please sign in to comment.