Samstag, 10. Dezember 2011

gwt-plug: getting started

Preparation

  1. Download gwt-plug
  2. Add the gwt-plug-x.x.jar and gwt-plug-x.x-sources.jar to your project's classpath
  3. Add gwt-plug's GWT module as dependency to your GWT application's *.gwt.xml files:
    <inherits name="de.richsource.gwtplug.GwtPlug" />

Create the API

To make your GWT applications talk to each other, you must define a common language. In this case this language is defined by a Java interface.
Let's create one:
public interface ExampleService {
    String doSomethingWithThatString(String input);
}


To mark that interface as exportable to other GWT appications running in the same host page, we have to create a marker interface that extends "de.richsource.gwtplug.client.PluginComponent" with our newly created "ExampleService" as generic argument:
public interface ExampleServicePluginComponent extends PluginComponent<ExampleService> {}

Both, the "ExampleService" and the marker "ExampleServicePluginComponent" must be accessible by your GWT applications that will be able to communicate. But it's not necessary to have both interfaces in the same GWT module.

Export an instance

Now you need an implementation of the "ExampleService":
public class ExampleServiceImpl implements ExampleService {
    String doSomethingWithThatString(String input) {
        return "Foo"+input;
    }
}


You can create an instance of the "ExampleService" as you would normally do:
ExampleService instance = new ExampleServiceImpl();

To export your instance in one of your plugins do:
PluginComponentManager.initialize();
PluginComponentManager.export(instance); }


Import and use an instance

On the other side you have the consuming GWT application. That one doesn't need to provide an implementation of the "ExampleService". Instead you must register a listener at the "PluginComponentManager" to get informed if a new plugin component is registered with the plugin system:
PluginComponentManager.addListener(new PluginComponentRegisteredListener() {
    public void pluginComponentRegistered(Object importedComponent) {
        if(importedComponent instanceof ExampleService) {
            String result = ((ExampleService) importedComponent).doSomethingWithThatString("bar");
            RootPanel.get().add(new Label(result));
        }
    }
}); PluginComponentManager.initialize();


The "PluginComponentManager.initialize();" must be called after registering the listener to ensure that you always get informed for new plugin components even if they are already added.

Additional information

The common steps to initialize your plugins are:
  1. Register listeners to handle new plugin components added by other GWT applications
  2. Initialize the "PluginComponentManager" to register for new plugin components and read the already added components once
  3. Export plugin components to be used by other GWT allications
This order must be used to make it correctly working.

Keine Kommentare:

Kommentar veröffentlichen