Search in sources :

Example 6 with PyObject

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;
}
Also used : PyLong(org.python.core.PyLong) ByteArrayInputStream(java.io.ByteArrayInputStream) CustomScriptType(org.xdi.model.custom.script.CustomScriptType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BaseExternalType(org.xdi.model.custom.script.type.BaseExternalType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PyObject(org.python.core.PyObject) PythonException(org.xdi.exception.PythonException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with PyObject

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;
}
Also used : PythonException(org.xdi.exception.PythonException) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject) PythonException(org.xdi.exception.PythonException) IOException(java.io.IOException) PyException(org.python.core.PyException)

Aggregations

PyObject (org.python.core.PyObject)7 IOException (java.io.IOException)3 PythonException (org.xdi.exception.PythonException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 PyException (org.python.core.PyException)2 PyLong (org.python.core.PyLong)2 HasFields (com.google.refine.expr.HasFields)1 RegistrationScript (org.gluu.oxtrust.service.python.interfaces.RegistrationScript)1 PyList (org.python.core.PyList)1 PyString (org.python.core.PyString)1 PythonInterpreter (org.python.util.PythonInterpreter)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 CustomScriptType (org.xdi.model.custom.script.CustomScriptType)1 BaseExternalType (org.xdi.model.custom.script.type.BaseExternalType)1