Package adams.core.scripting
Class Jython
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.scripting.Jython
-
- All Implemented Interfaces:
LoggingSupporter
,SizeOfHandler
,Serializable
public class Jython extends LoggingObject
A helper class for Jython.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
CLASS_PYTHONINERPRETER
the classname of the Python interpreter.static String
CLASS_PYTHONOBJECTINPUTSTREAM
the classname of the Python ObjectInputStream.protected Object
m_Interpreter
the interpreter.protected boolean
m_Present
whether the Jython classes are in the Classpath.protected static Jython
m_Singleton
the singleton.static String
REGISTRY_FILE
the registry file name (without path).static String
REGISTRY_JAVAACCESSIBILITY
the registry key for the Java accessibility.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Jython()
default constructor, tries to instantiate a Python Interpreter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkRegistry()
Makes sure that the jython registry ($HOME/.jython) is present and contains the "python.security.respectJavaAccessibility" property.Object
deserialize(InputStream in)
deserializes the Python Object from the stream.Object
getInterpreter()
returns the currently used Python Interpreter.static Jython
getSingleton()
Returns the singleton.Object
invoke(Object o, String methodName, Class[] paramClasses, Object[] paramValues)
executes the specified method and returns the result, if any.Object
invoke(String methodName, Class[] paramClasses, Object[] paramValues)
executes the specified method on the current interpreter and returns the result, if any.boolean
isPresent()
returns whether the Jython classes are present or not, i.e.Object[]
loadScriptObject(Class cls, PlaceholderFile scriptFile, JythonScript inlineScript, String scriptOptions, Variables vars)
Loads the scripts object and sets its options.static void
main(String[] args)
If no arguments are given, it just prints the presence of the Jython classes, otherwise it expects a Jython filename to execute.Object
newInstance(File file, Class template)
loads the module and returns a new instance of it as instance of the provided Java class template.Object
newInstance(File file, Class template, File[] paths)
loads the module and returns a new instance of it as instance of the provided Java class template.Object
newInterpreter()
initializes and returns a Python Interpreter.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
-
-
-
Field Detail
-
CLASS_PYTHONINERPRETER
public static final String CLASS_PYTHONINERPRETER
the classname of the Python interpreter.- See Also:
- Constant Field Values
-
CLASS_PYTHONOBJECTINPUTSTREAM
public static final String CLASS_PYTHONOBJECTINPUTSTREAM
the classname of the Python ObjectInputStream.- See Also:
- Constant Field Values
-
REGISTRY_FILE
public static final String REGISTRY_FILE
the registry file name (without path).- See Also:
- Constant Field Values
-
REGISTRY_JAVAACCESSIBILITY
public static final String REGISTRY_JAVAACCESSIBILITY
the registry key for the Java accessibility.- See Also:
- Constant Field Values
-
m_Present
protected boolean m_Present
whether the Jython classes are in the Classpath.
-
m_Interpreter
protected Object m_Interpreter
the interpreter.
-
m_Singleton
protected static Jython m_Singleton
the singleton.
-
-
Method Detail
-
checkRegistry
protected void checkRegistry()
Makes sure that the jython registry ($HOME/.jython) is present and contains the "python.security.respectJavaAccessibility" property.
-
getInterpreter
public Object getInterpreter()
returns the currently used Python Interpreter.- Returns:
- the interpreter, can be null
-
invoke
public Object invoke(String methodName, Class[] paramClasses, Object[] paramValues)
executes the specified method on the current interpreter and returns the result, if any.- Parameters:
methodName
- the name of the methodparamClasses
- the classes of the parametersparamValues
- the values of the parameters- Returns:
- the return value of the method, if any (in that case null)
-
isPresent
public boolean isPresent()
returns whether the Jython classes are present or not, i.e. whether the classes are in the classpath or not- Returns:
- whether the Jython classes are available
-
newInterpreter
public Object newInterpreter()
initializes and returns a Python Interpreter.- Returns:
- the interpreter or null if Jython classes not present
-
newInstance
public Object newInstance(File file, Class template)
loads the module and returns a new instance of it as instance of the provided Java class template.- Parameters:
file
- the Jython module filetemplate
- the template for the returned Java object- Returns:
- the Jython object
-
newInstance
public Object newInstance(File file, Class template, File[] paths)
loads the module and returns a new instance of it as instance of the provided Java class template. The paths are added to 'sys.path' - can be used if the module depends on other Jython modules.- Parameters:
file
- the Jython module filetemplate
- the template for the returned Java objectpaths
- additional paths to add to "sys.path"- Returns:
- the Jython object
-
invoke
public Object invoke(Object o, String methodName, Class[] paramClasses, Object[] paramValues)
executes the specified method and returns the result, if any.- Parameters:
o
- the object the method should be called from, e.g., a Python InterpretermethodName
- the name of the methodparamClasses
- the classes of the parametersparamValues
- the values of the parameters- Returns:
- the return value of the method, if any (in that case null)
-
deserialize
public Object deserialize(InputStream in)
deserializes the Python Object from the stream.- Parameters:
in
- the stream to use- Returns:
- the deserialized object
-
loadScriptObject
public Object[] loadScriptObject(Class cls, PlaceholderFile scriptFile, JythonScript inlineScript, String scriptOptions, Variables vars)
Loads the scripts object and sets its options.- Parameters:
cls
- the class to instantiatescriptFile
- the external file to loadinlineScript
- the inline script to load if external file points to a directoryscriptOptions
- the options to setvars
- the variables to use for expanding- Returns:
- element 0: error messsage (null if ok), element 1: script object
-
getSingleton
public static Jython getSingleton()
Returns the singleton.- Returns:
- the singleton
-
main
public static void main(String[] args)
If no arguments are given, it just prints the presence of the Jython classes, otherwise it expects a Jython filename to execute.- Parameters:
args
- commandline arguments
-
-