Skip to content
nicholashagen edited this page Mar 19, 2012 · 2 revisions

Previous - Assets | Table of Contents | Next - Applications

Plugins in TeaServlet provide the ability to load custom classes that may be used by other plugins or applications. The most typical example is a data source plugin. The data source plugin could provide a standard API for accessing any type of general data source. Specific implementations of that plugin may provide access through JDBC, Hibernate, etc. A given application could utilize that plugin and its API to retrieve data and provide the resulting data to the templates.

Plugins must implement the org.teatrove.trove.util.plugin.Plugin interface. Generally, plugins will extend the org.teatrove.trove.util.plugin.PluginAdapter class that provides basic stubbed methods. Plugins will then implement the getName method and init method. The init method provides configuration for the plugin via the associated init section.

The following is an example XML and class for a custom plugin.

<teaservlet>
    <plugins>
        <UserDatabase>
            <class>com.example.DataSourcePlugin</class>
            <init>
                <databaseName>User</databaseName>
            </init>
        </UserDatabase>
    </plugins>
</teaservlet>
import org.teatrove.trove.uti.plugin.*;
public class DataSourcePlugin extends PluginAdapter {
    private String databaseName;

    public DataSourcePlugin() { super(); }

    @Override
    public void init(PluginConfig config) {
        this.databaseName = config.getProperties().getProperty("databaseName");
        // initialize database connection
    }

    public Object getObject(Object key) {
        // lookup from database for the given key
    }
}

The PluginConfig contains more than just the initialization properties, however. The following are some of the more important methods:

Plugin getPlugin(String name) This will look up a previously declared plugin. If the plugin does not exist, then null is returned. Note that plugins are loaded sequentially in the order they are defined. As such, dependent plugins of a plugin must be defined first.

Plugin[] getPlugins() This will return all currently loaded plugins. Note that plugins are loaded sequentially in the order they are defined. As such, only those plugins previously specified will be returned.

PropertyMap getProperties() This will return the list of properties within the init section of the plugin.

Log getLog() This will return the logger associated with this specific plugin that may be used to log data. The individual log settings may be configured by providing a log section within the plugin section.

PluginContext getPluginContext() This provides access to the underlying PluginContext that the TeaServlet uses to manage the plugins. The PluginContext has three main purposes. First, it provides access to the already created plugins through the getPlugins and getPlugin methods. Second, it provides the ability to add plugins to the system directly through the addPlugin methods. Finally, it provides access to the underlying ResourceFactory to lookup resources. For example, if a plugin needed to load another configuration file, it could use the ResourceFactory to lookup that configuration and support the TeaServlet configuration formats and locations. See TeaServlet Configuration for more information on resource factories and configuration files.

Plugins may be used for any number of use cases. Plugins are also not exposed to the Tea templates other than through applications that choose to expose them.

Previous - Assets | Table of Contents | Next - Applications