mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
28.30.2015 de77eadea038afda4df88c24d8c1a97b5d4b2a3f
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * CDDL HEADER START
 * 
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * CDDL HEADER END
 *
 * Copyright 2013 ForgeRock AS.
 * Portions Copyright 2013 IS4U.
 */
 
package org.forgerock.opendj.virtual;
 
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
 
import org.forgerock.opendj.ldap.AbstractSynchronousConnection;
import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.ConnectionEventListener;
import org.forgerock.opendj.ldap.Entry;
import org.forgerock.opendj.ldap.ErrorResultException;
import org.forgerock.opendj.ldap.ErrorResultIOException;
import org.forgerock.opendj.ldap.IntermediateResponseHandler;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.SearchResultHandler;
import org.forgerock.opendj.ldap.SearchResultReferenceIOException;
import org.forgerock.opendj.ldap.requests.AddRequest;
import org.forgerock.opendj.ldap.requests.BindRequest;
import org.forgerock.opendj.ldap.requests.CompareRequest;
import org.forgerock.opendj.ldap.requests.DeleteRequest;
import org.forgerock.opendj.ldap.requests.ExtendedRequest;
import org.forgerock.opendj.ldap.requests.ModifyDNRequest;
import org.forgerock.opendj.ldap.requests.ModifyRequest;
import org.forgerock.opendj.ldap.requests.Requests;
import org.forgerock.opendj.ldap.requests.SearchRequest;
import org.forgerock.opendj.ldap.requests.UnbindRequest;
import org.forgerock.opendj.ldap.responses.BindResult;
import org.forgerock.opendj.ldap.responses.CompareResult;
import org.forgerock.opendj.ldap.responses.ExtendedResult;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
 
/**
 * Join connection implementation.
 */
public class JoinConnection extends AbstractSynchronousConnection 
{
  private final LDAPConnectionFactory ldapFactory;
  private final JDBCConnectionFactory jdbcFactory;
  private final Connection ldapConnection;
  private final JDBCConnection jdbcConnection;
  private JDBCMapper jdbcMapper;
  private ConnectionEntryReader ldapEntries;
  private List<Entry> jdbcEntries;
 
  /**
   * Creates a new join connection.
   *
   * @param ldapfactory
   *            The LDAPConnectionFactory which provides connections to the
   *            Directory Server.
   * @param jdbcfactory
   *            The JDBCConnectionFactory which provides connections to the
   *            Database Server. 
   * @throws ErrorResultException
   *            If the connection request failed for some reason.
   * @throws SQLException
   *            If a database access error occurs.       
   * @throws IOException
   *            If an I/O exception error occurs.             
   */
  public JoinConnection(final LDAPConnectionFactory ldapfactory, final JDBCConnectionFactory jdbcfactory) throws ErrorResultException, SQLException, IOException
  {
    this.ldapFactory = ldapfactory;
    this.jdbcFactory = jdbcfactory;
    this.ldapConnection = ldapFactory.getConnection();
    this.jdbcConnection = (JDBCConnection) jdbcFactory.getConnection(); 
  }
 
  /**
   * Loads the mapping component for the JDBC connection.
   *
   * @param jdbcMapper
   *            The JDBCMapper object used to map the directory and database 
   *            structure.
   * @throws SQLException
   *            If the SQL query has an invalid format.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.  
   * @throws IOException
   *            If an I/O exception error occurs. 
   */
  public void loadMapper(final JDBCMapper jdbcMapper)
  {
    try
    {
      jdbcConnection.initializeMapper(jdbcMapper);
    }
    catch (SQLException e)
    {
      System.out.println(e.toString());
    }
    catch (ErrorResultException e)
    {
      System.out.println(e.toString());
    }
    catch (IOException e)
    {
      System.out.println(e.toString());
    }
  }
 
  /**
   * Authenticates to the Directory server and the Database Server using the provided bind requests.
   *       
   * @param ldapBindRequest
   *            The bind request for the Directory Server.
   * @param jdbcBindRequest
   *            The bind request for the Database Server.
   * @return The result of the operation.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.
   * @throws SQLException
   *            If the SQL query has an invalid format.
   * @throws IOException
   *            If an I/O exception error occurs. 
   */
  public BindResult bind(final BindRequest ldapBindRequest, final BindRequest jdbcBindRequest) throws ErrorResultException, SQLException, IOException
  {
    BindResult r = ldapConnection.bind(ldapBindRequest);
    if(r.isSuccess()) r = jdbcConnection.bind(jdbcBindRequest);
 
    jdbcMapper = new JDBCMapper(jdbcConnection, ldapConnection);
    jdbcMapper.setDatabaseName(jdbcFactory.getDatabaseName());
    jdbcConnection.initializeMapper(jdbcMapper);
    return r;
  }
 
