Creating a plugin (Part 3)
Creating a plugin (Part 3)
Embedding JARs in a plugin
Section(s): back office, community, configuration, front office, learning, plugin
In the last part of this tutorial, we will learn how to embed JARs in a plugin, and how to use them.
We assume you have followed the 2 first parts of the tutorial before reading this one:
Part 1 of this tutorial gives you the bases about WEDIA WMS plugins whereas part 2 gives you explanations about how to set parameters on plugins.
Embedding JARs
JARs can be embedded in a lib directory at the root of your plugin directory. In our example, if you wanted to add one or more JAR in the helloworld pluging, you would create a lib directory under the helloworld one. In that directory, you would put all JARs you need.
Using the JARs
Plugins have a special class loader that automatically loads JARs embedded in the lib directory. Although JARs in a plugin are partitioned inside the plugin in other word, you cannot from a plugin "foo" acces JARs embedded in a plugin "bar".
You can access static methods of java classes using the com.noheto.plugins.IPlugin invoke method
Because of this limitation, if you need to use a third party JAR, you will add the third party jar in the lib directory, create your own jar containing classes that will define proxy static methods for accessing the third party JAR. For instance, you cannot acces directly a constructor using the IPlugin#invoke method. You would create a factory class for creating instances of the requested class.
Limits of reflection
Because the invoke method proceeds by reflection to find out which method it should invoke on a class, you might be blocked quickly if the requested method is waiting for a Map as you cannot have an instance of a Map exactly : it would be an HashMap or TreeMap or other but could not be a Map as Map is an interface.
Groovy to the rescue
Fortunatly, WEDIA WMS provides a work around method by using groovy : the noheto:script is defined to let you insert groovy script either by writing the grrovy script directly in the body tag or by specifying the path to a groovy script (have a look to the taglib documentation for more details).
<%@page pageEncoding="ISO-8859-15" %>
<%@taglib prefix="noheto" uri="/WEB-INF/noheto.tld" %>
<%@taglib prefix="c" uri="/WEB-INF/c.tld" %>
<noheto:skipPage test="${not pageContext.request.included}"/>
<noheto:script>
def myMap = [:]
myMap["foo"] = "bar"
def res = plugin.invoke("my.package.MyClass", "mapBasedStaticMethod", myMap)
</noheto:script>

Read also