How to implement plugin support for a Java Application : Part 2
As I mentioned in the previous article, in this article I will implement two plugins to draw two different types of circles. Here are the two circle types, that will be added,
- redcircle : This is a circle which has a red colored border. And this circle is filled with red color.
- bluebordercircle : This circle is not filled with any color. However this circle contains a blue colored border.
Without wasting time, let’s move into code. I am going to start with the redcircle.
1. redcircle
Step 1: Create Maven Module
As the first step I have created a maven module named “red_circle” in circle_drawer/plugins/ directory. The structure of the module is shown below,

Step 2: Define Parent
As the next step I defined circle_drawer as the parent of this red_circle module. After that pom.xml was looked like below,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.drawer.circle</groupId>
<artifactId>circle_drawer</artifactId>
<version>0.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<groupId>com.drawer.circle.plugins.red_circle</groupId>
<artifactId>red_circle</artifactId>
</project>
Step 3: Add red_circle module to circle_drawer
As the next step I wanted to add the red_circle module into the circle_drawer parent project. Even if red_circle resides in circle_drawer, circle_drawer doesn’t know about red_circle. Hence, I have to introduce the red_circle module to circle_drawer by adding it to pom.xml. After doing that, the pom.xml of circle_drawer looked like below.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.drawer.circle</groupId>
<artifactId>circle_drawer</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
<module>app</module>
<module>plugins/red_circle</module>
</modules>
</project>
Step 4: Let’s Try to build the project
As the next step I am going to build the project using the maven build command.
I am using eclipse IDE. For build I had to right click on “circle_drawer”, and go to Run As-> Maven Build. I used a clean package as the goal. And then clicked Run. After successful built, output was as below,

Step 5: Add app as dependency
As I mentioned before, all plugins should use the Circle.java interface when implementing plugins. Since the Circle.java resides in the app. We should add app as a dependency. For this I have to add the following lines into the pom.xml of red_circle.
<dependencies>
<dependency>
<groupId>com.drawer.circle.app</groupId>
<artifactId>app</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
Then final pom.xml of red_circle was as below,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.drawer.circle</groupId>
<artifactId>circle_drawer</artifactId>
<version>0.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<groupId>com.drawer.circle.plugins.red_circle</groupId>
<artifactId>red_circle</artifactId>
<dependencies>
<dependency>
<groupId>com.drawer.circle.app</groupId>
<artifactId>app</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>
Step 6: Coding plugins.PluginCircle.java class
As the next step I am going to implement the logic for the drawing circle. For this first I need to create a package named plugins in src/main/java of red_circle. Then I created a PluginCircle.java class in the plugins package. This class implements the Circle interface. After all, I just had to override the getName() and drawCircle() methods. Final class is shown below,

package plugins;import java.awt.Color;
import java.awt.Graphics;import javax.swing.JFrame;
import javax.swing.JPanel;import c_interface.Circle;public class PluginCircle implements Circle{public String getName() {
return "redcircle";
}public void drawCircle(final int radius) {
JPanel pnlCircle = new JPanel() {
public void paintComponent(Graphics g) {
int X = 250-radius;
int Y = 250-radius;
g.setColor(Color.red);
g.fillOval(X, Y, radius * 2, radius * 2);
g.drawOval(X, Y, radius * 2, radius * 2);
}
};
JFrame frame = new JFrame();
frame.add(pnlCircle);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Step 7 : Rebuild
As the step 7 rebuild the maven project circle_drawer as shown in step 4. Final built result looked like below,

Step 8 : copy jars into plugin directory
Now just go to the target directory of red_circle and copy the jar file. Now paste that jar file into circle_drawer/app/library/plugins as shown below,

That’s all.. Now let’s test
Testing
Now just run the app module. We can see that now it supports two circle types. Since we tried basic circle in previous article, this time I am going to try redcircle, which we added through plugin. Output will be as below.


2. bluebordercircle
I am not going to describe all the steps since they are the same as red_circle. Code for the PluginCircle.java will be as below,
package plugins;import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;import c_interface.Circle;public class PluginCircle implements Circle {public String getName() {
return "bluebordercircle";
}public void drawCircle(final int radius) {
JPanel pnlCircle = new JPanel() {
public void paintComponent(Graphics g) {
int X = 250-radius;
int Y = 250-radius;
g.setColor(Color.BLUE);
g.drawOval(X, Y, radius * 2, radius * 2);
}
};
JFrame frame = new JFrame();
frame.add(pnlCircle);
frame.setSize(500, 500);
frame.setVisible(true);
}}
After adding jar file of bluebordercircle, the output of app module will be as follow,


As shown in both articles. The circle drawer java application only supports basic circle type. And we can add any number of new circle types by introducing new plugins.
Finally we were able to build an expandable circle drawer due to this plugin architecture. I hope that you could able to gain some precious knowledge after reading both articles. Thanks for reading 😀.