mirror of https://github.com/apache/lucene.git
SOLR-3260: better messages when ScriptTransform fails on init (remove Reflection in Trunk, JRE1.6+ assurred)
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1303470 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
acd6f46934
commit
dfc345b5c7
|
@ -19,10 +19,13 @@ package org.apache.solr.handler.dataimport;
|
||||||
import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
|
import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
|
||||||
import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
|
import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.script.Invocable;
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptEngineManager;
|
||||||
|
import javax.script.ScriptException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* A {@link Transformer} instance capable of executing functions written in scripting
|
* A {@link Transformer} instance capable of executing functions written in scripting
|
||||||
|
@ -40,11 +43,8 @@ import java.util.Map;
|
||||||
* @since solr 1.3
|
* @since solr 1.3
|
||||||
*/
|
*/
|
||||||
public class ScriptTransformer extends Transformer {
|
public class ScriptTransformer extends Transformer {
|
||||||
private Object engine;
|
private Invocable engine;
|
||||||
|
private String functionName;
|
||||||
private Method invokeFunctionMethod;
|
|
||||||
|
|
||||||
private String functionName;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object transformRow(Map<String, Object> row, Context context) {
|
public Object transformRow(Map<String, Object> row, Context context) {
|
||||||
|
@ -53,17 +53,9 @@ public class ScriptTransformer extends Transformer {
|
||||||
initEngine(context);
|
initEngine(context);
|
||||||
if (engine == null)
|
if (engine == null)
|
||||||
return row;
|
return row;
|
||||||
return invokeFunctionMethod.invoke(engine, functionName, new Object[]{
|
return engine.invokeFunction(functionName, new Object[]{row, context});
|
||||||
row, context});
|
|
||||||
} catch (DataImportHandlerException e) {
|
} catch (DataImportHandlerException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
wrapAndThrow(SEVERE,e,
|
|
||||||
"Could not invoke method :"
|
|
||||||
+ functionName
|
|
||||||
+ "\n <script>\n"
|
|
||||||
+ context.getScript()
|
|
||||||
+ "</script>");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
wrapAndThrow(SEVERE,e, "Error invoking script for entity " + context.getEntityAttribute("name"));
|
wrapAndThrow(SEVERE,e, "Error invoking script for entity " + context.getEntityAttribute("name"));
|
||||||
}
|
}
|
||||||
|
@ -78,27 +70,23 @@ public class ScriptTransformer extends Transformer {
|
||||||
throw new DataImportHandlerException(SEVERE,
|
throw new DataImportHandlerException(SEVERE,
|
||||||
"<script> tag is not present under <dataConfig>");
|
"<script> tag is not present under <dataConfig>");
|
||||||
}
|
}
|
||||||
Object scriptEngineMgr = null;
|
ScriptEngineManager scriptEngineMgr = new ScriptEngineManager();
|
||||||
try {
|
ScriptEngine scriptEngine = scriptEngineMgr.getEngineByName(scriptLang);
|
||||||
scriptEngineMgr = Class.forName("javax.script.ScriptEngineManager")
|
if (scriptEngine == null) {
|
||||||
.newInstance();
|
throw new DataImportHandlerException(SEVERE,
|
||||||
} catch (Exception e) {
|
"Cannot load Script Engine for language: " + scriptLang);
|
||||||
wrapAndThrow(SEVERE, e, "<script> can be used only in java 6 or above");
|
}
|
||||||
|
if (scriptEngine instanceof Invocable) {
|
||||||
|
engine = (Invocable) scriptEngine;
|
||||||
|
} else {
|
||||||
|
throw new DataImportHandlerException(SEVERE,
|
||||||
|
"The installed ScriptEngine for: " + scriptLang
|
||||||
|
+ " does not implement Invocable. Class is "
|
||||||
|
+ scriptEngine.getClass().getName());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Method getEngineMethod = scriptEngineMgr.getClass().getMethod(
|
scriptEngine.eval(scriptText);
|
||||||
"getEngineByName", String.class);
|
} catch (ScriptException e) {
|
||||||
engine = getEngineMethod.invoke(scriptEngineMgr, scriptLang);
|
|
||||||
} catch (Exception e) {
|
|
||||||
wrapAndThrow(SEVERE, e, "Cannot load Script Engine for language: "
|
|
||||||
+ scriptLang);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Method evalMethod = engine.getClass().getMethod("eval", String.class);
|
|
||||||
invokeFunctionMethod = engine.getClass().getMethod("invokeFunction",
|
|
||||||
String.class, Object[].class);
|
|
||||||
evalMethod.invoke(engine, scriptText);
|
|
||||||
} catch (Exception e) {
|
|
||||||
wrapAndThrow(SEVERE, e, "'eval' failed with language: " + scriptLang
|
wrapAndThrow(SEVERE, e, "'eval' failed with language: " + scriptLang
|
||||||
+ " and script: \n" + scriptText);
|
+ " and script: \n" + scriptText);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,8 +53,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
|
||||||
sep.applyTransformer(map);
|
sep.applyTransformer(map);
|
||||||
assertEquals(map.get("name"), "Hello Scott");
|
assertEquals(map.get("name"), "Hello Scott");
|
||||||
} catch (DataImportHandlerException e) {
|
} catch (DataImportHandlerException e) {
|
||||||
assumeFalse("JRE does not contain a JavaScript engine (OpenJDK)",
|
|
||||||
e.getMessage().startsWith("<script> can be used only in java 6 or above"));
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,8 +84,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
|
||||||
sep.applyTransformer(map);
|
sep.applyTransformer(map);
|
||||||
assertEquals(map.get("name"), "Hello Scott");
|
assertEquals(map.get("name"), "Hello Scott");
|
||||||
} catch (DataImportHandlerException e) {
|
} catch (DataImportHandlerException e) {
|
||||||
assumeFalse("JRE does not contain a JavaScript engine (OpenJDK)",
|
|
||||||
e.getMessage().startsWith("<script> can be used only in java 6 or above"));
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,8 +99,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
|
||||||
.item(0));
|
.item(0));
|
||||||
assertTrue(config.script.text.indexOf("checkNextToken") > -1);
|
assertTrue(config.script.text.indexOf("checkNextToken") > -1);
|
||||||
} catch (DataImportHandlerException e) {
|
} catch (DataImportHandlerException e) {
|
||||||
assumeFalse("JRE does not contain a JavaScript engine (OpenJDK)",
|
|
||||||
e.getMessage().startsWith("<script> can be used only in java 6 or above"));
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,8 +126,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
|
||||||
sep.applyTransformer(map);
|
sep.applyTransformer(map);
|
||||||
assertNull(map.get("$hasMore"));
|
assertNull(map.get("$hasMore"));
|
||||||
} catch (DataImportHandlerException e) {
|
} catch (DataImportHandlerException e) {
|
||||||
assumeFalse("JRE does not contain a JavaScript engine (OpenJDK)",
|
|
||||||
e.getMessage().startsWith("<script> can be used only in java 6 or above"));
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue