⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persistenceldap.java

📁 OPIAM stands for Open Identity and Access Management. This Suite will provide modules for user & rig
💻 JAVA
字号:
/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.persistence;

import opiam.admin.faare.MessageUtil;
import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.SearchResult;
import opiam.admin.faare.config.javabeans.JBClassDescriptor;
import opiam.admin.faare.config.javabeans.JBFieldDescriptor;
import opiam.admin.faare.config.javabeans.JBLdapConfig;
import opiam.admin.faare.config.javabeans.JBRessource;
import opiam.admin.faare.config.javabeans.JBSearchReferenceListDescriptor;
import opiam.admin.faare.exception.PersistenceException;
//DW/2620/BeginPatch
import opiam.admin.faare.persistence.javabeans.JBDynamicGroup;
//DW/2620/EndPatch
import opiam.admin.faare.persistence.javabeans.JBTop;
import opiam.admin.faare.persistence.javabeans.JBUser;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.javabeans.SearchArgument;
import opiam.admin.faare.service.services.acl.AclsService;
import opiam.admin.faare.service.services.references.ReferenceElement;
import opiam.admin.faare.service.services.references.ReferenceList;
import opiam.admin.faare.utils.StringUtil;

import netscape.ldap.LDAPAttribute;
import netscape.ldap.LDAPAttributeSet;
import netscape.ldap.LDAPConnection;
import netscape.ldap.LDAPDN;
import netscape.ldap.LDAPEntry;
import netscape.ldap.LDAPException;
import netscape.ldap.LDAPModification;
import netscape.ldap.LDAPModificationSet;
import netscape.ldap.LDAPReferralException;
//DW/2662/BeginPatch
import netscape.ldap.util.DN;
//DW/2662/EndPatch

//DW/2584/BeginPatch
import netscape.ldap.LDAPSearchConstraints;

//DW/2584/EndPatch
import netscape.ldap.LDAPSearchResults;

//DW/2584/BeginPatch
import netscape.ldap.LDAPSortKey;

//DW/2584/EndPatch
import netscape.ldap.LDAPUrl;

//DW/2584/BeginPatch
import netscape.ldap.controls.LDAPSortControl;

//DW/2584/EndPatch
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;

import org.apache.log4j.Logger;

import java.lang.reflect.Constructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;


/**
 * This class is the Persistence layer main class.
 */
