aspose file tools*
The moose likes Java in General and the fly likes Writing Plug-Ins (Not web) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing Plug-Ins (Not web)" Watch "Writing Plug-Ins (Not web)" New topic
Author

Writing Plug-Ins (Not web)

Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
I want to write a file editor and provide the ability to use plug-ins. If this were a picture editor then I would want to allow people to write their own plug-in to load new picture formats. Another plug-in example would be winAmp. People can write their own visualizations and just drop them into the specific directory.
Any ideas?
Best regards,
Aaron R>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

This is exactly the type of thing interfaces are very good for.
You will provide and publish an interface that your code will treat all plug-ins as, and others can write classes that implement that interface.
You load the class(es) as an implementation of that interface and the details of the implementation are not important to you.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
Thanks for the reply. What I want to do though, is allow people to compile a class which implements my interface and then use it at runtime - ie load the class dynamically.
I stumbled across the ClassLoader class. If I understand correctly, I need to implement a custom class loader, which will handle loading classes which implement my interface. IE -

The PersonalClassLoader would work with classes which implement the CustomFileReader interface.
Can someone tell me if I'm on the right path or not?
Regards,
Aaron R>
PS - Sorry if the pseudo code is goofy. I've been doing C# for the past few months and my syntax-s might be slightly jumbled.
[ August 29, 2003: Message edited by: Aaron Roberts ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

You don't necessarily have to write a ClassLoader. The simplest possibility is if you've got the name of the class to load in the String "className" (say somebody typed it in) and the interface is named PlugIn then you can get an instance with just one line of code:
PlugIn plugin = (PlugIn) Class.forName(className).newInstance();


[Jess in Action][AskingGoodQuestions]
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
My intent was to have a directory where people would place their plug-in. At run time I would scan the directory and then load each plug-in. If the name of the file was also the name of the class, would I be able to still use the method you mentioned? Can you point me to some pseudo code?
Thanks for the input! Its really appreciated!
Best regards,
Aaron R>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Yes, absolutely, you can do this. In Java, the name of the file is always the name of
the class (modulo package names and things.)
As far as directory-scanning pseudocode, use java.io.File.list() to get an array of File objects representing the contents of a directory, then use a for-loop to loop over it, and then do whatever you're going to do (strip off the .class extension and use the Class.forName() thing we talked about.) It's really an easy thing to do.
The very popular Eclipse IDE has a plug-in architecture, and the way
they handle the details of class names and package names, etc, is that
a plugin is always a JAR file or a subdirectory, and the plugin always includes a file
name plugin.xml, and the XML file specifies what class the IDE should
load (as well as other configuration info.) That way a plugin can consist of multiple classes, only one of
which implements your interface.
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
Just a followup. I was able to get everything working correctly, after a bit of tinkering.
I discovered the following things -
- If you want the plugins located in another directory, then you have to put the plugin under a package with the same name as the deployment directory.
- If you want to have the plugin reside in the same directory as the class which contains the static main method, then just compile the plugin with no package description.
Below is some sample code

Below is the interface for the plugins -

And lastly here is a sample plugin -

You would place the compiled plugin under the PluginDemo/plugin directory. I hope this helps!
Regards,
Aaron
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I wrote an editor with following similarities:
User can write their own plugins.
They put their plugins in a special subdirectory (plugins/).
The app reads all class-Files in this directory, which end with 'Plugin.class' (so their Plugin may invoke other own classes in that directory, which are used by their plugins, without being an own plugin).
The plugins are loaded with 'Class.forName (...)'.
The editor is MUCH more simple than eclipse.
And the plugins are very restricted: They get marked text, may process on this, and give some other text back, so the interface is defined as:

The full project is available at:
http://freshmeat.net/projects/pled


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing Plug-Ins (Not web)