Availability

Child pages
  • Jython Macro

Skip to end of metadata
Go to start of metadata

Description

Java scripting using Jython

Parameters

Pre-defined variables

Usage

Using Jython libraries?

 
{jython}
out.println("Hello world");
{jython}
 
{jython:output=wiki|script=#example.py|var1=test}
{jython} 
 
{jython:var1=hello|var2=world}
print "Output parameter"
print var1 + var2
out.println(var1 + var2)
{jython}

Security

This macro support Macro Security for Confluence.

 

13 Comments

  1. Does anyone have any additional examples for using jython? I've tried the following within the jython macro:

    from java.lang import *
    from java.sql import *
     
    driverName="com.mysql.jdbc.Driver"
    Class.forName(driverName)
     
    url =  "jdbc:mysql://localhost/mydatabase?user=user&password=passwd"
    con = DriverManager.getConnection(url)
    stmt =  con.createStatement()
    sql = "select * from table1"
     
    rs = stmt.executeQuery(sql)
    list = \[\]
     
    while (rs.next()):
        print rs.getString(1)
     
    rs.close()
    stmt.close()
    con.close()
    

    And this produced an error:
    Traceback (innermost last):
      File "<string>", line 1, in ?
    ImportError: no module named java

    I couldn't find any examples that showed how to use Confluence data sources.

  2. Hi All,

    Can someone please provide a 'if then else' example?

    Whenever I construct jython code that depends on white space, confluence returns the error "Unable to find source-code formatter for language: python."

    Thank you,

    Andrew

  3. I'm trying to write a jython script that works with groups. I'm using some of the beanshell script "user by group" as a basis but I'm getting stuck very early on.

    The beanshell script says:

    import com.atlassian.core.user.GroupUtils;

    so I've written:

    from com.atlassian.core.user import GroupUtils

    but jython reports:

    ImportError: cannot import name GroupUtils

    Any suggestions?

     

    1. ... and changing the syntax to this:

      import com.atlassian.core.user.GroupUtils

      results in:

      ImportError: No module named GroupUtils

       

        1. Thanks for updating the script. Eagerly awaiting version 4.3 (smile)

           

          1. Use the snapshot. Release is unlikely for some time.


  4. Hi,

    I would like to use the pre-defined variable labelManager, but this is not working. When I try to define it with the ContainerManer.getComponent('labelManager'), I get the error: ImportError: No module named atlassian. I wanted to import com.atlassian.spring.container.ContainerManager. Without this, he doesn't know what to do with the ContainerManager.

    Am I missing something? Is there some other possibility, of how I can obtain usage of the variable labelManager?

    The funny thing is, I can use the pre-defined variable context without any problems...

    1. labelManager is pre-defined which means you just need to reference it. For example: 

      out.println("labelManager: " + labelManager.toString())

      1. Hi Bob,

        and thanks for the answer!

        The strange thing is, if I use the labelManager as you showed in your example, I get an "error running script". The error information tells me:

        Traceback (innermost last):
          File "<string>", line 1, in ?
        NameError: labelManager

        I got the same error writting it in Beanshell. There my workaround was to use the ContainerManager, to define the variable again. I thought I can use it in Jython as well.

        What's very confusing is, as I mentioned it before, I can use context without any problems. For example:

        ceo = context.getEntity()

         is working.

        1. That is strange as all the variables are tested in the automated test - in fact, I copied/pasted from the test for the example. Are you sure you are on the lastest plugin version?  Some replacement variables have been added over time.

          1. Ok, I see. I work with Confluence 3.4.9. This could be the reason, why it's not working.

            Well then, I hope I'll get updated as soon as possible =)

            Thanks again!!

            1. Ugh! Ok, well then the approach to use the containerManager is the right way to go.

              import com.atlassian.spring.container.ContainerManager
              
              labelManager = com.atlassian.spring.container.ContainerManager.getComponent("labelManager");
              out.println("labelManager: " + labelManager.toString())