public final class PersistenceLDAP
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(PersistenceLDAP.class);

    /** Default selected attributes in search operations. */
    private static String[] _attrsSelected = {"*", "modifytimestamp"};

    /** Selected attributes in search operations, without modifytimestamp. */
    private static String[] _attrsWithoutModifyTimestamp = {"*"};

    /** Modifytimestamp as selected attributes in search operations. */
    private static String[] _attrsModifyTimestamp = {"modifytimestamp"};

    /** Object class attribute. */
    private static String _objectclassLdap = "objectclass";

    /** Dname field in mapped attributes. */
    private static String _dnLdap = "dn";

    /**
     * Utility class.
     */
    private PersistenceLDAP()
    {
    }

    /**
     * Checks whether the entry is in the resource given by the name and
     * in the current profile in the userContext.
     *
     * @param jbTop Entry to be checked.
     * @param resourceName Resource name.
     * @param userContext Current user context.
     *
     * @return true if it is, false if it is not.
     */
    public static boolean isJbTopInResource(JBTop jbTop, String resourceName,
        UserContext userContext)
    {
        JBRessource jbRessource = userContext.findJBRessourceByName(resourceName);

        if (jbRessource == null)
        {
            _logger.info(resourceName + " not found in isJbTopInResource");

            return false;
        }

        String filter = jbRessource.getLdapFilterParam();

        if (filter == null)
        {
            // pas de filtrage supplementaire, depend uniquement du type
            return (jbRessource.getType().equals(jbTop.getClass().getName()));
        }
        //DW/2674//BeginPatch
        if (!(jbRessource.getType().equals(jbTop.getClass().getName())))
        {
        	return false;
        }
        //DW/2674//EndPatch

        try
        {
            LDAPConnection ld = userContext.getLd();

            String[] attrs = {"dn"};
            String base = jbTop.getDn();
            LDAPSearchResults res = ld.search(base, LDAPConnection.SCOPE_BASE,
                    filter, attrs, false);

            //On parcourt les r閟ultats
            if (res.hasMoreElements())
            {
                return true;
            }
        }
        catch (LDAPException le)
        {
            _logger.error(MessageUtil.formatMessage("MSG_LDAP_ERROR",
                    LdapUtil.getLDAPErrorMessage(le)));
        }

        return false;
    }

    /**
     * Gets search reference list, searching corresponding entries.
     *
     * @param desc Reference list decsriptor.
     *
     * @return Generated search reference list.
     * @throws PersistenceException if a LDAP error occurs.
     */
    public static ReferenceList getReferenceList(
        JBSearchReferenceListDescriptor desc) throws PersistenceException
    {
        ReferenceList result = new ReferenceList(desc);

        try
        {
            UserContext userContext = new UserContext();
            userContext.setLdapConfig(PropertiesManager.getInstance()
                                                       .getPersistenceDescriptionMap()
                                                       .getLdapConfig());

            LDAPConnection ld = LdapUtil.getAppliConnection(userContext);

            LDAPSearchResults res = ld.search(desc.getDn(),
                    LDAPConnection.SCOPE_SUB, desc.getFilter(),
                    desc.getAttributes(), false);

            //On parcourt les r閟ultats
            Object o = null;
            LDAPAttribute ldapAttr = null;
            LDAPEntry entry = null;
            String label;
            String value;
            String attrType;

            while (res.hasMoreElements())
            {
                o = res.next();

                if (o instanceof LDAPEntry)
                {
                    entry = (LDAPEntry) o;

                    // traitement de LABEL
                    attrType = desc.getLabelattr();

                    if (attrType.compareToIgnoreCase("dn") == 0)
                    {
                        label = entry.getDN();
                    }
                    else
                    {
                        ldapAttr = entry.getAttribute(attrType);

                        if (ldapAttr == null)
                        {
                            _logger.info(
                                "error getReferenceList labelattr not exists");

                            continue;
                        }
                        else
                        {
                            label = ldapAttr.getStringValueArray()[0];
                        }
                    }

                    // traitement de VALUE
                    attrType = desc.getValueattr();

                    if (attrType.compareToIgnoreCase("dn") == 0)
                    {
                        value = entry.getDN();
                    }
                    else
                    {
                        ldapAttr = entry.getAttribute(attrType);

                        if (ldapAttr == null)
                        {
                            _logger.info(
                                "error getReferenceList Valueattr not exists");

                            continue;
                        }
                        else
                        {
                            value = ldapAttr.getStringValueArray()[0];
                        }
                    }

                    // creation de ref element
                    ReferenceElement elt = new ReferenceElement(label, value);
                    result.addReference(elt);
                }
            }

            if (desc.isToSort())
            {
                // trier par le label
                result.sortReference();
            }
        }
        catch (LDAPException le)
        {
            _logger.error(MessageUtil.formatMessage("MSG_LDAP_ERROR",
                    LdapUtil.getLDAPErrorMessage(le)));
            throw new PersistenceException(MessageUtil.formatMessage(
                    "MSG_LDAP_ERROR", LdapUtil.getLDAPErrorMessage(le)));
        }

        return result;
    }

    /**
     * Loads a JBTop object from its dname.
     *
     * @param dn Dname of the object.
     * @param userContext Current user context.
     * @return JBTop Loaded object
     * @throws PersistenceException if an error occurs.
     */
    public static JBTop load(String dn, UserContext userContext)
        throws PersistenceException
    {
        _logger.debug("-----------LOAD1 BY DN DEBUT--------------");

        JBTop jbTop = load(dn, true, userContext);

        return jbTop;
    }

    //-- load

    /**
     * Loads a JBTop object from its dname.
     *
     * @param dn Dname of the object.
     * @param checkDateModify If true, load from directory only if modified since last loading.
     * @param userContext Current user context.
     * @return JBTop Loaded object
     * @throws PersistenceException if an error occurs.
     */
    public static JBTop load(String dn, boolean checkDateModify,
        UserContext userContext) throws PersistenceException
    {
        _logger.debug("-----------LOAD1 DEBUT--------------");

        if (dn == null)
        {
            throw new PersistenceException(MessageUtil.formatMessage(
                    "MSG_ARGUMENT_NULL"));
        }

        if (userContext == null)
        {
            throw new PersistenceException(MessageUtil.formatMessage(
                    "MSG_ARGUMENT_NULL"));
        }

        JBTop jbTop = null;

        try
        {
            Map hCache = userContext.getCache();
            LDAPConnection ld = userContext.getLd();

            if (hCache == null)
            {
                throw new PersistenceException(MessageUtil.formatMessage(
                        "MSG_VARIABLE_NULL", "hCache", "load", "PersistenceLDAP"));
            }

            //1er cas: on ne prend pas en compte la derni鑢e date de modification
            // de l'entr閑 LDAP
            if (!checkDateModify)
            {
                if (getFromCache(dn, hCache) != null)
                {
                    //Un object avec le m阭e DN se trouve dans le cache, on le retourne
                    jbTop = (JBTop) getFromCache(dn, hCache);
                }
                else
                {
                    //Object non trouv

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -