Example usage for org.apache.solr.core CoreContainer CoreContainer

List of usage examples for org.apache.solr.core CoreContainer CoreContainer

Introduction

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

Prototype

public CoreContainer(NodeConfig config, Properties properties) 

Source Link

Usage

From source file:at.pagu.soldockr.core.EmbeddedSolrServerFactory.java

License:Apache License

/**
 * @param path Any Path expression valid for use with {@link ResourceUtils}
 * @return new instance of {@link EmbeddedSolrServer}
 * @throws ParserConfigurationException//from   ww w .  j  av  a  2s.  c  om
 * @throws IOException
 * @throws SAXException
 */
public final EmbeddedSolrServer createPathConfiguredSolrServer(String path)
        throws ParserConfigurationException, IOException, SAXException {
    String solrHome = System.getProperty(SOLR_HOME_SYSTEM_PROPERTY);

    if (StringUtils.isBlank(solrHome)) {
        solrHome = StringUtils.remove(ResourceUtils.getURL(path).toString(), "file:/");
    }
    return new EmbeddedSolrServer(new CoreContainer(solrHome, new File(solrHome + "/solr.xml")), null);
}

From source file:com.googlecode.fascinator.indexer.SolrWrapperQueueConsumer.java

License:Open Source License

/**
 * Initialize a Solr core object./*from  w ww  . ja v  a  2s .  c  o m*/
 * 
 * @param coreName : The core to initialize
 * @return SolrServer : The initialized core
 */
private SolrServer initCore(String core) {
    boolean isEmbedded = globalConfig.getBoolean(false, "indexer", core, "embedded");
    try {
        // Embedded Solr
        if (isEmbedded) {
            // Solr over HTTP - Needed to run commits
            // so the core web server sees them.
            String uri = globalConfig.getString(null, "indexer", core, "uri");
            if (uri == null) {
                log.error("No URI provided for core: '{}'", core);
                return null;
            }
            URI solrUri = new URI(uri);
            commit = new CommonsHttpSolrServer(solrUri.toURL());
            username = globalConfig.getString(null, "indexer", core, "username");
            password = globalConfig.getString(null, "indexer", core, "password");
            if (username != null && password != null) {
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
                HttpClient hc = ((CommonsHttpSolrServer) solr).getHttpClient();
                hc.getParams().setAuthenticationPreemptive(true);
                hc.getState().setCredentials(AuthScope.ANY, credentials);
            }

            // First time execution
            if (coreContainer == null) {
                String home = globalConfig.getString(DEFAULT_SOLR_HOME, "indexer", "home");
                log.info("Embedded Solr Home = {}", home);
                File homeDir = new File(home);
                if (!homeDir.exists()) {
                    log.error("Solr directory does not exist!");
                    return null;
                }
                System.setProperty("solr.solr.home", homeDir.getAbsolutePath());
                File coreXmlFile = new File(homeDir, "solr.xml");
                coreContainer = new CoreContainer(homeDir.getAbsolutePath(), coreXmlFile);
                for (SolrCore aCore : coreContainer.getCores()) {
                    log.info("Loaded core: {}", aCore.getName());
                }
            }
            String coreName = globalConfig.getString(null, "indexer", core, "coreName");
            if (coreName == null) {
                log.error("No 'coreName' node for core: '{}'", core);
                return null;
            }
            return new EmbeddedSolrServer(coreContainer, coreName);

            // Solr over HTTP
        } else {
            String uri = globalConfig.getString(null, "indexer", core, "uri");
            if (uri == null) {
                log.error("No URI provided for core: '{}'", core);
                return null;
            }

            URI solrUri = new URI(uri);
            CommonsHttpSolrServer thisCore = new CommonsHttpSolrServer(solrUri.toURL());
            username = globalConfig.getString(null, "indexer", core, "username");
            password = globalConfig.getString(null, "indexer", core, "password");
            if (username != null && password != null) {
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
                HttpClient hc = thisCore.getHttpClient();
                hc.getParams().setAuthenticationPreemptive(true);
                hc.getState().setCredentials(AuthScope.ANY, credentials);
            }
            return thisCore;
        }

    } catch (MalformedURLException mue) {
        log.error(core + " : Malformed URL", mue);
    } catch (URISyntaxException urise) {
        log.error(core + " : Invalid URI", urise);
    } catch (IOException ioe) {
        log.error(core + " : Failed to read Solr configuration", ioe);
    } catch (ParserConfigurationException pce) {
        log.error(core + " : Failed to parse Solr configuration", pce);
    } catch (SAXException saxe) {
        log.error(core + " : Failed to load Solr configuration", saxe);
    }
    return null;
}

