Use a single identifier generator in all commands
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / RemovePluginCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.atomic.AtomicBoolean;
7 import java.util.function.Supplier;
8
9 import net.pterodactylus.fcp.PluginRemoved;
10 import net.pterodactylus.fcp.ProtocolError;
11 import net.pterodactylus.fcp.RemovePlugin;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
16
17 /**
18  * Default {@link RemovePluginCommand} implementation based on {@link FcpDialog}.
19  *
20  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
21  */
22 public class RemovePluginCommandImpl implements RemovePluginCommand {
23
24         private final ListeningExecutorService threadPool;
25         private final ConnectionSupplier connectionSupplier;
26         private final RemovePlugin removePlugin;
27
28         public RemovePluginCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
29                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
30                 this.connectionSupplier = connectionSupplier;
31                 removePlugin = new RemovePlugin(identifierGenerator.get());
32         }
33
34         @Override
35         public RemovePluginCommand waitFor(int milliseconds) {
36                 removePlugin.setMaxWaitTime(milliseconds);
37                 return this;
38         }
39
40         @Override
41         public RemovePluginCommand purge() {
42                 removePlugin.setPurge(true);
43                 return this;
44         }
45
46         @Override
47         public Executable<Boolean> plugin(String pluginClass) {
48                 removePlugin.setPluginName(pluginClass);
49                 return this::execute;
50         }
51
52         private ListenableFuture<Boolean> execute() {
53                 return threadPool.submit(this::executeDialog);
54         }
55
56         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
57                 try (RemovePluginDialog removePluginDialog = new RemovePluginDialog()) {
58                         return removePluginDialog.send(removePlugin).get();
59                 }
60         }
61
62         private class RemovePluginDialog extends FcpDialog<Boolean> {
63
64                 private final AtomicBoolean finished = new AtomicBoolean();
65                 private final AtomicBoolean pluginRemoved = new AtomicBoolean();
66
67                 public RemovePluginDialog() throws IOException {
68                         super(threadPool, connectionSupplier.get());
69                 }
70
71                 @Override
72                 protected boolean isFinished() {
73                         return finished.get();
74                 }
75
76                 @Override
77                 protected Boolean getResult() {
78                         return pluginRemoved.get();
79                 }
80
81                 @Override
82                 protected void consumePluginRemoved(PluginRemoved pluginRemoved) {
83                         this.pluginRemoved.set(true);
84                         finished.set(true);
85                 }
86
87                 @Override
88                 protected void consumeProtocolError(ProtocolError protocolError) {
89                         finished.set(true);
90                 }
91
92         }
93
94 }