Skip to content

Apache Lucene open-source search software

SOLR-269: Using a factory pattern to generate UpdateRequestProcessor.

Ryan McKinley579b316839b8
diff --git a/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java b/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java
index 57a0a94d93e..2abd95ec8ad 100644
--- a/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java
+++ b/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java
@@ -20,7 +20,6 @@ package org.apache.solr.handler;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
-import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.logging.Logger;
@@ -57,15 +56,18 @@ import org.apache.solr.update.DeleteUpdateCommand;
* To change the UpdateRequestProcessor implementation, add the configuration parameter:
*
* <requestHandler name="/update" class="solr.StaxUpdateRequestHandler" >
- * <str name="update.processor.class">org.apache.solr.handler.UpdateRequestProcessor</str>
+ * <str name="update.processor.class">org.apache.solr.handler.UpdateRequestProcessor</str>
+ * <lst name="update.processor.args">
+ * ... (optionally pass in arguments to the factory init method) ...
+ * </lst>
* </requestHandler>
- *
*/
public class StaxUpdateRequestHandler extends XmlUpdateRequestHandler
{
public static Logger log = Logger.getLogger(StaxUpdateRequestHandler.class.getName());
- public static final String UPDATE_PROCESSOR_CLASS = "update.processor.class";
+ public static final String UPDATE_PROCESSOR_FACTORY = "update.processor.factory";
+ public static final String UPDATE_PROCESSOR_ARGS = "update.processor.args";
// XML Constants
public static final String ADD = "add";
@@ -82,7 +84,7 @@ public class StaxUpdateRequestHandler extends XmlUpdateRequestHandler
public static final String ALLOW_DUPS = "allowDups";
private XMLInputFactory inputFactory;
- private Constructor<? extends UpdateRequestProcessor> processorConstructor;
+ private UpdateRequestProcessorFactory processorFactory;
@SuppressWarnings("unchecked")
@Override
@@ -90,44 +92,20 @@ public class StaxUpdateRequestHandler extends XmlUpdateRequestHandler
{
super.init(args);
inputFactory = BaseXMLInputFactory.newInstance();
-
- Class<? extends UpdateRequestProcessor> clazz = null;
- String className = null;
- try {
- if( args != null ) {
- className = (String)args.get( UPDATE_PROCESSOR_CLASS );
- if( className != null ) {
- clazz = Config.findClass( className, new String[]{} );
- }
- }
- if( clazz == null ) {
- clazz = UpdateRequestProcessor.class;
- }
- processorConstructor = clazz.getConstructor( SolrQueryRequest.class );
- }
- catch (Exception e) {
- throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,
- "error initializing processor class: "+className, e );
- }
- if( processorConstructor == null ) {
- throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,
- "invalid processor: " + clazz );
- }
- }
- /**
- * Let subclasses override what processor is used. The default behavior is
- * to use one configured through the init parameter "processor"
- */
- protected UpdateRequestProcessor getUpdateRequestProcessor( SolrQueryRequest req )
- {
- try {
- return processorConstructor.newInstance( new Object[] {req} );
+ // Initialize the UpdateRequestProcessorFactory
+ NamedList<Object> factoryargs = null;
+ if( args != null ) {
+ String className = (String)args.get( UPDATE_PROCESSOR_FACTORY );
+ factoryargs = (NamedList<Object>)args.get( UPDATE_PROCESSOR_ARGS );
+ if( className != null ) {
+ processorFactory = (UpdateRequestProcessorFactory)Config.newInstance( className, new String[]{} );
+ }
}
- catch( Exception ex ) {
- throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,
- "error making UpdateRequestProcessor", ex );
+ if( processorFactory == null ) {
+ processorFactory = new UpdateRequestProcessorFactory();
}
+ processorFactory.init( factoryargs );
}
@Override
@@ -167,7 +145,7 @@ public class StaxUpdateRequestHandler extends XmlUpdateRequestHandler
InstantiationException, IllegalAccessException,
TransformerConfigurationException
{
- UpdateRequestProcessor processor = getUpdateRequestProcessor( req );
+ UpdateRequestProcessor processor = processorFactory.getInstance( req );
AddUpdateCommand addCmd = null;
while (true) {
@@ -352,7 +330,6 @@ public class StaxUpdateRequestHandler extends XmlUpdateRequestHandler
}
else if ("field".equals(parser.getLocalName())) {
if (!isNull) {
- doc.addField(name, text.toString() );
if(boost != null) {
// The lucene API and solr XML field specification make it possible to set boosts
// on multi-value fields even though lucene indexing does not support this.
@@ -366,6 +343,7 @@ public class StaxUpdateRequestHandler extends XmlUpdateRequestHandler
doc.setBoost( name, boost );
}
}
+ doc.addField(name, text.toString() );
}
}
break;
diff --git a/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java b/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java
new file mode 100644
index 00000000000..a94c581e488
--- /dev/null
+++ b/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java
@@ -0,0 +1,47 @@
+/**
+ * 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;
+
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+
+/**
+ * A factory to generate UpdateRequestProcessors for each request. The default
+ * implementation does nothing except pass the commands directly to the
+ * UpdateHandler
+ *
+ * @author ryan
+ * @since solr 1.3
+ */
+public class UpdateRequestProcessorFactory
+{
+ public UpdateRequestProcessorFactory()
+ {
+
+ }
+
+ public void init( NamedList<Object> args )
+ {
+ // by default nothing...
+ }
+
+ public UpdateRequestProcessor getInstance( SolrQueryRequest req )
+ {
+ return new UpdateRequestProcessor( req );
+ }
+}