Skip to content

Apache Lucene open-source search software

SOLR-2581: UIMAToSolrMapper wrongly instantiates Type with reflection

Koji Sekiguchi52e432be3af3
diff --git a/solr/contrib/uima/CHANGES.txt b/solr/contrib/uima/CHANGES.txt
index 80b3c500fc5..be2132e8f91 100644
--- a/solr/contrib/uima/CHANGES.txt
+++ b/solr/contrib/uima/CHANGES.txt
@@ -31,6 +31,9 @@ Bug Fixes
* SOLR-2579: UIMAUpdateRequestProcessor ignore error fails if text.length() < 100.
(Elmer Garduno via koji)
+* SOLR-2581: UIMAToSolrMapper wrongly instantiates Type with reflection.
+ (Tommaso Teofili via koji)
+
================== 3.2.0 ==================
Upgrading from Solr 3.1
diff --git a/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAToSolrMapper.java b/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAToSolrMapper.java
index dfc531a34e1..80a96ef2e68 100644
--- a/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAToSolrMapper.java
+++ b/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAToSolrMapper.java
@@ -21,6 +21,7 @@ import java.util.Map;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField;
+import org.apache.solr.uima.processor.exception.FieldMappingException;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.Type;
@@ -49,27 +50,24 @@ public class UIMAToSolrMapper {
/**
* map features of a certain UIMA type to corresponding Solr fields based on the mapping
- *
- * @param typeName
- * name of UIMA type to map
+ *
+ * @param typeName name of UIMA type to map
* @param featureFieldsmapping
*/
- public void map(String typeName, Map<String, MapField> featureFieldsmapping) {
+ public void map(String typeName, Map<String, MapField> featureFieldsmapping) throws FieldMappingException {
try {
- FeatureStructure fsMock = (FeatureStructure) Class.forName(typeName).getConstructor(
- JCas.class).newInstance(cas);
- Type type = fsMock.getType();
+ Type type = cas.getTypeSystem().getType(typeName);
for (FSIterator<FeatureStructure> iterator = cas.getFSIndexRepository().getAllIndexedFS(type); iterator
- .hasNext();) {
+ .hasNext(); ) {
FeatureStructure fs = iterator.next();
for (String featureName : featureFieldsmapping.keySet()) {
MapField mapField = featureFieldsmapping.get(featureName);
String fieldNameFeature = mapField.getFieldNameFeature();
String fieldNameFeatureValue = fieldNameFeature == null ? null :
- fs.getFeatureValueAsString(type.getFeatureByBaseName(fieldNameFeature));
+ fs.getFeatureValueAsString(type.getFeatureByBaseName(fieldNameFeature));
String fieldName = mapField.getFieldName(fieldNameFeatureValue);
log.info(new StringBuffer("mapping ").append(typeName).append("@").append(featureName)
- .append(" to ").append(fieldName).toString());
+ .append(" to ").append(fieldName).toString());
String featureValue = null;
if (fs instanceof Annotation && "coveredText".equals(featureName)) {
featureValue = ((Annotation) fs).getCoveredText();
@@ -77,12 +75,13 @@ public class UIMAToSolrMapper {
featureValue = fs.getFeatureValueAsString(type.getFeatureByBaseName(featureName));
}
log.info(new StringBuffer("writing ").append(featureValue).append(" in ").append(
- fieldName).toString());
+ fieldName).toString());
document.addField(fieldName, featureValue, 1.0f);
}
}
} catch (Exception e) {
- log.error(e.getLocalizedMessage());
+ throw new FieldMappingException(e);
}
}
+
}
diff --git a/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAUpdateRequestProcessor.java b/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAUpdateRequestProcessor.java
index f0aa2b2e89c..9483a97cae9 100644
--- a/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAUpdateRequestProcessor.java
+++ b/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/UIMAUpdateRequestProcessor.java
@@ -17,24 +17,23 @@ package org.apache.solr.uima.processor;
* limitations under the License.
*/
-import java.io.IOException;
-import java.util.Map;
-
import org.apache.solr.common.SolrException;
-import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrException.ErrorCode;
+import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.core.SolrCore;
import org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField;
import org.apache.solr.uima.processor.ae.AEProvider;
import org.apache.solr.uima.processor.ae.AEProviderFactory;
import org.apache.solr.update.AddUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor;
-import org.apache.uima.UIMAException;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
+import java.io.IOException;
+import java.util.Map;
+
/**
* Update document(s) to be indexed with UIMA extracted information
*
@@ -69,7 +68,7 @@ public class UIMAUpdateRequestProcessor extends UpdateRequestProcessor {
String[] texts = getTextsToAnalyze(solrInputDocument);
for (int i = 0; i < texts.length; i++) {
text = texts[i];
- if (text != null && !"".equals(text)) {
+ if (text != null && text.length()>0) {
/* process the text value */
JCas jcas = processText(text);
@@ -83,7 +82,7 @@ public class UIMAUpdateRequestProcessor extends UpdateRequestProcessor {
}
}
}
- } catch (UIMAException e) {
+ } catch (Exception e) {
String logField = solrUIMAConfiguration.getLogField();
String optionalFieldInfo = logField == null ? "." :
new StringBuilder(". ").append(logField).append("=")
@@ -110,7 +109,7 @@ public class UIMAUpdateRequestProcessor extends UpdateRequestProcessor {
private String[] getTextsToAnalyze(SolrInputDocument solrInputDocument) {
String[] fieldsToAnalyze = solrUIMAConfiguration.getFieldsToAnalyze();
boolean merge = solrUIMAConfiguration.isFieldsMerging();
- String[] textVals = null;
+ String[] textVals;
if (merge) {
StringBuilder unifiedText = new StringBuilder("");
for (int i = 0; i < fieldsToAnalyze.length; i++) {
diff --git a/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/exception/FieldMappingException.java b/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/exception/FieldMappingException.java
new file mode 100644
index 00000000000..12eaf74a40d
--- /dev/null
+++ b/solr/contrib/uima/src/main/java/org/apache/solr/uima/processor/exception/FieldMappingException.java
@@ -0,0 +1,26 @@
+package org.apache.solr.uima.processor.exception;
+
+/*
+ * 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.
+ */
+
+/**
+ * Exception thrown when an error happening while mapping UIMA CAS model to Solt fields
+ */
+public class FieldMappingException extends Exception {
+ public FieldMappingException(Exception e) {
+ }
+}