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 StringALTERNATIVE_COMMAND_PROCESSORthe property for the alternative AbstractCommandProcessor-derived class.static StringCOMMAND_PROCESSORthe property for the AbstractCommandProcessor-derived class.static StringCOMMENTthe start of a comment.static StringFILENAMEthe name of the props file.static StringLOG_FILEthe property for the scripting log file.protected AbstractDatabaseConnectionm_DbConnthe database connection to use.protected List<String>m_Historythe history of all (successfully run) commands since instantiation.protected Stringm_LastErrorthe last last error that was encountered.protected booleanm_LoggingEnabledwhether logging is enabled.protected ScriptingEngineThreadm_ProcessingThreadthe thread to use for executing the commands.protected AbstractCommandProcessorm_Processorthe command processor for executing the commands.protected List<String>m_Recordedthe currently recorded commands.protected booleanm_Recordingwhether the commands are currently being recorded.protected HashSet<ScriptingInfoListener>m_ScriptingInfoListenersthe listeners for changes in commands being run, etc.static StringSCRIPT_DIRECTORYthe 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 protectedAbstractScriptingEngine()Initializes the engine.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidadd(BasePanel panel, File commandFile)Adds the commands from the given file to the queue.voidadd(BasePanel panel, String command)Adds the given command to the queue.voidadd(BasePanel panel, String[] commands)Adds the given commands to the queue.voidadd(BasePanel panel, String cmd, ScriptingCommandCode code)Adds the given command to the queue.voidadd(BasePanel panel, List<String> commands)Adds the given commands to the queue.voidadd(ScriptingCommand command)Adds the given command to the queue.voidaddScriptingInfoListener(ScriptingInfoListener l)Adds the scripting info listener to the internal list.voidaddToHistory(String cmd)Adds the command to the history.static booleancheck(String cmd)Checks whether the given string represents a command and not a comment or empty line.voidclear()Removes all commands from the queue.voiddatabaseConnectionStateChanged(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.AbstractDatabaseConnectiongetDatabaseConnection()Returns the currently used database connection object, can be null.protected abstract AbstractDatabaseConnectiongetDefaultDatabaseConnection()Returns the default database connection.protected abstract StringgetDefinitionKey()Returns the properties key to use for retrieving the properties.StringgetLastError()Returns the last error that was encountered, or null if no error occurred.ScriptingEngineThreadgetProcessingThread()Returns the thread for processing the scripting commands.AbstractCommandProcessorgetProcessor()Returns the command processor to use.protected abstract PropertiesgetProperties()Provides access to the properties object.List<String>getRecordedCommands()Returns the last recorded commands.StringgetScriptsHome()Returns the scripts home directory.booleanhasLastError()Returns whether there was an error during the last run.booleanhasRecording()Returns whether there are any commands in the recording buffer.booleanisEmpty()Returns whether there are no commands in the queue currently.booleanisProcessing()Returns whether a command is currently being processed.booleanisRecording()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.voidnotifyScriptingInfoListeners(String cmd)Notifies all scripting info listeners.protected PropertiesreadProperties()Reads the properties.voidremoveScriptingInfoListener(ScriptingInfoListener l)Removes the scripting info listener from the internal list.static booleansave(String[] script, File file)Saves the script content in the given file.static booleansave(List<String> script, File file)Saves the script content in the given file.voidsetDatabaseConnection(AbstractDatabaseConnection value)Sets the database connection object to use.voidsetLastError(String value)Sets the last error.voidstartRecording()Starts the recording of commands.voidstop()Stops the execution of the script (will also attempt to stop the currently running step).voidstopEngine()Stops the scripting engine (incl. the processing thread).voidstopRecording()Stops the recording of commands.protected voidupdatePrefix()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:
getDatabaseConnectionin interfaceDatabaseConnectionProvider- Returns:
- the current object
-
setDatabaseConnection
public void setDatabaseConnection(AbstractDatabaseConnection value)
Sets the database connection object to use.- Specified by:
setDatabaseConnectionin 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:
stopEnginein 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:
hasLastErrorin 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:
getLastErrorin 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:
databaseConnectionStateChangedin interfaceDatabaseConnectionChangeListener- Parameters:
e- the event
-
-