  /**
   * Sends the provided add request to the back-ends of the subordinate connections.
   *       
   * @param request
   *            The add request.
   * @return The result of the operation.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.
   */
  @Override
  public Result add(AddRequest request) throws ErrorResultException
  {
    Result r = ldapConnection.add(request);
    if(r.isSuccess()) r = jdbcConnection.add(request);
    return r;
  }
 
  @Override
  public void addConnectionEventListener(ConnectionEventListener listener)
  {
    // TODO Auto-generated method stub
  }
 
  @Override
  public BindResult bind(BindRequest request) throws ErrorResultException
  {
    // TODO Auto-generated method stub
    return null;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public void close(UnbindRequest request, String reason)
  {
    if(ldapConnection != null) ldapConnection.close();
    if(jdbcConnection != null) jdbcConnection.close();
  }
 
  /**
   * Sends the provided compare request to the back-ends of the subordinate connections.
   *       
   * @param request
   *            The compare request.
   * @return The result of the operation.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.
   */
  @Override
  public CompareResult compare(CompareRequest request) throws ErrorResultException
  {
    CompareResult r = ldapConnection.compare(request);
    if(r.isSuccess()) r = jdbcConnection.compare(request);
    return r;
  }
 
  /**
   * Sends the provided delete request to the back-ends of the subordinate connections.
   *       
   * @param request
   *            The delete request.
   * @return The result of the operation.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.
   */
  @Override
  public Result delete(DeleteRequest request) throws ErrorResultException
  {
    Result r = ldapConnection.delete(request);
    if(r.isSuccess()) r = jdbcConnection.delete(request);
    return r;
  }
 
  @Override
  public <R extends ExtendedResult> R extendedRequest(ExtendedRequest<R> request, IntermediateResponseHandler handler) throws ErrorResultException
  {
    // TODO Auto-generated method stub
    return null;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isClosed()
  {
    if(ldapConnection.isClosed() && jdbcConnection.isClosed()) return true;
    else return false;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isValid()
  {
    if(ldapConnection.isValid() && jdbcConnection.isValid()) return true;
    else return false;
  }
 
  /**
   * Sends the provided modify request to the back-ends of the subordinate connections.
   *       
   * @param request
   *            The modify request.
   * @return The result of the operation.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.
   */
  @Override
  public Result modify(ModifyRequest request) throws ErrorResultException
  {
    Result r = ldapConnection.modify(request);
    if(r.isSuccess()) r = jdbcConnection.modify(request);
    return r;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public Result modifyDN(ModifyDNRequest request) throws ErrorResultException
  {
    Result r = ldapConnection.modifyDN(request);
    return r;
  }
 
  @Override
  public void removeConnectionEventListener(ConnectionEventListener listener)
  {
    // TODO Auto-generated method stub
  }
 
  /**
   * Returns a ConnectionEntryReader to iterate over the directory search results of the last search request.
   *       
   * @return The ConnectionEntryReader to iterate over the directory search results.
   */
  public ConnectionEntryReader getLDAPSearchEntries()
  {
    return ldapEntries;
  }
 
  /**
   * Returns a list containing the database search results of the last search request.
   *       
   * @return The list containing the database search results.
   * @throws SearchResultReferenceIOException
   *            If the iteration over the set of search results using a ConnectionEntryReader 
   *            encountered a SearchResultReference.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.
   */
  public List<Entry> getJDBCSearchEntries() throws SearchResultReferenceIOException, ErrorResultIOException
  {
    return jdbcEntries;
  }
 
  /**
   * Sends the provided search request to the back-ends of the subordinate connections and saves
   * the entry and record results.
   *       
   * @param request
   *            The search request.
   * @param handler
   *            A search result handler which can be used to asynchronously process the 
   *            search result entries and references as they are received, may be null.
   * @return The result of the operation.
   * @throws ErrorResultException
   *            If the result code indicates that the request failed for some
   *            reason.
   */
  @Override
  public Result search(SearchRequest request, SearchResultHandler handler)throws ErrorResultException
  {
    if(request.getAttributes().get(0).isEmpty()){
      request = Requests.newSearchRequest(request.getName(), request.getScope(), request.getFilter());
    }
    ldapEntries = ldapConnection.search(request);
    Result r = jdbcConnection.search(request, handler);
    if(r.isSuccess()) jdbcEntries = jdbcConnection.getSearchEntries();
    return r;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public String toString()
  {
    return ldapConnection.toString() + " " + jdbcConnection.toString();
  }
}