This little library makes it possible to create Live plugins for you own apps. This means you can create plugins that can be added/removed while your app is running. Note it is a little experiment that shows the power of B4J. The 'compile to library' feature of B4J is very handy to create plugins.
Things to know when watching the video:
I've added the ABPlugin library to the DemoApp, but this is not in the video. Notice the DemoApp keeps running while I write a new 'divider' plugin!
Code:
For the DemoApp:
For a plugin. You'll need all three methods (Initialize, GetNiceName and Run) and they MUST by in this syntax (you cannot add extra parameters to initialize for example).
The demo and library files are in the attached Zip.
UPDATE: the 1.20 version should also support JDK9+ and an extra example on how to use a ResumableSub (for this empale you must also attach the jHttpUtils and Json libraries in the calling program)
Things to know when watching the video:
I've added the ABPlugin library to the DemoApp, but this is not in the video. Notice the DemoApp keeps running while I write a new 'divider' plugin!
Code:
For the DemoApp:
B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private Button1 As Button
Private ComboBox1 As ComboBox
Private Label1 As Label
Private TextField1 As TextField
Private TextField2 As TextField
Private plugin As ABPlugin
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1")
' initialize the plugin lib
plugin.Initialize("plugin", File.Combine(File.DirApp, "plugins"), "MyKey")
' start the plugin lib, check for plugins every 5 seconds
plugin.Start(5000)
MainForm.Show
End Sub
Sub Button1_Action
If ComboBox1.Value = Null Then Return
If TextField1.text = "" Then Return
If TextField2.text = "" Then Return
' create a map with the parameters of the method
Dim params As Map
params.Initialize
params.Put("val1", TextField1.Text)
params.Put("val2", TextField2.Text)
' run the plugin!
Dim result As Double = plugin.RunPlugin(ComboBox1.Value, "calculate", params)
Label1.Text = TextField1.Text & " " & ComboBox1.Value & " " & TextField2.Text & " = " & result
End Sub
' new plugins are found, or plugins have been removed
Sub plugin_PluginsChanged()
Log("plugins have changed!")
Dim plugins As List = plugin.GetAvailablePlugins
ComboBox1.Items.Clear
For i = 0 To plugins.Size - 1
ComboBox1.Items.Add(plugins.Get(i))
Next
End Sub
For a plugin. You'll need all three methods (Initialize, GetNiceName and Run) and they MUST by in this syntax (you cannot add extra parameters to initialize for example).
B4X:
Sub Class_Globals
End Sub
'Initializes the object. You can NOT add parameters to this method!
Public Sub Initialize() As String
Log("Initializing plugin " & GetNiceName)
' Here return a key to prevent running unauthorized plugins
Return "MyKey"
End Sub
' must be available
public Sub GetNiceName() As String
Return "/"
End Sub
' must be available
public Sub Run(Tag As String, Params As Map) As Object
Select Case Tag
Case "calculate"
Dim val1 As Double = Params.Get("val1")
Dim val2 As Double = Params.Get("val2")
Dim ret As Double = val1/val2
Return ret
End Select
Return 0
End Sub
The demo and library files are in the attached Zip.
UPDATE: the 1.20 version should also support JDK9+ and an extra example on how to use a ResumableSub (for this empale you must also attach the jHttpUtils and Json libraries in the calling program)
Attachments
Last edited: