Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one
   *  or more contributor license agreements.  See the NOTICE file
   *  distributed with this work for additional information
   *  regarding copyright ownership.  The ASF licenses this file
   *  to you under the Apache License, Version 2.0 (the
   *  "License"); you may not use this file except in compliance
   *  with the License.  You may obtain a copy of the License at
   *
  *    http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing,
  *  software distributed under the License is distributed on an
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  *  KIND, either express or implied.  See the License for the
  *  specific language governing permissions and limitations
  *  under the License.
  *
  */
 package org.apache.directory.studio.connection.core.io.jndi;
 
 
 import java.util.List;
 
 
 import  org.apache.directory.api.ldap.model.exception.LdapURLEncodingException;
 import  org.apache.directory.api.ldap.model.message.Referral;
 import  org.apache.directory.api.ldap.model.url.LdapUrl;
 import  org.apache.directory.studio.common.core.jobs.StudioProgressMonitor;
A naming enumeration that handles referrals itself.

Author(s):
Apache Directory Project
 
 {
     private final LdapContext ctx;
     private NamingException initialReferralException;


    
Creates a new instance of ReferralNamingEnumeration.

Parameters:
connection the connection
ctx the JNDI context
searchBase the search base
filter the filter
searchControls the search controls
aliasesDereferencingMethod the aliases dereferencing method
referralsHandlingMethod the referrals handling method
controls the LDAP controls
monitor the progress monitor
referralsInfo the referrals info
 
     public JndiStudioNamingEnumerationConnection connectionLdapContext ctx,
         NamingEnumeration<SearchResultdelegate,
         NamingException initialReferralExceptionString searchBaseString filterSearchControls searchControls,
         AliasDereferencingMethod aliasesDereferencingMethodReferralHandlingMethod referralsHandlingMethod,
         Control[] controlslong requestNum, StudioProgressMonitor monitorReferralsInfo referralsInfo )
     {
         superconnectionsearchBasefiltersearchControlsaliasesDereferencingMethodreferralsHandlingMethod,
             controlsrequestNummonitorreferralsInfo );
         this. = ctx;
         this. = delegate;
         this. = delegate;
         this. = initialReferralException;
         this. = requestNum;
     }


    

See also:
javax.naming.NamingEnumeration.close()
 
     public void close() throws NamingException
     {
         .close();
     }


    

See also:
javax.naming.NamingEnumeration.hasMore()
    public boolean hasMore() throws NamingException
    {
        NamingException logResultDoneException = null;
        boolean done = false;
        while ( true )
        {
            try
            {
                if (  != null )
                {
                    NamingException referralException = ;
                     = null;
                    throw referralException;
                }
                boolean hasMore =  != null && .hasMore();
                if ( !hasMore && !done &&  != null && .hasMoreReferrals() )
                {
                    done = checkReferral();
                }
                else
                {
                    done = !hasMore;
                    return hasMore;
                }
            }
            catch ( PartialResultException pre )
            {
                done = true;
                logResultDoneException = pre;
                // ignore exception if referrals handling method is IGNORE
                // report exception if referrals handling method is FOLLOW or MANGAGE
                if (  == . )
                {
                    return false;
                }
                else
                {
                    throw pre;
                }
            }
            catch ( ReferralException re )
            {
                done = true;
                logResultDoneException = re;
                 = JNDIConnectionWrapper.handleReferralExceptionre );
                if ( .hasMoreReferrals() )
                {
                    logResultDoneException = null;
                    done = checkReferral();
                }
            }
            catch ( NamingException ne )
            {
                done = true;
                logResultDoneException = ne;
                throw ne;
            }
            finally
            {
                if ( done )
                {
                    for ( IJndiLogger logger : ConnectionCorePlugin.getDefault().getJndiLoggers() )
                    {
                        logger.logSearchResultDonelogResultDoneException );
                    }
                }
            }
        }
    }


    

See also:
java.util.Enumeration.hasMoreElements()
    public boolean hasMoreElements()
    {
        throw new UnsupportedOperationException"Call hasMore() instead of hasMoreElements() !" ); //$NON-NLS-1$
    }


    

See also:
javax.naming.NamingEnumeration.next()
    public StudioSearchResult next() throws NamingException
    {
        StudioSearchResult studioSearchResult = null;
        NamingException namingException = null;
        try
        {
            SearchResult searchResult = .next();
            ++;
            if ( searchResult instanceof StudioSearchResult )
            {
                studioSearchResult = ( StudioSearchResult ) searchResult;
            }
            else
            {
                studioSearchResult = new StudioSearchResultsearchResultgetConnection(),  != nullnull );
            }
            return studioSearchResult;
        }
        catch ( NamingException ne )
        {
            namingException = ne;
            throw ne;
        }
        finally
        {
            if (  ==  )
            {
                for ( IJndiLogger logger : ConnectionCorePlugin.getDefault().getJndiLoggers() )
                {
                    logger.logSearchResultEntrystudioSearchResultnamingException );
                }
            }
        }
    }


    

