Results 1 to 4 of 4
  1. #1
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default Java plugin system

    Hi all.
    I have made an irc bot in java and he's working fine.
    But now I want to make a plugin system.
    It's meant to organise the code and make it easier to add other functions.
    I have absolutely no idea of how to start with it.
    How can I do this?

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    What you need to do, is define an interface (or set of them) that all plugins must comply with. Then, you use reflection to create instances of the plugin.

    For example, the interface definition...

    Java Code:
    public interface IRCPlugin {
      public void doSomething();
    }
    A plugin that complies with the interface...
    Java Code:
    public class IRCPluginA implements IRCPlugin {
      public void doSomething() {
         //  ...put the unique code here to do something
      }
    }
    Next, you need a system that allows you to load the plugins without recompiling. Here is a code frgament that demonstrates the idea. In this fragment, it is assumed a hot load is not necessary (i.e., must restart the program to pick up new plugins). If hot load is desired, you can use a custom ClassLoader to load them dynamically.

    Java Code:
    // obviously, you want to get the name of the plugin from a command line, environment variable, properties file, or whatever rather than hard coding like this
    
        IRCPlugin plugin = 
          (IRCPlugin) Class.forName("IRCPluginA").newInstance();
    
        plugin.doSomething();

  3. #3
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    I don't really get it.
    Could you explain it a bit more?

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Do you understand what interfaces are and their power? Do you understand basic reflection? Explain which parts you don't get.

Similar Threads

  1. Replies: 1
    Last Post: 03-06-2009, 12:40 AM
  2. UML to Java Plugin
    By javaplus in forum Eclipse
    Replies: 0
    Last Post: 11-22-2007, 11:30 AM
  3. How to access system calls in java
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 04:12 PM
  4. Java Plugin Framework (JPF) 1.0.2 and 1.5.1
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 11:16 AM
  5. AnyEdit Plugin For Eclipse Plugin
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-18-2007, 01:28 PM

Posting Permissions

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