Results 1 to 4 of 4
  1. #1
    TE5LA is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Question Java plugin framework advice please....

    I have a large java project which produces a single java application. Within my project I have six classes implement an interface within my project and I load one of these classes dynamically on initialization. I want to migrate this simple class loading to a plugin framework so that my application loads one of six jar files instead of one of six classes and maybe have it so people could provide me with plugins.

    Could you please advise on the best way to do this....

    Initial thoughts:
    > Update my ant build scripts to iterate through each of the java files I want as .jar plugins and compile it with the following
    - the plugin interface (required to compile no?)
    - the class files which contain the objects my plugin interface passes (required to compile no?)

    > create some plugin manager class within my application to load and go hunting through the jar files to find the one I want?

    ##### Is the above crazy? should I be creating the plugins in a different Java project? like six of them? If so they would require me to be copying over the interface class and the object classes that are passed through the plugin interface each time I edit it :/


    Questions:
    > For someone to go create a plugin or if I wanted to go create a new project and use it to make a plugin I would then have to go publish/copy my interface class and the class files containing the objects that are passed through the plugin interface??
    > If the above solution (in initial thoughts) sounds sane what is the best way to have the plugin manage locate the correct jar file its looking for?


    Thanks!!!!!!!!!
    Last edited by TE5LA; 05-31-2014 at 09:08 PM.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java plugin framework advice please....

    Hi,
    the common pattern that I see is dependency injection.
    So a plugin (jar file) is simply copied to the classpath and the configuration file is modified (which could simply name a class to use).

    And yes: all classes/interfaces you want the plugin to know must be public. I don't know if my solution is really optimized, but I have multiple projects:
    - a library with just the required stuff for other projects
    - server project
    - plugin projects
    And between server and plugin I do not have a direct dependency. They only use the library.

    There are some nice frameworks (e.g. spring) but you can do it on your own, too.
    In one project I simply have a config.properties in which I define the class to load and then I simply load that class and create a new instance.

    Konrad

  3. #3
    TE5LA is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java plugin framework advice please....

    Can you possible provide me with examples please? I'm trying to understand how I implement this from a high level.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java plugin framework advice please....

    Hi,

    Ok, create a jar with some classes / Interfaces e.g.
    - AddOnManager Interface which has an init() method. And all implementation should have a standard constructor (which you cannot enforce in an interface as far as I know.)
    - Maybe a getBusinessInstance() method. Feel free to add any arguments. All this does not count ...
    Then you might have an abstract class (we already had an interface so I just choose this now) Business - No idea what you want to put inside ...
    ==> You create a simple jar from this.

    Now you generate a Server. This server simply gets
    - a config.properties files in which you define AddOnClass which can be a classname.
    - The Server then simply does something like this:
    Java Code:
            try {
                Class addOnClass = Server.class.getClassLoader().loadClass(addOnClass);
                AddOnManager addOn = addOnClass.newInstance();
                Business business = addOn.getBusiness();
            } catch (Exception ex) {
              // Handle exceptions. If the admin writes a classname that cannot be found or that is not an AddOnManager then you get an exception.
            }
    This can be put into another separate jar / war / whatever you use to distribute your application. (Needs more stuff e.g. a class with a main method and manifest and such things ...

    And you have to create the addon itself.
    - This will have a dependency to the first jar we created. (So this jar must be on the classpath)
    - You just implement a class that implements AddOnManager (and returns a new Business instance).
    - You simply extend the Business class.

    The last block can be done by anyone. So it is possible that you generate the first 2 blocks and someone else generates the last block.
    And all that is required is: the jar file with the addon must be copied into the classpath and then the config file must be altered. And I can develop an addon to your application any time and you do not need any information about my addon. There is no dependency of your code to my code.

    With kind regards,

    Konrad

Similar Threads

  1. Need advice if a job in java is for me?
    By k8mcrmy in forum Jobs Discussion
    Replies: 1
    Last Post: 04-30-2013, 09:28 AM
  2. New to Java and need an advice
    By wael_tahon in forum New To Java
    Replies: 2
    Last Post: 11-03-2012, 01:52 PM
  3. Replies: 2
    Last Post: 06-21-2011, 05:49 AM
  4. Replies: 0
    Last Post: 12-16-2010, 10:14 AM
  5. Java Plugin Framework (JPF) 1.0.2 and 1.5.1
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 10:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •