jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1291437 - in /jackrabbit/branches/2.4: ./ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/ jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/
Date Mon, 20 Feb 2012 20:45:28 GMT
Author: jukka
Date: Mon Feb 20 20:45:28 2012
New Revision: 1291437

URL: http://svn.apache.org/viewvc?rev=1291437&view=rev
Log:
2.4: Merged revision 1291424 (JCR-3236)

Added:
    jackrabbit/branches/2.4/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/SearchIndexTest.java
      - copied unchanged from r1291424, jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/SearchIndexTest.java
Modified:
    jackrabbit/branches/2.4/   (props changed)
    jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingConfigurationImpl.java
    jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JackrabbitAnalyzer.java
    jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java

Propchange: jackrabbit/branches/2.4/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Feb 20 20:45:28 2012
@@ -1,3 +1,3 @@
 /jackrabbit/branches/JCR-2272:1173165-1176545
 /jackrabbit/sandbox/JCR-2415-lucene-3.0:1060860-1064038
-/jackrabbit/trunk:1221447,1221579,1221593,1221789,1221818,1225179,1225191,1225196,1225207,1225525,1225528,1226452,1226472,1226515,1226750,1226863,1227171,1227240,1227590,1227593,1227615,1228058,1228149,1228155,1228160,1230507,1230681,1230688,1231204,1232035,1232100,1232404,1232831,1232920,1232922,1233069,1233344,1233446,1233468,1233471,1234807,1235192,1235375,1235423,1236709,1236775,1236819-1236821,1240053,1245443
+/jackrabbit/trunk:1221447,1221579,1221593,1221789,1221818,1225179,1225191,1225196,1225207,1225525,1225528,1226452,1226472,1226515,1226750,1226863,1227171,1227240,1227590,1227593,1227615,1228058,1228149,1228155,1228160,1230507,1230681,1230688,1231204,1232035,1232100,1232404,1232831,1232920,1232922,1233069,1233344,1233446,1233468,1233471,1234807,1235192,1235375,1235423,1236709,1236775,1236819-1236821,1240053,1245443,1291424

