How to install plugins in java?

I'm not learning how to do wordpress plugins, I suddenly have a question about how to install the plugin in java. Previously, only follow the style of request to write to it, but never made this type of plugin ever. Ask Mr. Gúc. After a digging digging, I also called to grab some parts, note here for me to ask the same questions.

plugin

What is a plugin?

Saying that, the plugin is not strange, typically like the popular browser types like Chrome, Firefox, Opera do not install a bunch of plugins every time when the browser has nothing unusual to pull the first few plugins. check out first. Simply put, Plugin is a set of software to support, extending the functionality of a larger application software, based on the game but the protocol and API provided by the big software.

What's the plugin interesting?

The plugin architecture is really great, it allows extensions (even changes) of the functions of the host applications (temporarily called the master application), with a well-designed plug-in applications How many custom functions each plugin brings to a utility that extends the system's capabilities is extremely great, I would like to chew it again and have to design it well. there.

With the open source community evolving today, with a good base application and a dark API plugin, the development speed will be unbelievable. For example, the slack of a very popular messaging and exchange system is only a few years old, but the number of plugins in the slack repository has been huge, mostly by third parties, really. Increasing the value of the original application by many times and a team cannot do it all.

Then, how to install this guy?

For script languages ​​such as Javascript, PHP, python … with the characteristics of each run is a translation , it seems there is no problem just using a callback guy to finish this I will talk about on a beautiful day not far. With Java the story may not seem so easy, Java doesn't have a callback or anything, I'm a Java chicken so I found this a little bit sweet.

application-architecture

Basically to be able to plug in the plugin, the application must have the simplest architecture as shown in the image above, the two basic components are application core and plugin manager :
application core: this guy is the master application responsible for ensuring system logic, performing tasks and hook plugins that perform their tasks at runtime.
plugins manager: the guy who is under the direction of the core is responsible for registering plugins to add, remove, turn on, turn off the plugins.

In order to be able to plug in the plugin, we must have an interface plugin, which is the interface that all plugins will inherit, plugin manager loads the plugins and initials (this is to create the entity of each plugin) into a List Then plugins from which application core will be removed from the first one and force it to perform certain tasks.

Well, I explain the banana too much, no one understands it. For visually vivid invites see the example below.

An intuitive example

In this example, I will create a single application with 1 number input, the plugins will be added to that variable to produce the final result plus subtract multiplication.

Ok, first we have to have a common interface guy for plugins. temporarily called PluginFunction.java .

Next we have to have a main to run the application logic and call the plugins, called PluginDemo.java contains the main function.

I see in getPlugins a for loop to load all of the classes that the interface is PluginFunction then create an instance of that class and stored in an array plugins . Then the runPlugins function will browse all the plugins set parameters that run the run function of each plugin and print out the results.

In addition, the getPlugins function has one more interesting thing: the setSecurityManager line, because these are plugins independent of the system, we have to consider it, but if any young person is happy for the plugin to remove the operating system. I just sat there crying ? .

This is the content of PluginSecurityManager.java file

In addition, we need a loader to load additional plugins PluginClassLoader.java

Haiz, basically the setup system is done, next we will need 1 or 2 plugins to test the player to see if it runs smoothly, we will put these files in the plugins directory.

Equality of parameters:

Add 1 unit to the parameter:

TryToExit.java tried to allow this young person to abuse it, in PluginSecurityManager we have set the plugin to exit the program.

Next is the translation and test run, if there is nothing wrong, it will get this output, I use java 8 so I spit out the Note, but nevertheless it can run anyway ? :

In fact, this toppic also adds another problem: build to jar file and install plugin with xml but this time is very busy. Please make an appointment for another occasion.

Thanks for the sample code of Ulf Dittmer , you can download code for reference.

Phuu … thanks ae for watching this far, the song that was long-spinning had tired of asking me to stop the key here. Wish ae happy weekend.
hello cordially and win.

ITZone via blogk.xyz

Share the news now