Results 1 to 6 of 6

Thread: program structure - allow plugins

  1. #1

    Thread Starter
    Hyperactive Member Kagey's Avatar
    Join Date
    Sep 2000
    Location
    The Wilderness of New Brunswick
    Posts
    294

    program structure - allow plugins

    I was wondering if someone could point to (a) resourse(s) dealing with structuring your program to allow for plugins. (i'm thinking of a text editor right now).

    I tried a search, but there doesn't seem to be a great ubundance,(or i suck at searching).

    thanks for any help.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't know about Java in particular but usually for plug-ins in windows you have a directory where dlls are put in. The app loads every library in this directory and looks for a few key functions in there, which it then uses to initialize the plug-in and communicate with it. It will pass a few function pointers or similar paradigms to the DLL to allow talk-back.
    COM is used much now.

    Of course for Java this won't work. I have to admit that I have no idea what to do there.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You could write an interface that all extension classes have to implement. Then read in the names of all extension classes from some registration file. Use system class loader to load the classes you find in this file. Like this:
    Code:
    package your.package;
    import java.lang.reflect.*;
    import java.util.*;
    
    public interface Extension
    {
      // blabla...
    }
    
    // in the main class
    Vector plugins;
    
    void loadPlugins(BufferedReader regFile)
    {
      ClassLoader cl = ClassLoader.getSystemClassLoader();
      String className;
      while((className = regFile.readLine()) != null) {
        try {
          Class ext = cl.loadClass(className);
          Class[] ints = ext.getInterfaces();
          boolean valid = false;
          for(int i=0; i < ints.length; ++i) {
            if(ints[i].getName().equals("Lyour.package.Extension"))
              valid = true;
          }
          if(valid)
            plugins.add(ext);
        } catch(ClassNotFoundException e) {
          log("Invalid entry in reg file.");
        }
      }
    }
    
    void initPlugins()
    {
      for(int i=0; i < plugins.size(); ++i) {
        Extension e = (Extension)plugins[i].newInstance();
        e.init(this, blabla);
        // ...
      }
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Hyperactive Member Kagey's Avatar
    Join Date
    Sep 2000
    Location
    The Wilderness of New Brunswick
    Posts
    294
    excellent. This method agree's with other methods I've finally found information about on the web.

    Thanks for the kick start!

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Proof-of-concept:
    To compile the main class: go to the extraction directory and type
    javac .\extensible\Extensible.java .\extensible\Extension.java

    To compile a plugin:
    javac .\extensible\Extension.java .\your\package\ClassName.java

    To run:
    java extensible.Extensible
    Attached Files Attached Files
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Hyperactive Member Kagey's Avatar
    Join Date
    Sep 2000
    Location
    The Wilderness of New Brunswick
    Posts
    294
    cool, thanks.

    I will play around with it and try to add my own extensions and see if i can get it to work with mine.

Posting Permissions

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



Click Here to Expand Forum to Full Width