package com.cadrlife.devsearch.agent; import com.cadrlife.devsearch.agent.service.RepoCreator; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.base.Throwables; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.name.Names; import com.jcraft.jsch.JSch; import org.apache.http.client.HttpClient; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import javax.inject.Named; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.X509Certificate; import java.util.Properties; import java.util.concurrent.*; public class AgentModule extends AbstractModule { private String checkoutPath = ""; private String configFile = ""; private int threadCount; @Override protected void configure() { Properties properties = loadProperties(configFile); Names.bindProperties(binder(), properties); bind(Properties.class).toInstance(properties); Preconditions.checkArgument(!Strings.isNullOrEmpty(checkoutPath), "checkout-path must be set"); JSch.setConfig("StrictHostKeyChecking", "no"); //Evidently some CVS servers dont like re-use of connections. Ours apparently likes them though System.setProperty("javacvs.multiple_commands_warning", "false"); } private Properties loadProperties(String path) { Properties properties = new Properties(); try (FileInputStream inStream = new FileInputStream(new File(path))){ properties.load(inStream); properties.setProperty("cvs.temp.dir", provideRepoRootPath().resolve("cvs-temp").toFile().getAbsolutePath()); } catch (IOException e) { Throwables.propagate(e); } return properties; } @Provides @Singleton Client provideClient( @Named("elasticsearch.cluster.name") String clusterName, @Named("elasticsearch.host") String host, @Named("elasticsearch.port") int port) { Settings settings = ImmutableSettings.settingsBuilder() .put("cluster.name", clusterName).build(); return new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(host, port)); } @Provides @Singleton RepoCreator provideRepoCreator(Properties properties) { return new RepoCreator(properties, checkoutPath); } @Provides ObjectMapper provideObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return objectMapper; } @Provides @Singleton ListeningExecutorService provideExecutorService() { return MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(getThreadCount())); } @Provides HttpClient provideHttpClient() { return new DefaultHttpClient(getClientConnectionManager()); } public static ClientConnectionManager getClientConnectionManager() { // TrustStrategy acceptingTrustStrategy = new TrustStrategy() { // @Override // public boolean isTrusted(X509Certificate[] certificate, String authType) { // return true; // } // }; // SSLSocketFactory sf = null; // try { // sf = new SSLSocketFactory(acceptingTrustStrategy, // SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | UnrecoverableKeyException e) { // throw new RuntimeException(e); // } // SchemeRegistry registry = new SchemeRegistry(); // registry.register(new Scheme("https", 8443, sf)); // return new PoolingClientConnectionManager(registry); return new PoolingClientConnectionManager(); } // @Provides // @Singleton // Node provideNode(@Named("elasticsearch.hosts") String hosts, @Named("elasticsearch.cluster.name") String clusterName) { // return NodeBuilder.nodeBuilder() // .settings(ImmutableSettings.settingsBuilder() // .put("discovery.zen.ping.multicast.enabled", "false") // .put("discovery.zen.ping.unicast.hosts", hosts)) // .clusterName(clusterName).client(true).node(); // } // @Provides // Client provideClient(Node node) { // return node.client(); // } @Provides @Named("checkout.root") Path provideRepoRootPath() { return Paths.get(checkoutPath); } public void setCheckoutPath(String checkoutPath) { this.checkoutPath = checkoutPath; } public void setConfigFile(String configFile) { this.configFile = configFile; } public int getThreadCount() { return threadCount; } public void setThreadCount(int threadCount) { this.threadCount = threadCount; } }