Need JAVA program assistance with new JIRA service

Posted on 2009-02-17
Last Modified: 2013-12-19
I found the following Java program that is coded as a service using JNDI called LDAPUserSyncService.  I did not code this, however, it is available for use.  This service queries LDAP user objects based on a configurable filter and creates new Jira users for new users it finds in LDAP. It also has the ability to put a subset of users in a group called internal-users for users whose email matched a particular domain. A more detailed explanation is that it  queries LDAP, iterates through each user to see if the user already exists in LDAP...if not it created the user and then puts it in the jira-users group and then checks if the email address contains (fictitious domain for this example) and if so it puts the user in the internal group as well. The program does not do anything to handle paging or results if the number of users is greater than the max returned by LDAP though. I would like to add this functionality.   There is also an XML file referenced by the getObjectConfiguration() method in the code.  I have attached that for review.  Between the code included here and the attached file it appears a nice solution to automating user creation in JIRA by syncing to LDAP is in place.   My questions are the following:

1.  Where on the host/server system for JIRA should I put the service code and the XML file?
2.  Compiling....some previous users have mentioned having problems compiling and I have not been able to find what their resolution was. One mentioned having problems because of no reference to AbstractService.  I am somewhat new to JAVA so could use some pointers on the command sequence for compiling the service code.  
3.  Any post-implementation issues anyone might see that could occur with this program and XML file?

This is a very long and detailed question so I am quite grateful for anyone's assistance.  This one is worth a lot.  Thank you in advance.

public class LDAPUserSyncService extends AbstractService {


    private static org.apache.log4j.Category log =



    public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";

    public static String GROUP_INTERNAL =  "Internal-Users";

    public static String LDAP_USER_ID_ATTRIBUTE = "sAMAccountName";


    public void run() {

        try {

            log.debug("Running com.myco.jira.service.LDAPUserSyncService");

            String LDAP_HOST = getProperty("LDAP_HOST");

            String LDAP_USER = getProperty("LDAP_USER");

            String LDAP_PASSWORD = getProperty("LDAP_PASSWORD");

            String LDAP_SEARCHBASE = getProperty("LDAP_SEARCHBASE");

            String LDAP_FILTER = getProperty("LDAP_FILTER");   


            Hashtable env = new Hashtable();

            env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

            env.put(Context.PROVIDER_URL, LDAP_HOST);

            env.put(Context.SECURITY_AUTHENTICATION, "simple");

            env.put(Context.SECURITY_PRINCIPAL, LDAP_USER);

            env.put(Context.SECURITY_CREDENTIALS, LDAP_PASSWORD);


            DirContext ctx = new InitialDirContext(env);


            SearchControls constraints = new SearchControls();



            NamingEnumeration results =, LDAP_FILTER, constraints);


            while(results != null && results.hasMore()) {

                SearchResult sr = (SearchResult);

                Attributes attrs = sr.getAttributes();

                log.debug("Processing " + attrs.get("dn"));


                // need sn, givenName, mail

                String first = (attrs.get("givenName") != null) ? (String) attrs.get("givenName").get() : null;

                String last = (attrs.get("sn") != null) ? (String) attrs.get("sn").get() : null;

                String mail = (attrs.get("mail") != null) ? (String) attrs.get("mail").get() : null;

                String userId = (attrs.get(LDAP_USER_ID_ATTRIBUTE) != null) ? (String) attrs.get(LDAP_USER_ID_ATTRIBUTE).get() : null;


                if(first != null && last != null && mail != null && userId != null) {


                    // check if user is in Jira

                    if(!userExists(userId.toLowerCase())) {


                        // create user, if a myco company user then add to internal group

                        if(createJiraUser(userId, first, last, mail) != null) {


                                addUserGroup(userId, GROUP_INTERNAL);

                        } else {

                            log.error("LDAP User " + userId + " could not be created");





        } catch (NamingException nex) {

            log.error("Caught exception trying to Synch LDAP Users: " + nex.toString());

        } catch (ObjectConfigurationException oce) {

            log.error("Caught exception trying to Synch LDAP Users.  Configuration setup failed: " + oce.toString());




    private User createJiraUser(String userId, String fname, String lname, String email) {


        UserManager userMgr = UserManager.getInstance();

        User osUser = null;

        try {

            osUser = userMgr.createUser(userId.toLowerCase());

            osUser.setFullName(fname + " " + lname);



            Group jiraUserGroup = userMgr.getGroup("jira-users");



            log.debug("Successfully added LDAP user "+ userId);

        } catch (ImmutableException e) {

            log.error("Error creating User in Jira: " + userId, e);

        } catch (DuplicateEntityException e) {

            log.error("Error creating User in Jira: " + userId, e);

        } catch (EntityNotFoundException e) {

            log.error("Could not find group jira-users.  Error creating User in Jira: " + userId, e);



        // to be sure the user was created, return the reloaded user from Jira

        return osUser;




    private boolean userExists(String userId) {

        UserManager userMgr = UserManager.getInstance();

        boolean exists = false;

        try {

            if(userMgr.getUser(userId) != null)

                exists = true;

        } catch (EntityNotFoundException e) {

            exists = false;



        return exists;



    private void addUserGroup(String userId, String groupName){

        UserManager userMgr = UserManager.getInstance();


        try {

            User osUser = userMgr.getUser(userId);


            Group group = userMgr.getGroup(groupName);



            log.debug("Successfully added LDAP user " + userId + " to group: " + groupName);

        } catch (ImmutableException e) {

            log.error("Error adding User to acis-users group: " + userId, e);

        } catch (EntityNotFoundException e) {

            log.error("Could not find group acis-users.  Error adding User to acis-users group:  " + userId, e);




    public ObjectConfiguration getObjectConfiguration() throws ObjectConfigurationException {

        return getObjectConfiguration("LDAPUSERSYNCSERVICE", "com/myco/jira/service/LDAPUserSyncService.xml", null);



Open in new window

Question by:trishajay
    LVL 92

    Accepted Solution

    you need to build a jira plugin, have a read here for more details

    To compile it you will need the jira jars in your build path


    Author Closing Comment

    Thank you for your help.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    mergeTwo  challenge 13 53
    count8 challlenge 13 68
    for i loop in grovy 1 26
    Running Jira on Raspberry PI 2? 3 44
    Read about achieving the basic levels of HRIS security in the workplace.
    Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now