From source file:com.indoqa.solr.server.factory.EmbeddedSolrServerBuilder.java

License:Apache License

public static SolrServer build(String url, String embeddedSolrConfigurationDir) throws IOException {
    String solrHome = getNormalizedPath(embeddedSolrConfigurationDir);

    InputStream solrXmlInputStream = EmbeddedSolrServerBuilder.class
            .getResourceAsStream("/embedded-core-container/solr.xml");

    SolrResourceLoader loader = new SolrResourceLoader(solrHome);
    ConfigSolr configSolr = ConfigSolr.fromInputStream(loader, solrXmlInputStream);

    solrXmlInputStream.close();/*from   w  ww.  jav  a2 s.c  o  m*/

    CoreContainer container = new CoreContainer(loader, configSolr);
    container.load();

    String dataDir = getNormalizedPath(getDataDir(url));
    CoreDescriptor coreDescriptor = new CoreDescriptor(container, "Embedded Core", solrHome, CORE_DATADIR,
            dataDir);
    SolrCore core = container.create(coreDescriptor);

    return new EmbeddedSolrServer(container, core.getName());
}

From source file:edu.unc.lib.dl.data.ingest.solr.action.BaseEmbeddedSolrTest.java

License:Apache License

@Before
public void setUp() throws Exception {

    File home = new File("src/test/resources/config");
    File configFile = new File(home, "solr.xml");

    System.setProperty("solr.data.dir", "src/test/resources/config/data/");
    container = new CoreContainer("src/test/resources/config", configFile);

    server = new EmbeddedSolrServer(container, "access-master");

    driver = new SolrUpdateDriver();
    driver.setSolrServer(server);//from ww w  .  j  a  v a 2  s.co m
    driver.setUpdateSolrServer(server);
}

From source file:edu.unc.lib.dl.ui.service.AbstractSolrQueryLayerTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    initMocks(this);

    File home = new File("src/test/resources/config");
    File configFile = new File(home, "solr.xml");

    File dataDir = tmpFolder.newFolder("solrdata");
    System.setProperty("solr.data.dir", dataDir.getAbsolutePath());
    container = new CoreContainer("src/test/resources/config", configFile);

    server = new EmbeddedSolrServer(container, "access-master");

    searchSettings = new SearchSettings();
    Properties properties = new Properties();
    properties.load(new FileInputStream(new File("src/test/resources/search.properties")));
    searchSettings.setProperties(properties);

    solrSettings = new SolrSettings();
    Properties solrProperties = new Properties();
    solrProperties.load(new FileInputStream(new File("src/test/resources/solr.properties")));
    solrSettings.setProperties(solrProperties);

    stateFactory = new SearchStateFactory();
    stateFactory.setSearchSettings(searchSettings);

    facetUtil = new FacetFieldUtil();
    facetUtil.setSearchSettings(searchSettings);
    facetUtil.setSolrSettings(solrSettings);

    queryLayer = new SolrQueryLayerService();
    queryLayer.setCollectionsPid(new PID("cdr:Collections"));
    queryLayer.setSearchSettings(searchSettings);
    queryLayer.setSolrSettings(solrSettings);
    queryLayer.setSearchStateFactory(stateFactory);
    queryLayer.setFacetFieldUtil(facetUtil);
    setField(queryLayer, "server", server);
}

From source file:learning.SolrJTest.java

License:Mozilla Public License

private SolrServer startUpSolrServer() throws Exception {
    File solrConfigXml = new File(solrHomeRelativePath + solrXMLHomeRelativePath);
    String solrHome = solrHomeRelativePath;
    coreContainer = new CoreContainer(solrHome, solrConfigXml);
    SolrServer solrServer = new EmbeddedSolrServer(coreContainer, defaultCollectionName);
    return solrServer;
}

From source file:org.ambraproject.testutils.EmbeddedSolrServerFactory.java

License:Apache License

/**
 * Constructor.//from www. j a v a 2  s.c o m
 *
 * TODO: this constructor does a lot of IO, and can throw IOException, not ideal
 * in a constructor.  Refactor to eliminate this.
 */
