fi.vm.sade.organisaatio.service.search.EmbeddedSolrServerFactory.java Source code

Java tutorial

Introduction

Here is the source code for fi.vm.sade.organisaatio.service.search.EmbeddedSolrServerFactory.java

Source

/*
 * Copyright (c) 2012 The Finnish Board of Education - Opetushallitus
 *
 * This program is free software:  Licensed under the EUPL, Version 1.1 or - as
 * soon as they will be approved by the European Commission - subsequent versions
 * of the EUPL (the "Licence");
 *
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at: http://www.osor.eu/eupl/
 *
 * 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
 * European Union Public Licence for more details.
 */
package fi.vm.sade.organisaatio.service.search;

import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.core.CoreContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile(value = { "embedded-solr" })
public class EmbeddedSolrServerFactory extends SolrServerFactory {

    private final Logger log = LoggerFactory.getLogger(getClass());

    EmbeddedSolrServer server = null;

    @Value("${organisaatio.embedded-solr.home:#{null}}")
    private String solrHome;

    @Value("${organisaatio.embedded-solr.dataDir:#{null}}")
    private String solrData;

    public SolrServer getSolrServer() {
        log.info("**** Using embedded Solr server ****");
        if (StringUtils.isEmpty(solrHome)) {
            solrHome = "../organisaatio-service/src/main/resources/solr/";
        }

        if (StringUtils.isEmpty(solrData)) {
            solrData = "target/solr-data";
        }

        if (server == null) {
            System.setProperty("solr.solr.home", solrHome);
            System.setProperty("solr.data.dir", solrData);
            CoreContainer.Initializer initializer = new CoreContainer.Initializer();
            CoreContainer coreContainer = initializer.initialize();
            server = new EmbeddedSolrServer(coreContainer, "organisaatiot");
            System.clearProperty("solr.solr.home");
            System.clearProperty("solr.data.dir");
        }
        return server;
    }
}