Today I’m going to do something I’m typically pretty lousy at, personally, which is finishing what I’ve started. Over the course of my three previous Star Wars themed Plugin Architecture series blog posts, we walked through the development of a search plugin for Confluence. Starting from the cleanest slate possible — a plugin that does nothing at all — we saw how to:

  • add a servlet…
  • make it look visually integrated into Confluence…
  • internationalize it, more or less…
  • find what you’re looking for in the plugin API documentation…
  • create a REST interface to all needed functionality…
  • geek out with Google’s libraries…
  • and make everything awesome with AUI and jQuery…

… but there’s still one big goal I haven’t achieved yet, which is making this plugin work in products other than Confluence. Good news: given all that we’ve done so far, this task turns out to be pretty easy (and consequently, this post will be pretty short). Ready, go.

Step 1: The Omniscient AMPS Plugin

Up to this point, we’ve been using the

1
maven-confluence-plugin

to take care of building our source into a plugin JAR, and running that plugin in a local Confluence testing instance. Without getting into too much detail about the guts of the Atlassian Maven Plugin Suite, I’ll just wave my hands a bit and say that

1
maven-confluence-plugin

is a thin wrapper around a product-agnostic

1
<a href="http://confluence.atlassian.com/display/DEVNET/AMPS+Plugin+for+Maven">maven-amps-plugin</a>

. Its configuration doesn’t look too different:

1 - amps plugin source.png
Notably, I’ve added product sections in its configuration section for both Confluence and JIRA. I’ve also removed one of the last vestiges of the Confluence plugin archetype, which was the

1
provided

-scope dependency on Confluence itself, and replaced it with its transitive dependencies that we still need, namely the servlet API and the Google collections library. Not too exciting.
Previously, to run Confluence with our plugin installed, we’d use the

1
<a href="http://confluence.atlassian.com/display/DEVNET/atlas-run">atlas-run</a>

script from the Plugin SDK, which turns into

1
mvn confluence:run

under the covers. We can use the

1
maven-amps-plugin

pretty much the same way, specifying the product we want with a system property:

1
mvn amps:run -Dproduct=jira

. Again, not too exciting.

Step 2: Fixes for JIRA (Slightly More Exciting)

However, one thing we immediately notice upon running JIRA is that our handy

1
web-item

, that provided the link from Confluence’s “Browse” menu to our servlet page, is missing. So let’s add a new one for JIRA:

2 - web-item source.png
It looks pretty much how you’d expect:

3 - web-item rendered.png
… but we’re not out of the woods yet. If you click on the link and try to search now, the REST request will yield an error response, prompting the same awe-inspiring error alert we’ve seen before:

4 - oh noes.png
Oh noes! The issue here, which you can find easily using your Javascript debugger of choice, is that we still think our application’s base URL is

1
http://localhost:1990/confluence

. I’d sloppily left that base URL hardcoded in the Javascript, but that’s not going to fly anymore. We need some way to tell the Javascript code where it can find our REST API, and it turns out that we already actually have all the tools we need to do it:

5 - uri fixup source.png
In the first post of this series, we used an

1
<a href="http://docs.atlassian.com/sal-api/2.2.1/sal-api/apidocs/com/atlassian/sal/api/message/I18nResolver.html">I18nResolver</a>

instance that the Atlassian Template Renderer conveniently provided by default in its renderer context, to take care of internationalization in the Velocity template. What we’re doing here is simply adding an additional object to that renderer context, that we already had: the

1
SearchResource

class. We call

1
buildSelfLink()

in our template with a placeholder value of

1
{0}

, and finally access and format the final resource URI the same way we did with the search summary text, using

1
AJS

.

There are a few potentially subtle tricky bits about that approach, notably the use of

1
decodeURI()

. Since I chose to use a placeholder value and

1
format()

instead of just string concatenation, the placeholder ends up getting URI-encoded by

1
UriBuilder

in

1
buildSelfLink()

. We don’t actually want it encoded, so we have to decode it back again.
Does it work?

6 - uri fixup rendered.png
Yes.

Step 3: FishEye and Crucible

At this point we’re going to start seeing something funny (for some definition of “funny”) about the different implementations of the Shared Access Layer APIs in each product. I’ve added an additional

1
&lt;product&gt;

section in the

1
maven-amps-plugin

‘s configuration for

1
fecru

, and a new

1
web-item

like before, and to my surprise nothing blows up. But when I try to search for anything, I get no results. What’s up with that?

Time for

1
amps:debug

, which we haven’t really talked about yet. This may be review for many people, but it’s probably worth covering. When you’re trying to debug issues in your Java code, running in an Atlassian application host through the magic of the Plugin SDK, you need to be able to hook your debugger up to that application, which means Maven needs to tell the forked JVM to start itself up with the magic

1
-Xdebug

args necessary for remote debugging. Running

1
mvn amps:debug

takes care of that for you, and helpfully tells you that it’s listening for debugger socket connections on port 5005.

So, over in my IDE, I set a breakpoint in

1
SearchResource.get()

and this is what I see:

7 - param missing.png
Oh yeah. We went to the trouble of including error messages in our search result REST representation, but I never actually bothered displaying them anywhere. Damn my laziness, but hey, we found the problem: Fisheye and Crucible’s implementation of SAL’s

1
<a href="http://docs.atlassian.com/sal-api/2.2.1/sal-api/apidocs/com/atlassian/sal/api/search/SearchProvider.html">SearchProvider</a>

API seems to want an extra

1
application

parameter passed to it, which Confluence and JIRA didn’t need.

Fine, can do:

8 - application param source.png
We inject yet another component,

1
<a href="http://docs.atlassian.com/sal-api/2.2.1/sal-api/apidocs/com/atlassian/sal/api/search/query/SearchQueryParser.html">SearchQueryParser</a>

(declared with

1
&lt;component-import&gt;

as usual in the plugin descriptor), into

1
SearchResource

‘s constructor. (In my opinion, it should really be called

1
SearchQueryBuilder

instead, since it uses the familiar “builder pattern” to construct search queries, but whatever.) We conveniently already have an

1
<a href="http://docs.atlassian.com/sal-api/2.2.1/sal-api/apidocs/com/atlassian/sal/api/ApplicationProperties.html">ApplicationProperties</a>

instance that we’d used previously to discover our own base URL, which we can reuse to determine the value for the

1
application

parameter, so we feed that to the

1
SearchQueryParser

, tell it to

1
build()

, and we should be good:

9 - fecru fixed.png

Step 4: Bamboo?

This whole “cross-product search plugin” thing is going smashingly so far. However, I’ll cut to the chase and just tell you right now that we’ve gone as far as we can go with it, at least for the purposes of this tutorial. I was wondering what Bamboo’s implementation of

1
SearchProvider

might do, and I got my answer through another fun debugging session:

10 - bamboo fail.png
The answer, evidently, is: nothing.

Conclusion

So, what have we learned today? Well, hopefully the most significant lesson overall is that by avoiding product-specific APIs, and preferring to use functionality provided by SAL and other platform components, it doesn’t take too much to coerce a plugin to work in multiple products. Compared to the previous posts in this series, I didn’t get into too many specific areas of the platform, or design and implementation tricks, but there were a couple worth noting:

  • The
    1
    maven-amps-plugin

    : how to run and debug plugins in multiple products.

  • The
    1
    template-context-item

    module type: how to inject your own components into the template renderer context, and how that can help with code reuse.

And with that, we’re done! The final code is available for download. As an “exercise for the reader,” as it were, I’d recommend writing some unit and integration tests… Oh wait, I haven’t written my tutorial on best practices for automated plugin testing yet. If you’d like to see that, or any other topic covered, please let us know!.