Modified: jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingConfigurationImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingConfigurationImpl.java?rev=1291437&r1=1291436&r2=1291437&view=diff
==============================================================================
--- jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingConfigurationImpl.java
(original)
+++ jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingConfigurationImpl.java
Mon Feb 20 20:45:28 2012
@@ -159,41 +159,26 @@ public class IndexingConfigurationImpl
                 for (int j = 0; j < childNodes.getLength(); j++) {
                     Node analyzerNode = childNodes.item(j);
                     if (analyzerNode.getNodeName().equals("analyzer")) {
-                        String analyzerClassName = analyzerNode.getAttributes().getNamedItem("class").getNodeValue();
-                        try {
-                            @SuppressWarnings("rawtypes")
-                            Class clazz = Class.forName(analyzerClassName);
-                            if (clazz == JackrabbitAnalyzer.class) {
-                                log.warn("Not allowed to configure " + JackrabbitAnalyzer.class.getName()
+  " for a property. "
-                                        + "Using default analyzer for that property.");
-                            }
-                            else if (Analyzer.class.isAssignableFrom(clazz)) {
-                                Analyzer analyzer = (Analyzer) clazz.newInstance();
-                                NodeList propertyChildNodes = analyzerNode.getChildNodes();
-                                for (int k = 0; k < propertyChildNodes.getLength(); k++)
{
-                                    Node propertyNode = propertyChildNodes.item(k);
-                                    if (propertyNode.getNodeName().equals("property")) {
-                                        // get property name
-                                        Name propName = resolver.getQName(getTextContent(propertyNode));
-                                        String fieldName = nsMappings.translateName(propName);
-                                        // set analyzer for the fulltext property fieldname
-                                        int idx = fieldName.indexOf(':');
-                                        fieldName = fieldName.substring(0, idx + 1)
-                                                    + FieldNames.FULLTEXT_PREFIX + fieldName.substring(idx
+ 1);
-                                        Object prevAnalyzer = analyzers.put(fieldName, analyzer);
-                                        if (prevAnalyzer != null) {
-                                            log.warn("Property " + propName.getLocalName()
-                                                    + " has been configured for multiple
analyzers. "
-                                                    + " Last configured analyzer is used");
-                                        }
-                                    }
+                        Analyzer analyzer = JackrabbitAnalyzer.getAnalyzerInstance(
+                                analyzerNode.getAttributes().getNamedItem("class").getNodeValue());
+                        NodeList propertyChildNodes = analyzerNode.getChildNodes();
+                        for (int k = 0; k < propertyChildNodes.getLength(); k++) {
+                            Node propertyNode = propertyChildNodes.item(k);
+                            if (propertyNode.getNodeName().equals("property")) {
+                                // get property name
+                                Name propName = resolver.getQName(getTextContent(propertyNode));
+                                String fieldName = nsMappings.translateName(propName);
+                                // set analyzer for the fulltext property fieldname
+                                int idx = fieldName.indexOf(':');
+                                fieldName = fieldName.substring(0, idx + 1)
+                                        + FieldNames.FULLTEXT_PREFIX + fieldName.substring(idx
+ 1);
+                                Object prevAnalyzer = analyzers.put(fieldName, analyzer);
+                                if (prevAnalyzer != null) {
+                                    log.warn("Property " + propName.getLocalName()
+                                            + " has been configured for multiple analyzers.
"
+                                            + " Last configured analyzer is used");
                                 }
-                            } else {
-                                log.warn("org.apache.lucene.analysis.Analyzer is not a superclass
of "
-                                        + analyzerClassName + ". Ignoring this configure
analyzer" );
                             }
-                        } catch (ClassNotFoundException e) {
-                            log.warn("Analyzer class not found: " + analyzerClassName, e);
                         }
                     }
                 }

Modified: jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JackrabbitAnalyzer.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JackrabbitAnalyzer.java?rev=1291437&r1=1291436&r2=1291437&view=diff
==============================================================================
--- jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JackrabbitAnalyzer.java
(original)
+++ jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/JackrabbitAnalyzer.java
Mon Feb 20 20:45:28 2012
@@ -18,12 +18,15 @@ package org.apache.jackrabbit.core.query
 
 import java.io.IOException;
 import java.io.Reader;
+import java.lang.reflect.Constructor;
 import java.util.Collections;
 
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.TokenStream;
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.util.Version;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This is the global jackrabbit lucene analyzer. By default, all
@@ -34,14 +37,68 @@ import org.apache.lucene.util.Version;
  * indexed with a specific analyzer. If configured, this analyzer is used to
  * index the text of the property and to parse searchtext for this property.
  */
+public class JackrabbitAnalyzer extends Analyzer {
 
-public class JackrabbitAnalyzer  extends Analyzer {
+    private static Logger log =
+            LoggerFactory.getLogger(JackrabbitAnalyzer.class);
+
+    private static final Analyzer DEFAULT_ANALYZER =
+            new StandardAnalyzer(Version.LUCENE_24, Collections.emptySet());
 
     /**
-     * The default Jackrabbit analyzer if none is configured in <code><SearchIndex></code>
-     * configuration.
+     * Returns a new instance of the named Lucene {@link Analyzer} class,
+     * or the default analyzer if the given class can not be instantiated.
+     *
+     * @param className name of the analyzer class
+     * @return new analyzer instance, or the default analyzer
      */
-    private Analyzer defaultAnalyzer =  new StandardAnalyzer(Version.LUCENE_24, Collections.emptySet());
+    static Analyzer getAnalyzerInstance(String className) {
+        Class<?> analyzerClass;
+        try {
+            analyzerClass = Class.forName(className);
+        } catch (ClassNotFoundException e) {
+            log.warn(className + " could not be found", e);
+            return DEFAULT_ANALYZER;
+        }
+        if (!Analyzer.class.isAssignableFrom(analyzerClass)) {
+            log.warn(className + " is not a Lucene Analyzer");
+            return DEFAULT_ANALYZER;
+        } else if (JackrabbitAnalyzer.class.isAssignableFrom(analyzerClass)) {
+            log.warn(className + " can not be used as a JackrabbitAnalyzer component");
+            return DEFAULT_ANALYZER;
+        }
+
+        Exception cause = null;
+        Constructor<?>[] constructors = analyzerClass.getConstructors();
+        for (Constructor<?> constructor : constructors) {
+            Class<?>[] types = constructor.getParameterTypes();
+            if (types.length == 1 && types[0] == Version.class) {
+                try {
+                    return (Analyzer) constructor.newInstance(Version.LUCENE_24);
+                } catch (Exception e) {
+                    cause = e;
+                }
+            }
+        }
+        for (Constructor<?> constructor : constructors) {
+            if (constructor.getParameterTypes().length == 0) {
+                try {
+                    return (Analyzer) constructor.newInstance();
+                } catch (Exception e) {
+                    cause = e;
+                }
+            }
+        }
+
+        log.warn(className + " could not be instantiated", cause);
+        return DEFAULT_ANALYZER;
+    }
+
+    /**
+     * The default Jackrabbit analyzer if none is configured in
+     * <code>&lt;SearchIndex&gt;</code> configuration.
+     */
+    private Analyzer defaultAnalyzer = DEFAULT_ANALYZER;
 
     /**
      * The indexing configuration.
@@ -62,6 +119,14 @@ public class JackrabbitAnalyzer  extends
         defaultAnalyzer = analyzer;
     }
 
+    String getDefaultAnalyzerClass() {
+        return defaultAnalyzer.getClass().getName();
+    }
+
+    void setDefaultAnalyzerClass(String className) {
+        setDefaultAnalyzer(getAnalyzerInstance(className));
+    }
+
     /**
      * Creates a TokenStream which tokenizes all the text in the provided
      * Reader. If the fieldName (property) is configured to have a different

Modified: jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java?rev=1291437&r1=1291436&r2=1291437&view=diff
==============================================================================
--- jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
(original)
+++ jackrabbit/branches/2.4/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
Mon Feb 20 20:45:28 2012
@@ -99,6 +99,7 @@ import org.apache.lucene.search.Similari
 import org.apache.lucene.search.Sort;
 import org.apache.lucene.search.SortField;
 import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.util.Version;
 import org.apache.tika.config.TikaConfig;
 import org.apache.tika.fork.ForkParser;
 import org.apache.tika.parser.AutoDetectParser;
@@ -1867,11 +1868,12 @@ public class SearchIndex extends Abstrac
     //--------------------------< properties >----------------------------------
 
     /**
-     * Sets the analyzer in use for indexing. The given analyzer class name
-     * must satisfy the following conditions:
+     * Sets the default analyzer in use for indexing. The given analyzer
+     * class name must satisfy the following conditions:
      * <ul>
      *   <li>the class must exist in the class path</li>
-     *   <li>the class must have a public default constructor</li>
+     *   <li>the class must have a public default constructor, or
+     *       a constructor that takes a Lucene {@link Version} argument</li>
      *   <li>the class must be a Lucene Analyzer</li>
      * </ul>
      * <p>
@@ -1886,21 +1888,16 @@ public class SearchIndex extends Abstrac
      * @param analyzerClassName the analyzer class name
      */
     public void setAnalyzer(String analyzerClassName) {
-        try {
-            Class<?> analyzerClass = Class.forName(analyzerClassName);
-            analyzer.setDefaultAnalyzer((Analyzer) analyzerClass.newInstance());
-        } catch (Exception e) {
-            log.warn("Invalid Analyzer class: " + analyzerClassName, e);
-        }
+        analyzer.setDefaultAnalyzerClass(analyzerClassName);
     }
 
     /**
-     * Returns the class name of the analyzer that is currently in use.
+     * Returns the class name of the default analyzer that is currently in use.
      *
      * @return class name of analyzer in use.
      */
     public String getAnalyzer() {
-        return analyzer.getClass().getName();
+        return analyzer.getDefaultAnalyzerClass();
     }
 
     /**



Mime
View raw message