Example usage for javax.naming.directory DirContext rename

List of usage examples for javax.naming.directory DirContext rename

Introduction

In this page you can find the example usage for javax.naming.directory DirContext rename.

Prototype

public void rename(Name oldName, Name newName) throws NamingException;

Source Link

Document

Binds a new name to the object bound to an old name, and unbinds the old name.

Usage

From source file:org.wso2.carbon.user.core.ldap.ReadWriteLDAPUserStoreManager.java

protected void updateLDAPRoleName(RoleContext context, String newRoleName) throws UserStoreException {

    String roleName = context.getRoleName();
    String groupSearchFilter = ((LDAPRoleContext) context).getSearchFilter();
    String roleNameAttributeName = ((LDAPRoleContext) context).getRoleNameProperty();
    String searchBase = ((LDAPRoleContext) context).getSearchBase();

    DirContext mainContext = this.connectionSource.getContext();
    DirContext groupContext = null;
    NamingEnumeration<SearchResult> groupSearchResults = null;

    try {//from   w  w w .j  a  v a2s  .  c o  m

        groupSearchFilter = groupSearchFilter.replace("?", escapeSpecialCharactersForFilter(roleName));
        String[] returningAttributes = { roleNameAttributeName };
        groupSearchResults = searchInGroupBase(groupSearchFilter, returningAttributes,
                SearchControls.SUBTREE_SCOPE, mainContext, searchBase);
        SearchResult resultedGroup = null;
        while (groupSearchResults.hasMoreElements()) {
            resultedGroup = groupSearchResults.next();
        }

        if (resultedGroup == null) {
            throw new UserStoreException("Could not find user role " + roleName + " in LDAP server.");
        }

        String groupNameRDN = resultedGroup.getName();
        String newGroupNameRDN = roleNameAttributeName + "=" + newRoleName;

        groupContext = (DirContext) mainContext.lookup(groupSearchBase);
        groupContext.rename(groupNameRDN, newGroupNameRDN);

        String roleNameWithDomain = UserCoreUtil.addDomainToName(roleName, getMyDomainName());
        String newRoleNameWithDomain = UserCoreUtil.addDomainToName(newRoleName, getMyDomainName());
        this.userRealm.getAuthorizationManager().resetPermissionOnUpdateRole(roleNameWithDomain,
                newRoleNameWithDomain);
    } catch (NamingException e) {
        String errorMessage = "Error occurred while modifying the name of role: " + roleName;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeNamingEnumeration(groupSearchResults);
        JNDIUtil.closeContext(groupContext);
        JNDIUtil.closeContext(mainContext);
    }
}