/*
 * Copyright (C) 2013 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.b3p.viewer;

import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrResourceLoader;

/**
 * This class will initialize the solr server: the setup of the index and the
 * initialization of the SolrServer object. This in order to limit the number of
 * connections to the index.
 *
 * @author Meine Toonen
 */
public class SolrInitializer implements ServletContextListener {

    public static final String DATA_DIR = "flamingo.data.dir";
    private static final String SOLR_DIR = ".solr/";
    private static final String SOLR_CORE_NAME = "autosuggest";
    private static SolrServer server;
    private static CoreContainer coreContainer;
    private static final Log log = LogFactory.getLog(SolrInitializer.class);
    private ServletContext context;
    private String datadirectory;
    
    private final String SETUP_SOLR = "flamingo.solr.setup";
    private boolean setupSolr = false;
    
    private static final String SOLR_CONF_DIR="/WEB-INF/classes/solr/autosuggest";

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.debug("SolrInitializer initializing");
        this.context = sce.getServletContext();
        datadirectory = context.getInitParameter(DATA_DIR);
        
        System.setProperty("solr.solr.home", datadirectory + File.separator + SOLR_DIR);
        File dataDirectory = new File(datadirectory);
        if (!isCorrectDir(dataDirectory)) {
            log.error("Cannot read/write data dir " + datadirectory + ". Solr searching not possible.");
            return;
        }
        log.info("Data dir set " + datadirectory);
        File solrDir = new File(dataDirectory, SOLR_DIR);
        if (setupSolr && !solrDir.exists()) {
            setupSolr(solrDir);
        }

        inializeSolr(solrDir);
    }

    private void setupSolr(File solrdir) {
        log.debug("Setup the solr directory");

        copyConf(solrdir);  
        coreContainer = new CoreContainer(solrdir.getPath());
    }

    private void inializeSolr(File solrDir) {
        log.debug("Initialize the Solr Server instance");
        String path = solrDir.getPath();
        SolrResourceLoader loader = new SolrResourceLoader(path);
        if(coreContainer == null){
            coreContainer = new CoreContainer(loader);
        }
        coreContainer.load();
        server = new EmbeddedSolrServer(coreContainer, SOLR_CORE_NAME);
    }

    public static SolrServer getServerInstance() {
        return server;
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        coreContainer.shutdown();
        server.shutdown();
        log.debug("SolrInitializer destroyed");
    }

    private boolean isCorrectDir(File f) {
        return f.isDirectory() && f.canRead() && f.canWrite();
    }
    
    private void copyConf(File solrDir){
        try {
            File conf = new File( this.context.getRealPath("/WEB-INF/classes/solr/solr.xml"));
            boolean solrDirCreated = solrDir.mkdir();
            FileUtils.copyFile(conf, new File(solrDir, "solr.xml"));
            
            File coreConfiguration = new File(context.getRealPath(SOLR_CONF_DIR));
            
            File coreDir = new File(solrDir, SOLR_CORE_NAME);
            FileUtils.copyDirectory(coreConfiguration, coreDir);
        
        } catch (Exception ex) {
            log.error("Setup of the solr directory failed: ",ex);
        }
        
    }
    
    private void init(){
        String setupSolrParam  = context.getInitParameter(SETUP_SOLR);
        if(setupSolrParam != null){
            this.setupSolr = Boolean.parseBoolean(setupSolrParam);
        }
    }
}

@@ -1070,124 +1070,25 @@
 lrj.
-embedded.EmbeddedSolrServer;%0Aimport org.apache.solr.core.CoreContainer;%0Aimport org.apache.solr.core.SolrResourceLoad
+impl.HttpSolrServ
 er;%0A
@@ -1386,265 +1386,37 @@
    p
-ublic static final String DATA_DIR = %22flamingo.data.dir%22;%0A    private static final String SOLR_DIR = %22.solr/%22;%0A    private static final String SOLR_CORE_NAME = %22autosuggest%22;%0A    private static SolrServer server;%0A    private static CoreContainer coreContain
+rivate static SolrServer serv
 er;%0A
@@ -1492,16 +1492,21 @@
 class);%0A
+    %0A
     priv
@@ -1541,42 +1541,236 @@
     
-private String datadirectory;%0A    
+%0A    // Defaults%0A    private static final String SOLR_DIR = %22.solr/%22;%0A    private static final String SOLR_CORE_NAME = %22autosuggest%22;%0A    %0A    // Configuration names%0A    public static final String DATA_DIR = %22flamingo.data.dir%22;
 %0A   
@@ -1827,16 +1827,47 @@
 setup%22;%0A
+    %0A    // Context parameters%0A
     priv
@@ -1897,16 +1897,50 @@
  false;%0A
+    private String datadirectory;%0A
     %0A   
@@ -2207,57 +2207,13 @@
     
-datadirectory = context.getInitParameter(DATA_DIR
+init(
 );%0A 
@@ -2767,24 +2767,33 @@
 r(solrDir);%0A
+        %0A
     %7D%0A%0A    p
@@ -2911,70 +2911,8 @@
 ;  %0A
-        coreContainer = new CoreContainer(solrdir.getPath());%0A
     
@@ -3071,212 +3071,85 @@
     
-SolrResourceLoader loader = new SolrResourceLoader(path);%0A        if(coreContainer == null)%7B%0A            coreContainer = new CoreContainer(loader);%0A        %7D%0A        coreContainer.load();%0A        server =
+server = new HttpSolrServer(%22http://localhost:8084/solr/%22 +SOLR_CORE_NAME);//
  new
@@ -3366,42 +3366,8 @@
 ) %7B%0A
-        coreContainer.shutdown();%0A
     
@@ -4413,20 +4413,80 @@
 ;%0A        %7D%0A
+        datadirectory = context.getInitParameter(DATA_DIR);%0A
     %7D%0A%7D%0A

Initialize an http server instead of embedded. This solves a lot of problem, one of which is the locking of the index by the 2 instances