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 static org.elasticsearch.node.NodeBuilder.nodeBuilder;
public class EmbeddedRunnerNoDataMain {
public static void main(String[] args) throws IOException {
new EmbeddedRunnerNoDataMain().start(args);
}
private void start(String[] args) {
displayBanner();
Node node = null;
try {
NodeBuilder nodeBuilder = nodeBuilder();
String pluginDir = findPluginPath();
// System.getProperty("user.home")
int httpPort = 9200;
nodeBuilder.settings()
.put("path.plugins", pluginDir)
.put("http.port", httpPort)
.put("transport.tcp.port", 9300)
.put("discovery.zen.ping.multicast.enabled", "false")
.put("discovery.zen.ping.unicast.hosts","btsm01p");
//node = nodeBuilder.clusterName("elasticsearch").client(true).node();
node = nodeBuilder.clusterName("elasticsearch").client(true).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 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;
}
}