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

Glenn Van Lint
24.36.2013 ef09aa34d156bcbc9e82f1f8f32f4a88c445aa85
Correction of header copyright.
Applied correct formatting style.
Removed hard-coded values: "opendj_db" in fillTablesList() and "dc=example,dc=com" in search requests to work dynamically.
1 files modified
121 ■■■■■ changed files
opendj3/opendj-virtual/src/main/java/org/forgerock/opendj/virtual/JDBCMapper.java 121 ●●●●● patch | view | raw | blame | history
opendj3/opendj-virtual/src/main/java/org/forgerock/opendj/virtual/JDBCMapper.java
@@ -1,32 +1,27 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 * 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-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under 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 Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 * 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 2010 Sun Microsystems, Inc.
 *      Portions copyright 2011 ForgeRock AS
 * Copyright 2013 ForgeRock AS.
 * Portions Copyright 2013 IS4U.
 */
package org.forgerock.opendj.virtual;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@@ -44,16 +39,19 @@
import org.forgerock.opendj.ldap.SearchScope;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
import org.forgerock.opendj.ldif.EntryReader;
public final class JDBCMapper {
    
    final private java.sql.Connection jdbcConnection;
    final private Connection ldapConnection;
    final private ArrayList<String> tables = new ArrayList<String>();
  private String dbName;
  final private ArrayList<String> tablesList = new ArrayList<String>();
  final private ArrayList<String> baseDNList = new ArrayList<String>();
    final private Map<String, ArrayList<String>> tableColumnsMap = new HashMap<String, ArrayList<String>>();
    final private Map<String, String> tableColumnNullableMap = new HashMap<String, String>();
    final private Map<String, String> tableColumnDataTypeMap = new HashMap<String, String>();
    final private ArrayList <String> organizationalUnits = new ArrayList<String>();
  final private Map<String, ArrayList<String>> organizationalUnitsMap = new HashMap<String, ArrayList<String>>();
    final private Map<String, ArrayList<String>> organizationalUnitAttributesMap = new HashMap<String, ArrayList<String>>();
    private Map<String, String> SQLToLDAPMap = new HashMap<String, String>();
    
@@ -62,32 +60,44 @@
        this.ldapConnection = ldapconnection;
    }
    
  public void setDbName(String DBName){
    this.dbName = DBName;
  }
    public void closeConnections() throws SQLException{
        this.jdbcConnection.close();
        this.ldapConnection.close();
    }
    
    public void fillMaps() throws ErrorResultException, ErrorResultIOException, SearchResultReferenceIOException, SQLException{
  public void fillMaps() throws ErrorResultException, SQLException, IOException{
    fillBaseDNList();
        fillTablesList();
        fillTableColumnsMap();
        fillOrganizationalUnitsList();
    fillOrganizationalUnitsMap();
        fillOrganizationalUnitAttributesMap();
    }
    
  private void fillBaseDNList() throws IOException{
    EntryReader reader = this.ldapConnection.search(" ", SearchScope.SINGLE_LEVEL, "objectClass=*");
    while(reader.hasNext()){
      baseDNList.add(reader.readEntry().getName().toString());
    }
  }
    private void fillTablesList() throws SQLException{
        final String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE " +
                "TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='opendj_db'";
        final Statement st = jdbcConnection.createStatement();
        "TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = '" + this.dbName + "'";
    final Statement st = this.jdbcConnection.createStatement();
        final ResultSet rs = st.executeQuery(sql);
        
        while(rs.next()){
            tables.add(rs.getString(1));
      tablesList.add(rs.getString(1));
        }
    }
    
    private void fillTableColumnsMap() throws SQLException, ErrorResultIOException, SearchResultReferenceIOException{
        for(int i =0; i < tables.size(); i++){
            final String tableName = tables.get(i);
    for(int i =0; i < tablesList.size(); i++){
      final String tableName = tablesList.get(i);
            final String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + tableName + "'";
            final Statement st = jdbcConnection.createStatement();
            final ResultSet rs = st.executeQuery(sql);
@@ -107,7 +117,11 @@
    }
    
    public ArrayList<String> getTables() throws SQLException{
        return tables;
    return tablesList;
  }
  public ArrayList<String> getBaseDNs(){
    return baseDNList;
    }
    
    public ArrayList<String> getTableColumns(String tableName) throws SQLException{
@@ -129,49 +143,63 @@
        return String.class;
    }
    
    public ArrayList<String> getOrganizationalUnits(){
        return organizationalUnits;
  public ArrayList<String> getOrganizationalUnits(String baseDN){
    return organizationalUnitsMap.get(baseDN);
    }
    
    public ArrayList<String> getOrganizationalUnitAttributes(String organizationalUnitName){
        final ArrayList<String> organizationalUnitAttributesList = organizationalUnitAttributesMap.get(organizationalUnitName);
        return organizationalUnitAttributesList;
  public ArrayList<String> getOrganizationalUnitAttributes(String baseDN, String organizationalUnitName){
    return organizationalUnitAttributesMap.get(baseDN + ":" + organizationalUnitName);
    }
    
    private void fillOrganizationalUnitsList() throws ErrorResultException, ErrorResultIOException, SearchResultReferenceIOException{
        ConnectionEntryReader reader = ldapConnection.search("dc=example,dc=com", SearchScope.WHOLE_SUBTREE, "ou=*");
  private void fillOrganizationalUnitsMap() throws ErrorResultException, ErrorResultIOException, SearchResultReferenceIOException{
    for(int i = 0; i < baseDNList.size(); i++){
      String baseDN = baseDNList.get(i);
      ConnectionEntryReader reader = ldapConnection.search(baseDN, SearchScope.WHOLE_SUBTREE, "ou=*");
      ArrayList<String> organizationalUnitsList = new ArrayList<String>();
        while (reader.hasNext()) {
            final SearchResultEntry entry = reader.readEntry();
            final Iterator<Attribute> it = entry.getAllAttributes().iterator();
            organizationalUnits.add(it.next().firstValueAsString());
        organizationalUnitsList.add(it.next().firstValueAsString());
      }
      organizationalUnitsMap.put(baseDN, organizationalUnitsList);
        }
    }
    
    private void fillOrganizationalUnitAttributesMap() throws ErrorResultIOException, SearchResultReferenceIOException{
        for(int i=0; i < organizationalUnits.size(); i++){
            final String organizationalUnitName = organizationalUnits.get(i);
            final ConnectionEntryReader reader = ldapConnection.search("ou=" + organizationalUnitName + ",dc=example,dc=com", SearchScope.WHOLE_SUBTREE, "uid=*");
    for(int i = 0; i < baseDNList.size(); i++){
      String baseDN = baseDNList.get(i);
      ArrayList<String> organizationalUnitsList = organizationalUnitsMap.get(baseDN);
      for(int j = 0; j < organizationalUnitsList.size(); j++){
        final String organizationalUnitName = organizationalUnitsList.get(j);
        final ConnectionEntryReader reader = ldapConnection.search("ou=" + organizationalUnitName + "," + baseDN, SearchScope.WHOLE_SUBTREE, "objectClass=*");
        final ArrayList<String> attributesList;
            
            if(reader.hasNext()){
                final Iterator<Attribute> it = reader.readEntry().getAllAttributes().iterator();
                final ArrayList <String> attributesList = new ArrayList <String>();
          attributesList = new ArrayList<String>();
                
                while(it.hasNext()){
                    final Attribute att = it.next();
                    attributesList.add(att.getAttributeDescriptionAsString());
                }            
                organizationalUnitAttributesMap.put(organizationalUnitName, attributesList);
        }
        else{
          attributesList = new ArrayList<String>();
          attributesList.add("");
        }
        organizationalUnitAttributesMap.put(baseDN + ":" + organizationalUnitName, attributesList);
            }
        }
    }
    
    public void addCurrentMapToMapping(String tableName, String[] columnNames, String OUName, String[] attributeNames){
  public void addCurrentMapToMapping(String tableName, String[] columnNames, String baseDN, String OUName, String[] attributeNames){
        String mappingKey, mappingValue;
        
        for(int i = 0; i < columnNames.length; i++){
            mappingKey = tableName + ":" + columnNames[i];
            mappingValue = OUName + ":" + attributeNames[i];
      mappingValue = baseDN + ":" + OUName + ":" + attributeNames[i];
            SQLToLDAPMap.put(mappingKey, mappingValue);
        }
    }
@@ -198,10 +226,10 @@
        this.SQLToLDAPMap = m;
    }
    public String getTableNameFromMapping(String organizationalUnit) {
  public String getTableNameFromMapping(String DN, String organizationalUnit) {
        for (Entry<String, String> entry : SQLToLDAPMap.entrySet()) {
            String mappingValue = entry.getValue();
            if (mappingValue.contains(organizationalUnit)) {
      if (mappingValue.contains(DN + ":" + organizationalUnit)) {
                String mappingKey = entry.getKey();
                String stringSplitter[] = mappingKey.split(":");
                String tableName = stringSplitter[0];
@@ -211,10 +239,10 @@
        return null;
    }
    public String getColumnNameFromMapping(String tableName, String organizationalUnitName, String attributeName) {
  public String getColumnNameFromMapping(String tableName, String baseDN, String organizationalUnitName, String attributeName) {
        for (Entry<String, String> entry : SQLToLDAPMap.entrySet()) {
            String mappingValue = entry.getValue();
            if (mappingValue.equals(organizationalUnitName + ":" + attributeName)) {
      if (mappingValue.equals(baseDN + ":" + organizationalUnitName + ":" + attributeName)) {
                String mappingKey = entry.getKey();
                if(mappingKey.contains(tableName)){
                    String stringSplitter[] = mappingKey.split(":");
@@ -228,4 +256,3 @@
}