use of org.python.core.PyObject in project oxCore by GluuFederation.
the class CustomScriptManager method createExternalTypeFromStringWithPythonException.
public BaseExternalType createExternalTypeFromStringWithPythonException(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) throws PythonException {
String script = customScript.getScript();
if (script == null) {
return null;
}
CustomScriptType customScriptType = customScript.getScriptType();
BaseExternalType externalType = null;
InputStream bis = null;
try {
bis = new ByteArrayInputStream(script.getBytes("UTF-8"));
externalType = pythonService.loadPythonScript(bis, customScriptType.getPythonClass(), customScriptType.getCustomScriptType(), new PyObject[] { new PyLong(System.currentTimeMillis()) });
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(bis);
}
if (externalType == null) {
return null;
}
boolean initialized = false;
try {
initialized = externalType.init(configurationAttributes);
} catch (Exception ex) {
log.error("Failed to initialize custom script: '{0}'", ex, customScript.getName());
}
if (initialized) {
return externalType;
}
return null;
}
use of org.python.core.PyObject in project oxCore by GluuFederation.
the class PythonService method loadPythonScript.
@SuppressWarnings("unchecked")
private <T> T loadPythonScript(String scriptPythonType, Class<T> scriptJavaType, PyObject[] constructorArgs, PythonInterpreter interpreter) throws PythonException {
PyObject scriptPythonTypeObject = interpreter.get(scriptPythonType);
if (scriptPythonTypeObject == null) {
return null;
}
PyObject scriptPythonTypeClass;
try {
scriptPythonTypeClass = scriptPythonTypeObject.__call__(constructorArgs);
} catch (Exception ex) {
log.error("Failed to initialize python class", ex.getMessage());
throw new PythonException(String.format("Failed to initialize python class '%s'", scriptPythonType), ex);
}
Object scriptJavaClass = scriptPythonTypeClass.__tojava__(scriptJavaType);
if (!ReflectHelper.assignableFrom(scriptJavaClass.getClass(), scriptJavaType)) {
return null;
}
return (T) scriptJavaClass;
}
Aggregations