You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ry...@apache.org on 2007/06/30 20:34:48 UTC

svn commit: r552195 - in /lucene/solr/trunk/src/java/org/apache/solr/handler: StaxUpdateRequestHandler.java UpdateRequestProcessorFactory.java

Author: ryan
Date: Sat Jun 30 11:34:47 2007
New Revision: 552195

URL: http://svn.apache.org/viewvc?view=rev&rev=552195
Log:
SOLR-269:  Using a factory pattern to generate UpdateRequestProcessor.

Added:
    lucene/solr/trunk/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java   (with props)
Modified:
    lucene/solr/trunk/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java

Modified: lucene/solr/trunk/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java?view=diff&rev=552195&r1=552194&r2=552195
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java Sat Jun 30 11:34:47 2007
@@ -20,7 +20,6 @@
 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 @@
  * 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 static final String ALLOW_DUPS = "allowDups"; 
   
   private XMLInputFactory inputFactory;
-  private Constructor<? extends UpdateRequestProcessor> processorConstructor;
+  private UpdateRequestProcessorFactory processorFactory;
   
   @SuppressWarnings("unchecked")
   @Override
@@ -90,44 +92,20 @@
   {
     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 @@
           InstantiationException, IllegalAccessException,
           TransformerConfigurationException 
   {
-    UpdateRequestProcessor processor = getUpdateRequestProcessor( req );
+    UpdateRequestProcessor processor = processorFactory.getInstance( req );
     
     AddUpdateCommand addCmd = null;   
     while (true) {
@@ -352,7 +330,6 @@
         } 
         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 @@
                 doc.setBoost( name, boost );
               }
             }
+            doc.addField(name, text.toString() );
           }
         }
         break;

Added: lucene/solr/trunk/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java?view=auto&rev=552195
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java (added)
+++ lucene/solr/trunk/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java Sat Jun 30 11:34:47 2007
@@ -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 );
+  }
+}

Propchange: lucene/solr/trunk/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/solr/trunk/src/java/org/apache/solr/handler/UpdateRequestProcessorFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL