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