Skip to content

Commit e6d6ad1

Browse files
authored
Merge pull request #2251 from digma-ai/register-only-one-schema-handler-factory
Stateless schema handler factory and register only one instance per jcef type Closes #2248
2 parents 05a7e22 + 6efbdf5 commit e6d6ad1

25 files changed

+285
-134
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/analytics/AnalyticsService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class AnalyticsService implements Disposable {
5959

6060
private static final Logger LOGGER = Logger.getInstance(AnalyticsService.class);
6161

62-
private final Environment environment;
62+
private Environment environment;
6363

6464
private final Project project;
6565

@@ -522,6 +522,8 @@ public HttpResponse lowLevelCall(HttpRequest request) throws AnalyticsServiceExc
522522
public void dispose() {
523523
try {
524524
analyticsProviderProxy.close();
525+
analyticsProviderProxy = null;
526+
environment = null;
525527
} catch (Exception e) {
526528
Log.warnWithException(LOGGER, project, e, "exception while closing AnalyticsProvider {}", e.getMessage());
527529
}

src/main/java/org/digma/intellij/plugin/documentation/DocumentationFileEditor.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import javax.swing.*;
1212
import java.beans.PropertyChangeListener;
1313

14+
import static org.digma.intellij.plugin.ui.jcef.JBcefBrowserPropertiesKt.JCEF_DOCUMENTATION_FILE_PROPERTY_NAME;
15+
1416
public class DocumentationFileEditor extends UserDataHolderBase implements FileEditor {
1517

16-
private final VirtualFile file;
18+
private final DocumentationVirtualFile file;
1719

1820
@Nullable
1921
private JCefComponent jCefComponent;
2022

23+
private boolean disposed = false;
2124

2225
public DocumentationFileEditor(Project project, DocumentationVirtualFile file) {
2326
this.file = file;
@@ -28,10 +31,10 @@ public DocumentationFileEditor(Project project, DocumentationVirtualFile file) {
2831
private JCefComponent createJcefComponent(Project project, DocumentationVirtualFile file) {
2932

3033
if (JBCefApp.isSupported()) {
31-
return new JCefComponent.JCefComponentBuilder(project, "Documentation", DocumentationService.getInstance(project),
34+
return new JCefComponent.JCefComponentBuilder(project, "Documentation", this,
3235
DocumentationConstants.DOCUMENTATION_URL,
33-
new DocumentationMessageRouterHandler(project),
34-
new DocumentationSchemeHandlerFactory(project, file))
36+
new DocumentationMessageRouterHandler(project))
37+
.withArg(JCEF_DOCUMENTATION_FILE_PROPERTY_NAME,file)
3538
.withDownloadAdapter(new DownloadHandlerAdapter())
3639
.build();
3740

@@ -82,7 +85,7 @@ public boolean isModified() {
8285

8386
@Override
8487
public boolean isValid() {
85-
return true;
88+
return !disposed;
8689
}
8790

8891
@Override
@@ -101,6 +104,8 @@ public void dispose() {
101104
jCefComponent.dispose();
102105
jCefComponent = null;
103106
}
107+
disposed = true;
108+
file.setValid(false);
104109
}
105110

106111
}

src/main/java/org/digma/intellij/plugin/documentation/DocumentationFileEditorProvider.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
2121
return new DocumentationFileEditor(project, (DocumentationVirtualFile) file);
2222
}
2323

24-
@Override
25-
public void disposeEditor(@NotNull FileEditor editor) {
26-
FileEditorProvider.super.disposeEditor(editor);
27-
}
28-
29-
3024
@Override
3125
public @NotNull @NonNls String getEditorTypeId() {
3226
return DOCUMENTATION_EDITOR_TYPE;

src/main/java/org/digma/intellij/plugin/documentation/DocumentationFileType.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
import com.intellij.openapi.fileTypes.ex.FakeFileType;
44
import com.intellij.openapi.util.NlsContexts;
55
import com.intellij.openapi.vfs.VirtualFile;
6-
import org.jetbrains.annotations.Nls;
7-
import org.jetbrains.annotations.NonNls;
8-
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.*;
97

10-
@SuppressWarnings("UnstableApiUsage")
118
public class DocumentationFileType extends FakeFileType {
129

1310
private DocumentationFileType() {
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package org.digma.intellij.plugin.documentation;
22

3-
import com.intellij.openapi.diagnostic.Logger;
43
import com.intellij.openapi.project.Project;
5-
import org.digma.intellij.plugin.ui.jcef.BaseResourceHandler;
4+
import org.cef.browser.CefBrowser;
5+
import org.digma.intellij.plugin.errorreporting.ErrorReporter;
6+
import org.digma.intellij.plugin.log.Log;
7+
import org.digma.intellij.plugin.ui.jcef.*;
68
import org.jetbrains.annotations.*;
79

810
import java.io.InputStream;
11+
import java.util.Collections;
912

1013
public class DocumentationResourceHandler extends BaseResourceHandler {
1114

12-
private static final Logger LOGGER = Logger.getInstance(DocumentationResourceHandler.class);
13-
14-
15-
private final Project project;
16-
private final DocumentationVirtualFile documentationVirtualFile;
17-
18-
public DocumentationResourceHandler(@NotNull Project project, @NotNull String path, @NotNull DocumentationVirtualFile file) {
19-
super(path);
20-
this.project = project;
21-
this.documentationVirtualFile = file;
15+
public DocumentationResourceHandler(@NotNull CefBrowser browser, @NotNull String path) {
16+
super(path, browser);
2217
}
2318

2419
@Override
@@ -29,6 +24,18 @@ public boolean isIndexHtml(@NotNull String path) {
2924
@Nullable
3025
@Override
3126
public InputStream buildIndexFromTemplate(@NotNull String path) {
32-
return new DocumentationIndexTemplateBuilder(documentationVirtualFile).build(project);
27+
Project project = JBcefBrowserPropertiesKt.getProject(getBrowser());
28+
if (project == null) {
29+
Log.log(getLogger()::warn, "project is null , should never happen");
30+
ErrorReporter.getInstance().reportError(null, "DocumentationResourceHandler.buildIndexFromTemplate", "project is null", Collections.emptyMap());
31+
return null;
32+
}
33+
DocumentationVirtualFile file = (DocumentationVirtualFile) JBcefBrowserPropertiesKt.getProperty(getBrowser(), JBcefBrowserPropertiesKt.JCEF_DOCUMENTATION_FILE_PROPERTY_NAME);
34+
if (file == null) {
35+
Log.log(getLogger()::warn, "DocumentationVirtualFile is null , should never happen");
36+
ErrorReporter.getInstance().reportError(null, "DocumentationResourceHandler.buildIndexFromTemplate", "DocumentationVirtualFile is null", Collections.emptyMap());
37+
return null;
38+
}
39+
return new DocumentationIndexTemplateBuilder(file).build(project);
3340
}
3441
}

src/main/java/org/digma/intellij/plugin/documentation/DocumentationSchemeHandlerFactory.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package org.digma.intellij.plugin.documentation;
22

3-
import com.intellij.openapi.project.Project;
3+
import org.cef.browser.CefBrowser;
44
import org.cef.handler.CefResourceHandler;
55
import org.digma.intellij.plugin.ui.jcef.BaseSchemeHandlerFactory;
66
import org.jetbrains.annotations.NotNull;
77

88
public class DocumentationSchemeHandlerFactory extends BaseSchemeHandlerFactory {
99

10-
private final DocumentationVirtualFile documentationVirtualFile;
11-
12-
public DocumentationSchemeHandlerFactory(Project project, DocumentationVirtualFile file) {
13-
super(project);
14-
this.documentationVirtualFile = file;
15-
}
16-
1710

1811
@NotNull
1912
@Override
20-
public CefResourceHandler createResourceHandler(@NotNull String resourceName, boolean resourceExists) {
13+
public CefResourceHandler createResourceHandler(@NotNull String resourceName, boolean resourceExists, @NotNull CefBrowser browser) {
2114
if (resourceExists) {
22-
return new DocumentationResourceHandler(getProject(), resourceName, documentationVirtualFile);
15+
return new DocumentationResourceHandler(browser, resourceName);
2316
} else {
24-
return new DocumentationResourceHandler(getProject(), DocumentationConstants.DOCUMENTATION_RESOURCE_FOLDER_NAME + "/index.html", documentationVirtualFile);
17+
return new DocumentationResourceHandler(browser, DocumentationConstants.DOCUMENTATION_RESOURCE_FOLDER_NAME + "/index.html");
2518
}
2619
}
2720

src/main/java/org/digma/intellij/plugin/jaegerui/JaegerUIFileEditor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import javax.swing.*;
1212
import java.beans.PropertyChangeListener;
1313

14+
import static org.digma.intellij.plugin.ui.jcef.JBcefBrowserPropertiesKt.JCEF_JAEGER_UI_FILE_PROPERTY_NAME;
15+
1416
public class JaegerUIFileEditor extends UserDataHolderBase implements FileEditor {
1517

1618
private final JaegerUIVirtualFile file;
@@ -31,8 +33,8 @@ private JCefComponent createJcefComponent(Project project, JaegerUIVirtualFile f
3133
if (JBCefApp.isSupported()) {
3234
return new JCefComponent.JCefComponentBuilder(project, "JaegerUI", this,
3335
JaegerUIConstants.JAEGER_UI_URL,
34-
new JaegerUIMessageRouterHandler(project),
35-
new JaegerUiSchemeHandlerFactory(project, file))
36+
new JaegerUIMessageRouterHandler(project))
37+
.withArg(JCEF_JAEGER_UI_FILE_PROPERTY_NAME, file)
3638
.withDownloadAdapter(new DownloadHandlerAdapter())
3739
.build();
3840

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package org.digma.intellij.plugin.jaegerui;
22

33
import com.intellij.openapi.project.Project;
4-
import org.digma.intellij.plugin.ui.jcef.BaseResourceHandler;
4+
import org.cef.browser.CefBrowser;
5+
import org.digma.intellij.plugin.errorreporting.ErrorReporter;
6+
import org.digma.intellij.plugin.log.Log;
7+
import org.digma.intellij.plugin.ui.jcef.*;
58
import org.jetbrains.annotations.*;
69

710
import java.io.InputStream;
11+
import java.util.Collections;
812

913
public class JaegerUiResourceHandler extends BaseResourceHandler {
1014

11-
private final Project project;
12-
private final JaegerUIVirtualFile file;
13-
14-
public JaegerUiResourceHandler(Project project, @NotNull String path, JaegerUIVirtualFile file) {
15-
super(path);
16-
this.project = project;
17-
this.file = file;
15+
public JaegerUiResourceHandler(@NotNull CefBrowser browser, @NotNull String path) {
16+
super(path,browser);
1817
}
1918

2019
@Override
@@ -25,6 +24,18 @@ public boolean isIndexHtml(@NotNull String path) {
2524
@Nullable
2625
@Override
2726
public InputStream buildIndexFromTemplate(@NotNull String path) {
27+
Project project = JBcefBrowserPropertiesKt.getProject(getBrowser());
28+
if (project == null) {
29+
Log.log(getLogger()::warn, "project is null , should never happen");
30+
ErrorReporter.getInstance().reportError(null, "JaegerUiResourceHandler.buildIndexFromTemplate", "project is null", Collections.emptyMap());
31+
return null;
32+
}
33+
JaegerUIVirtualFile file = (JaegerUIVirtualFile) JBcefBrowserPropertiesKt.getProperty(getBrowser(),JBcefBrowserPropertiesKt.JCEF_JAEGER_UI_FILE_PROPERTY_NAME);
34+
if (file == null){
35+
Log.log(getLogger()::warn, "JaegerUIVirtualFile is null , should never happen");
36+
ErrorReporter.getInstance().reportError(null, "JaegerUiResourceHandler.buildIndexFromTemplate", "JaegerUIVirtualFile is null", Collections.emptyMap());
37+
return null;
38+
}
2839
return new JaegerUiIndexTemplateBuilder(file).build(project);
2940
}
3041
}

src/main/java/org/digma/intellij/plugin/jaegerui/JaegerUiSchemeHandlerFactory.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.digma.intellij.plugin.jaegerui;
22

3-
import com.intellij.openapi.project.Project;
3+
import org.cef.browser.CefBrowser;
44
import org.cef.handler.CefResourceHandler;
55
import org.digma.intellij.plugin.ui.jcef.BaseSchemeHandlerFactory;
66
import org.jetbrains.annotations.NotNull;
@@ -9,21 +9,14 @@
99

1010
public class JaegerUiSchemeHandlerFactory extends BaseSchemeHandlerFactory {
1111

12-
private final JaegerUIVirtualFile file;
13-
14-
public JaegerUiSchemeHandlerFactory(Project project, JaegerUIVirtualFile file) {
15-
super(project);
16-
this.file = file;
17-
}
18-
1912

2013
@NotNull
2114
@Override
22-
public CefResourceHandler createResourceHandler(@NotNull String resourceName, boolean resourceExists) {
15+
public CefResourceHandler createResourceHandler(@NotNull String resourceName, boolean resourceExists, @NotNull CefBrowser browser) {
2316
if (resourceExists) {
24-
return new JaegerUiResourceHandler(getProject(), resourceName, file);
17+
return new JaegerUiResourceHandler(browser, resourceName);
2518
} else {
26-
return new JaegerUiResourceHandler(getProject(), JaegerUIConstants.JAEGER_UI_RESOURCE_FOLDER_NAME + "/index.html", file);
19+
return new JaegerUiResourceHandler(browser, JaegerUIConstants.JAEGER_UI_RESOURCE_FOLDER_NAME + "/index.html");
2720
}
2821
}
2922

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/BaseResourceHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.digma.intellij.plugin.ui.jcef
22

33
import com.intellij.openapi.diagnostic.Logger
4+
import org.cef.browser.CefBrowser
45
import org.cef.callback.CefCallback
56
import org.cef.handler.CefLoadHandler
67
import org.cef.handler.CefResourceHandler
@@ -13,7 +14,7 @@ import java.io.IOException
1314
import java.io.InputStream
1415
import kotlin.math.min
1516

16-
abstract class BaseResourceHandler(private val path: String) : CefResourceHandler {
17+
abstract class BaseResourceHandler(private val path: String, protected val browser: CefBrowser) : CefResourceHandler {
1718

1819
val logger = Logger.getInstance(this::class.java)
1920

@@ -24,7 +25,6 @@ abstract class BaseResourceHandler(private val path: String) : CefResourceHandle
2425

2526
abstract fun isIndexHtml(path: String): Boolean
2627

27-
//todo: probably path is not necessary
2828
abstract fun buildIndexFromTemplate(path: String): InputStream?
2929

3030

0 commit comments

Comments
 (0)