Active Objects in a ScriptRunner plugin


I'm digging up an oldie-but-goodie: using Active Objects in a ScriptRunner groovy class (impemlements CannedScript).  Everything I could find on the topic is in these two posts from 2012-2013: 

As-expected, I can't obtain an ActiveObjects instance through OSGi.  And also noted in the posts above, there are good reasons for NOT using SQL.

Before I go ahead and store my data in a separate MongoDB instance, has anyone gotten anywhere using Active Objects in a groovy class?




1 answer

So, ScriptRunner has come a long way since those posts! While you certainly could store your data in some external store, you should be able to use Active Objects in the context of your CannedScript.

First, I'm going to assume you've already exhausted the somewhat simpler option: using Atlassian's PluginSettings API. For a lot of scripters out there, that's going to be the more maintainable way forward. Indeed, we use that in our very own Template Comments for JIRA Service Desk plugin, as you can see in its source (particularly the CannedCommentsService).

That said, we do use ActiveObjects too.

So, with that assumption, a relatively simple way to get an instance of ActiveObjects would be like so:

import com.atlassian.activeobjects.external.ActiveObjects
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
def ao = ScriptRunnerImpl.getOsgiService(ActiveObjects)

Then you could use the ao variable to access an instance of ActiveObjects.

Another path forward would be to use the ScriptRunner class com.onresolve.scriptrunner.runner.util.AOPropertyPersister, which has some static methods for saving data.

This sample script shows how to save a simple POGO (Plain Old Groovy Object) using AOPropertyPersister.

import com.onresolve.scriptrunner.runner.util.AOPropertyPersister

String myActiveObjectsPropertyKey = "com.mycompany.something.something.darkside"

class MyConfig {
int id
String name
String data

def instance = new MyConfig(id: 1, name: "mine", data: "Some stuff here")
AOPropertyPersister.saveTyped(instance, myActiveObjectsPropertyKey) //This saves the data

def data = AOPropertyPersister.loadTyped(myActiveObjectsPropertyKey, MyConfig) //This loads an instance of the above class
log.debug data
assert == "mine"
assert == 1
assert == "Some stuff here"
log.debug "Finished executing demo script of AO"

Full disclosure: both those APIs (ScriptRunnerImpl and AOPropertyPersister) are technically subject to change. We have no immediate plans to break them, but as always, make sure to test your scripts in a test environment when undergoing upgrades and so forth. That goes double for use cases where you get into persistence and other things that, as Gary Bernhardt so eloquently put it, are stateful and fail. :)

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

478 views 8 28
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you