Package net.sf.basedb.core.plugin
Class AbstractPlugin
java.lang.Object
net.sf.basedb.core.plugin.AbstractPlugin
- All Implemented Interfaces:
Plugin
- Direct Known Subclasses:
AbstractAnalysisPlugin
,AbstractExporterPlugin
,AbstractFileUnpacker
,AbstractFlatFileImporter
,AbstractIOSupport
,BfsExporterPlugin
,CdfFileReporterImporter
,CloneReportersPlugin
,HelpImporter
,IntensityCalculatorPlugin
,PluginConfigurationImporter
This is an abstract base class useful for developing plugins.
It's main purpose is to help with validating and storing request
parameters into the plugin or job configuration.
- Version:
- 2.0
- Author:
- Enell, Nicklas
- Last modified
- $Date: 2023-12-20 14:32:15 +0100 (Wed, 20 Dec 2023) $
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.sf.basedb.core.plugin.Plugin
Plugin.MainType
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final PluginParameter<String>
Section definition for grouping annotation options.protected ParameterValues
protected static final String
The name of the parameter that asks if annotations should be copied or not.protected ParameterValues
private DbControl
private File
private PrintWriter
protected static final String
The name of the parameter that asks if annotations should be overwritten or not.protected SessionControl
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected <T> PluginParameter<T>
Clone a plug-in parameter and set the default value to the value that is already stored in the job or configuration parameters.protected void
Close the log file created bycreateLogFile(String)
.protected void
createLogFile
(String path) Create a log file in the BASE file system for logging detailed information from a plug-in.void
done()
Clears the variables set by theinit
method.protected PluginParameter<Boolean>
getCopyAnnotationsParmeter
(String label, String description, Boolean defaultValue) Parameter definition that asks if annotations should be copied from the plug-in definition/configuration to other items.protected PluginConfiguration
Get the plug-in configuration that this is currently in use or null.protected Job
Get the job that is executing this plugin or null.protected Object
Get the value for a job or configuration parameter.protected PluginParameter<Boolean>
getOverwriteAnnotationsParameters
(String label, String description, Boolean defaultValue) Parameter definition that asks if copied annotations should overwrite existing annotations or not.Return null, which gives the plugin the same permissions as the logged in user.void
init
(SessionControl sc, ParameterValues configuration, ParameterValues job) Store copies of the session control, plugin and job configuration.protected boolean
Checks if logging is enabled.protected void
Log a message to the log file created bycreateLogFile(String)
.protected void
Log a message and stack trace to the log file created bycreateLogFile(String)
.boolean
Returns FALSE, since that is how the plugins used to work before this method was introduced.protected <T> void
storeValue
(ParameterValues values, Request request, PluginParameter<T> parameter) Copy a parameter value from aRequest
to aParameterValues
object.protected <T> void
storeValue
(ParameterValues values, PluginParameter<T> parameter, T value) Store a value in aParameterValues
object.protected <T> void
storeValues
(ParameterValues values, Request request, PluginParameter<T> parameter) Copy a list of parameter values from aRequest
to aParameterValues
object.boolean
Returns TRUE, since that is how the plugins used to work before this method was introduced.validateRequestParameters
(List<PluginParameter<?>> parameters, Request request) Validate the parameter values in a request against a list of plugin parameter definitions.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.sf.basedb.core.plugin.Plugin
getMainType, run
-
Field Details
-
sc
-
configuration
-
job
-
logDc
-
logfile
-
logger
-
annotationSection
Section definition for grouping annotation options.- Since:
- 2.5
-
COPY_ANNOTATIONS
The name of the parameter that asks if annotations should be copied or not.- Since:
- 2.5
- See Also:
-
OVERWRITE_ANNOTATIONS
The name of the parameter that asks if annotations should be overwritten or not.- Since:
- 2.5
- See Also:
-
-
Constructor Details
-
AbstractPlugin
protected AbstractPlugin()Create a newAbstractPlugin
.
-
-
Method Details
-
init
public void init(SessionControl sc, ParameterValues configuration, ParameterValues job) throws BaseException Store copies of the session control, plugin and job configuration. These are available to subclasses in thesc
,configuration
andjob
variables. If a subclass overrides this method it is recommended that it also callssuper.init(sc, configuration, job)
.- Specified by:
init
in interfacePlugin
- Parameters:
sc
- ASessionControl
object that the plugin can use to communicate with the core.configuration
- The configuration parameters for the pluginjob
- The job parameters for the plugin- Throws:
BaseException
- if there is an error.
-
done
public void done()Clears the variables set by theinit
method. If a subclass overrides this method it is recommended that it also callssuper.done()
. -
supportsConfigurations
public boolean supportsConfigurations()Returns TRUE, since that is how the plugins used to work before this method was introduced.- Specified by:
supportsConfigurations
in interfacePlugin
- Returns:
- TRUE or FALSE
-
requiresConfiguration
public boolean requiresConfiguration()Returns FALSE, since that is how the plugins used to work before this method was introduced.- Specified by:
requiresConfiguration
in interfacePlugin
- Returns:
- TRUE or FALSE
-
getPermissions
Return null, which gives the plugin the same permissions as the logged in user.- Specified by:
getPermissions
in interfacePlugin
- Returns:
- A collection of permissions or null to not use permissions
-
validateRequestParameters
protected List<Throwable> validateRequestParameters(List<PluginParameter<?>> parameters, Request request) Validate the parameter values in a request against a list of plugin parameter definitions. The validation is done by calling theParameterType.validate(String, List)
for each parameter in the list of parameter definitions. If all parameters are valid this method returns null, otherwise a list of exceptions that ocurred during the validation. This list can be directly propagated to theResponse.setError(String, List)
method. For example:// In Plugin.invoke() method: List<PluginParameter<?>> parameters = getParametersForCommand(request.getCommand()); List<Throwable> errors = validateRequestParameters(parameters, request); if (errors != null) { response.setError(errors.size()+ " invalid parameter(s) were found in the request", errors); return; }
- Parameters:
parameters
- A list containing the parameter definitionsrequest
- The request- Returns:
- A list of exceptions, or null if all parameters are valid
-
storeValue
protected <T> void storeValue(ParameterValues values, Request request, PluginParameter<T> parameter) throws BaseException Copy a parameter value from aRequest
to aParameterValues
object. This method makes it easy for a subclass to directly store a request parameter into either the plugin or job configuration. Use this method when the parameter valus is a single value.- Parameters:
values
- TheParameterValues
object where the value should be stored, use eitherconfiguration
orjob
request
- TheRequest
objectparameter
- ThePluginParameter
object for the parameter- Throws:
BaseException
- If the parameter value is invalid
-
storeValue
Store a value in aParameterValues
object. This method is useful when a plugin needs to store a value that is not part of the request.- Type Parameters:
T
- The type of the parameter- Parameters:
values
- TheParameterValues
object where the value should be stored, use eitherconfiguration
orjob
parameter
- ThePluginParameter
object for the parametervalue
- The value to save- Throws:
BaseException
- If the parameter value is invalid
-
storeValues
protected <T> void storeValues(ParameterValues values, Request request, PluginParameter<T> parameter) throws BaseException Copy a list of parameter values from aRequest
to aParameterValues
object. This method makes it easy for a subclass to directly store a request parameter into either the plugin or job configuration. Use this method when the parameter value is a list of values.- Parameters:
values
- TheParameterValues
object where the value should be stored, use eitherconfiguration
orjob
request
- TheRequest
objectparameter
- TheParameterType
object for the parameter- Throws:
BaseException
- If the parameter value is invalid
-
getCurrentJob
Get the job that is executing this plugin or null.- Parameters:
dc
- The DbControl to use for database access- Returns:
- The job or null
-
getCurrentConfiguration
Get the plug-in configuration that this is currently in use or null.- Parameters:
dc
- The DbControl to use for database access- Returns:
- The configuration or null
- Since:
- 2.5
-
getCopyAnnotationsParmeter
protected PluginParameter<Boolean> getCopyAnnotationsParmeter(String label, String description, Boolean defaultValue) Parameter definition that asks if annotations should be copied from the plug-in definition/configuration to other items.- Parameters:
label
- The label to use for the parameter or null to use the default label (Copy annotations)description
- The description to use for the parameter or null to use the default descriptiondefaultValue
- The default value
-
getOverwriteAnnotationsParameters
protected PluginParameter<Boolean> getOverwriteAnnotationsParameters(String label, String description, Boolean defaultValue) Parameter definition that asks if copied annotations should overwrite existing annotations or not.- Parameters:
label
- The label to use for the parameter or null to use the default label (Overwrite existing)description
- The description to use for the parameter or null to use the default descriptiondefaultValue
- The default value- Since:
- 2.5
-
getJobOrConfigurationValue
Get the value for a job or configuration parameter. This method first checks the job parameters. If there is no value it will check the configuration parameters.- Parameters:
name
- The name of the parameter- Returns:
- The parameter value, or null
- Since:
- 2.15
-
cloneParameterWithDefaultValue
Clone a plug-in parameter and set the default value to the value that is already stored in the job or configuration parameters.- Parameters:
pp
- The parameter to clone- Returns:
- The cloned parameter
- Since:
- 2.15
-
createLogFile
Create a log file in the BASE file system for logging detailed information from a plug-in. The log file will be created if it doesn't exists. The log file is handled in it's own transaction. CallcloseLogFile()
to close the log file and commit changes to the database. If this method is called when a log file is open, it does nothing.- Parameters:
path
- The path to log to, if null this method does nothing- Since:
- 2.8
-
closeLogFile
protected void closeLogFile()Close the log file created bycreateLogFile(String)
.- Since:
- 2.8
-
isLogging
protected boolean isLogging()Checks if logging is enabled.- Since:
- 3.19.10
-
log
Log a message to the log file created bycreateLogFile(String)
. If no log file has been created, this method does nothing.- Parameters:
message
- The message to log- Since:
- 2.8
-
log
Log a message and stack trace to the log file created bycreateLogFile(String)
. If not log file exists, this method does nothing.- Parameters:
message
- The message to logt
- A stack trace- Since:
- 2.8
-