use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.
the class ScriptableApiSpi method afterExecute.
@Override
public void afterExecute(Api api, ApiConsumer consumer, ApiService service, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
if (spi == null) {
return;
}
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
if (!engine.has(spi, Functions.AfterExecute)) {
return;
}
Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
if (jsApi == null) {
throw new ApiServiceExecutionException("api or spi not attached on Api OnStart");
}
try {
// invoke afterExecute
engine.invoke(spi, Functions.AfterExecute, jsApi, consumer, service, request, response);
} catch (ScriptingEngineException ex) {
ex.setScript(Json.getString(api.getRuntime(), Api.Spec.Runtime.Function));
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
}
use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.
the class ScriptableApiSpi method onRequest.
@Override
public void onRequest(Api api, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
if (spi == null) {
return;
}
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
if (!engine.has(spi, Functions.OnRequest)) {
return;
}
Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
if (jsApi == null) {
throw new ApiServiceExecutionException("api or spi not attached on Api OnStart");
}
// invoke onRequest
try {
engine.invoke(spi, Functions.OnRequest, jsApi, request, response);
} catch (ScriptingEngineException ex) {
ex.setScript(Json.getString(api.getRuntime(), Api.Spec.Runtime.Function));
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
}
use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.
the class DefaultScriptingEngine method eval.
@Override
public Object eval(Supported supported, final Api api, ApiResource resource, ScriptContext sContext) throws ScriptingEngineException {
if (supported == null) {
throw new ScriptingEngineException("Unsupported Scripting Engine ");
}
ScriptEngine engine = engines.get(supported);
if (engine == null) {
throw new ScriptingEngineException("Unsupported Scripting Engine " + supported);
}
// get platform libs
ScriptObjectMirror libs = (ScriptObjectMirror) platform.get(Libs);
String[] libsKeys = libs.getOwnKeys(true);
CachedScript cached = scripts.get(resource.owner() + Lang.COLON + resource.path());
Reader reader = null;
if (cached == null || cached.timestamp < resource.timestamp().getTime()) {
InputStream rio = null;
try {
StringBuilder startScript = new StringBuilder(ScriptStart);
// add platform libraries
for (String lib : libsKeys) {
startScript.append(Var).append(Lang.SPACE).append(lib).append(Lang.EQUALS).append(Libs).append(Lang.DOT).append(lib).append(Lang.SEMICOLON);
}
startScript.append(Native);
for (String d : Denied) {
startScript.append(d);
}
String sStartScript = startScript.toString();
startScript.setLength(0);
rio = resource.toInput();
// format String script = platform.callMember (, arg);
List<InputStream> blocks = new ArrayList<InputStream>();
blocks.add(new ByteArrayInputStream(sStartScript.getBytes()));
blocks.add(rio);
blocks.add(new ByteArrayInputStream(ScriptEnd.getBytes()));
reader = new InputStreamReader(new SequenceInputStream(Collections.enumeration(blocks)));
cached = new CachedScript();
cached.script = ((Compilable) engine).compile(reader);
cached.timestamp = resource.timestamp().getTime();
scripts.put(resource.owner() + Lang.COLON + resource.path(), cached);
} catch (Exception e) {
throw new ScriptingEngineException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(rio);
}
}
if (sContext == null) {
return null;
}
Bindings bindings = new SimpleBindings();
bindings.put(JavaClass, new Function<String, Class<?>>() {
@Override
public Class<?> apply(String type) {
try {
return api.getClassLoader().loadClass(type);
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException(cnfe);
}
}
});
// add platform libraries
bindings.put(Libs, libs);
try {
Iterator<String> vars = sContext.vars();
while (vars.hasNext()) {
String var = vars.next();
bindings.put(var, sContext.var(var));
}
return cached.script.eval(bindings);
} catch (ScriptException e) {
throw new ScriptingEngineException(e.getMessage(), e);
} finally {
bindings.clear();
}
}
use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.
the class DefaultScriptingEngine method invoke.
@Override
public Object invoke(Object scriptable, String function, Object... args) throws ScriptingEngineException {
if (scriptable == null) {
scriptable = platform;
}
if (!(scriptable instanceof ScriptObjectMirror)) {
throw new ScriptingEngineException("object is not a valid scriptable object");
}
ScriptObjectMirror som = (ScriptObjectMirror) scriptable;
if (args != null && args.length > 0) {
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
if (arg == null || arg instanceof ScriptObjectMirror) {
continue;
}
Class<?> type = arg.getClass();
Scriptable ann = type.getAnnotation(Scriptable.class);
if (ann == null) {
continue;
}
Object jsArg = null;
if (Referenceable.class.isAssignableFrom(arg.getClass())) {
jsArg = ((Referenceable) arg).getReference();
}
if (jsArg == null) {
jsArg = platform.callMember(ann.name(), arg);
if (Referenceable.class.isAssignableFrom(arg.getClass())) {
((Referenceable) arg).setReference(jsArg);
}
}
args[i] = jsArg;
}
}
try {
return som.callMember(function, args);
} catch (Throwable err) {
throw new ScriptingEngineException(err.getMessage(), err);
}
}
use of com.bluenimble.platform.scripting.ScriptingEngineException in project serverless by bluenimble.
the class ScriptableApiServiceSpi method onStart.
@Override
public void onStart(Api api, ApiService service, ApiContext context) throws ApiManagementException {
SpecAndSpiPair helper = (SpecAndSpiPair) api.getHelper();
if (helper == null) {
throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting or couldn't start correctly.");
}
Object jsApi = helper.spec();
if (jsApi == null) {
throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting");
}
JsonObject runtime = service.getRuntime();
String script = Json.getString(runtime, Api.Spec.Runtime.Function);
if (Lang.isNullOrEmpty(script)) {
throw new ApiManagementException("script not found in " + ApiUtils.RuntimeKey);
}
String[] path = Lang.split(script, Lang.SLASH);
ApiResource rScript = null;
try {
rScript = api.getResourcesManager().get(path);
} catch (ApiResourcesManagerException ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
if (rScript == null) {
throw new ApiManagementException("function '" + Lang.join(path, Lang.SLASH) + "' not found");
}
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
// create the spec
Object jsService = null;
try {
jsService = engine.invoke(null, ApiService.class.getSimpleName(), service);
} catch (Exception ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
if (jsService == null) {
throw new ApiManagementException("can't create 'service spec' js object");
}
// create the spi
Object spi = null;
try {
spi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
} catch (Exception ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
if (spi == null) {
throw new ApiManagementException("script returned an undefined object");
}
service.setHelper(new SpecAndSpiPair(jsService, spi));
if (!engine.has(spi, Functions.OnStart)) {
return;
}
// invoke onStart
try {
engine.invoke(spi, Functions.OnStart, jsApi, jsService, context);
} catch (ScriptingEngineException ex) {
ex.setScript(script);
throw new ApiManagementException(ex.getMessage(), ex);
}
}
Aggregations