Running DWR inside a Plugin

The DWR plugin library has been updated to allow it to work alongside labels, which clashes with this library when included in the browser.

Incase anyone reading does not know what the Direct Web Remoting project is, then go here. In essence it removes alot of the grunt work of AJAX with Java performing most (if not all) of the marshalling and transit for you. You include some JavaScript files (dynamically generated by DWR) and then you can use the simple DWR JavaScript API to talk to your Java classes.

Adding DWR To Your Plugin

These instructions assume you are using Maven to build your project.

Setup

  1. Install the Plugin Developer Utilities to aid in building and deploying the plugin.
  2. Add the DWR dependency to your project.xml file:
    <dependency>
        <id>dwr</id>
        <version>1.1.1-confPluginHack2</version>
        <properties>
            <plugin.bundle>true</plugin.bundle>
        </properties>
    </dependency>
  3. Add Atlassian's Maven repository ('http://files.adaptavist.com/maven') to your remote repository list in project.properties:
    maven.repo.remote=http://files.adaptavist.com/maven,http://repository.atlassian.com,http://www.ibiblio.org/maven,http://dist.codehaus.org/

If you are upgrading this (or any <plugin.bundle>true</plugin.bundle>) library then you should run maven clean first to remove the temporary files.

Building

Once you have installed the Plugin Developer Utilities, you can use a couple of handy commands to build your plugin with the required classes included. There are two ways to do this, depending on whether you are building for a version of Confluence prior to 2.1.1a, or only for versions after that point.

All versions of Confluence

This will build your plugin jar by exploding any dependencies with <plugin.bundle>true</plugin.bundle> and copying it's contents into the plugin jar's contents.

maven plugin-dev-utils:class-jar

Confluence 2.1.1a or later only

This will build your plugin jar by including any dependencies with <plugin.bundle>true</plugin.bundle> and copying them into the META-INF/lib directory of your plugin jar.

maven plugin-dev-utils:lib-jar

Including

You will need to declare the DWR Servlet in your atlassian-plugin.xml, something like (replace myplugin in the url-pattern and base-path variables with a suitable unique key to avoid servlet collision, you may also wish to change the xml filename too):

<servlet name='Direct Web Remoter Servlet' key='dwrServlet' class='uk.ltd.getaheadplugin.dwr.DWRServlet'>
    <description>Direct Web Remoter Servlet</description>
    <url-pattern>/myplugin/dwr/*</url-pattern>
    <init-param>
      <param-name>base-path</param-name>
      <param-value>/myplugin/dwr</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>myplugin-dwr.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>scriptCompressed</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Note: the debug parameter can be removed before releasing your plugin - leaving it on will aid with testing. To my knowledge all it does is give more verbose logging, and give you a page like this: http://confluence.atlassian.com/dwr/index.html (yours will be at http://conf.domain.dom/plugins/servlet/myplugin/dwr/index.html).

Finally, you will need to define the configuration xml file (set by the config parameter above). To find out what this should contain you will need to refer to the documentation - however there will be a sample below.

Using DWR In Your Plugin

This is going to be a useless hello world example, but it will show you how DWR can be effected in your application (and just how simple it really is). For more advanced cases, I would suggest looking through the interesting DWR example pages - Confluence and JIRA even make the "live sites" section!

Create a class called test.HelloWorld with the following contents:

public String sayHello(String name, int age) {
    if (age < 0 || age > 150) {
        return name +", I can't believe that you are "+ age +" years old!";
    }
    return "Hello "+ name +", you are "+ age +" year"+ (age != 1 ? "s" : "") +" old.";
}

Specify the DWR configuration xml file as:

<?xml version="1.0" encoding="UTF-8"?>
<dwr>
  <allow>
    <create creator="new" javascript="HelloWorld">
        <param name="class" value="test.HelloWorld"/>        
    </create>
  </allow>
</dwr>

and thats it! Compile and deploy your plugin and proceed to http://conf.domain.dom/plugins/servlet/myplugin/dwr/index.html where by you should be shown HelloWorld as a class known to DWR. Click on it and you will be displayed with a page allowing you to type in your name and age. Do so and click Execute. It should return an result consistent with the sayHello method logic.

Note: You may need to restart Confluence each time you upload a new version of the Servlet plugin - this is fixed partially in 2.1.5 (meaning you can disable/enable the servlet module to get the desired effect) and completely in 2.2 (so you can just uninstall/reinstall normally) with CONF-5598.

Finally ...

Try specifying the DWR configuration xml file as:

<?xml version="1.0" encoding="UTF-8"?>
<dwr>
  <allow>
    <create creator="new" javascript="HelloWorld">
        <param name="class" value="java.util.Date"/>        
    </create>
  </allow>
</dwr>

See how the test page for HelloWorld changes, and have fun playing with the possibilities!

Labels

 
(None)
  1. Oct 02, 2006

    Vincent Thoulé says:

    Hi Dan, Does this hack is managed by Atlassian ? Does this hack is available w...

    Hi Dan,

    Does this hack is managed by Atlassian ?
    Does this hack is available with JIRA ?

    Rgds
    Vincent

    1. Oct 02, 2006

      Dan Hardiker says:

      In it's current state it is fitforpurpose using the above details. Please report...

      In it's current state it is fit-for-purpose using the above details. Please report any problems here.

      1. This hack is managed by me personally.
      2. This hack is not currently version tracked properly.

      It needs a bit of an overhaul to make it really managable, there are some changes I made which are no longer needed in Confluence v2.2.1a - I'm waiting for DWR 2.0 to be released for production use before I do that.