Add command that returns information about a plugin
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / GetPluginInfoCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.Optional;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.atomic.AtomicBoolean;
8 import java.util.concurrent.atomic.AtomicReference;
9
10 import net.pterodactylus.fcp.GetPluginInfo;
11 import net.pterodactylus.fcp.PluginInfo;
12 import net.pterodactylus.fcp.ProtocolError;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
17
18 /**
19  * Default {@link GetPluginInfoCommand} implementation based on {@link FcpDialog}.
20  *
21  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
22  */
23 public class GetPluginInfoCommandImpl implements GetPluginInfoCommand {
24
25         private static final RandomIdentifierGenerator IDENTIFIER = new RandomIdentifierGenerator();
26         private final ListeningExecutorService threadPool;
27         private final ConnectionSupplier connectionSupplier;
28         private final GetPluginInfo getPluginInfo = new GetPluginInfo(IDENTIFIER.generate());
29
30         public GetPluginInfoCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
31                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32                 this.connectionSupplier = connectionSupplier;
33         }
34
35         @Override
36         public Executable<Optional<PluginInfo>> plugin(String pluginName) {
37                 getPluginInfo.setPluginName(pluginName);
38                 return this::execute;
39         }
40
41         private ListenableFuture<Optional<PluginInfo>> execute() {
42                 return threadPool.submit(this::executeDialog);
43         }
44
45         private Optional<PluginInfo> executeDialog() throws IOException, ExecutionException, InterruptedException {
46                 try (GetPluginInfoDialog getPluginInfoDialog = new GetPluginInfoDialog()) {
47                         return getPluginInfoDialog.send(getPluginInfo).get();
48                 }
49         }
50
51         private class GetPluginInfoDialog extends FcpDialog<Optional<PluginInfo>> {
52
53                 private final AtomicBoolean finished = new AtomicBoolean();
54                 private final AtomicReference<PluginInfo> pluginInfo = new AtomicReference<>();
55
56                 public GetPluginInfoDialog() throws IOException {
57                         super(threadPool, connectionSupplier.get());
58                 }
59
60                 @Override
61                 protected boolean isFinished() {
62                         return finished.get();
63                 }
64
65                 @Override
66                 protected Optional<PluginInfo> getResult() {
67                         return Optional.ofNullable(pluginInfo.get());
68                 }
69
70                 @Override
71                 protected void consumePluginInfo(PluginInfo pluginInfo) {
72                         this.pluginInfo.set(pluginInfo);
73                         finished.set(true);
74                 }
75
76                 @Override
77                 protected void consumeProtocolError(ProtocolError protocolError) {
78                         finished.set(true);
79                 }
80
81         }
82
83 }