Example usage for org.apache.solr.core SolrCore getIndexDir

List of usage examples for org.apache.solr.core SolrCore getIndexDir

Introduction

In this page you can find the example usage for org.apache.solr.core SolrCore getIndexDir.

Prototype

public String getIndexDir() 

Source Link

Usage

From source file:org.springframework.data.rest.webmvc.solr.SolrInfrastructureConfig.java

License:Apache License

/**
 * {@link SpringJUnit4ClassRunner} executes {@link ClassRule}s before the actual shutdown of the
 * {@link ApplicationContext}. This causes the {@link TemporaryFolder} to vanish before Solr can gracefully shutdown.
 * <br />// w ww .j  a  v  a2 s .co m
 * To prevent error messages popping up we register a {@link CloseHook} re adding the index directory and removing it
 * after {@link SolrCore#close()}.
 * 
 * @param factory
 */
private void attachCloseHook(SolrClientFactory factory) {

    EmbeddedSolrServer server = (EmbeddedSolrServer) factory.getSolrClient();

    for (SolrCore core : server.getCoreContainer().getCores()) {

        core.addCloseHook(new CloseHook() {

            private String path;

            @Override
            public void preClose(SolrCore core) {

                CoreDescriptor cd = core.getCoreDescriptor();

                if (cd == null) {
                    return;
                }

                File tmp = new File(core.getIndexDir()).getParentFile();

                if (tmp.exists()) {
                    return;
                }

                try {

                    File indexFile = new File(tmp, "index");
                    indexFile.mkdirs();

                    this.path = indexFile.getPath();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void postClose(SolrCore core) {

                if (!StringUtils.hasText(this.path)) {
                    return;
                }

                File tmp = new File(this.path);

                if (tmp.exists() && tmp.getPath().startsWith(FileUtils.getTempDirectoryPath())) {

                    try {
                        FileUtils.deleteDirectory(tmp);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}