See also:
java.util.Enumeration.nextElement()
    {
        throw new UnsupportedOperationException"Call next() instead of nextElement() !" ); //$NON-NLS-1$
    }


    
Gets the connection.

Returns:
the connection
    public Connection getConnection()
    {
        if (  instanceof JndiStudioNamingEnumeration )
        {
            return ( ( JndiStudioNamingEnumeration )  ).getConnection();
        }
        else
        {
            return ;
        }
    }


    
Gets the response controls.

Returns:
the response controls, may be null
Throws:
NamingException the naming exception
    public Control[] getResponseControls() throws NamingException
    {
        return  != null ? .getResponseControls() : null;
    }
    private boolean checkReferral()
    {
        try
        {
            boolean done = false;
            // ignore exception if referrals handling method is IGNORE
            // follow referral if referrals handling method is FOLLOW
            // follow manually if referrals handling method is FOLLOW_MANUALLY
            if (  == . )
            {
                done = true;
                 = null;
            }
            else if (  == . )
            {
                 = new NamingEnumeration<SearchResult>()
                {
                    List<Stringurls = new ArrayList<String>();
                    {
                        while ( .hasMoreReferrals() )
                        {
                            Referral referral = .getNextReferral();
                            for ( IJndiLogger logger : ConnectionCorePlugin.getDefault().getJndiLoggers() )
                            {
                                logger.logSearchResultReferencereferralnull );
                            }
                            .addAllreferral.getLdapUrls() );
                        }
                    }
                    public SearchResult nextElement()
                    {
                        throw new UnsupportedOperationException"Call next() instead of nextElement() !" ); //$NON-NLS-1$
                    }
                    public boolean hasMoreElements()
                    {
                        throw new UnsupportedOperationException"Call hasMore() instead of hasMoreElements() !" ); //$NON-NLS-1$
                    }
                    public SearchResult next() throws NamingException
                    {
                        try
                        {
                            LdapUrl url = new LdapUrl( .remove( 0 ) );
                            SearchResult searchResult = new SearchResulturl.getDn().getName(), null,
                                new BasicAttributes(),
                                false );
                            searchResult.setNameInNamespaceurl.getDn().getName() );
                            StudioSearchResult ssr = new StudioSearchResultsearchResultnullfalseurl );
                            return ssr;
                        }
                        catch ( LdapURLEncodingException e )
                        {
                            throw new NamingExceptione.getMessage() );
                        }
                    }
                    public boolean hasMore() throws NamingException
                    {
                        return !.isEmpty();
                    }
                    public void close() throws NamingException
                    {
                        .clear();
                         = null;
                    }
                };
            }
            else if (  == . )
            {
                Referral referral = .getNextReferral();
                for ( IJndiLogger logger : ConnectionCorePlugin.getDefault().getJndiLoggers() )
                {
                    logger.logSearchResultReferencereferralnull );
                }
                List<Stringurls = new ArrayList<String>( referral.getLdapUrls() );
                LdapUrl url = new LdapUrl( urls.get( 0 ) );
                Connection referralConnection = ConnectionWrapperUtils.getReferralConnectionreferralthis );
                if ( referralConnection != null )
                {
                    done = false;
                    String referralSearchBase = url.getDn() != null && !url.getDn().isEmpty() ? url.getDn().getName()
                        : ;
                    String referralFilter = url.getFilter() != null && url.getFilter().length() == 0 ? url.getFilter()
                        : ;
                    SearchControls referralSearchControls = new SearchControls();
                    referralSearchControls.setSearchScopeurl.getScope().getScope() > -1 ? url.getScope().getScope()
                        : 
                            .getSearchScope() );
                    referralSearchControls.setReturningAttributesurl.getAttributes() != null
                        && url.getAttributes().size() > 0 ? url.getAttributes().toArray(
                        new String[url.getAttributes().size()] ) : .getReturningAttributes() );
                    referralSearchControls.setCountLimit.getCountLimit() );
                    referralSearchControls.setTimeLimit.getTimeLimit() );
                    referralSearchControls.setDerefLinkFlag.getDerefLinkFlag() );
                    referralSearchControls.setReturningObjFlag.getReturningObjFlag() );
                     = referralConnection.getConnectionWrapper().searchreferralSearchBasereferralFilter,
                        referralSearchControls,
                         );
                }
                else
                {
                    done = true;
                     = null;
                }
            }
            return done;
        }
        catch ( LdapURLEncodingException e )
        {
            return false;
        }
    }
New to GrepCode? Check out our FAQ X