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:
James Dyer 2012-03-21 17:22:13 +00:00
parent acd6f46934
commit dfc345b5c7
2 changed files with 24 additions and 44 deletions

View File

@ -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.SEVERE;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
* <p>
* A {@link Transformer} instance capable of executing functions written in scripting
@ -40,11 +43,8 @@ import java.util.Map;
* @since solr 1.3
*/
public class ScriptTransformer extends Transformer {
private Object engine;
private Method invokeFunctionMethod;
private String functionName;
private Invocable engine;
private String functionName;
@Override
public Object transformRow(Map<String, Object> row, Context context) {
@ -53,17 +53,9 @@ public class ScriptTransformer extends Transformer {
initEngine(context);
if (engine == null)
return row;
return invokeFunctionMethod.invoke(engine, functionName, new Object[]{
row, context});
return engine.invokeFunction(functionName, new Object[]{row, context});
} catch (DataImportHandlerException e) {
throw e;
} catch (InvocationTargetException e) {
wrapAndThrow(SEVERE,e,
"Could not invoke method :"
+ functionName
+ "\n <script>\n"
+ context.getScript()
+ "</script>");
} catch (Exception e) {
wrapAndThrow(SEVERE,e, "Error invoking script for entity " + context.getEntityAttribute("name"));
}
@ -78,27 +70,23 @@ public class ScriptTransformer extends Transformer {
throw new DataImportHandlerException(SEVERE,
"<script> tag is not present under <dataConfig>");
}
Object scriptEngineMgr = null;
try {
scriptEngineMgr = Class.forName("javax.script.ScriptEngineManager")
.newInstance();
} catch (Exception e) {
wrapAndThrow(SEVERE, e, "<script> can be used only in java 6 or above");
ScriptEngineManager scriptEngineMgr = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineMgr.getEngineByName(scriptLang);
if (scriptEngine == null) {
throw new DataImportHandlerException(SEVERE,
"Cannot load Script Engine for language: " + scriptLang);
}
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 {
Method getEngineMethod = scriptEngineMgr.getClass().getMethod(
"getEngineByName", String.class);
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) {
scriptEngine.eval(scriptText);
} catch (ScriptException e) {
wrapAndThrow(SEVERE, e, "'eval' failed with language: " + scriptLang
+ " and script: \n" + scriptText);
}

View File

@ -53,8 +53,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
sep.applyTransformer(map);
assertEquals(map.get("name"), "Hello Scott");
} 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;
}
}
@ -86,8 +84,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
sep.applyTransformer(map);
assertEquals(map.get("name"), "Hello Scott");
} 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;
}
}
@ -103,8 +99,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
.item(0));
assertTrue(config.script.text.indexOf("checkNextToken") > -1);
} 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;
}
}
@ -132,8 +126,6 @@ public class TestScriptTransformer extends AbstractDataImportHandlerTestCase {
sep.applyTransformer(map);
assertNull(map.get("$hasMore"));
} 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;
}
}