Commit b0445ede authored by Kurt Zeilenga's avatar Kurt Zeilenga
Browse files

Initial revision

parents
Copyright 2002 The OpenLDAP Foundation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted only as authorized by the OpenLDAP
Public License. A copy of this license is available at
http://www.OpenLDAP.org/license.html or in file LICENSE in the
top-level directory of the distribution.
Individual files and/or contributed packages may contain material
copyright by other parties and use subject to additional restrictions.
This work is derived from materials developed by Octet String, Inc.
This work contains materials derived from public sources.
Additional Information about OpenLDAP can be obtained at
<http://www.openldap.org/>.
---
******************************************************************************
* Copyright (C) 2002 Octet String, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM OCTET STRING, INC.,
* COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************
The OpenLDAP Public License
Version 2.7, 7 September 2001
Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain copyright statements
and notices,
2. Redistributions in binary form must reproduce applicable copyright
statements and notices, this list of conditions, and the following
disclaimer in the documentation and/or other materials provided
with the distribution, and
3. Redistributions must contain a verbatim copy of this document.
The OpenLDAP Foundation may revise this license from time to time.
Each revision is distinguished by a version number. You may use
this Software under terms of this license revision or under the
terms of any subsequent revision of the license.
THIS SOFTWARE IS PROVIDED BY THE OPENLDAP FOUNDATION AND ITS
CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE OPENLDAP FOUNDATION, ITS CONTRIBUTORS, OR THE AUTHOR(S)
OR OWNER(S) OF THE SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
The names of the authors and copyright holders must not be used in
advertising or otherwise to promote the sale, use or other dealing
in this Software without specific, written prior permission. Title
to copyright in this Software shall at all times remain with
copyright holders.
OpenLDAP is a registered trademark of the OpenLDAP Foundation.
Copyright 1999-2001 The OpenLDAP Foundation, Redwood City,
California, USA. All Rights Reserved. Permission to copy and
distribute verbatim copies of this document is granted.
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Created by mlb on March 8, 2002, 11:12 PM -->
<project basedir="." default="build" name="jdbcLdap">
<target description="Initializes properties" name="init">
<property name="example" value="example"/>
<property name="src" value="src"/>
<property name="classes" value="classes"/>
<property name="jars" value="bin"/>
<property name="lib" value="lib"/>
<property name="docsdir" value="docs"/>
<property name="reports.tests" value="testResults"/>
<property name="test.ldapConnString" value="jdbc:ldap://localhost:389/dc=idrs,dc=com"/>
</target>
<target depends="init" name="build" description="Builds the drivers" >
<mkdir dir="${classes}"/>
<javac destdir="${classes}" includes="com/octetstring/**" srcdir="${src}">
<classpath>
<pathelement location="${lib}/junit.jar" />
</classpath>
</javac>
</target>
<target depends="init" description="jars the classes into the bin directory and copys the jar to the lib directory" name="jar">
<mkdir dir="${jars}"/>
<delete file="${jars}/jdbcLdap.jar"/>
<jar jarfile="${jars}/jdbcLdap.jar">
<fileset dir="${classes}" includes="**/*.class" excludes="**/TestSQL.class"/>
</jar>
<copy file="${jars}/jdbcLdap.jar" todir="${lib}"/>
<mkdir dir="${example}"/>
<copy file="${src}/TestSQL.java" todir="${example}"/>
</target>
<target depends="init,build,jar" description="performs both a build and jar" name="build-jar"/>
<target depends="init" description="executes javadoc on the sources in src and places the docs in docs" name="doc">
<mkdir dir="tmp"/>
<copy todir="tmp">
<fileset dir="src"/>
</copy>
<copy todir="tmp">
<fileset dir="${src}"/>
</copy>
<javadoc Private="true" author="true" destdir="${docsdir}" packagenames="com.octetstring.*" sourcepath="tmp">
</javadoc>
<delete dir="tmp"/>
</target>
<target depends="build-jar" name="unit-test" description="Runs all JUnitTests">
<delete dir="testResults" />
<mkdir dir="testResults"/>
<mkdir dir="testResults/html"/>
<junit printsummary="yes" haltonfailure="no" fork="yes" >
<sysproperty key="ldapConnString" value="${test.ldapConnString}" />
<test name="com.octetstring.jdbcLdap.junit.sql.TestDriver" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestSqlToLdap" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestSelect" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestSelectRetrieve" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestUnpack" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestResultSet" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestStatement" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestInsert" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestDelete" todir="testResults"/>
<test name="com.octetstring.jdbcLdap.junit.sql.TestUpdate" todir="testResults"/>
<classpath>
<pathelement path="${lib}/jdbcLdap.jar" />
</classpath>
<formatter type="xml" />
</junit>
<junitreport todir="${reports.tests}">
<fileset dir="${reports.tests}">
<include name="*.xml"/>
</fileset>
<report format="frames" todir="${reports.tests}/html"/>
</junitreport>
</target>
</project>
/* **************************************************************************
*
* Copyright (C) 2002 Octet String, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM OCTET STRING, INC.,
* COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
/*
* TestSQL.java
*
* Created on May 23, 2002, 12:07 PM
*/
import com.octetstring.jdbcLdap.sql.statements.JdbcLdapSelect;
import com.octetstring.jdbcLdap.jndi.*;
import com.octetstring.jdbcLdap.sql.*;
import com.octetstring.jdbcLdap.sql.statements.*;
import java.sql.*;
import javax.naming.directory.*;
/**
*
* @author pjh, OctetString, Inc (c)2002
*/
public class TestSQL {
/** Creates a new instance of Test */
public static void main(String[] args) throws Exception {
System.out.println("Welcome to the JDBC LDAP Demo Application.");
//JndiLdapConnection con;
boolean doInsert;
boolean doDelete;
boolean doDeleteMulti;
Class.forName("com.octetstring.jdbcLdap.sql.JdbcLdapDriver");
// ldapConnString should be of the form...
// jdbc:ldap://host[:port]/base dn
String ldapConnectString =
"jdbc:ldap://127.0.0.1:389/o=acme.com?SEARCH_SCOPE:=subTreeScope";
java.sql.Connection con;
con =
DriverManager.getConnection(
ldapConnectString,
"cn=Admin",
"manager");
System.out.println("Connection established");
try {
System.out.println("Attempting to insert test records...");
Statement stmt = con.createStatement();
int count;
count =
stmt.executeUpdate(
"INSERT INTO ou=Product Development (objectClass,objectClass,ou) VALUES (top,organizationalunit,Product Development)");
count
+= stmt.executeUpdate(
"INSERT INTO cn=Marc Boorshtein, OctetString,ou=Product Development (objectClass,objectClass,objectClass,sn,cn) VALUES (top,person,organizationalPerson,Boorshtein,Marc Boorshtein, OctetString)");
count
+= stmt.executeUpdate(
"INSERT INTO cn=Phil Hunt,ou=Product Development (objectClass,objectClass,objectClass,sn,cn) VALUES (top,person,organizationalPerson,Hunt,Phil Hunt)");
count
+= stmt.executeUpdate(
"INSERT INTO cn=Steve Boorsh,ou=Product Development (objectClass,objectClass,objectClass,sn,cn) VALUES (top,person,organizationalPerson,Boorsh,Steve Boorsh)");
count
+= stmt.executeUpdate(
"INSERT INTO cn=Sherry Boorsh,ou=Product Development (objectClass,objectClass,objectClass,sn,cn) VALUES (top,person,organizationalPerson,Boorsh,Sherry Boorsh)");
if (count < 5) {
System.out.println("Test Insert failed.");
} else {
System.out.println("Test Insert succeeded.");
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("Attempting to delete a record...");
String SQL = "DELETE FROM ou=Product Development WHERE sn=?";
PreparedStatement ps = con.prepareStatement(SQL);
ps.setString(1, "boorshtein");
ps.execute();
System.out.println("Test single deletion successful.");
} catch (Exception e) {
System.out.println("Test single deletion failed.");
e.printStackTrace();
return;
}
try {
System.out.println("Test query by sn...");
Statement stmt = con.createStatement();
ResultSet rs =
stmt.executeQuery(
"SELECT * FROM ou=Product Development WHERE sn=Boorsh");
System.out.println("Query result rows = " + rs.getFetchSize());
boolean valuesPrinted = false;
while (rs.next()) {
valuesPrinted = true;
// Note, but, getString is case sensitive.
// DN must be uppercase, rest are lowercase
System.out.println(
"DN="
+ rs.getString("DN")
+ ", sn="
+ rs.getString("sn")
+ ", cn="
+ rs.getString("cn"));
}
if (!valuesPrinted)
System.out.println("Query returned no results.");
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("Attempting to delete remaining records...");
String SQL =
"DELETE FROM ou=Product Development WHERE objectclass=*";
Statement del = con.createStatement();
int res = del.executeUpdate(SQL);
System.out.println("Result=" + res);
if (res < 3) {
System.out.println(
"Test deletion of remaining entries failed.");
return;
} else {
System.out.println(
"Test deletion of remaining entries successful.");
}
} catch (Exception e) {
e.printStackTrace();
}
con.close();
System.out.println("Test run finished.");
}
}
/* **************************************************************************
*
* Copyright (C) 2002 Octet String, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM OCTET STRING, INC.,
* COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
/*
* Delete.java
*
* Created on March 13, 2002, 5:50 PM
*/
package com.octetstring.jdbcLdap.jndi;
import javax.naming.*;
import javax.naming.directory.*;
import com.octetstring.jdbcLdap.sql.statements.*;
import com.octetstring.jdbcLdap.sql.*;
import java.sql.*;
/**
*Deletes an entry
*@author Marc Boorshtein, OctetString
*/
public class Delete {
RetrieveResults res = new RetrieveResults();
SearchResult seres;
public int doDelete(JdbcLdapDelete del) throws SQLException {
DirContext con = del.getCon().getContext();
StringBuffer buf = new StringBuffer();
SqlStore store = del.getSqlStore();
int count = 0;
if (store.getSimple()) {
try {
con.destroySubcontext(store.getFrom());
}
catch (NamingException ne) {
throw new SQLNamingException(ne);
}
return 1;
}
else {
try {
NamingEnumeration enum = res.searchUpIns(del);
while (enum.hasMore()) {
seres = (SearchResult) enum.next();
buf.setLength(0);
con.destroySubcontext(buf.append(seres.getName()).append(',').append(store.getFrom()).toString());
count++;
}
enum.close();
return count;
}
catch (NamingException ne) {
throw new SQLNamingException(ne);
}
}
}
}
/* **************************************************************************
*
* Copyright (C) 2002 Octet String, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM OCTET STRING, INC.,
* COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
/*
* FieldStore.java
*
* Created on March 14, 2002, 10:20 AM
*/
package com.octetstring.jdbcLdap.jndi;
import java.sql.Types;
/**
*Stores Meta information about a field
*@author Marc Boorshtein, OctetString
*/
public class FieldStore {
static final String DECIMAL = ".";
static final String DASH = "-";
static final String COLON = ":";
String name;
int numVals;
int type;
boolean determined;
/** Creates new FieldStore */
public FieldStore() {
numVals = 0;
determined = false;
}
public FieldStore(String name, int vals) {
this.name = name;
this.numVals = vals;
determined = false;
}
/**
*Returns the name of the field
*/
public String getName() {
return name;
}
/**
*Returns the number of values for this field
*/
public int getNumVals() {
return numVals;
}
/**
*Retrieves data type
*@return SQL type
*/
public int getType() {
if (determined) {
return type;
}
else {
return java.sql.Types.VARCHAR;
}
}
/**
*Determines the type of data if it has not been determined
*@param val Value to try and determine
*/
public void determineType(String val) {
long ltmp;
int itmp;
int pos1,pos2;
//if it is already determined or it is empty, skip
if (determined || (val == null || val.length() == 0)) return;
//firt dtermine if first char is numeric
if (Character.isDigit(val.charAt(0))) {
mightNumeric(val);
}
else {
//not numeric
type = Types.VARCHAR;
determined = true;
}
}
protected void mightNumeric(String val) {
//MIGHT be numeric OR a date/time
int pos1 = val.indexOf(DASH);
int pos2 = val.indexOf(COLON);
long ltmp;
//is this a date or time?
if (pos1 != -1 || pos2 != -1) {
mightDateTime(val,pos1,pos2);
}
//first see if there is a "."
else if(val.indexOf(DECIMAL) != -1) {
//try making it into a double
try {
Double.valueOf(val);
type = Types.DOUBLE;
determined = true;
}
catch (Exception e) {
//not a decimal, must be a string
type = Types.VARCHAR;
determined = true;
}
}
else {
//might be an integer
try {
//we'll try to make a long
ltmp = Long.parseLong(val);
type = Types.INTEGER;
determined = true;
}
catch (Exception e) {
//string
type = Types.VARCHAR;
determined = true;
}
}
}
protected void mightDateTime(String val,int pos1, int pos2) {
//lets see if its a timestamp or a datetime
if (pos1 != -1 && pos2 != -1 && pos1 < pos2) {
//could be a date time
try {
java.sql.Timestamp.valueOf(val);
type = Types.TIMESTAMP;
determined = true;
}
catch (IllegalArgumentException e) {
//not a decimal, must be a string
type = Types.VARCHAR;
determined = true;
}
}
else {
//now we need to determine if it might be a date or a time
if (pos1 != -1 || pos2 != -1) {
if (pos1 == -1) {
//might be a time
try {
java.sql.Time.valueOf(val);
type = Types.TIME;
determined = true;
}
catch (IllegalArgumentException e) {
//not a decimal, must be a string
type = Types.VARCHAR;
determined = true;
}
}
else {
//might be a date
try {
java.sql.Date.valueOf(val);
type = Types.DATE;
determined = true;
}
catch (IllegalArgumentException e) {
//not a decimal, must be a string
type = Types.VARCHAR;
determined = true;
}
}
}
else {
type = Types.VARCHAR;
determined = true;
}
}
}
/* **************************************************************************
*
* Copyright (C) 2002 Octet String, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM OCTET STRING, INC.,
* COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
******************************************************************************/
/*
* Insert.java
*
* Created on March 13, 2002, 5:50 PM
*/
package com.octetstring.jdbcLdap.jndi;
import javax.naming.*;
import java.util.*;
import javax.naming.directory.*