-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Modifies ViewComponent with changed api's so that it can be executed with a thread allowing caller not to block. - Add ViewComponentBuilder concept and create is as a bean similar to TerminalUIBuilder. - Relates #997
- Loading branch information
Showing
6 changed files
with
298 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...ng-shell-core/src/main/java/org/springframework/shell/component/ViewComponentBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.shell.component; | ||
|
||
import org.jline.terminal.Terminal; | ||
|
||
import org.springframework.shell.component.view.TerminalUIBuilder; | ||
import org.springframework.shell.component.view.control.View; | ||
|
||
/** | ||
* Builder that can be used to configure and create a {@link ViewComponent}. | ||
* | ||
* @author Janne Valkealahti | ||
*/ | ||
public class ViewComponentBuilder { | ||
|
||
private final TerminalUIBuilder terminalUIBuilder; | ||
private final ViewComponentExecutor viewComponentExecutor; | ||
private final Terminal terminal; | ||
|
||
public ViewComponentBuilder(TerminalUIBuilder terminalUIBuilder, ViewComponentExecutor viewComponentExecutor, | ||
Terminal terminal) { | ||
this.terminalUIBuilder = terminalUIBuilder; | ||
this.viewComponentExecutor = viewComponentExecutor; | ||
this.terminal = terminal; | ||
} | ||
|
||
/** | ||
* Build a new {@link ViewComponent} instance and configure it using this builder. | ||
* | ||
* @param view the view to use with view component | ||
* @return a configured {@link ViewComponent} instance. | ||
*/ | ||
public ViewComponent build(View view) { | ||
return new ViewComponent(terminalUIBuilder.build(), terminal, viewComponentExecutor, view); | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
...g-shell-core/src/main/java/org/springframework/shell/component/ViewComponentExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.shell.component; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.springframework.core.task.SimpleAsyncTaskExecutor; | ||
import org.springframework.shell.component.ViewComponent.ViewComponentRun; | ||
|
||
/** | ||
* Executor for {@code ViewComponent}. Purpose of this executor is to run | ||
* component in a thread so that it doesn't need to block from a command. | ||
* | ||
* @author Janne Valkealahti | ||
*/ | ||
public class ViewComponentExecutor implements AutoCloseable { | ||
|
||
private final Logger log = LoggerFactory.getLogger(ViewComponentExecutor.class); | ||
private final SimpleAsyncTaskExecutor executor; | ||
private Future<?> future; | ||
|
||
public ViewComponentExecutor() { | ||
this.executor = new SimpleAsyncTaskExecutor(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
this.executor.close(); | ||
} | ||
|
||
private static class FutureViewComponentRun implements ViewComponentRun { | ||
|
||
private Future<?> future; | ||
|
||
private FutureViewComponentRun(Future<?> future) { | ||
this.future = future; | ||
} | ||
|
||
@Override | ||
public void await() { | ||
try { | ||
this.future.get(); | ||
} catch (InterruptedException e) { | ||
} catch (ExecutionException e) { | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
this.future.cancel(true); | ||
} | ||
|
||
@Override | ||
public boolean isDone() { | ||
return this.future.isDone(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Execute runnable and return state which can be used for further operations. | ||
* | ||
* @param runnable the runnable | ||
* @return run state | ||
*/ | ||
public ViewComponentRun start(Runnable runnable) { | ||
if (future != null && !future.isDone()) { | ||
throw new IllegalStateException("Can run component as there is existing one in non stopped state"); | ||
} | ||
future = executor.submit(() -> { | ||
log.debug("About to run component"); | ||
runnable.run(); | ||
log.debug("Finished run component"); | ||
}); | ||
return new FutureViewComponentRun(future); | ||
} | ||
|
||
/** | ||
* Stop a {@code ViewComponent} which has been previously started with this | ||
* executor. | ||
*/ | ||
public void stop() { | ||
if (future != null) { | ||
future.cancel(true); | ||
} | ||
future = null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.