• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems implementing plug-ins....!

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm on a project that involves the use of Plug-ins. Each plug-in is implemented as a class.

As per the advice given to me in my earlier post, I've created an interface called Plugin.
And each class that wants to act as a plug-in, implements this interface.

Furthermore, I Have a String array that holds class names of all available plug-ins.
Now, my problem is to initialize the object of type Plugin with any of the strings in the string array.

The following program illustrates this problem.



Please advise...!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at Class in the JavaDoc. The bits you'll need to figure out are forName and newInstance:

Class.forName(someString).newInstance();

To have a variable called "plugin" that can point to an instance of any of your various plugin classes, they'll all need something in common. Are you comfortable with extending an abstract class or implementing an interface?

Then you'll extend your recipe to:

AbstractPlugin plugin = Class.forName...

How will you choose which plugin to use? Maybe an index into your array of classnames?

Show us what you come up with!
 
Parth Bhatt
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i think i've got my solution... Thanks a ton, Stan...

Actually, what i plan to do is : have a plugins directory inside my application directory. My application will discover the plugins at startup.

The following apply to each plugin :

1) Each plugin comes as a jar file.
2) A class in each plugin(i.e. jar file) implements an interface called Plugin, which forces the class to have a certain set of methods.
3) Each jar file has a "plugin.properties" file which has the name of the class that implements plugin

for example : This is what my interface Plugin would look like :


now each class that wants to act as a plugin(this will be inside the jar file) implements this interface.

for example : take into consideration the class MyPlugin :




Inside My main class, i'll have the following lines of code :



Which plugin to load depends upon the option that the user chooses from the GUI...

Loading a perticular Plugin involves,
1) reading the jarfile for the class that implements Plugin in the file plugin.properties.
2) creating an instance of that class.

The second step is where i was stuck...
What i had posted as a question was a simplified version of this problem so thst it would be easy for people to answet it...!

Thanks again Stan, Do commment upon this mechenism of implementing plugins.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The mechanism seems OK, but this:

Class.forName("MyPlugin").newInstance()

will only work if the jar files are in your classpath. If they're not, you will need to create your own classloader which knows how to load classes/jar files from a directory named "plugins".
 
Parth Bhatt
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ulf,

Yup !! i'm aware of the fact that my jar file should be in the CLASSPATH... i plan to create an Environment Variable on the target machine for this pourpose...!

Thanks for validating my idea...
 
reply
    Bookmark Topic Watch Topic
  • New Topic