|
26 | 26 | import java.util.Map; |
27 | 27 | import java.util.concurrent.CancellationException; |
28 | 28 | import java.util.concurrent.CompletableFuture; |
| 29 | +import java.util.concurrent.CompletionException; |
29 | 30 | import java.util.concurrent.ConcurrentHashMap; |
30 | 31 | import java.util.concurrent.ExecutionException; |
31 | 32 | import java.util.function.BiConsumer; |
|
38 | 39 | import org.netbeans.modules.nbcode.java.project.ProjectConfigurationUtils; |
39 | 40 | import org.netbeans.modules.nbcode.java.project.ProjectContext; |
40 | 41 | import org.netbeans.modules.nbcode.java.project.ProjectContextInfo; |
| 42 | +import org.openide.util.NbBundle; |
41 | 43 |
|
42 | 44 | /** |
43 | 45 | * |
44 | 46 | * @author atalati |
45 | 47 | */ |
| 48 | +@NbBundle.Messages({ |
| 49 | + "MSG_JshellResetError=Some internal error occurred while trying to reset notebook session" |
| 50 | +}) |
46 | 51 | public class NotebookSessionManager { |
47 | 52 |
|
48 | 53 | private static final Logger LOG = Logger.getLogger(NotebookSessionManager.class.getName()); |
@@ -101,12 +106,15 @@ private JShell jshellBuildWithProject(Project prj, JshellStreamsHandler streamsH |
101 | 106 |
|
102 | 107 | public CompletableFuture<JShell> createSession(NotebookDocument notebookDoc) { |
103 | 108 | String notebookId = notebookDoc.getUri(); |
| 109 | + return createSession(notebookId); |
| 110 | + } |
104 | 111 |
|
| 112 | + public CompletableFuture<JShell> createSession(String notebookId) { |
105 | 113 | return sessions.computeIfAbsent(notebookId, id -> { |
106 | 114 | JshellStreamsHandler handler = new JshellStreamsHandler(id, CodeEval.getInstance().outStreamFlushCb, CodeEval.getInstance().errStreamFlushCb); |
107 | 115 | jshellStreamsMap.put(id, handler); |
108 | 116 |
|
109 | | - CompletableFuture<JShell> future = jshellBuilder(notebookDoc.getUri(), handler); |
| 117 | + CompletableFuture<JShell> future = jshellBuilder(notebookId, handler); |
110 | 118 |
|
111 | 119 | future.thenAccept(jshell -> onJshellInit(notebookId, jshell)) |
112 | 120 | .exceptionally(ex -> { |
@@ -241,15 +249,17 @@ public ProjectContextInfo getNotebookPrjNameContext(String notebookId) { |
241 | 249 |
|
242 | 250 | public void closeSession(String notebookUri) { |
243 | 251 | CompletableFuture<JShell> future = sessions.remove(notebookUri); |
244 | | - JShell jshell = future.getNow(null); |
245 | | - if (jshell != null) { |
246 | | - jshell.close(); |
247 | | - } |
248 | | - JshellStreamsHandler handler = jshellStreamsMap.remove(notebookUri); |
249 | | - if (handler != null) { |
250 | | - handler.close(); |
| 252 | + if (future != null) { |
| 253 | + JShell jshell = future.getNow(null); |
| 254 | + if (jshell != null) { |
| 255 | + jshell.close(); |
| 256 | + } |
| 257 | + JshellStreamsHandler handler = jshellStreamsMap.remove(notebookUri); |
| 258 | + if (handler != null) { |
| 259 | + handler.close(); |
| 260 | + } |
| 261 | + notebookPrjMap.remove(notebookUri); |
251 | 262 | } |
252 | | - notebookPrjMap.remove(notebookUri); |
253 | 263 | } |
254 | 264 |
|
255 | 265 | private CompletableFuture<Project> getProjectContextForNotebook(String notebookUri) { |
@@ -283,4 +293,14 @@ private CompletableFuture<Project> getProjectContextForNotebook(String notebookU |
283 | 293 | }); |
284 | 294 |
|
285 | 295 | } |
| 296 | + |
| 297 | + public CompletableFuture<Void> resetSession(String notebookUri) { |
| 298 | + closeSession(notebookUri); |
| 299 | + return createSession(notebookUri) |
| 300 | + .thenApply(jshell -> (Void) null) |
| 301 | + .exceptionally(ex -> { |
| 302 | + LOG.log(Level.SEVERE, "Error creating new session after reset", ex); |
| 303 | + throw new CompletionException(Bundle.MSG_JshellResetError(), ex); |
| 304 | + }); |
| 305 | + } |
286 | 306 | } |
0 commit comments