SOLR-2675: CoreAdminHandler now allows arbitrary properties to be specified when CREATEing a new SolrCore using property.* request params

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1159418 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2011-08-18 21:13:56 +00:00
parent 7354f8d9e8
commit 75d3e1e37d
4 changed files with 140 additions and 2 deletions

View File

@ -377,6 +377,10 @@ New Features
relevant document of each group matching the query. This feature has the
same impact on the StatsComponent. (Martijn van Groningen)
* SOLR-2675: CoreAdminHandler now allows arbitrary properties to be
specified when CREATEing a new SolrCore using property.* request
params. (Yury Kats, hossman)
Optimizations
----------------------

View File

@ -46,6 +46,8 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
/**
*
@ -319,8 +321,20 @@ public class CoreAdminHandler extends RequestHandlerBase {
if (opts != null)
cd.setShardId(opts);
}
dcore.setCoreProperties(null);
// Process all property.name=value parameters and set them as name=value core properties
Properties coreProperties = new Properties();
Iterator<String> parameterNamesIterator = params.getParameterNamesIterator();
while (parameterNamesIterator.hasNext()) {
String parameterName = parameterNamesIterator.next();
if(parameterName.startsWith(CoreAdminParams.PROPERTY_PREFIX)) {
String parameterValue = params.get(parameterName);
String propertyName = parameterName.substring(CoreAdminParams.PROPERTY_PREFIX.length()); // skip prefix
coreProperties.put(propertyName, parameterValue);
}
}
dcore.setCoreProperties(coreProperties);
SolrCore core = coreContainer.create(dcore);
coreContainer.register(name, core, false);
rsp.add("core", core.getName());

View File

@ -0,0 +1,117 @@
/**
* 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.handler.admin;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.admin.CoreAdminHandler;
import org.apache.solr.common.params.CoreAdminParams;
import org.apache.solr.response.SolrQueryResponse;
import java.io.File;
import java.io.IOException;
import javax.xml.xpath.XPathExpressionException;
import org.apache.commons.io.FileUtils;
import org.apache.solr.SolrTestCaseJ4;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xml.sax.SAXException;
public class CoreAdminHandlerTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
initCore("solrconfig.xml", "schema.xml");
}
@Test
public void testCoreAdminHandler() throws Exception {
final File workDir = new File(TEMP_DIR, this.getClass().getName());
if (workDir.exists()) {
FileUtils.deleteDirectory(workDir);
}
assertTrue("Failed to mkdirs workDir", workDir.mkdirs());
final CoreContainer cores = h.getCoreContainer();
cores.setPersistent(false); // we'll do this explicitly as needed
final CoreAdminHandler admin = new CoreAdminHandler(cores);
String instDir = null;
{
SolrCore template = null;
try {
template = cores.getCore("collection1");
instDir = template.getCoreDescriptor().getInstanceDir();
} finally {
if (null != template) template.close();
}
}
final File instDirFile = new File(instDir);
assertTrue("instDir doesn't exist: " + instDir, instDirFile.exists());
final File instPropFile = new File(workDir, "instProp");
FileUtils.copyDirectory(instDirFile, instPropFile);
// create a new core (using CoreAdminHandler) w/ properties
SolrQueryResponse resp = new SolrQueryResponse();
admin.handleRequestBody
(req(CoreAdminParams.ACTION,
CoreAdminParams.CoreAdminAction.CREATE.toString(),
CoreAdminParams.INSTANCE_DIR, instPropFile.getAbsolutePath(),
CoreAdminParams.NAME, "props",
CoreAdminParams.PROPERTY_PREFIX + "hoss","man",
CoreAdminParams.PROPERTY_PREFIX + "foo","baz"),
resp);
assertNull("Exception on create", resp.getException());
// verify props are in persisted file
final File xml = new File(workDir, "persist-solr.xml");
cores.persistFile(xml);
assertXmlFile
(xml
,"/solr/cores/core[@name='props']/property[@name='hoss' and @value='man']"
,"/solr/cores/core[@name='props']/property[@name='foo' and @value='baz']"
);
}
public void assertXmlFile(final File file, String... xpath)
throws IOException, SAXException {
try {
String xml = FileUtils.readFileToString(file, "UTF-8");
String results = h.validateXPath(xml, xpath);
if (null != results) {
String msg = "File XPath failure: file=" + file.getPath() + " xpath="
+ results + "\n\nxml was: " + xml;
fail(msg);
}
} catch (XPathExpressionException e2) {
throw new RuntimeException("XPath is invalid", e2);
}
}
}

View File

@ -68,6 +68,9 @@ public interface CoreAdminParams
/** The shard id in solr cloud */
public final static String SHARD = "shard";
/** Prefix for core property name=value pair **/
public final static String PROPERTY_PREFIX = "property.";
/** If you unload a core, delete the index too */
public final static String DELETE_INDEX = "deleteIndex";