11diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspServerTelemetryManager.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspServerTelemetryManager.java
2- index d82646afb1..b008279cc4 100644
2+ index d82646afb1..7f4c88acd1 100644
33--- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspServerTelemetryManager.java
44+++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspServerTelemetryManager.java
55@@ -21,6 +21,7 @@ package org.netbeans.modules.java.lsp.server.protocol;
@@ -10,7 +10,7 @@ index d82646afb1..b008279cc4 100644
1010 import java.math.BigInteger;
1111 import java.nio.charset.StandardCharsets;
1212 import java.security.MessageDigest;
13- @@ -28,25 +29,29 @@ import java.security.NoSuchAlgorithmException;
13+ @@ -28,25 +29,30 @@ import java.security.NoSuchAlgorithmException;
1414 import java.util.ArrayList;
1515 import java.util.Collection;
1616 import java.util.Collections;
@@ -22,6 +22,7 @@ index d82646afb1..b008279cc4 100644
2222+ import java.util.NavigableMap;
2323+ import java.util.TreeMap;
2424 import java.util.WeakHashMap;
25+ + import java.util.concurrent.CompletableFuture;
2526 import java.util.concurrent.Future;
2627- import java.util.concurrent.atomic.AtomicBoolean;
2728+ import java.util.function.Function;
@@ -44,7 +45,7 @@ index d82646afb1..b008279cc4 100644
4445 import org.openide.util.Lookup;
4546
4647 /**
47- @@ -55,130 +60,200 @@ import org.openide.util.Lookup;
48+ @@ -55,130 +61,205 @@ import org.openide.util.Lookup;
4849 */
4950 public class LspServerTelemetryManager {
5051
@@ -85,11 +86,11 @@ index d82646afb1..b008279cc4 100644
8586+
8687+ private static final LspServerTelemetryManager instance = new LspServerTelemetryManager();
8788+ }
88- +
89+
8990+ private final WeakHashMap<LanguageClient, WeakReference<Future<Void>>> clients = new WeakHashMap<>();
9091+ private volatile boolean telemetryEnabled = false;
9192+ private long lspServerIntializationTime;
92-
93+ +
9394+ public boolean isTelemetryEnabled() {
9495+ return telemetryEnabled;
9596+ }
@@ -172,9 +173,10 @@ index d82646afb1..b008279cc4 100644
172173+ return close == null || close.isDone();
173174+ }
174175+
175- + public void sendWorkspaceInfo(LanguageClient client, List<FileObject> workspaceClientFolders, Collection<Project> projects, long timeToOpenProjects) {
176+ + public CompletableFuture<Void> sendWorkspaceInfo(LanguageClient client, List<FileObject> workspaceClientFolders, Collection<Project> projects, long timeToOpenProjects) {
176177 JsonObject properties = new JsonObject();
177- JsonArray prjProps = new JsonArray();
178+ - JsonArray prjProps = new JsonArray();
179+ + List<CompletableFuture<JsonObject>> createProjectFutures = new ArrayList<>();
178180
179181- Map<String, Project> mp = prjs.stream()
180182- .collect(Collectors.toMap(project -> project.getProjectDirectory().getPath(), project -> project));
@@ -215,14 +217,14 @@ index d82646afb1..b008279cc4 100644
215217+ String projectPath = p.getKey();
216218+ if (prjPathWithSlash == null) {
217219+ if (prjPath.equals(projectPath)) {
218- + prjProps .add(createProjectInfo(prjPath, p.getValue(), workspaceFolder, client));
220+ + createProjectFutures .add(createProjectInfo(prjPath, p.getValue(), workspaceFolder, client));
219221+ noProjectFound = false;
220222+ break;
221223+ }
222224+ prjPathWithSlash = prjPath + '/';
223225+ }
224226+ if (projectPath.startsWith(prjPathWithSlash)) {
225- + prjProps .add(createProjectInfo(p.getKey(), p.getValue(), workspaceFolder, client));
227+ + createProjectFutures .add(createProjectInfo(p.getKey(), p.getValue(), workspaceFolder, client));
226228+ noProjectFound = false;
227229+ continue;
228230+ }
@@ -235,7 +237,7 @@ index d82646afb1..b008279cc4 100644
235237- Exceptions.printStackTrace(ex);
236238+ if (noProjectFound) {
237239+ // No project found
238- + prjProps .add(createProjectInfo(prjPath, null, workspaceFolder, client));
240+ + createProjectFutures .add(createProjectInfo(prjPath, null, workspaceFolder, client));
239241+ }
240242+ } catch (NoSuchAlgorithmException e) {
241243+ LOG.log(Level.INFO, "NoSuchAlgorithmException while creating workspaceInfo event: {0}", e.getMessage());
@@ -245,24 +247,25 @@ index d82646afb1..b008279cc4 100644
245247 }
246248
247249- properties.add("prjsInfo", prjProps);
248- + properties.add("projectInfo", prjProps);
250+ + return CompletableFuture.allOf(createProjectFutures.toArray(new CompletableFuture[0]))
251+ + .thenApply((ignored) -> {
252+ + JsonArray prjProps = new JsonArray();
253+ + createProjectFutures.forEach((f) -> prjProps.add(f.join()));
254+ + return prjProps;
255+ + })
256+ + .thenAccept((prjProps) -> {
257+ + properties.add("projectInfo", prjProps);
258+ + properties.addProperty("projInitTimeTaken", timeToOpenProjects);
259+ + properties.addProperty("numProjects", workspaceClientFolders.size());
260+ + properties.addProperty("lspInitTimeTaken", System.currentTimeMillis() - this.lspServerIntializationTime);
261+ + this.sendTelemetry(client, new TelemetryEvent(MessageType.Info.toString(), LspServerTelemetryManager.WORKSPACE_INFO_EVT, properties));
262+ + });
263+ + }
249264
250265- properties.addProperty("timeToOpenPrjs", timeToOpenPrjs);
251266- properties.addProperty("numOfPrjsOpened", workspaceClientFolders.size());
252267- properties.addProperty("lspServerInitializationTime", System.currentTimeMillis() - this.lspServerIntiailizationTime);
253- + properties.addProperty("projInitTimeTaken", timeToOpenProjects);
254- + properties.addProperty("numProjects", workspaceClientFolders.size());
255- + properties.addProperty("lspInitTimeTaken", System.currentTimeMillis() - this.lspServerIntializationTime);
256-
257- - this.sendTelemetry(client, new TelemetryEvent(MessageType.Info.toString(), this.WORKSPACE_INFO_EVT, properties));
258- + this.sendTelemetry(client, new TelemetryEvent(MessageType.Info.toString(), LspServerTelemetryManager.WORKSPACE_INFO_EVT, properties));
259- }
260- -
261- - private boolean isEnablePreivew(FileObject source, String prjType) {
262- - if (prjType.equals(this.STANDALONE_PRJ)) {
263- - NbCodeLanguageClient client = Lookup.getDefault().lookup(NbCodeLanguageClient.class);
264- +
265- + private JsonObject createProjectInfo(String prjPath, Project prj, FileObject workspaceFolder, LanguageClient client) throws NoSuchAlgorithmException {
268+ + private CompletableFuture<JsonObject> createProjectInfo(String prjPath, Project prj, FileObject workspaceFolder, LanguageClient client) throws NoSuchAlgorithmException {
266269+ JsonObject obj = new JsonObject();
267270+ String prjId = getPrjId(prjPath);
268271+ obj.addProperty("id", prjId);
@@ -286,27 +289,32 @@ index d82646afb1..b008279cc4 100644
286289+ String javaVersion = getProjectJavaVersion();
287290+ obj.addProperty("javaVersion", javaVersion);
288291+ obj.addProperty("buildTool", projectType.name());
289- + boolean isPreviewFlagEnabled = isPreviewEnabled(projectDirectory, projectType, client);
290- + obj.addProperty("isPreviewEnabled", isPreviewFlagEnabled);
291- + return obj;
292+ + return isPreviewEnabled(projectDirectory, projectType, client).thenApply(isPreviewFlagEnabled -> {
293+ + obj.addProperty("isPreviewEnabled", isPreviewFlagEnabled);
294+ + return obj;
295+ + });
292296+ }
293- +
294- + public boolean isPreviewEnabled(FileObject source, ProjectType prjType) {
297+
298+ - this.sendTelemetry(client, new TelemetryEvent(MessageType.Info.toString(), this.WORKSPACE_INFO_EVT, properties));
299+ + public CompletableFuture<Boolean> isPreviewEnabled(FileObject source, ProjectType prjType) {
295300+ return isPreviewEnabled(source, prjType, null);
296- + }
301+ }
302+ -
303+ - private boolean isEnablePreivew(FileObject source, String prjType) {
304+ - if (prjType.equals(this.STANDALONE_PRJ)) {
305+ - NbCodeLanguageClient client = Lookup.getDefault().lookup(NbCodeLanguageClient.class);
297306+
298- + public boolean isPreviewEnabled(FileObject source, ProjectType prjType, LanguageClient languageClient) {
307+ + public CompletableFuture<Boolean> isPreviewEnabled(FileObject source, ProjectType prjType, LanguageClient languageClient) {
299308+ if (prjType == ProjectType.standalone) {
300- + NbCodeLanguageClient client = languageClient instanceof NbCodeLanguageClient ? (NbCodeLanguageClient) languageClient : null ;
309+ + NbCodeLanguageClient client = languageClient instanceof NbCodeLanguageClient ? (NbCodeLanguageClient) languageClient : null;
301310 if (client == null) {
302311- return false;
303312+ client = Lookup.getDefault().lookup(NbCodeLanguageClient.class);
304313+ if (client == null) {
305- + return false;
314+ + return CompletableFuture.completedFuture( false) ;
306315+ }
307316 }
308317- AtomicBoolean isEnablePreviewSet = new AtomicBoolean(false);
309- + boolean[] isEnablePreviewSet = {false};
310318 ConfigurationItem conf = new ConfigurationItem();
311319- conf.setSection(client.getNbCodeCapabilities().getAltConfigurationPrefix() + "runConfig.vmOptions");
312320- client.configuration(new ConfigurationParams(Collections.singletonList(conf))).thenAccept(c -> {
@@ -316,22 +324,21 @@ index d82646afb1..b008279cc4 100644
316324-
317325- return isEnablePreviewSet.get();
318326+ conf.setSection(client.getNbCodeCapabilities().getConfigurationPrefix() + "runConfig.vmOptions");
319- + client.configuration(new ConfigurationParams(Collections.singletonList(conf)))
320- + .thenAccept (c -> {
321- + isEnablePreviewSet[0] = c != null && !c.isEmpty()
327+ + return client.configuration(new ConfigurationParams(Collections.singletonList(conf)))
328+ + .thenApply (c -> {
329+ + return c != null && !c.isEmpty()
322330+ && ((JsonPrimitive) c.get(0)).getAsString().contains(ENABLE_PREVIEW);
323331+ });
324- + return isEnablePreviewSet[0];
325332 }
326333-
327334+
328335 Result result = CompilerOptionsQuery.getOptions(source);
329336- return result.getArguments().contains(this.ENABLE_PREVIEW);
330- + return result.getArguments().contains(ENABLE_PREVIEW);
337+ + return CompletableFuture.completedFuture( result.getArguments().contains(ENABLE_PREVIEW) );
331338 }
332339
333340 private String getPrjId(String prjPath) throws NoSuchAlgorithmException {
334- @@ -187,15 +262 ,50 @@ public class LspServerTelemetryManager {
341+ @@ -187,15 +268 ,50 @@ public class LspServerTelemetryManager {
335342
336343 BigInteger number = new BigInteger(1, hash);
337344
0 commit comments