package com.cadrlife.devsearch.esplugin;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import java.io.*;
import java.net.Socket;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
public class EmbeddedRunnerMain {
public static void main(String[] args) throws IOException {
new EmbeddedRunnerMain().start(args);
}
private void start(String[] args) {
displayBanner();
Node node = null;
try {
if (args.length < 1) {
throw new RuntimeException("Need more args. First is data dir");
}
Path dataPath = new File(args[0]).toPath();
Files.createDirectories(dataPath);
if (args.length > 1) {
monitorHeartBeat(Integer.parseInt(args[1]));
}
NodeBuilder nodeBuilder = nodeBuilder();
// String host = "btsm01p:9300";
String pluginDir = findPluginPath();
// System.getProperty("user.home")
System.out.println("Using data dir " + dataPath);
int httpPort = 9200;
nodeBuilder.settings()
.put("path.plugins", pluginDir)
.put("path.data", dataPath.toString())
.put("http.port", httpPort)
.put("transport.tcp.port", 9300)
.put("discovery.zen.ping.multicast.enabled", "false");
// .put("discovery.zen.ping.unicast.hosts", host);
//node = nodeBuilder.clusterName("elasticsearch").client(true).node();
node = nodeBuilder.clusterName("elasticsearch").node();
addShutdownHook(nodeAsClosable(node), "Node");
String baseUrl = "http://localhost:" + httpPort;
initializeClient(baseUrl);
Thread.sleep(2000);
System.out.println("Now navigate to " + baseUrl + "/_plugin/dev-search/ to test");
Thread.sleep(Integer.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
node.close();
} catch (Exception e) { }
}
System.exit(1);
}
private void displayBanner() {
System.out.println(
"________ _________ .__ \n" +
"\\______ \\ ____ ___ __ / _____/ ____ _____ _______ ____ | |__ \n" +
" | | \\ _/ __ \\ \\ \\/ / \\_____ \\ _/ __ \\ \\__ \\ \\_ __ \\_/ ___\\ | | \\ \n" +
" | ` \\\\ ___/ \\ / / \\\\ ___/ / __ \\_ | | \\/\\ \\___ | Y \\\n" +
"/_______ / \\___ > \\_/ /_______ / \\___ >(____ / |__| \\___ >|___| /\n" +
" \\/ \\/ \\/ \\/ \\/ \\/ \\/ ");
}
private Closeable nodeAsClosable(final Node node) {
return new Closeable() {
@Override
public void close() throws IOException {
node.close(); // C'mon ES, would it have killed you to make Node implement Closable?
}
};
}
private void monitorHeartBeat(final int socketPort) throws IOException {
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Opening receiving heartbeart socket on " + socketPort);
Socket socket = new Socket("localhost", socketPort);
socket.setSoTimeout(6000);
final BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
addShutdownHook(socketReader, "heartbeat listener socket");
for (;;) {
try {
String s = socketReader.readLine();
if (s == null) {
throw new RuntimeException("Null from socket");
}
} catch (Exception exception) {
System.out.println("Stopped receiving ALIVE signal. (" + exception.getMessage()+ "). Assuming invoking app is dead.");
System.exit(0);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void addShutdownHook(final Closeable closeable, final String description) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Closing " + description);
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void initializeClient(String baseUrl) throws IOException {
String initUrl = baseUrl + "/_dev-search/init";
System.out.println("Calling db INIT URL " + initUrl);
try (InputStream stream = new URL(initUrl).openStream()) {
BufferedReader rd = new BufferedReader(new InputStreamReader(stream));
String line;
StringBuilder result = new StringBuilder();
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("INIT URL returned: " + result);
}
}
private String findPluginPath() {
String userDir = System.getProperty("user.dir").replaceAll("\\\\?/?dev-search-elasticsearch-plugin$", "");
String pluginPath = userDir + "/dev-search-elasticsearch-plugin/src/dist";
if (!new File(pluginPath).exists()) {
throw new RuntimeException("Could not find plugin dist dir at " + pluginPath);
}
return pluginPath;
}
}