JMSL Tutorial: Plug-ins

Writing your own plug-in scanner.

JMSL's ClassBrowser iterates through the CLASSPATH and examines the folders and .class files it encounters there. ScoreFrame uses it to scan for plug-ins. When ScoreFrame starts as a stand-alone, it creates a ClassBrowser and adds the "jmsl_plugins" path to it, like so:
	ClassBrowser browser = new ClassBrowser();
	browser.addSearchPath("jmsl_plugins");
The ClassBrowser matches folders it encounters against its valid search paths. In this example, it will only consider the contents of a folder named "jmsl_plugins". You could add additional folder names.

IMPORTANT: for this example to work, a folder named "jmsl_plugins" must be in the CLASSPATH (as opposed to simply being in the same directory as the application, for example). Java needs to be able to instantiate a plugin, and it cannot instantiate a class that is not in its CLASSPATH.

The ClassBrowser can have ClassListeners added to it. Whenever it encounters a class in a valid search folder, it will notify its listeners of the class name via the public void handleClass(String fullyQualifiedClassName) method defined in ClassListener.

We use a special class named ClassFinder which implements ClassListener. ClassFinder is handed a particlar class name. When it receives notification of a class name, it stores only those classes which are sublasses of the class it is looking for (or implement the interface it is looking for. In the example below it is looking for classes that implement "Instrument"). We add the ClassFinder to the ClassBrowser, and tell the browser to browse the classpath. Whenever the ClassBrowser encounters a class, it will notify the ClassFinder listener. If the class is a subclass of what the ClassFinder is looking for, the ClassFinder will store it.
	ClassFinder findInstruments = new ClassFinder(com.softsynth.jmsl.Instrument.class.getName());
	browser.addClassListener(findInstruments);
	browser.browse();
	// get Vector of found classnames that extend or implement Instrument
	foundInstrumentList = findInstruments.getFoundClasses();

The last line above retrieves the Vector of classes successfully scanned. Next we will build hierarchical menus from found classes.

NOTE: We cannot show a working example of this process since a browser is not permitted to scan your classpath. But your jmslexamples folder contains an example called PluginScannerExample.java which you can examine in detail.

Previous Tutorial Index Tutorial Contents Next
  (C) 2000 Nick Didkovsky and Phil Burk, All Rights Reserved
  JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.