public EmbeddedSolrServerFactory() throws IOException {

    // Build a directory structure for the embedded solr server's solr home,
    // and copy configuration files there from the classpath.
    solrHome = System.getProperty("java.io.tmpdir") + File.separator + "EmbeddedSolrTest_"
            + System.currentTimeMillis();
    File conf = new File(solrHome + File.separator + "collection1" + File.separator + "conf");
    if (!conf.mkdirs()) {
        throw new RuntimeException("Could not create dir " + conf.getCanonicalPath());
    }
    destSolrXmlFile = solrHome + File.separator + "solr.xml";
    copyResource("solr/collection1/conf/test-solr.xml", destSolrXmlFile);
    copyResource("solr/collection1/conf/test-solr-config.xml",
            conf.getCanonicalPath() + File.separator + "solrconfig.xml");
    copyResource("solr/collection1/conf/test-solr-schema.xml",
            conf.getCanonicalPath() + File.separator + "schema.xml");
    copyResource("solr/collection1/conf/stopwords.txt",
            conf.getCanonicalPath() + File.separator + "stopwords.txt");
    copyResource("solr/collection1/conf/author_stopwords.txt",
            conf.getCanonicalPath() + File.separator + "author_stopwords.txt");

    System.setProperty("solr.solr.home", solrHome);
    CoreContainer coreContainer = new CoreContainer(solrHome, new File(destSolrXmlFile));
    server = new EmbeddedSolrServer(coreContainer, "collection1");
    log.info("EmbeddedSolrServer started with solr home " + solrHome);
}

From source file:org.anon.smart.d2cache.store.index.solr.SolrConnection.java

License:Open Source License

public void connect(StoreConfig cfg) throws CtxException {

    assertion().assertTrue((cfg instanceof SolrConfig),
            "Cannot create solr indexing with no solr configuration");
    _config = cfg;//from   ww w . ja  v a  2 s  . c  om
    try {
        if (_container == null) {
            SolrConfig scfg = (SolrConfig) cfg;
            _home = new File(scfg.getIndexHome());
            assertion().assertTrue(_home.exists(),
                    "The solr home directory does not exist. Please setup solr correctly."
                            + scfg.getIndexHome());
            File solr = new File(_home, CORE_CONFIG);
            assertion().assertTrue(solr.exists(),
                    "The solr config file does not exist. Please setup solr correctly." + CORE_CONFIG);
            _container = new CoreContainer(scfg.getIndexHome(), solr);
            //_container.load(scfg.getIndexHome(), solr);
        }

    } catch (Exception e) {
        System.out.println(">>>>>>>>>>>>>>>>>exceptionconnecting to: ");
        e.printStackTrace();
        except().rt(e, new CtxException.Context("SolrConnection.connect", "Exception"));
    }
}

From source file:org.dataconservancy.dcs.access.maven.IndexArchiveMojo.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    final ElmArchiveStore archive = prepareArchive(getElmEntityDirectory(), getElmMetadataDirectory());

    //  <bean id="solrServer" class="org.apache.solr.client.solrj.embedded.EmbeddedSolrServer">
    //    <constructor-arg>
    //      <bean class="org.apache.solr.core.CoreContainer">
    //        <constructor-arg value="${solr.solr.home}"/>
    //        <constructor-arg value="${solr.solr.config}"/>
    //      </bean>
    //    </constructor-arg>
    //    <constructor-arg value="${solr.containername}"/>
    //  </bean>

    final EmbeddedSolrServer solrServer;

    try {/*from w  w  w . ja v a 2s . c  om*/
        solrServer = new EmbeddedSolrServer(new CoreContainer(solrHome.getAbsolutePath(), solrCoreConfig),
                solrContainerName);
    } catch (ParserConfigurationException e) {
        throw new MojoExecutionException(e.getMessage());
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    } catch (SAXException e) {
        throw new MojoExecutionException(e.getMessage());
    }

    final DcsSolrAccessService accessSvc;

    try {
        accessSvc = new DcsSolrAccessService(solrServer, archive);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    }

    try {
        accessSvc.indexArchive();
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    }

}

From source file:org.dataconservancy.dcs.index.dcpsolr.SolrHomeSetBySpringPropertiesTest.java

License:Apache License

@Test
public void testStartSolrDefaultContainer()
        throws IOException, SolrServerException, SAXException, ParserConfigurationException {
    System.setProperty("solr.solr.home", solrHome);
    final File solrXml = new File(solrHome, "solr.xml");
    assertTrue("Cannot find '" + solrXml.getAbsolutePath(),
            solrXml.exists() && solrXml.canRead() && solrXml.isFile());
    final CoreContainer container = new CoreContainer(solrHome, solrXml);
    final EmbeddedSolrServer server = new EmbeddedSolrServer(container, "default");

    // TODO: I'm not sure how to interpret the ping status response.
    //        final int pingStatus = server.ping().getStatus();
    //        assertTrue("Unable to start Embedded Solr Server: " + pingStatus,
    //                pingStatus >= 200 && pingStatus < 400);
}