Creating Your Own Plugin
In this section, we'll take a look at an example of creating JBStrap plugins. The sample code uses RestAPI to execute a meteorological data query for a specific city via OpenWeather API.
How to develop a plugin
Using archetypes
The first step of development is to create a new project using the Maven archetype JBStrap-plugin . This can be done either with Maven tools or by entering the following Maven command.
Maven commands are entered into a new folder, after which all necessary files are created in the folder. Import the folder into the developer tool as a plugin project. If you create the project within a developer tool, you can start with the development right away.
Plugin main class
The plugin's main class is in the project's root folder, having the same name as the plugin. The main class implements the JBStrapPlugin interface . This interface requires the implementation of methods necessary for the initialization and correct functionality of plugins. The methods are the following:
- getPluginName : This method must return a text value that contains the name of the plugin. The name of the plugin can be any text value that will appear in the server log when the application is started, as the name of the loaded plugin.
- getPluginVersion : The method must return a text value that contains the version number of the plugin. The version number of the plugin can be in any format and can contain alphanumeric and numeric characters. The version number of the plugin will also appear in the server log after launching the application.
- getPluginBuildNumber : The method must return the plugin build number in text format. The plugin build number will appear after the version number in the server log when the plugin is loaded. The build number can also consist of numeric and alphanumeric characters.
- getPluginFullName : The method must return the full name of the plugin as a text value.
- getPluginAuthor : The method must return the developer's name as a text value
- getPluginAuthorURL : The method must return the developer's URL
- getDescription : The method should return a short description of the plugin.
- init : In this method, the initialization of the plugin must be implemented. This method will be called automatically by the framework after loading the plugin, so here you can make the necessary settings required for the plugin to work. The method may throw an error, in which case the plugin will stop loading, the error will appear in the server log, and the plugin will not work.
The framework does not define any restrictions regarding the structure of plugins, so if you implement one of the classes above, you already have a plugin that can be loaded into the JBStrap framework. After that, all you have to do is make the plugin work.
Implementation
Now that we know the theory of plugin development, let’s create our weather plugin. Our plugin will be named weather plugin.
In the main class of the plugin, we implemented the methods required by the JBStrapPlugin interface and added a configuration option. Using this option, you can set and get the API key. The API is free to use, but it requires registration. Upon registration, you will receive an API key that can be set using this method. Click here to complete the registration .
Once the main class is ready, make a simple enum that will configure temperature units to be used. This enum is called Unit .
Once this has been done, create a class calling Rest API. This class is used as a service. It will generate a URL for the query and will execute the query itself. The class is named RestService .
When creating the class, you must specify a default address used for the query. You can then add parameters to the class that will be included in the URL as a query parameter. The remote server will send a response based on these parameters. We will use the RestService execute method to execute your query. The return value of the method will be the status of the HTTP request. Depending on the status of the request, in this example, we will receive either an HTML content or a JSON content. If the query is successful, the HTML content containing the current weather conditions for the specified city is returned. In the event of an error, the call returns a JSON with the error message text in the message attribute. The result obtained in this way can be requested from RestService using the getResponse method.
We now have everything we need to describe the weather in a city. In the plugin, also create a component that will display the city weather or, in the event of an error, the text of the error message. This component is called CityWeather , and when you create it, you will need to specify a city. Extend the component from the DIV component, in which the information will be displayed using an HTMLFlow component.
You will be able to place the component made in this way on any page and display the current weather in a specific city. If you change the unit of measure or the name of the city on the component, it will automatically query the data again and will already display the new data. If you want to manually update the data, you must call the refresh method for the component.
We can assign click event handlers, mouse cursor event handlers to the component, and the component also supports drag & drop.
At this point, the plugin is ready to use. Now all you have to do is compile the plugin you created and you can load it into the application. You can also compile the plugin from the development tool you are using, or enter the following maven statement in the folder containing the plugin's source code:
Whether compiled with the development tool or using the code above, the compilation will produce a JAR file that will be included in our local maven repository. Then the plugin needs to be added to the pom.xml file of our application and also loaded into the framework. You can do this by following these steps:
If you're ready with that, create a page in the application with an input field where the user can enter a city name. Also, place a combo box into the application, listing measurement units to display data. A plugin component on the page will display weather data. London is set as a default location for weather data.
If the user rewrites the name of the city on the page and presses ENTER or changes the measurement unit, weather data will be queried automatically. The current data is also queried when the user clicks the ' Apply ' button.