summaryrefslogtreecommitdiff
path: root/src/main/java/org/cristalise/lookup/ldap/LDAPPathSet.java
blob: dc82591f6769e750b5ae0a84e54e38101ad5a1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.cristalise.lookup.ldap;

import java.util.Iterator;

import org.cristalise.kernel.lookup.Path;
import org.cristalise.kernel.utils.Logger;

import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchResults;

/**************************************************************************
 *
 * $Revision: 1.6 $
 * $Date: 2005/12/01 14:23:14 $
 *
 * Copyright (C) 2003 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/



public class LDAPPathSet implements Iterator<Path> {
    LDAPSearchResults results;
    LDAPEntry nextEntry;
    LDAPLookup ldap;

    public LDAPPathSet(LDAPLookup ldap) { // empty
    	this.ldap = ldap;
        results = null;
    } 

    public LDAPPathSet(LDAPSearchResults results, LDAPLookup ldap) {
    	this.ldap = ldap;
        this.results = results;
    }

    @Override
	public boolean hasNext() {
        if (results == null) return false;
        if (nextEntry != null) return true;
        if (results.hasMore())
        try {
            nextEntry = results.next();
            return true;
        } catch (LDAPException ex) {
        	if (ex.getResultCode()!=32) {// no results
        		Logger.error(ex);
        		Logger.error("Error loading LDAP result set: "+ex.getMessage());
        	}
        }
        return false;
    }

    @Override
	public Path next() {
        if (results == null) return null;
        try {
            if (nextEntry == null)
                nextEntry = results.next();
            Path nextPath = ldap.nodeToPath(nextEntry);
            nextEntry = null;
            return nextPath;
        } catch (Exception ex) {
            Logger.error("Error loading next path");
            Logger.error(ex);
            nextEntry = null;
            if (hasNext()) {
                Logger.error("Skipping to next entry");
                return next();
            }
            else
                return null;
        }
    }

	@Override
	public void remove() {
		// do nothing
		
	}
}