// Path: olastic-web/src/main/java/com/hevelian/olastic/config/ESConfig.java
// public interface ESConfig {
// 
//     /**
//      * Return's attribute name to specify it in context.
//      * 
//      * @return attribute name
//      */
//     static String getName() {
//         return "ElasticsearchConfiguration";
//     }
// 
//     /**
//      * Return's {@link Client} instance.
//      * 
//      * @return client
//      */
//     Client getClient();
// 
//     /**
//      * Close {@link Client}.
//      */
//     void close();
// 
//     /**
//      * Return's indices from client.
//      * 
//      * @return indices set
//      */
//     Set<String> getIndices();
// 
// }
// 
// Path: olastic-web/src/main/java/com/hevelian/olastic/config/ESConfigImpl.java
// public class ESConfigImpl implements ESConfig {
//     /** Elasticsearch client. */
//     protected final Client client;
// 
//     /**
//      * Creates a new Elasticsearch client configuration with predefined
//      * properties.
//      * 
//      * @param host
//      *            host name
//      * @param port
//      *            port number
//      * @param cluster
//      *            cluster name
//      * @throws UnknownHostException
//      *             if no IP address for the host could be found
//      */
//     public ESConfigImpl(String host, int port, String cluster) throws UnknownHostException {
//         Settings settings = Settings.builder().put("cluster.name", cluster).build();
//         this.client = initClient(settings,
//                 new InetSocketTransportAddress(InetAddress.getByName(host), port));
//         initESClient();
//     }
// 
//     /**
//      * Initialize's Elasticsearch {@link Client}.
//      * 
//      * @param settings
//      *            the settings passed to transport client
//      * @param address
//      *            transport address that will be used to connect to
//      * @return client instance
//      */
//     protected Client initClient(Settings settings, TransportAddress address) {
//         PreBuiltTransportClient preBuildClient = new PreBuiltTransportClient(settings);
//         preBuildClient.addTransportAddress(address);
//         return preBuildClient;
//     }
// 
//     /**
//      * Initializes {@link ESClient} instance to execute all queries in
//      * application.
//      */
//     protected void initESClient() {
//         ESClient.init(client);
//     }
// 
//     @Override
//     public void close() {
//         client.close();
//     }
// 
//     @Override
//     public Client getClient() {
//         return client;
//     }
// 
//     @Override
//     public Set<String> getIndices() {
//         try {
//             return client.admin().indices().stats(new IndicesStatsRequest()).actionGet()
//                     .getIndices().keySet();
//         } catch (NoNodeAvailableException e) {
//             throw new ODataRuntimeException("Elasticsearch has no node available.", e);
//         }
//     }
// 
// }


import java.net.UnknownHostException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.hevelian.olastic.config.ESConfig;
import com.hevelian.olastic.config.ESConfigImpl;
import lombok.extern.log4j.Log4j2;

package com.hevelian.olastic.listeners;





/**
 * Application context listener to get Elasticsearch properties from context and
 * initialize {@link ESConfig}.
 * 
 * @author rdidyk
 */
@Log4j2
public class AppContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext ctx = sce.getServletContext();
        String host = ctx.getInitParameter("elastic.host");
        String port = ctx.getInitParameter("elastic.port");
        String cluster = ctx.getInitParameter("elastic.cluster");
        try {
            ESConfig config = new ESConfigImpl(host, Integer.valueOf(port), cluster);
            ctx.setAttribute(ESConfig.getName(), config);
        } catch (UnknownHostException e) {
            log.debug(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext ctx = sce.getServletContext();

// Path: olastic-web/src/main/java/com/hevelian/olastic/config/ESConfig.java
// public interface ESConfig {
// 
//     /**
//      * Return's attribute name to specify it in context.
//      * 
//      * @return attribute name
//      */
//     static String getName() {
//         return "ElasticsearchConfiguration";
//     }
// 
//     /**
//      * Return's {@link Client} instance.
//      * 
//      * @return client
//      */
//     Client getClient();
// 
//     /**
//      * Close {@link Client}.
//      */
//     void close();
// 
//     /**
//      * Return's indices from client.
//      * 
//      * @return indices set
//      */
//     Set<String> getIndices();
// 
// }
// 
// Path: olastic-web/src/main/java/com/hevelian/olastic/config/ESConfigImpl.java
// public class ESConfigImpl implements ESConfig {
//     /** Elasticsearch client. */
//     protected final Client client;
// 
//     /**
//      * Creates a new Elasticsearch client configuration with predefined
//      * properties.
//      * 
//      * @param host
//      *            host name
//      * @param port
//      *            port number
//      * @param cluster
//      *            cluster name
//      * @throws UnknownHostException
//      *             if no IP address for the host could be found
//      */
//     public ESConfigImpl(String host, int port, String cluster) throws UnknownHostException {
//         Settings settings = Settings.builder().put("cluster.name", cluster).build();
//         this.client = initClient(settings,
//                 new InetSocketTransportAddress(InetAddress.getByName(host), port));
//         initESClient();
//     }
// 
//     /**
//      * Initialize's Elasticsearch {@link Client}.
//      * 
//      * @param settings
//      *            the settings passed to transport client
//      * @param address
//      *            transport address that will be used to connect to
//      * @return client instance
//      */
//     protected Client initClient(Settings settings, TransportAddress address) {
//         PreBuiltTransportClient preBuildClient = new PreBuiltTransportClient(settings);
//         preBuildClient.addTransportAddress(address);
//         return preBuildClient;
//     }
// 
//     /**
//      * Initializes {@link ESClient} instance to execute all queries in
//      * application.
//      */
//     protected void initESClient() {
//         ESClient.init(client);
//     }
// 
//     @Override
//     public void close() {
//         client.close();
//     }
// 
//     @Override
//     public Client getClient() {
//         return client;
//     }
// 
//     @Override
//     public Set<String> getIndices() {
//         try {
//             return client.admin().indices().stats(new IndicesStatsRequest()).actionGet()
//                     .getIndices().keySet();
//         } catch (NoNodeAvailableException e) {
//             throw new ODataRuntimeException("Elasticsearch has no node available.", e);
//         }
//     }
// 
// }


// Path: olastic-web/src/main/java/com/hevelian/olastic/listeners/AppContextListener.java
import java.net.UnknownHostException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.hevelian.olastic.config.ESConfig;
import com.hevelian.olastic.config.ESConfigImpl;
import lombok.extern.log4j.Log4j2;

package com.hevelian.olastic.listeners;





/**
 * Application context listener to get Elasticsearch properties from context and
 * initialize {@link ESConfig}.
 * 
 * @author rdidyk
 */
@Log4j2
public class AppContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext ctx = sce.getServletContext();
        String host = ctx.getInitParameter("elastic.host");
        String port = ctx.getInitParameter("elastic.port");
        String cluster = ctx.getInitParameter("elastic.cluster");
        try {
            ESConfig config = new ESConfigImpl(host, Integer.valueOf(port), cluster);
            ctx.setAttribute(ESConfig.getName(), config);
        } catch (UnknownHostException e) {
            log.debug(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext ctx = sce.getServletContext();

        ESConfig config = (ESConfig) ctx.getAttribute(ESConfig.getName());
