• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 934
  • Last Modified:

Need JAVA program assistance with new JIRA service

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 myco.com (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 = ctx.search(LDAP_SEARCHBASE, LDAP_FILTER, constraints);
            while(results != null && results.hasMore()) {
                SearchResult sr = (SearchResult) results.next();
                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

1 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

trishajayAuthor Commented:
Thank you for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now