Skip to content

Commit 7a5d8ed

Browse files
committed
add task as argument of Executable to easily access the current task, especially to check if a cancel request has been done
1 parent dda17dc commit 7a5d8ed

File tree

74 files changed

+319
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+319
-284
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/application/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public static IAsync<ApplicationBootstrapException> start(
361361
loading.addToJoin(loadPref);
362362

363363
// init locale
364-
Task<Void, NoException> loadLocale = Task.cpu("Initialize localization", Task.Priority.RATHER_IMPORTANT, () -> {
364+
Task<Void, NoException> loadLocale = Task.cpu("Initialize localization", Task.Priority.RATHER_IMPORTANT, t -> {
365365
String lang = app.getPreference(PROPERTY_LANGUAGE_TAG);
366366
if (lang == null) lang = app.getProperty(PROPERTY_LANGUAGE_TAG);
367367
if (lang != null) {

net.lecousin.core/src/main/java/net/lecousin/framework/application/ApplicationBootstrap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface ApplicationBootstrap {
2323
static void main(Artifact artifact, String[] args, boolean debugMode, ApplicationBootstrap startup) {
2424
IAsync<ApplicationBootstrapException> start = Application.start(artifact, args, debugMode);
2525
Task<IAsync<Exception>, Exception> t = Task.cpu("Start application",
26-
() -> startup.start(LCCore.getApplication(), new FakeWorkProgress()));
26+
ctx -> startup.start(LCCore.getApplication(), new FakeWorkProgress()));
2727
t.startOn(start, false);
2828
start.block(0);
2929
t.getOutput().block(0);

net.lecousin.core/src/main/java/net/lecousin/framework/application/LCCore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static synchronized void start() {
109109
initEnvironment();
110110

111111
instance.start();
112-
Task.cpu("Initializing framework tools", () -> {
112+
Task.cpu("Initializing framework tools", t -> {
113113
MemoryManager.init();
114114
return null;
115115
}).start();

net.lecousin.core/src/main/java/net/lecousin/framework/application/launcher/DynamicLibrariesManager.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import net.lecousin.framework.application.libraries.classpath.LoadLibraryPluginsFile;
4141
import net.lecousin.framework.collections.CollectionsUtil;
4242
import net.lecousin.framework.collections.Tree;
43+
import net.lecousin.framework.concurrent.CancelException;
4344
import net.lecousin.framework.concurrent.Executable;
4445
import net.lecousin.framework.concurrent.async.Async;
4546
import net.lecousin.framework.concurrent.async.AsyncSupplier;
@@ -137,12 +138,14 @@ public ApplicationClassLoader start(Application app) {
137138
private void loadSplashFile(File splashFile) {
138139
Task<byte[], IOException> read = ReadFullFile.create(splashFile, Task.Priority.URGENT);
139140
read.start();
140-
Task<Void,NoException> load = Task.cpu("Loading splash image", Task.Priority.URGENT, () -> {
141+
Task<Void,NoException> load = Task.cpu("Loading splash image", Task.Priority.URGENT, t -> {
141142
ImageIcon img = new ImageIcon(read.getOutput().getResult());
142143
if (splash == null) return null;
143144
synchronized (splash) {
144-
while (!splash.isReady())
145+
while (!splash.isReady()) {
146+
if (t.isCancelling()) throw t.getCancelEvent();
145147
if (!ThreadUtil.wait(splash, 0)) return null;
148+
}
146149
}
147150
splash.setLogo(img, true);
148151
return null;
@@ -164,10 +167,11 @@ private void developmentMode(long stepDevProjects, long stepDependencies, long s
164167
private AsyncSupplier<List<LibraryDescriptor>, LibraryManagementException> loadDevProjects(long stepDevProjects) {
165168
JoinPoint<LibraryManagementException> jpDevProjects = new JoinPoint<>();
166169
ArrayList<AsyncSupplier<? extends LibraryDescriptor, LibraryManagementException>> devProjects = new ArrayList<>(devPaths.size());
167-
Task.cpu("Load development projects", Task.Priority.IMPORTANT, () -> {
170+
Task.cpu("Load development projects", Task.Priority.IMPORTANT, t -> {
168171
int nb = devPaths.size();
169172
long w = stepDevProjects;
170173
for (File dir : devPaths) {
174+
if (t.isCancelling()) throw t.getCancelEvent();
171175
long step = w / nb--;
172176
w -= step;
173177
AsyncSupplier<? extends LibraryDescriptor, LibraryManagementException> load =
@@ -303,7 +307,7 @@ private void loadApplicationLibrary(
303307
libraries.put(descr.getGroupId() + ':' + descr.getArtifactId(), lib);
304308
appLib = lib;
305309
loadLibrary(lib, resolveConflicts.getOutput().getResult(), addPlugins, splash, stepLoad).start();
306-
lib.load.thenStart(Task.cpu("Finishing to initialize", Task.Priority.IMPORTANT, () -> {
310+
lib.load.thenStart(Task.cpu("Finishing to initialize", Task.Priority.IMPORTANT, t -> {
307311
if (canStartApp.hasError()) return null;
308312
app.getDefaultLogger().debug("Libraries initialized.");
309313
ExtensionPoints.allPluginsLoaded();
@@ -470,12 +474,14 @@ private ResolveVersionConflicts(
470474
private long work;
471475

472476
@Override
473-
public Map<String, LibraryDescriptor> execute() throws LibraryManagementException {
477+
public Map<String, LibraryDescriptor> execute(Task<Map<String, LibraryDescriptor>, LibraryManagementException> taskContext)
478+
throws LibraryManagementException, CancelException {
474479
if (progress != null) progress.setText("Resolving dependencies versions");
475480
app.getDefaultLogger().debug("Resolving version conflicts");
476481
Map<String, LibraryDescriptor> versions = new HashMap<>();
477482
for (Map.Entry<String, Map<String, List<Tree.Node<DependencyNode>>>> group : artifacts.entrySet()) {
478483
for (Map.Entry<String, List<Tree.Node<DependencyNode>>> artifact : group.getValue().entrySet()) {
484+
if (taskContext.isCancelling()) throw taskContext.getCancelEvent();
479485
// create a mapping of versions, and remove errors
480486
Map<Version, List<Tree.Node<DependencyNode>>> artifactVersions = getArtifactVersions(artifact);
481487
if (artifactVersions.isEmpty()) {
@@ -558,13 +564,14 @@ private LoadLibrary(
558564
private long work;
559565

560566
@Override
561-
public Void execute() {
567+
public Void execute(Task<Void, NoException> taskContext) throws CancelException {
562568
if (app.getDefaultLogger().debug()) app.getDefaultLogger().debug(
563569
"Loading " + lib.descr.getGroupId() + ':' + lib.descr.getArtifactId() + ':' + lib.descr.getVersionString());
564570

565571
JoinPoint<LibraryManagementException> jp = new JoinPoint<>();
566572
int nb = lib.descr.getDependencies().size() + (addPlugins != null ? addPlugins.size() : 0);
567573
for (LibraryDescriptor.Dependency dep : lib.descr.getDependencies()) {
574+
if (taskContext.isCancelling()) throw taskContext.getCancelEvent();
568575
String key = dep.getGroupId() + ':' + dep.getArtifactId();
569576
LibraryDescriptor d = versions.get(key);
570577
long step = work / nb--;
@@ -574,6 +581,7 @@ public Void execute() {
574581
}
575582
if (addPlugins != null) {
576583
for (LibraryDescriptor d : addPlugins) {
584+
if (taskContext.isCancelling()) throw taskContext.getCancelEvent();
577585
String key = d.getGroupId() + ':' + d.getArtifactId();
578586
long step = work / nb--;
579587
work -= step;
@@ -630,7 +638,7 @@ private Init(Lib lib) {
630638
private IAsync<Exception> previousStep = null;
631639

632640
@Override
633-
public Void execute() {
641+
public Void execute(Task<Void, NoException> taskContext) throws CancelException {
634642
if (app.getDefaultLogger().debug()) app.getDefaultLogger().debug(
635643
"Initializing " + lib.descr.getGroupId() + ':' + lib.descr.getArtifactId() + ':'
636644
+ lib.descr.getVersionString());
@@ -641,10 +649,14 @@ public Void execute() {
641649
if (!loadExtensionPoints(jp))
642650
return null;
643651

652+
if (taskContext.isCancelling()) throw taskContext.getCancelEvent();
653+
644654
// custom extension points
645655
if (!loadCustomExtensionPoints(jp))
646656
return null;
647657

658+
if (taskContext.isCancelling()) throw taskContext.getCancelEvent();
659+
648660
// plugins
649661
if (!loadPlugins(jp))
650662
return null;
@@ -956,7 +968,7 @@ else if (task.getSynch().hasError())
956968
Task<IAsync<Exception>, ApplicationBootstrapException> startApp() {
957969
Task<IAsync<Exception>, ApplicationBootstrapException> task =
958970
Task.cpu(app.getGroupId() + ':' + app.getArtifactId() + ':' + app.getVersion().toString(), Task.Priority.NORMAL,
959-
() -> {
971+
ctx -> {
960972
if (splash != null) splash.setText("Starting application " + appCfg.getName());
961973
@SuppressWarnings("rawtypes")
962974
Class cl;

net.lecousin.core/src/main/java/net/lecousin/framework/application/libraries/classpath/DefaultLibrariesManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.lecousin.framework.application.ApplicationClassLoader;
2020
import net.lecousin.framework.application.libraries.LibrariesManager;
2121
import net.lecousin.framework.application.libraries.LibraryManagementException;
22+
import net.lecousin.framework.concurrent.CancelException;
2223
import net.lecousin.framework.concurrent.Executable;
2324
import net.lecousin.framework.concurrent.async.Async;
2425
import net.lecousin.framework.concurrent.async.IAsync;
@@ -84,7 +85,7 @@ private static BufferedReadableCharacterStream loadNextFile(Enumeration<URL> url
8485

8586
private class Start implements Executable<Void, NoException> {
8687
@Override
87-
public Void execute() {
88+
public Void execute(Task<Void, NoException> taskContext) {
8889
String cp = System.getProperty("java.class.path");
8990
app.getDefaultLogger().info("Starting DefaultLibrariesManager with classpath = " + cp);
9091
URL[] addcp = acl.getURLs();
@@ -142,7 +143,7 @@ private Start2(LinkedList<IAsync<Exception>> tasks) {
142143
private LinkedList<IAsync<Exception>> tasks;
143144

144145
@Override
145-
public Void execute() {
146+
public Void execute(Task<Void, NoException> taskContext) {
146147
for (CustomExtensionPoint custom : ExtensionPoints.getCustomExtensionPoints()) {
147148
String path = custom.getPluginConfigurationFilePath();
148149
if (path == null) continue;
@@ -156,7 +157,7 @@ public Void execute() {
156157
Async<Exception> plugins = new Async<>();
157158
tasks.getLast().thenStart("Load plugins from libraries", Task.Priority.NORMAL, () -> loadPlugins(plugins), false);
158159
tasks.add(plugins);
159-
tasks.getLast().thenStart("Finalize libraries loading", Task.Priority.NORMAL, () -> {
160+
tasks.getLast().thenStart("Finalize libraries loading", Task.Priority.NORMAL, t -> {
160161
ExtensionPoints.allPluginsLoaded();
161162
started.unblock();
162163
return null;
@@ -231,10 +232,11 @@ public CustomExtensionPointLoader(CustomExtensionPoint ep, String filePath) {
231232
private String filePath;
232233

233234
@Override
234-
public Void execute() throws Exception {
235+
public Void execute(Task<Void, Exception> taskContext) throws Exception, CancelException {
235236
app.getDefaultLogger().info("Loading plugin files for custom extension point " + ep.getClass().getName() + ": " + filePath);
236237
Enumeration<URL> urls = acl.getResources(filePath);
237238
while (urls.hasMoreElements()) {
239+
if (taskContext.isCancelling()) throw taskContext.getCancelEvent();
238240
URL url = urls.nextElement();
239241
app.getDefaultLogger().info(" - Plugin file found: " + url.toString());
240242
InputStream input = url.openStream();

net.lecousin.core/src/main/java/net/lecousin/framework/collections/TurnArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ private void checkDecrease() {
339339

340340
private class DecreaseSize implements Executable<Void, NoException> {
341341
@Override
342-
public Void execute() {
342+
public Void execute(Task<Void, NoException> taskContext) {
343343
synchronized (TurnArray.this) {
344344
decreaseTask = null;
345345
decrease();

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/Executable.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.function.Function;
55
import java.util.function.Supplier;
66

7+
import net.lecousin.framework.concurrent.threads.Task;
78
import net.lecousin.framework.exception.NoException;
89
import net.lecousin.framework.util.Runnables;
910

@@ -15,7 +16,7 @@
1516
public interface Executable<T, E extends Exception> {
1617

1718
/** Execute. */
18-
T execute() throws E, CancelException;
19+
T execute(Task<T, E> taskContext) throws E, CancelException;
1920

2021
/** Executable from a Runnable. */
2122
class FromRunnable implements Executable<Void, NoException> {
@@ -26,7 +27,7 @@ public FromRunnable(java.lang.Runnable run) {
2627
private java.lang.Runnable run;
2728

2829
@Override
29-
public Void execute() {
30+
public Void execute(Task<Void, NoException> t) {
3031
run.run();
3132
return null;
3233
}
@@ -50,7 +51,7 @@ public void setInput(T input) {
5051
}
5152

5253
@Override
53-
public Void execute() throws CancelException {
54+
public Void execute(Task<Void, NoException> t) throws CancelException {
5455
consumer.accept(input);
5556
return null;
5657
}
@@ -76,7 +77,7 @@ public void setInput(T input) {
7677
}
7778

7879
@Override
79-
public Void execute() throws E, CancelException {
80+
public Void execute(Task<Void, E> t) throws E, CancelException {
8081
consumer.accept(input);
8182
return null;
8283
}
@@ -102,7 +103,7 @@ public void setInput(IN input) {
102103
}
103104

104105
@Override
105-
public OUT execute() throws CancelException {
106+
public OUT execute(Task<OUT, NoException> t) throws CancelException {
106107
return fct.apply(input);
107108
}
108109

@@ -128,7 +129,7 @@ public void setInput(IN input) {
128129
}
129130

130131
@Override
131-
public OUT execute() throws E, CancelException {
132+
public OUT execute(Task<OUT, E> t) throws E, CancelException {
132133
return fct.apply(input);
133134
}
134135

@@ -147,7 +148,7 @@ public FromSupplier(Supplier<T> supplier) {
147148
private Supplier<T> supplier;
148149

149150
@Override
150-
public T execute() throws CancelException {
151+
public T execute(Task<T, NoException> t) throws CancelException {
151152
return supplier.get();
152153
}
153154
}
@@ -166,7 +167,7 @@ public FromSupplierThrows(Runnables.SupplierThrows<T, E> supplier) {
166167
private Runnables.SupplierThrows<T, E> supplier;
167168

168169
@Override
169-
public T execute() throws E, CancelException {
170+
public T execute(Task<T, E> t) throws E, CancelException {
170171
return supplier.get();
171172
}
172173
}

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/tasks/PropertiesFileSaver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public PropertiesFileSaver(Properties properties, ICharacterStream.Writable.Buff
6969
private boolean closeStreamAtEnd;
7070

7171
@Override
72-
public Void execute() throws IOException, CancelException {
72+
public Void execute(Task<Void, IOException> taskContext) throws IOException, CancelException {
7373
try {
7474
for (Map.Entry<Object,Object> p : properties.entrySet()) {
7575
output.writeSync(p.getKey().toString());

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/tasks/drives/CreateDirectory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public CreateDirectory(File dir, boolean recursive, boolean failIfExists) {
2929
private boolean failIfExists;
3030

3131
@Override
32-
public Void execute() throws IOException {
32+
public Void execute(Task<Void, IOException> taskContext) throws IOException {
3333
boolean created;
3434
if (recursive)
3535
created = dir.mkdirs();

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/tasks/drives/DirectoryReader.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.ArrayList;
99

1010
import net.lecousin.framework.application.LCCore;
11+
import net.lecousin.framework.concurrent.CancelException;
1112
import net.lecousin.framework.concurrent.Executable;
1213
import net.lecousin.framework.concurrent.threads.Task;
1314
import net.lecousin.framework.concurrent.threads.Task.Priority;
@@ -113,7 +114,7 @@ public FileInfo[] getFiles() {
113114
}
114115

115116
@Override
116-
public Result execute() throws AccessDeniedException {
117+
public Result execute(Task<Result, AccessDeniedException> taskContext) throws AccessDeniedException, CancelException {
117118
File[] list = dir.listFiles();
118119
if (list == null)
119120
throw new AccessDeniedException("Directory " + dir.getAbsolutePath());
@@ -128,6 +129,8 @@ public Result execute() throws AccessDeniedException {
128129
long work = progress != null ? progress.getRemainingWork() : 0;
129130
int nb = list.length;
130131
for (int i = 0; i < list.length; ++i) {
132+
if (taskContext.isCancelling())
133+
throw taskContext.getCancelEvent();
131134
long step = work / nb--;
132135
work -= step;
133136
FileInfo f = new FileInfo();
@@ -171,7 +174,7 @@ private void readFile(FileInfo f, Result result) {
171174
}
172175

173176
/** Task to list only sub-directories. */
174-
public static class ListSubDirectories implements Executable<ArrayList<File>,AccessDeniedException> {
177+
public static class ListSubDirectories implements Executable<ArrayList<File>, AccessDeniedException> {
175178

176179
/** Create task. */
177180
public static Task<ArrayList<File>,AccessDeniedException> task(File dir, Priority priority) {
@@ -186,11 +189,13 @@ public ListSubDirectories(File dir) {
186189
private File dir;
187190

188191
@Override
189-
public ArrayList<File> execute() throws AccessDeniedException {
192+
public ArrayList<File> execute(Task<ArrayList<File>, AccessDeniedException> taskContext)
193+
throws AccessDeniedException, CancelException {
190194
String[] names = dir.list();
191195
if (names == null) throw new AccessDeniedException("Directory " + dir.getAbsolutePath());
192196
ArrayList<File> result = new ArrayList<>();
193197
for (int i = 0; i < names.length; ++i) {
198+
if (taskContext.isCancelling()) throw taskContext.getCancelEvent();
194199
File f = new File(dir, names[i]);
195200
if (f.isDirectory())
196201
result.add(f);

0 commit comments

Comments
 (0)