Apache Solr open-source search software
SOLR-2675: CoreAdminHandler now allows arbitrary properties to be specified when CREATEing a new SolrCore using property.* request params
Chris M. Hostetter75d3e1e37dd6
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 8a24f9149fe..165fbfa1172 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -377,6 +377,10 @@ New Featuresrelevant document of each group matching the query. This feature has thesame 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----------------------diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.javaindex 9f528622935..18d8e48a154 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java@@ -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());diff --git a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.javanew file mode 100644index 00000000000..68ef593d2aa--- /dev/null+++ b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java@@ -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);+ }+ }++}diff --git a/solr/solrj/src/java/org/apache/solr/common/params/CoreAdminParams.java b/solr/solrj/src/java/org/apache/solr/common/params/CoreAdminParams.javaindex 1e8e7022571..c86c37b9564 100644--- a/solr/solrj/src/java/org/apache/solr/common/params/CoreAdminParams.java+++ b/solr/solrj/src/java/org/apache/solr/common/params/CoreAdminParams.java@@ -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";