summaryrefslogtreecommitdiff
path: root/src/connectors/src/data/io/ldap/LDAPFlatDataReader.java
blob: dad75d1a6cd5ab3ef13ea317a3c62b3bb2fec78b (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes
 * Copyright (C) 2014  Ludovic Pouzenc <ludovic@pouzenc.fr>
 *  
 * This file is part of SSSync.
 *
 *  SSSync is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SSSync is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SSSync.  If not, see <http://www.gnu.org/licenses/>
 */

package data.io.ldap;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import com.unboundid.ldap.sdk.Attribute;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchResultListener;
import com.unboundid.ldap.sdk.SearchResultReference;
import com.unboundid.ldap.sdk.SearchScope;

import data.MVDataEntry;
import data.io.AbstractMVDataReader;

/**
 * Stream-oriented reader from a particular LDAP connection
 * Always returns lines/items sorted by lexicographical ascending key
 * Consistent even if there is a Writer on same LDAP connection (useful for sync)
 * 
 * @author lpouzenc
 */
public class LDAPFlatDataReader extends AbstractMVDataReader {

	private final LDAPConnection conn;
	private final String baseDN;
	private final String keyAttr;
	private final int lookAheadAmount;
	private final SortedSet<String> keys; //FIXME : TreeSet is sexy but costly (memory). Use ArrayList<String> or String[] and Collections.sort()
	
	private transient Iterator<String> keysItCached;
	private transient Iterator<String> keysItConsumed;
	private transient SortedMap<String, MVDataEntry> entries; //TODO : memory ineffcient
	// Could help ? http://trove4j.sourceforge.net/javadocs/gnu/trove/map/package-summary.html
	
	// Listener to feed LDAP search result in SortedMap without instantiating a big fat SearchResult
	private final SearchResultListener keysReqListener = new SearchResultListener() {
		private static final long serialVersionUID = 3364745402521913458L;

		@Override
		public void searchEntryReturned(SearchResultEntry searchEntry) {
			keys.add(searchEntry.getAttributeValue(keyAttr));
		}

		@Override
		public void searchReferenceReturned(SearchResultReference searchReference) {
			throw new RuntimeException("Unsupported : search request for all '" + keyAttr + "' has returned at least one reference (excepected : an entry)");
		}
	};

	/**
	 * Construct a new reader that wrap a particular LDAP search on a given connection
	 * @param dataSourceName Short name of this data source (for logging)
	 * @param conn Already initialized LDAP connection where run the search 
	 * @param baseDN Search base DN (will return childs of this DN)
	 * @param keyAttr Attribute name that is the primary key of the entry, identifying the entry in a unique manner
	 * @param lookAheadAmount Grab this amount of entries at once (in memory-sorted, 128 could be great)
	 * @throws LDAPException
	 */
	public LDAPFlatDataReader(String dataSourceName, LDAPConnection conn, String baseDN, String keyAttr, int lookAheadAmount) throws LDAPException {
		this.dataSourceName = dataSourceName;
		this.conn = conn;
		this.baseDN = baseDN;
		this.keyAttr = keyAttr;
		this.lookAheadAmount = lookAheadAmount;
		
		// Grab all the entries' keys from LDAP connection and put them in this.keys
		this.keys = new TreeSet<String>();
		SearchRequest keysReq = new SearchRequest(keysReqListener, baseDN, SearchScope.ONE, Filter.create("(objectClass=*)"), keyAttr);
		conn.search(keysReq);
		
	}

	/**
	 * {@inheritDoc}
	 * Note : multiple iterators on the same instance are not supported
	 */
	@Override
	public Iterator<MVDataEntry> iterator() {
		// Reset the search (it uses two different iterators on the same set)
		keysItCached = keys.iterator();
		keysItConsumed = keys.iterator();
		entries = new TreeMap<String, MVDataEntry>();

		return this;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean hasNext() {
		return (keysItConsumed==null)?false:keysItConsumed.hasNext();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public MVDataEntry next() {
		String wantedKey = keysItConsumed.next();
		
		// Feed the lookAhead buffer if it is empty (and there is more elements to grab)
		if ( entries.isEmpty() && keysItCached.hasNext()  ) {
			lookAhead(lookAheadAmount);
		}
		
		//FIXME : it is possible to have inconsistency between "entries" content and keysIt* values if some entry is deleted since we have read all the keys
		
		// Pop an entry from the lookAhead buffer
		MVDataEntry wantedEntry = entries.remove(wantedKey);
		if ( wantedEntry == null ) {
			throw new NoSuchElementException();
		}
		
		return wantedEntry;
	}

	/**
	 * Performs look-ahead of amount entries, using the next sorted keys previously queried.
	 * @param amount
	 */
	private void lookAhead(int amount) {
		if ( amount < 1 ) {
			throw new IllegalArgumentException("LookAhead amount has to be >= 1");
		}
		try {
			// Build a search that matches "amount" next entries
			Filter filter = Filter.createEqualityFilter(keyAttr, keysItCached.next());
			for (int i=0; ( i < amount-1 ) && keysItCached.hasNext(); i++) {
				filter = Filter.createORFilter(filter, Filter.createEqualityFilter(keyAttr, keysItCached.next()));
			}
			SearchRequest searchRequest = new SearchRequest(baseDN, SearchScope.ONE, filter, "*");
			
			// XXX Could use a second listener, as for the keys
			// Get all this entries in memory, convert them in MVDataEntry beans and store them in a SortedMap
			SearchResult search = conn.search(searchRequest);
			
			for (SearchResultEntry ldapEntry: search.getSearchEntries()) {
				String key = ldapEntry.getAttributeValue(keyAttr);
				MVDataEntry mvEntry = new MVDataEntry(key);
				
				for ( Attribute attr : ldapEntry.getAttributes() ) {
					mvEntry.put(attr.getName(), attr.getValues());
				}
				entries.put(key, mvEntry);
			}
		} catch (LDAPException e) {
			throw new RuntimeException(e);
		}
	}
}