Package adams.gui.scripting
Class AbstractScriptingEngine
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.gui.scripting.AbstractScriptingEngine
-
- All Implemented Interfaces:
ErrorProvider
,LoggingSupporter
,BackgroundScriptingEngine
,SizeOfHandler
,DatabaseConnectionHandler
,DatabaseConnectionProvider
,DatabaseConnectionUser
,DatabaseConnectionChangeListener
,Serializable
- Direct Known Subclasses:
ScriptingEngine
public abstract class AbstractScriptingEngine extends LoggingObject implements DatabaseConnectionHandler, DatabaseConnectionChangeListener, ErrorProvider, BackgroundScriptingEngine
Processes scripting commands.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
CommandProcessor
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
ALTERNATIVE_COMMAND_PROCESSOR
the property for the alternative AbstractCommandProcessor-derived class.static String
COMMAND_PROCESSOR
the property for the AbstractCommandProcessor-derived class.static String
COMMENT
the start of a comment.static String
FILENAME
the name of the props file.static String
LOG_FILE
the property for the scripting log file.protected AbstractDatabaseConnection
m_DbConn
the database connection to use.protected List<String>
m_History
the history of all (successfully run) commands since instantiation.protected String
m_LastError
the last last error that was encountered.protected boolean
m_LoggingEnabled
whether logging is enabled.protected ScriptingEngineThread
m_ProcessingThread
the thread to use for executing the commands.protected AbstractCommandProcessor
m_Processor
the command processor for executing the commands.protected List<String>
m_Recorded
the currently recorded commands.protected boolean
m_Recording
whether the commands are currently being recorded.protected HashSet<ScriptingInfoListener>
m_ScriptingInfoListeners
the listeners for changes in commands being run, etc.static String
SCRIPT_DIRECTORY
the property for the directory containing the scripts.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractScriptingEngine()
Initializes the engine.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
add(BasePanel panel, File commandFile)
Adds the commands from the given file to the queue.void
add(BasePanel panel, String command)
Adds the given command to the queue.void
add(BasePanel panel, String[] commands)
Adds the given commands to the queue.void
add(BasePanel panel, String cmd, ScriptingCommandCode code)
Adds the given command to the queue.void
add(BasePanel panel, List<String> commands)
Adds the given commands to the queue.void
add(ScriptingCommand command)
Adds the given command to the queue.void
addScriptingInfoListener(ScriptingInfoListener l)
Adds the scripting info listener to the internal list.void
addToHistory(String cmd)
Adds the command to the history.static boolean
check(String cmd)
Checks whether the given string represents a command and not a comment or empty line.void
clear()
Removes all commands from the queue.void
databaseConnectionStateChanged(DatabaseConnectionChangeEvent e)
A change in the database connection occurred.static String[]
filter(String[] cmds)
Filters all comments and empty lines.static List<String>
filter(List<String> cmds)
Filters all comments and empty lines.List<String>
getAvailableScripts()
Returns the available scripts in the scripts home directory.protected static List<String>
getAvailableScripts(String home)
Returns the available scripts in the scripts home directory.List<String>
getCommandHistory()
Returns the complete history of commands.AbstractDatabaseConnection
getDatabaseConnection()
Returns the currently used database connection object, can be null.protected abstract AbstractDatabaseConnection
getDefaultDatabaseConnection()
Returns the default database connection.protected abstract String
getDefinitionKey()
Returns the properties key to use for retrieving the properties.String
getLastError()
Returns the last error that was encountered, or null if no error occurred.ScriptingEngineThread
getProcessingThread()
Returns the thread for processing the scripting commands.AbstractCommandProcessor
getProcessor()
Returns the command processor to use.protected abstract Properties
getProperties()
Provides access to the properties object.List<String>
getRecordedCommands()
Returns the last recorded commands.String
getScriptsHome()
Returns the scripts home directory.boolean
hasLastError()
Returns whether there was an error during the last run.boolean
hasRecording()
Returns whether there are any commands in the recording buffer.boolean
isEmpty()
Returns whether there are no commands in the queue currently.boolean
isProcessing()
Returns whether a command is currently being processed.boolean
isRecording()
Whether commands are currently being recorded.static List<String>
load(File file)
Returns the content of the given file, null in case of an error.void
notifyScriptingInfoListeners(String cmd)
Notifies all scripting info listeners.protected Properties
readProperties()
Reads the properties.void
removeScriptingInfoListener(ScriptingInfoListener l)
Removes the scripting info listener from the internal list.static boolean
save(String[] script, File file)
Saves the script content in the given file.static boolean
save(List<String> script, File file)
Saves the script content in the given file.void
setDatabaseConnection(AbstractDatabaseConnection value)
Sets the database connection object to use.void
setLastError(String value)
Sets the last error.void
startRecording()
Starts the recording of commands.void
stop()
Stops the execution of the script (will also attempt to stop the currently running step).void
stopEngine()
Stops the scripting engine (incl.void
stopRecording()
Stops the recording of commands.protected void
updatePrefix()
Updates the prefix of the console object output streams.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
-
-
-
Field Detail
-
FILENAME
public static final String FILENAME
the name of the props file.- See Also:
- Constant Field Values
-
SCRIPT_DIRECTORY
public static final String SCRIPT_DIRECTORY
the property for the directory containing the scripts.- See Also:
- Constant Field Values
-
LOG_FILE
public static final String LOG_FILE
the property for the scripting log file.- See Also:
- Constant Field Values
-
COMMAND_PROCESSOR
public static final String COMMAND_PROCESSOR
the property for the AbstractCommandProcessor-derived class.- See Also:
- Constant Field Values
-
ALTERNATIVE_COMMAND_PROCESSOR
public static final String ALTERNATIVE_COMMAND_PROCESSOR
the property for the alternative AbstractCommandProcessor-derived class.- See Also:
- Constant Field Values
-
COMMENT
public static final String COMMENT
the start of a comment.- See Also:
- Constant Field Values
-
m_DbConn
protected AbstractDatabaseConnection m_DbConn
the database connection to use.
-
m_History
protected List<String> m_History
the history of all (successfully run) commands since instantiation.
-
m_Recording
protected boolean m_Recording
whether the commands are currently being recorded.
-
m_ScriptingInfoListeners
protected HashSet<ScriptingInfoListener> m_ScriptingInfoListeners
the listeners for changes in commands being run, etc.
-
m_LastError
protected String m_LastError
the last last error that was encountered.
-
m_LoggingEnabled
protected boolean m_LoggingEnabled
whether logging is enabled.
-
m_Processor
protected AbstractCommandProcessor m_Processor
the command processor for executing the commands.
-
m_ProcessingThread
protected ScriptingEngineThread m_ProcessingThread
the thread to use for executing the commands.
-
-
Method Detail
-
updatePrefix
protected void updatePrefix()
Updates the prefix of the console object output streams.
-
getDefaultDatabaseConnection
protected abstract AbstractDatabaseConnection getDefaultDatabaseConnection()
Returns the default database connection.- Returns:
- the default database connection
-
getProcessor
public AbstractCommandProcessor getProcessor()
Returns the command processor to use. Initializes it if necessary.- Returns:
- the processor
-
getProcessingThread
public ScriptingEngineThread getProcessingThread()
Returns the thread for processing the scripting commands.- Returns:
- the thread
-
getDatabaseConnection
public AbstractDatabaseConnection getDatabaseConnection()
Returns the currently used database connection object, can be null.- Specified by:
getDatabaseConnection
in interfaceDatabaseConnectionProvider
- Returns:
- the current object
-
setDatabaseConnection
public void setDatabaseConnection(AbstractDatabaseConnection value)
Sets the database connection object to use.- Specified by:
setDatabaseConnection
in interfaceDatabaseConnectionHandler
- Parameters:
value
- the object to use
-
isEmpty
public boolean isEmpty()
Returns whether there are no commands in the queue currently.- Returns:
- true if no commands wait to be processed
-
isProcessing
public boolean isProcessing()
Returns whether a command is currently being processed.- Returns:
- true if a command is currently being processed
-
stop
public void stop()
Stops the execution of the script (will also attempt to stop the currently running step).
-
stopEngine
public void stopEngine()
Stops the scripting engine (incl. the processing thread).- Specified by:
stopEngine
in interfaceBackgroundScriptingEngine
- See Also:
m_ProcessingThread
-
addToHistory
public void addToHistory(String cmd)
Adds the command to the history.- Parameters:
cmd
- the command to add
-
getCommandHistory
public List<String> getCommandHistory()
Returns the complete history of commands.- Returns:
- the history of commands
-
startRecording
public void startRecording()
Starts the recording of commands.
-
stopRecording
public void stopRecording()
Stops the recording of commands.
-
isRecording
public boolean isRecording()
Whether commands are currently being recorded.- Returns:
- true if the commands are currently being recorded
-
hasRecording
public boolean hasRecording()
Returns whether there are any commands in the recording buffer.- Returns:
- true if commands were recorded
-
getRecordedCommands
public List<String> getRecordedCommands()
Returns the last recorded commands.- Returns:
- the commands
-
clear
public void clear()
Removes all commands from the queue.
-
add
public void add(ScriptingCommand command)
Adds the given command to the queue.- Parameters:
command
- the command to add
-
add
public void add(BasePanel panel, String cmd, ScriptingCommandCode code)
Adds the given command to the queue.- Parameters:
panel
- the affected base panelcmd
- the command to executecode
- the code to execute after the command finishes
-
add
public void add(BasePanel panel, String command)
Adds the given command to the queue.- Parameters:
panel
- the affected base panelcommand
- the command to add
-
add
public void add(BasePanel panel, String[] commands)
Adds the given commands to the queue.- Parameters:
panel
- the affected base panelcommands
- the commands to add
-
add
public void add(BasePanel panel, List<String> commands)
Adds the given commands to the queue.- Parameters:
panel
- the affected base panelcommands
- the commands to add
-
add
public void add(BasePanel panel, File commandFile)
Adds the commands from the given file to the queue.- Parameters:
panel
- the affected base panelcommandFile
- the file with the commands to add
-
addScriptingInfoListener
public void addScriptingInfoListener(ScriptingInfoListener l)
Adds the scripting info listener to the internal list.- Parameters:
l
- the listener to add
-
removeScriptingInfoListener
public void removeScriptingInfoListener(ScriptingInfoListener l)
Removes the scripting info listener from the internal list.- Parameters:
l
- the listener to remove
-
notifyScriptingInfoListeners
public void notifyScriptingInfoListeners(String cmd)
Notifies all scripting info listeners.- Parameters:
cmd
- the command, if any
-
hasLastError
public boolean hasLastError()
Returns whether there was an error during the last run.- Specified by:
hasLastError
in interfaceErrorProvider
- Returns:
- true if an error was encountered during the last run
-
setLastError
public void setLastError(String value)
Sets the last error.- Parameters:
value
- the error
-
getLastError
public String getLastError()
Returns the last error that was encountered, or null if no error occurred.- Specified by:
getLastError
in interfaceErrorProvider
- Returns:
- the error string or null if no error occurred
-
check
public static boolean check(String cmd)
Checks whether the given string represents a command and not a comment or empty line.- Parameters:
cmd
- the command to check- Returns:
- true if proper command
-
filter
public static String[] filter(String[] cmds)
Filters all comments and empty lines.- Parameters:
cmds
- the commands to process- Returns:
- the "cleansed" commands
-
filter
public static List<String> filter(List<String> cmds)
Filters all comments and empty lines.- Parameters:
cmds
- the commands to process- Returns:
- the "cleansed" commands
-
load
public static List<String> load(File file)
Returns the content of the given file, null in case of an error.- Parameters:
file
- the file to load- Returns:
- the content/lines of the file
-
save
public static boolean save(String[] script, File file)
Saves the script content in the given file.- Parameters:
script
- the script to savefile
- the file to save the content to- Returns:
- true if successfully saved
-
save
public static boolean save(List<String> script, File file)
Saves the script content in the given file.- Parameters:
script
- the script to savefile
- the file to save the content to- Returns:
- true if successfully saved
-
getDefinitionKey
protected abstract String getDefinitionKey()
Returns the properties key to use for retrieving the properties.- Returns:
- the key
-
readProperties
protected Properties readProperties()
Reads the properties.- Returns:
- the properties
- See Also:
getDefinitionKey()
-
getProperties
protected abstract Properties getProperties()
Provides access to the properties object.- Returns:
- the properties
-
getScriptsHome
public String getScriptsHome()
Returns the scripts home directory. $HOME/.gcms/scripts- Returns:
- the scripts directory
-
getAvailableScripts
public List<String> getAvailableScripts()
Returns the available scripts in the scripts home directory.- Returns:
- a list of available scripts
- See Also:
getScriptsHome()
-
getAvailableScripts
protected static List<String> getAvailableScripts(String home)
Returns the available scripts in the scripts home directory.- Parameters:
home
- the scripts home- Returns:
- a list of available scripts
- See Also:
getScriptsHome()
-
databaseConnectionStateChanged
public void databaseConnectionStateChanged(DatabaseConnectionChangeEvent e)
A change in the database connection occurred.- Specified by:
databaseConnectionStateChanged
in interfaceDatabaseConnectionChangeListener
- Parameters:
e
- the event
-
-