com.ruimo.pluginlib
クラス PluginRepository<T extends Plugin>

java.lang.Object
  上位を拡張 com.ruimo.pluginlib.PluginRepository<T>

public class PluginRepository<T extends Plugin>
extends java.lang.Object

PluginRepository class. PluginResitory class reads plugins from the specified directory. All of plugins should be jar files that contains plugin configucation file named 'plugins.conf'. As jar files that do not include the plugin configuration file are simply added to the classpath, you can store helper jar files into the plugin directory as well as the plugin jars.

Plugin configuration file is simply executed by PluginRepository as a Groovy script. You can use full functionality of Groovy to initialize plugins.

Assume you have created a plugin named MyPlugin.

public class MyPlugin implements Plugin {
    public void hello() {
        System.out.println("Hello World");
    }

    public String getName() {
        return "Plugin example.";
    }
}

You need to prepare a plugin configuration file (plugins.conf) for this class:

plugins.register(new MyPlugin())

As this is a Groovy script, you can initialize your plugin as much as you like such as:

myPlugin = new MyPlugin()
myPlugin.path = 'contextpath'
myPlugin.timeout = 100
plugins.register(myPlugin)

If you have more than one plugins, you can store them in a single jar file and register them at the same time such as:

plugins.register(new MyPlugin1())
  .register(new MyPlugin2())
  .register(new MyPlugin3())
...

Or, you can make separate plugin jars for them, of course.

Once you finished to create the plugin and it's configuration file, store them into a jar file. The plugin configuration file should be located at the root level of the jar file.

myplugin.jar
  |
  +-- plugins.conf
  |
  +-- MyPlugin.class
  |
  ... You can store more than one plugin classes.
  ... You can store non-plugin classes as well.

To load the plugin in your application, Make an instance of the PluginRepository:

File pluginDir = new File("plugins");
PluginRepository>MyPlugin< pluginRepository = new PluginRepository>MyPlugin<(pluginDir);

Now you can access all of the registed plugins by using plugins() method.

for (MyPlugin plugin: pluginRepository) {
    plugin.hello();
}

This class is not thread safe. If you want to use this class in multi-threaded environment. Make sure to synchronize this object by yourself.

CAUTION!: Do not add plugin jars into the application classpath. Plugin jars are loaded by a separate class loader. You don't need and must not add plugin jars into your application classpath. Java class is loaded in 'parent first' manner. If you add plugin jars into application classpath, they will be loaded by application class loader (normaly system class loader) but the class loader for plugins. This will cause many hard to debug problems.

作成者:
S.Hanai

フィールドの概要
static java.lang.String PLUGIN_CONFIG_FILE_NAME
          Name of the plugin configuration file.
 
コンストラクタの概要
PluginRepository(java.io.File pluginDir)
          Constructor.
 
メソッドの概要
 java.lang.Iterable<T> plugins()
          Return an iteratable object that iterates over all of the regsitered plugins.
 PluginRepository<T> register(T plugin)
          Register a plugin.
 
クラス java.lang.Object から継承されたメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

フィールドの詳細

PLUGIN_CONFIG_FILE_NAME

public static final java.lang.String PLUGIN_CONFIG_FILE_NAME
Name of the plugin configuration file.

関連項目:
定数フィールド値
コンストラクタの詳細

PluginRepository

public PluginRepository(java.io.File pluginDir)
                 throws java.io.IOException
Constructor. Create an instance of PluginRepository(java.io.File). The created plugin repository reads jar files in the specified directory.

パラメータ:
pluginDir - Directory from where plugin jar files are read.
例外:
java.lang.NullPointerException - Thrown if pluginDir is null.
java.io.FileNotFoundException - Thrown if pluginDir does not exist.
java.io.IOException - Thrown if I/O error occured.
メソッドの詳細

plugins

public java.lang.Iterable<T> plugins()
Return an iteratable object that iterates over all of the regsitered plugins. If no plugins are registered, returned iteratable object ends iteration at once.

戻り値:
Iteratable object. Null will be never returned.

register

public PluginRepository<T> register(T plugin)
Register a plugin.

パラメータ:
plugin - A Plugin object to be registered.
例外:
java.lang.NullPointerException - Thrown if plugin is null.
java.lang.IllegalArgumentException - Thrown if the specified plugin is already registered.