mirror of https://github.com/apache/lucene.git
SOLR-1335 load core properties from a properties file
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@807914 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
97269bda04
commit
e1b225318d
|
@ -288,6 +288,8 @@ New Features
|
|||
|
||||
73. SOLR-1156: Sort TermsComponent results by frequency (Matt Weber via yonik)
|
||||
|
||||
74. SOLR-1384 : load core properties from a properties file (noble)
|
||||
|
||||
Optimizations
|
||||
----------------------
|
||||
1. SOLR-374: Use IndexReader.reopen to save resources by re-using parts of the
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.io.FileOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.io.InputStream;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
@ -45,6 +46,7 @@ import org.apache.solr.common.util.StrUtils;
|
|||
import org.apache.solr.common.util.FileUtils;
|
||||
import org.apache.solr.handler.admin.CoreAdminHandler;
|
||||
import org.apache.solr.schema.IndexSchema;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
@ -81,7 +83,7 @@ public class CoreContainer
|
|||
public Properties getContainerProperties() {
|
||||
return containerProperties;
|
||||
}
|
||||
|
||||
|
||||
// Helper class to initialize the CoreContainer
|
||||
public static class Initializer {
|
||||
protected String solrConfigFilename = null;
|
||||
|
@ -124,8 +126,11 @@ public class CoreContainer
|
|||
solrConfigFilename = cores.getConfigFile().getName();
|
||||
} else {
|
||||
// perform compatibility init
|
||||
cores = new CoreContainer(new SolrResourceLoader(solrHome));
|
||||
SolrConfig cfg = solrConfigFilename == null ? new SolrConfig() : new SolrConfig(solrConfigFilename);
|
||||
SolrResourceLoader resourceLoader = new SolrResourceLoader(solrHome, null, getCoreProps(solrHome, null,null));
|
||||
cores = new CoreContainer(resourceLoader);
|
||||
SolrConfig cfg = solrConfigFilename == null ?
|
||||
new SolrConfig(resourceLoader, SolrConfig.DEFAULT_CONF_FILE,null) :
|
||||
new SolrConfig(resourceLoader, solrConfigFilename,null);
|
||||
CoreDescriptor dcore = new CoreDescriptor(cores, "", ".");
|
||||
SolrCore singlecore = new SolrCore(null, null, cfg, null, dcore);
|
||||
abortOnConfigurationError = cfg.getBool(
|
||||
|
@ -138,6 +143,28 @@ public class CoreContainer
|
|||
}
|
||||
}
|
||||
|
||||
private static Properties getCoreProps(String instanceDir, String file, Properties defaults) {
|
||||
if(file == null) file = "conf"+File.separator+ "solrcore.properties";
|
||||
File corePropsFile = new File(file);
|
||||
if(!corePropsFile.isAbsolute()){
|
||||
corePropsFile = new File(instanceDir, file);
|
||||
}
|
||||
Properties p = defaults;
|
||||
if (corePropsFile.exists() && corePropsFile.isFile()) {
|
||||
p = new Properties(defaults);
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = new FileInputStream(corePropsFile);
|
||||
p.load(is);
|
||||
} catch (IOException e) {
|
||||
log.warn("Error loading properties ",e);
|
||||
} finally{
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initalize CoreContainer directly from the constructor
|
||||
*
|
||||
|
@ -231,6 +258,10 @@ public class CoreContainer
|
|||
if (opt != null) {
|
||||
p.setSchemaName(opt);
|
||||
}
|
||||
opt = DOMUtil.getAttr(node, "properties", null);
|
||||
if (opt != null) {
|
||||
p.setPropertiesName(opt);
|
||||
}
|
||||
opt = DOMUtil.getAttr(node, CoreAdminParams.DATA_DIR, null);
|
||||
if (opt != null) {
|
||||
p.setDataDir(opt);
|
||||
|
@ -364,7 +395,7 @@ public class CoreContainer
|
|||
String instanceDir = idir.getPath();
|
||||
|
||||
// Initialize the solr config
|
||||
SolrResourceLoader solrLoader = new SolrResourceLoader(instanceDir, libLoader, dcore.getCoreProperties());
|
||||
SolrResourceLoader solrLoader = new SolrResourceLoader(instanceDir, libLoader, getCoreProps(instanceDir, dcore.getPropertiesName(),dcore.getCoreProperties()));
|
||||
SolrConfig config = new SolrConfig(solrLoader, dcore.getConfigName(), null);
|
||||
IndexSchema schema = null;
|
||||
if(indexSchemaCache != null){
|
||||
|
@ -716,6 +747,10 @@ public class CoreContainer
|
|||
if (opt != null && !opt.equals(dcore.getDefaultSchemaName())) {
|
||||
writeAttribute(w,"schema",opt);
|
||||
}
|
||||
opt = dcore.getPropertiesName();
|
||||
if (opt != null) {
|
||||
writeAttribute(w,"properties",opt);
|
||||
}
|
||||
opt = dcore.dataDir;
|
||||
if (opt != null) writeAttribute(w,"dataDir",opt);
|
||||
if (dcore.getCoreProperties() == null || dcore.getCoreProperties().isEmpty())
|
||||
|
|
|
@ -30,6 +30,7 @@ public class CoreDescriptor {
|
|||
protected String instanceDir;
|
||||
protected String dataDir;
|
||||
protected String configName;
|
||||
protected String propertiesName;
|
||||
protected String schemaName;
|
||||
private final CoreContainer coreContainer;
|
||||
private Properties coreProperties;
|
||||
|
@ -83,6 +84,14 @@ public class CoreDescriptor {
|
|||
return "data" + File.separator;
|
||||
}
|
||||
|
||||
public String getPropertiesName() {
|
||||
return propertiesName;
|
||||
}
|
||||
|
||||
public void setPropertiesName(String propertiesName) {
|
||||
this.propertiesName = propertiesName;
|
||||
}
|
||||
|
||||
public String getDataDir() {
|
||||
String dataDir = this.dataDir;
|
||||
if (dataDir == null) dataDir = getDefaultDataDir();
|
||||
|
|
|
@ -0,0 +1,149 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.solr;
|
||||
|
||||
import org.apache.solr.util.AbstractSolrTestCase;
|
||||
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
|
||||
import org.apache.solr.client.solrj.SolrServer;
|
||||
import org.apache.solr.client.solrj.SolrServerException;
|
||||
import org.apache.solr.client.solrj.response.QueryResponse;
|
||||
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
|
||||
import org.apache.solr.common.params.ModifiableSolrParams;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* <p> Test for Loading core properties from a properties file </p>
|
||||
*
|
||||
* @version $Id$
|
||||
* @since solr 1.4
|
||||
*/
|
||||
public class TestSolrCoreProperties extends AbstractSolrTestCase {
|
||||
private static final String CONF_DIR = "." + File.separator + "solr" + File.separator + "conf" + File.separator;
|
||||
JettySolrRunner solrJetty;
|
||||
SolrServer client;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
// System.setProperty("foo.foo1", "f1");
|
||||
// System.setProperty("foo.foo2", "f2");
|
||||
setUpMe();
|
||||
System.setProperty("solr.solr.home", getHomeDir());
|
||||
System.setProperty("solr.data.dir", getDataDir());
|
||||
|
||||
solrJetty = new JettySolrRunner("/solr", 0);
|
||||
|
||||
solrJetty.start();
|
||||
String url = "http://localhost:" + solrJetty.getLocalPort() + "/solr";
|
||||
client = new CommonsHttpSolrServer(url);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
solrJetty.stop();
|
||||
AbstractSolrTestCase.recurseDelete(homeDir);
|
||||
}
|
||||
|
||||
public void testSimple() throws SolrServerException {
|
||||
ModifiableSolrParams params = new ModifiableSolrParams();
|
||||
params.add("q", "*:*");
|
||||
QueryResponse res = client.query(params);
|
||||
assertEquals(0, res.getResults().getNumFound());
|
||||
}
|
||||
|
||||
|
||||
|
||||
File homeDir;
|
||||
File confDir;
|
||||
|
||||
/**
|
||||
* if masterPort is null, this instance is a master -- otherwise this instance is a slave, and assumes the master is
|
||||
* on localhost at the specified port.
|
||||
*/
|
||||
|
||||
|
||||
public String getHomeDir() {
|
||||
return homeDir.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSchemaFile() {
|
||||
return CONF_DIR + "schema-replication1.xml";
|
||||
}
|
||||
|
||||
public String getConfDir() {
|
||||
return confDir.toString();
|
||||
}
|
||||
|
||||
public String getDataDir() {
|
||||
return dataDir.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSolrConfigFile() {
|
||||
return CONF_DIR + "solrconfig-solcoreproperties.xml";
|
||||
}
|
||||
|
||||
public void setUpMe() throws Exception {
|
||||
|
||||
String home = System.getProperty("java.io.tmpdir")
|
||||
+ File.separator
|
||||
+ getClass().getName() + "-" + System.currentTimeMillis();
|
||||
|
||||
|
||||
homeDir = new File(home);
|
||||
dataDir = new File(home, "data");
|
||||
confDir = new File(home, "conf");
|
||||
|
||||
|
||||
homeDir.mkdirs();
|
||||
dataDir.mkdirs();
|
||||
confDir.mkdirs();
|
||||
|
||||
File f = new File(confDir, "solrconfig.xml");
|
||||
copyFile(new File(getSolrConfigFile()), f);
|
||||
|
||||
f = new File(confDir, "schema.xml");
|
||||
copyFile(new File(getSchemaFile()), f);
|
||||
Properties p = new Properties();
|
||||
p.setProperty("foo.foo1","f1");
|
||||
p.setProperty("foo.foo2","f2");
|
||||
FileOutputStream fos = new FileOutputStream(confDir + File.separator + "solrcore.properties");
|
||||
p.store(fos,null);
|
||||
fos.close();
|
||||
IOUtils.closeQuietly(fos);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void copyFile(File src, File dst) throws IOException {
|
||||
BufferedReader in = new BufferedReader(new FileReader(src));
|
||||
Writer out = new FileWriter(dst);
|
||||
|
||||
for (String line = in.readLine(); null != line; line = in.readLine()) {
|
||||
out.write(line);
|
||||
}
|
||||
in.close();
|
||||
out.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
<?xml version="1.0" ?>
|
||||
|
||||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<!-- $Id: solrconfig-slave.xml 741684 2009-02-06 19:21:44Z shalin $
|
||||
$Source$
|
||||
$Name$
|
||||
-->
|
||||
|
||||
<config>
|
||||
|
||||
<dataDir>${solr.data.dir:./solr/data}</dataDir>
|
||||
|
||||
<indexDefaults>
|
||||
<useCompoundFile>false</useCompoundFile>
|
||||
<mergeFactor>10</mergeFactor>
|
||||
<ramBufferSizeMB>32</ramBufferSizeMB>
|
||||
<maxMergeDocs>2147483647</maxMergeDocs>
|
||||
<maxFieldLength>10000</maxFieldLength>
|
||||
<writeLockTimeout>1000</writeLockTimeout>
|
||||
<commitLockTimeout>10000</commitLockTimeout>
|
||||
|
||||
<writeLockTimeout>1000</writeLockTimeout>
|
||||
<commitLockTimeout>10000</commitLockTimeout>
|
||||
|
||||
<lockType>single</lockType>
|
||||
</indexDefaults>
|
||||
|
||||
<mainIndex>
|
||||
<useCompoundFile>false</useCompoundFile>
|
||||
<mergeFactor>10</mergeFactor>
|
||||
<ramBufferSizeMB>32</ramBufferSizeMB>
|
||||
<maxMergeDocs>2147483647</maxMergeDocs>
|
||||
<maxFieldLength>10000</maxFieldLength>
|
||||
|
||||
<unlockOnStartup>true</unlockOnStartup>
|
||||
</mainIndex>
|
||||
|
||||
<updateHandler class="solr.DirectUpdateHandler2">
|
||||
</updateHandler>
|
||||
|
||||
<requestHandler name="standard" class="solr.StandardRequestHandler">
|
||||
<bool name="httpCaching">true</bool>
|
||||
</requestHandler>
|
||||
|
||||
<!-- test query parameter defaults -->
|
||||
<requestHandler name="defaults" class="solr.StandardRequestHandler">
|
||||
|
||||
</requestHandler>
|
||||
<tag1>${foo.foo1}</tag1>
|
||||
<tag2>${foo.foo2}</tag2>
|
||||
|
||||
<!-- test query parameter defaults -->
|
||||
<requestHandler name="lazy" class="solr.StandardRequestHandler" startup="lazy">
|
||||
</requestHandler>
|
||||
|
||||
<requestHandler name="/update" class="solr.XmlUpdateRequestHandler"/>
|
||||
|
||||
<!-- enable streaming for testing... -->
|
||||
<requestDispatcher handleSelect="true">
|
||||
<requestParsers enableRemoteStreaming="true" multipartUploadLimitInKB="2048"/>
|
||||
<httpCaching lastModifiedFrom="openTime" etagSeed="Solr" never304="false">
|
||||
<cacheControl>max-age=30, public</cacheControl>
|
||||
</httpCaching>
|
||||
</requestDispatcher>
|
||||
|
||||
</config>
|
Loading…
Reference in New Issue