ldapservice.java

来自「jetspeed源代码」· Java 代码 · 共 1,485 行 · 第 1/3 页

JAVA
1,485
字号
     * @param url object to modify.
     * @param mods Modification items.
     * @exception NamingException
     * @return boolean true if success else false.
     */
    private boolean modifyAttribute(LDAPURL url, ModificationItem mods[])
        throws NamingException
    {
        DirContext ctx = connect(url);
        if(ctx == null) return false;

        try
        {
            ctx.modifyAttributes(url.getDN(), mods);
            checkAndCloseContext(ctx);
        }
        catch(ReferralException e)
        {
            LDAPURL myurl = getReferralUrl(e);
            return modifyAttribute(myurl, mods);
        }
        return true;
    }

    /**
     * Build LDAPURL Function
     *
     * Build <code>LDAPURL</code> with given DN.
     *
     * @param DN DN value for object.
     * @return LDAPURL build with given DN.
     */
    public LDAPURL buildURL(String DN)
    {
      return new LDAPURL(host,port,DN + "," + basedn);
    }

    /**
     * Read Attributes Function
     *
     * Return attributes for given <code>LDAPURL</code>.
     *
     * @param url object to read attributes.
     * @return Attributes attributes for given url.
     */
    public Attributes read(LDAPURL url)
    {
        DirContext ctx = connect(url);
        if(ctx == null) return null;
        
        Attributes attrs = null;
        try
        {
            if(showOpAttributes)
            {
                attrs = ctx.getAttributes(url.getDN(), attributesList);
            }
            else
            {
                attrs = ctx.getAttributes(url.getDN());
            }
            checkAndCloseContext(ctx);
        }
        catch(ReferralException e)
        {
            LDAPURL myurl = getReferralUrl(e);
            if(myurl.getDN().length() == 0)
            {
                myurl.setDN(url.getDN());
            }
            return read(myurl);
        }
        catch(CommunicationException e)
        {
            if(connector == null)
            {
                logger.debug("LDAP Service: Communication error : " + url.getBase(), e);
                return null;
            }
            if(connector.connectionFailed(url))
            {
                resetConnection(url);
			}
        }
        catch(NamingException e)
        {
            logger.debug("LDAP Service: Failed to read entry " + url.getDN(), e);
            return null;
        }
        return attrs;
    }

    /**
     * Rename Entry Function
     *
     * Rename given <code>LDAPURL</code> with given DN.
     *
     * @param url object to modify.
     * @param newDN DN value for new object.
     * @return boolean true if success else false.
     */
    public boolean renameEntry(LDAPURL url, String newDN)
    {
        DirContext ctx = connect(url);
        if(ctx == null) return false;

        try
        {
            ctx.rename(url.getDN(), newDN);
            checkAndCloseContext(ctx);
        }
        catch(ReferralException e)
        {
            logger.debug("LDAP Service: Failed to rename entry. (not supported for referrals)", e);
            return false;
        }
        catch(NamingException e)
        {
            logger.debug("LDAP Service: Failed to rename entry " + url.getDN(), e);
            return false;
        }
        return true;
    }

    /**
     * Sync Entry Function
     *
     * Sync given <code>LDAPURL</code> with given atrributes.
     *
     * @param url object to sync.
     * @param ats Modification items.
     * @return boolean true if success else false.
     */
    public boolean synchEntry(LDAPURL url, Attributes ats)
    {
        DirContext ctx = connect(url);
        if(ctx == null) return false;

        try
        {
            ctx.modifyAttributes(url.getDN(), 2, ats);
            checkAndCloseContext(ctx);
        }
        catch(ReferralException e)
        {
            LDAPURL myurl = getReferralUrl(e);
            return synchEntry(url, ats);
        }
        catch(NameNotFoundException _ex)
        {
            try
            {
                ctx.createSubcontext(url.getDN(), ats);
            }
            catch(NamingException _ex2)
            {
                return false;
            }
        }
        catch(NamingException e)
        {
            logger.debug("LDAP Service: Failed to synchronize entries", e);
            return false;
        }
        return true;
    }

    /**
     * Delete Attributes Function
     *
     * Delete Attributes for given <code>LDAPURL</code>.
     *
     * @param url object to modify.
     * @param ats Attributes to delete.
     * @return boolean true if success else false.
     */
    public boolean deleteAttrs(LDAPURL url, Attributes ats)
    {
        DirContext ctx = connect(url);
        if(ctx == null) return false;

        try
        {
            ctx.modifyAttributes(url.getDN(), DirContext.REMOVE_ATTRIBUTE, ats);
            checkAndCloseContext(ctx);
        }
        catch(ReferralException e)
        {
            LDAPURL myurl = getReferralUrl(e);
            return synchEntry(url, ats);
        }
        catch(NameNotFoundException _ex)
        {
            try
            {
                ctx.createSubcontext(url.getDN(), ats);
	            checkAndCloseContext(ctx);
            }
            catch(NamingException _ex2)
            {
                return false;
            }
        }
        catch(NamingException e)
        {
            logger.debug("LDAP Service: Failed to delete Attributes", e);
            return false;
        }
        return true;
    }

    /**
     * Delete Entry Function
     *
     * Delete given <code>LDAPURL</code>.
     *
     * @param url object to delete.
     * @return boolean true if success else false.
     */
    public boolean deleteEntry(LDAPURL url)
    {
        DirContext ctx = connect(url);
        if(ctx == null) return false;

        try
        {
            ctx.destroySubcontext(url.getDN());
            checkAndCloseContext(ctx);
        }
        catch(ReferralException e)
        {
            LDAPURL myurl = getReferralUrl(e);
            return deleteEntry(myurl);
        }
        catch(NamingException e)
        {
            logger.debug("LDAP Service: Failed to delete entry " + url.getDN(), e);
            return false;
        }
        return true;
    }

    /**
     * Find Entry Name Function
     *
     * Return entry name for given <code>LDAPURL</code>.
     *
     * @param url object to modify.
     * @return LDAPURL real entry DN.
     */
    public LDAPURL findEntryName(LDAPURL url)
    {
        DirContext ctx = connect(url);
        if(ctx == null) return null;
        
        Name name = parse(url.getDN());
        String base = name.getPrefix(name.size() - 1).toString();
        String dn = url.getDN();
        String rdn = name.get(name.size() - 1).toString();
        int i = 1;
        boolean foundName = true;

        while(foundName)
        {
            try
            {
                NamingEnumeration results = search(ctx, dn, "(objectclass=*)", DEFAULT_ATTR, 0, false);
                if(i == 1)
                    rdn = rdn + " copy";
                else
                if(i == 2)
                    rdn = rdn + " " + i;
                else
                if(i >= 3)
                    rdn = rdn.substring(0, rdn.length() - 1) + i;
                dn = rdn + ", " + base;
                i++;
            }
            catch(NameNotFoundException _ex)
            {
                foundName = false;
                return new LDAPURL(url.getHost(), url.getPort(), dn);
            }
            catch(NamingException _ex)
            {
                return null;
            }
        }
        
        checkAndCloseContext(ctx);

        return null;
    }
    
    /**
     * Delete Tree Function
     *
     * Delete record with all child node <code>LDAPURL</code>.
     *
     * @param url object to modify.
     * @return boolean true if success else false.
     */
    public boolean deleteTree(LDAPURL url)
    {
        DirContext ctx = connect(url);
        if(ctx == null) return false;

        String entryDN = null;
        LDAPURL myurl = null;
        String baseDN = url.getDN();

        try
        {
            for(NamingEnumeration results = search(ctx, baseDN, "(objectclass=*)", DEFAULT_ATTR, 1, false); results.hasMore();)
            {
                SearchResult si = (SearchResult)results.next();
                entryDN = getFixedDN(si.getName(), baseDN);
                myurl = new LDAPURL(url.getHost(), url.getPort(), entryDN);
                if(!deleteTree(myurl))
                {
                    return false;
                }
            }

			checkAndCloseContext(ctx);
        }
        catch(NamingException e)
        {
            logger.debug("LDAP Service: Delete tree failed", e);
            return false;
        }
        return deleteEntry(url);
    }

    /**
     * Transfer Function
     *
     * Transfer given <code>LDAPURL</code> to other <code>LDAPURL</code>.
     *
     * @param fromUrl object to transfer.
     * @param toUrl target object.
     * @param delete delete after transfer.
     * @param replace replace if exist.
     * @param withChildren transfer with childs.
     * @return boolean true if success else false.
     */
    public boolean transfer(LDAPURL fromUrl, LDAPURL toUrl, boolean delete, boolean replace, boolean withChildren)
    {
        LDAPURL dstUrl = toUrl;
        int rc = compare(fromUrl, toUrl);
        if(rc == 1)
            dstUrl = findEntryName(dstUrl);
        if(withChildren)
            return transferTreeSub(fromUrl, dstUrl, delete, replace);
        else
            return transferEntry(fromUrl, dstUrl, delete, replace);

    }

    /**
     * Transfer with updates Function
     *
     * Transfer updated <code>LDAPURL</code> with given modification items
     * to other <code>LDAPURL</code>.
     *
     * @param fromUrl object to transfer.
     * @param toUrl target object.
     * @param delete delete after transfer.
     * @param replace replace if exist.
     * @param ats attributes to update.
     * @return boolean true if success else false.
     */
    public boolean transferEntry(LDAPURL fromUrl, Attributes ats, LDAPURL toUrl, boolean delete, boolean replace)
    {
        if(delete && !deleteEntry(fromUrl))
            return false;
        if(updateEntry(toUrl, ats, replace))
            return true;
        if(delete)
            addEntry(fromUrl, ats);
        return false;
    }

    /**
     * Transfer without updates Function
     *
     * Transfer <code>LDAPURL</code> to other <code>LDAPURL</code>.
     *
     * @param fromUrl object to transfer.
     * @param toUrl target object.
     * @param delete delete after transfer.
     * @param replace replace if exist.
     * @return boolean true if success else false.
     */

    public boolean transferEntry(LDAPURL fromUrl, LDAPURL toUrl, boolean delete, boolean replace)
    {
        Attributes ats = read(fromUrl);
        if(ats == null)
            return false;
        else
            return transferEntry(fromUrl, ats, toUrl, delete, replace);
    }

    /**
     * Transfer Tree Function
     *
     * Transfer <code>LDAPURL</code> with all child to other <code>LDAPURL</code>.
     *
     * @param fromUrl object to transfer.
     * @param toUrl target object.
     * @param delete delete after transfer.
     * @param replace replace if exist.
     * @return boolean true if success else false.
     */
    private boolean transferTreeSub(LDAPURL fromUrl, LDAPURL toUrl, boolean delete, boolean replace)
    {
        DirContext ctx = connect(fromUrl);
        if(ctx == null) return false;

        Attributes ats = read(fromUrl);
        if(ats == null) return false;
        
        String srcDN = fromUrl.getDN();
        String dstDN = toUrl.getDN();
        boolean createdBase = false;
        boolean rc = false;
        boolean moreReferrals = true;

        while(moreReferrals)
        {
            try
            {
                NamingEnumeration results = search(ctx, srcDN, "(objectclass=*)", DEFAULT_ATTR, 1, false);
                if(!results.hasMore())
                {
                    if(!transferEntry(fromUrl, ats, toUrl, delete, replace))
                        return false;
                } else
                {
                    String name = null;
                    if(!createdBase)
                    {
                        if(!updateEntry(toUrl, ats, replace))
                            return false;
                        createdBase = true;
                    }
                    LDAPURL srcUrl;
                    LDAPURL dstUrl;
                    for(; results.hasMore(); transferTreeSub(srcUrl, dstUrl, delete, replace))
                    {
                        SearchResult si = (SearchResult)results.next();
                        name = fixName(si.getName());
                        String tmpSrcDN = getDN(name, srcDN);
                        srcUrl = new LDAPURL(fromUrl.getHost(), fromUrl.getPort(), tmpSrcDN);
                        String tmpDstDN = getDN(name, dstDN);
                        dstUrl = new LDAPURL(toUrl.getHost(), toUrl.getPort(), tmpDstDN);
                    }

                    if(delete && !deleteEntry(fromUrl))
                        return false;
                }
                moreReferrals = false;
            }
            catch(ReferralException e)
            {
                if(delete)
                {
                    moreReferrals = false;
                }
               	else
                {
                    if(!createdBase)
                    {
                        if(!updateEntry(toUrl, ats, replace)) return false;
                        createdBase = true;
                    }

                    LDAPURL srcUrl = getReferralUrl(e);
                    String tmpDstDN = getName(srcUrl.getDN()) + ", " + dstDN;
                    LDAPURL dstUrl = new LDAPURL(toUrl.getHost(), toUrl.getPort(), tmpDstDN);
                    boolean rs = transferTreeSub(srcUrl, dstUrl, delete, replace);
                    if(!rs)return false;

                    moreReferrals = e.skipReferral();
                    try
                    {
                    	// Close old context
                    	checkAndCloseContext(ctx);
                        ctx = (DirContext)e.getReferralContext();
                    }
                    catch(NamingException _ex) { }
                }
            }
            catch(NamingException e)

⌨️ 快捷键说明

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