Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner How to store and access configuration data for groovy scripts?

Hi ereryone,

I'm using Adaptavist ScriptRunner 5.3.6 for JIRA Server. I'm trying to setup up a couple of scripts, which need some configuration data, like JDBC URLs, usernames and passwords, which I don't want to hardwire into the scripts.

Where shall I store these data?

There are a two options imho:

  • use JNDI (but I'm having trouble creating an instance of javax.naming.InitialContext):
import javax.naming.InitialContext
def ic = new InitialContext()

// javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.naming.java.javaURLContextFactory

Also, how would I tell JNDI the usernames etc.?

  • read a .properties file. This works:
Properties properties = new Properties()
File propertiesFile = new File('/absolute/path/to/script/root/test.properties')
propertiesFile.withInputStream {
    properties.load(it)
}
log.info properties['test.message'] // works

But I don't like the absolute path. Where should I locate the .properties file? I would guess in my <script_root>, but just using the test.properties without the path won't find the file.

Is there a way to get the script root's absolute path? Using System.getenv() or System.getProperties() does not yield the application_data path.

Thanks for your help!

Arnold

3 answers

1 accepted

2 votes
Answer accepted

If you want the  script root's absolute path then try something like 

import com.onresolve.scriptrunner.runner.ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRoots = ScriptRunnerImpl.getPluginComponent(ScriptRunner).getRootsForDisplay()?.split(", ")?.toList()

Keep in mind that may have been more than one script roots configured. 

Hey Thanos,

this works fine for me. That way I can rely on a known file structure beneath (one of) my script root(s).

Thank you!

Arnold

2 votes

You can also store your properties in sal. You can find more info here:

https://developer.atlassian.com/server/framework/atlassian-sdk/storing-plugin-settings/

That looks interesting. I think this will be the orthodox solution for a grown-up plugin. Thanks for the link!

Arnold

Like Raul Pelaez likes this

Ok thank you. Alexey, I had a look at SAL an I'll explore that, when and if we need a grown-up plugin. For my scripting I'll go with a .properties file.

This is how I've done it:

1. Placed myproject.properties Under <script_root> :

db.driverName = oracle.jdbc.OracleDriver
db.url = jdbc:oracle:thin:@//bla:123/blubb
db.user = myuser
db.password = secret

2. Created an accessor class in <script_root>/mycompany/MyCompanyProperties.groovy:

package mycompany

import com.onresolve.scriptrunner.runner.ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import java.nio.file.Files
import java.io.File
import groovy.util.logging.Log4j

@Singleton
@Log4j
public class MyCompanyProperties {

    final String configFileName = 'myproject.properties'

    public Properties getProperties() {
        def scriptRoots = ScriptRunnerImpl.getPluginComponent(ScriptRunner).getRootsForDisplay()?.split(", ")?.toList()
        //def scriptRoots = ['a', 'b']
        File propertiesFile = null
        for (root in scriptRoots) {
            propertiesFile = new File("$root/$configFileName")
            if (Files.isReadable(propertiesFile.toPath())) {
                log.info "Found ${propertiesFile.toPath()}"
                break   
            }
        }
        // TODO: what if propertiesFile == null?
        Properties properties = new Properties()
        propertiesFile.withInputStream {
            properties.load(it)
        }    
        properties
    }
}

3. Use that class each time to access any configuration data in my scripts:

import mycompany.MyCompanyProperties 

def props = MyCompanyProperties.instance.properties

def driverName = props['db.driverName'];
def url        = props['db.url'];

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you