Author: vtence Date: Tue Nov 30 05:59:19 2004 New Revision: 107005 URL: http://svn.apache.org/viewcvs?view=rev&rev=107005 Log: First cut at JDBC realm implementation; work still in progress Added: incubator/directory/janus/trunk/jdbc/ incubator/directory/janus/trunk/jdbc/project.xml (contents, props changed) incubator/directory/janus/trunk/jdbc/src/ incubator/directory/janus/trunk/jdbc/src/java/ incubator/directory/janus/trunk/jdbc/src/java/org/ incubator/directory/janus/trunk/jdbc/src/java/org/apache/ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Expression.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCException.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SameCredentialsBuilder.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SelectorBuilder.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/test/ incubator/directory/janus/trunk/jdbc/src/test/org/ incubator/directory/janus/trunk/jdbc/src/test/org/apache/ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Columns.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/MemoryDatabase.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java (contents, props changed) incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java (contents, props changed)
Added: incubator/directory/janus/trunk/jdbc/project.xml Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/project.xml?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/project.xml Tue Nov 30 05:59:19 2004 @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project> + <extend>${basedir}/../project.xml</extend> + + <name>Janus JBDC Implementation</name> + <id>janus-jdbc</id> + <package>org.apache.janus</package> + + <shortDescription>JDBC implementation of Janus core components</shortDescription> + + <description> + JDBC implementation of Janus core components + </description> + + <dependencies> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>janus-api</artifactId> + <version>${pom.currentVersion}</version> + </dependency> + <dependency> + <groupId>${pom.groupId}</groupId> + <artifactId>janus-impl</artifactId> + <version>${pom.currentVersion}</version> + </dependency> + </dependencies> +</project> \ No newline at end of file Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/AndExpression.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,83 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import java.util.Collection; +import java.util.ArrayList; +import java.util.Arrays; + +public class AndExpression implements Expression +{ + private Expression m_left; + private Expression m_right; + + public AndExpression( Expression left, Expression right ) + { + m_left = left; + m_right = right; + } + + public String getQueryString( Table table ) + { + StringBuffer fragment = new StringBuffer(); + fragment.append( m_left.getQueryString( table ) ); + fragment.append( " " ).append( operand() ).append( " "); + fragment.append( m_right.getQueryString( table )); + + return fragment.toString(); + } + + public int getParameterCount() + { + return m_left.getParameterCount() + m_right.getParameterCount(); + } + + public String[] getParameterValues( Table table ) + { + Collection parameters = new ArrayList( ); + parameters.addAll( Arrays.asList( m_left.getParameterValues( table ) ) ); + parameters.addAll( Arrays.asList( m_right.getParameterValues( table ) ) ); + + return ( String[] ) parameters.toArray( new String[parameters.size()] ); + } + + protected String operand() + { + return "and"; + } + + public boolean equals( Object o ) + { + if ( this == o ) return true; + if ( !( o instanceof AndExpression ) ) return false; + + final AndExpression andExpression = ( AndExpression ) o; + + if ( !m_left.equals( andExpression.m_left ) ) return false; + if ( !m_right.equals( andExpression.m_right ) ) return false; + + return true; + } + + public int hashCode() + { + int result; + result = m_left.hashCode(); + result = 29 * result + m_right.hashCode(); + return result; + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Column.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,39 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public class Column +{ + private final String m_name; + private final Class m_type; + + public Column( String name, Class type ) + { + m_name = name; + m_type = type; + } + + public String getName() + { + return m_name; + } + + public Class getMappedType() + { + return m_type; + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Converter.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,24 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public interface Converter +{ + String convert( Object credential ); + + boolean supports( Class credentialType ); +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/EqExpression.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,80 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public class EqExpression implements Expression +{ + private final Class m_type; + private final Object m_value; + + public EqExpression( Class type, Object value ) + { + m_type = type; + m_value = value; + } + + public String getQueryString( Table table ) + { + StringBuffer statement = new StringBuffer(); + statement.append( table.getColumnName( m_type ) ); + statement.append( operand() ); + statement.append( "?" ); + + return statement.toString(); + } + + public int getParameterCount() + { + return 1; + } + + public String[] getParameterValues( Table table ) + { + return new String[] { table.getColumnValue( m_value ) }; + } + + protected String operand() + { + return "="; + } + + public boolean equals( Object o ) + { + if ( this == o ) return true; + if ( !( o instanceof EqExpression ) ) return false; + + final EqExpression eqExpression = ( EqExpression ) o; + + if ( !m_type.equals( eqExpression.m_type ) ) return false; + if ( !m_value.equals( eqExpression.m_value ) ) return false; + + return true; + } + + public int hashCode() + { + int result; + result = m_type.hashCode(); + result = 29 * result + m_value.hashCode(); + return result; + } + + public String toString() + { + return "eq(" + m_type.getName() + ", " + m_value.toString() + ")"; + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Expression.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Expression.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Expression.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,26 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public interface Expression +{ + String getQueryString( Table table ); + + int getParameterCount(); + + String[] getParameterValues( Table table ); +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/ExpressionBuilder.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,30 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public class ExpressionBuilder +{ + public Expression eq( Class type, Object value ) + { + return new EqExpression( type, value ); + } + + public Expression and( Expression left, Expression right ) + { + return new AndExpression( left, right ); + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCException.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCException.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCException.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,34 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import java.sql.SQLException; + +public class JDBCException extends RuntimeException +{ + private final SQLException m_cause; + + public JDBCException( SQLException cause ) + { + m_cause = cause; + } + + public SQLException getSQLException() + { + return m_cause; + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCRealm.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,69 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class JDBCRealm +{ + private final DataSource m_ds; + private final Table m_table; + + public JDBCRealm( DataSource ds, Table table ) + { + m_ds = ds; + m_table = table; + } + + public boolean find( JDBCSelector selector ) throws JDBCException + { + Connection conn = null; + boolean found = false; + try + { + conn = m_ds.getConnection(); + found = selector.select( conn ); + } + catch ( SQLException e ) + { + throw new JDBCException( e ); + } + finally + { + closeConnection( conn ); + } + + return found; + } + + private void closeConnection( Connection conn ) + { + if (conn == null) return; + + try + { + conn.close(); + } + catch ( SQLException ignored ) + { + } + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/JDBCSelector.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,98 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +public class JDBCSelector implements Selector +{ + private final Table m_table; + private final Collection m_expressions; + + public JDBCSelector( Table table ) + { + m_table = table; + m_expressions = new ArrayList(); + } + + public void addExpression( Expression expression ) + { + m_expressions.add( expression ); + } + + public boolean select( Connection c ) throws SQLException + { + PreparedStatement statement = c.prepareStatement( buildQueryString() ); + bindParameterValues ( statement ); + ResultSet rs = statement.executeQuery(); + boolean found = rs.next(); + statement.close(); + + return found; + } + + protected String buildQueryString() + { + StringBuffer statement = new StringBuffer(); + statement.append( "select * from " ).append( m_table.getName() ).append( " where " ); + + for ( Iterator it = m_expressions.iterator(); it.hasNext(); ) + { + Expression expression = ( Expression ) it.next(); + statement.append( expression.getQueryString( m_table ) ); + if ( it.hasNext() ) statement.append( " and " ); + } + + return statement.toString(); + } + + protected void bindParameterValues( PreparedStatement statement ) throws SQLException + { + int index = 1; + for ( Iterator it = m_expressions.iterator(); it.hasNext(); ) + { + Expression expression = ( Expression ) it.next(); + index += bindExpressionParameters( statement, index, expression ); + } + + } + + private int bindExpressionParameters( PreparedStatement statement, int startIndex, Expression expression ) + throws SQLException + { + String[] values = expression.getParameterValues( m_table ); + for ( int i = 0; i < values.length; i++ ) + { + statement.setString( startIndex + i, values[i] ); + } + + return values.length; + } + + protected String[] getParameterValues() + { + Collection parameters = new ArrayList(); + + return ( String[] ) parameters.toArray( new String[parameters.size()] ); + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SameCredentialsBuilder.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SameCredentialsBuilder.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SameCredentialsBuilder.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,43 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import org.apache.janus.authentication.CredentialSet; +import org.apache.janus.authentication.Credential; + +import java.util.Iterator; + +public class SameCredentialsBuilder implements SelectorBuilder +{ + private final ExpressionBuilder m_builder; + private final CredentialSet m_credentials; + + public SameCredentialsBuilder( ExpressionBuilder builder, CredentialSet credentials ) + { + m_builder = builder; + m_credentials = credentials; + } + + public void build( Selector selector ) + { + for ( Iterator it = m_credentials.elements().iterator(); it.hasNext(); ) + { + Credential credential = (Credential) it.next(); + selector.addExpression( m_builder.eq( credential.getClass(), credential.getValue() )); + } + } +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Selector.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,22 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public interface Selector +{ + void addExpression( Expression expression ); +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SelectorBuilder.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SelectorBuilder.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/SelectorBuilder.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,22 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public interface SelectorBuilder +{ + void build( Selector selector ); +} Added: incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/java/org/apache/janus/authentication/realm/Table.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,70 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Collections; + +public class Table +{ + private final String m_name; + private final Map m_columns; + private final Map m_converters; + + public Table( String name ) + { + m_name = name; + m_columns = new HashMap(); + m_converters = new HashMap(); + } + + public String getName() + { + return m_name; + } + + public Collection getColumns() + { + return Collections.unmodifiableCollection( m_columns.values() ); + } + + public void addColumn( Column column ) + { + m_columns.put( column.getMappedType(), column ); + } + + public String getColumnName( Class credentialType ) + { + return column( credentialType ).getName(); + } + + public String getColumnValue( Object credential ) + { + Converter converter = ( Converter ) m_converters.get( credential.getClass() ); + return converter != null ? converter.convert( credential ) : credential.toString(); + } + + private Column column( Class credentialType ) + { + Column column = ( Column ) m_columns.get( credentialType ); + return column; + } +} + Added: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Columns.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Columns.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Columns.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,32 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public class Columns +{ + private Columns() {} + + public static Column username() + { + return new Column( "username", UsernameCredential.class ); + } + + public static Column password() + { + return new Column( "password", PasswordCredential.class ); + } +} Added: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCRealmTest.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,94 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import junit.framework.TestCase; +import org.apache.janus.authentication.CredentialSet; + +public class JDBCRealmTest extends TestCase +{ + private JDBCRealm m_realm; + private MemoryDatabase m_db; + + public static void main( String[] args ) + { + junit.textui.TestRunner.run( JDBCRealmTest.class ); + } + + protected void setUp() throws Exception + { + m_db = new MemoryDatabase(); + Table table = Tables.users(); + m_db.create( table ); + m_db.insertInto( table, new String[] { "joe", "foo" } ); + m_db.insertInto( table, new String[] { "jane", "bar" } ); + m_realm = new JDBCRealm( m_db.getDataSource() , table ); + } + + protected void tearDown() throws Exception + { + m_db.dropAll(); + } + + /** + * o several same credential -> should be caught by authentication method + * o parametrize credential type -> column name + * <p/> + * undefined column for credential type? + */ + public void testExecutesSelectionQueriesAgainstDatabase() throws Exception + { + JDBCSelector selector = new JDBCSelector( Tables.users() ); + CredentialSet creds = new CredentialSet(); + creds.add( new UsernameCredential( "joe" ) ); + creds.add( new PasswordCredential( "foo" ) ); + SameCredentialsBuilder builder = new SameCredentialsBuilder( new ExpressionBuilder(), creds ); + builder.build( selector ); + + assertTrue( m_realm.find( selector ) ); + } + + public void testThrowsJDBCExceptionIfSQLExceptionOccurs() + { + JDBCSelector selector = new JDBCSelector( undefinedTable() ); + + CredentialSet creds = new CredentialSet(); + creds.add( new UsernameCredential( "joe" ) ); + creds.add( new PasswordCredential( "foo" ) ); + SameCredentialsBuilder builder = new SameCredentialsBuilder( new ExpressionBuilder(), creds ); + builder.build( selector ); + + try + { + m_realm.find( selector ); + fail( "JDBCException expected" ); + } + catch ( JDBCException expected ) + { + assertTrue( true ); + } + } + + private Table undefinedTable() + { + Table table = new Table( "UNDEFINED" ); + table.addColumn( Columns.username() ); + table.addColumn( Columns.password() ); + return table; + } +} + Added: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/JDBCSelectorTest.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,78 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import junit.framework.TestCase; + +import java.util.Arrays; +import java.sql.Connection; + +import org.jmock.Mock; + +public class JDBCSelectorTest extends TestCase +{ + private JDBCSelector m_selector; + private MemoryDatabase m_db; + private Connection m_connection; + + protected void setUp() throws Exception + { + m_db = new MemoryDatabase(); + Table table = Tables.users(); + m_db.create( table ); + m_db.insertInto( table, new String[] { "joe", "foo" } ); + m_db.insertInto( table, new String[] { "jane", "bar" } ); + m_selector = new JDBCSelector( table ); + + m_connection = m_db.getDataSource().getConnection(); + } + + protected void tearDown() throws Exception + { + m_connection.close(); + m_db.dropAll(); + } + + /** + * + * run selection on JDBC connection + * + * conversion of credential object values to string values + * expression not a JDBC expression? + */ + + public void testFindsDataBasedOnCredentialExpression() throws Exception + { + m_selector.addExpression( new EqExpression( UsernameCredential.class, "joe" ) ); + assertTrue( m_selector.select( m_connection ) ); + } + + public void testSupportsMultipleCredentialExpressions() throws Exception + { + m_selector.addExpression( new EqExpression( UsernameCredential.class, "joe" ) ); + m_selector.addExpression( new EqExpression( PasswordCredential.class, "foo" ) ); + + assertTrue( m_selector.select( m_connection ) ); + } + + public void testSupportsAndExpressions() throws Exception + { + m_selector.addExpression( new AndExpression( new EqExpression( UsernameCredential.class, "joe" ), + new EqExpression( PasswordCredential.class, "foo" ) ) ); + assertTrue( m_selector.select( m_connection ) ); + } +} Added: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/MemoryDatabase.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/MemoryDatabase.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/MemoryDatabase.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,105 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import org.hsqldb.jdbc.jdbcDataSource; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Iterator; +import java.util.Collection; +import java.util.ArrayList; + +public class MemoryDatabase +{ + private final jdbcDataSource m_ds; + private final Collection m_tables; + + public MemoryDatabase() + { + m_tables = new ArrayList(); + m_ds = new jdbcDataSource(); + m_ds.setDatabase( "jdbc:hsqldb:mem:testdb" ); + m_ds.setUser( "sa" ); + m_ds.setPassword( "" ); + } + + public DataSource getDataSource() + { + return m_ds; + } + + public void create( Table table ) throws SQLException + { + Connection c = m_ds.getConnection(); + Statement statement = c.createStatement(); + StringBuffer sb = new StringBuffer( ); + sb.append( "create table ").append( table.getName() ).append( " (\n"); + + for ( Iterator it = table.getColumns().iterator(); it.hasNext(); ) + { + Column column = (Column) it.next(); + sb.append( column.getName() ).append( " varchar(32)" ); + if ( it.hasNext() ) sb.append( ",\n" ); + } + sb.append( "\n)" ); + statement.executeUpdate( sb.toString() ); + c.close(); + + m_tables.add( table ); + } + + public void drop( Table table ) throws SQLException + { + Connection c = m_ds.getConnection(); + Statement statement = c.createStatement(); + StringBuffer sb = new StringBuffer( ); + sb.append( "drop table ").append( table.getName() ); + statement.executeUpdate( sb.toString() ); + c.close(); + + m_tables.remove( table ); + } + + public void insertInto( Table table, String[] values ) throws SQLException + { + Connection c = m_ds.getConnection(); + Statement statement = c.createStatement(); + StringBuffer sb = new StringBuffer( ); + sb.append( "insert into ").append( table.getName() ).append( " values( "); + for ( int i = 0; i < values.length; i++ ) + { + sb.append( "'" ).append( values[i] ).append( "'" ); + if ( i < values.length - 1 ) sb.append( ","); + } + sb.append( ")"); + statement.executeUpdate( sb.toString() ); + c.close(); + } + + public void dropAll() throws SQLException + { + Table[] tables = ( Table[] ) m_tables.toArray( new Table[m_tables.size()] ); + for ( int i = 0; i < tables.length; i++ ) + { + drop( tables[i] ); + + } + } +} Added: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/SameCredentialsBuilderTest.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,50 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +import org.apache.janus.authentication.CredentialSet; +import org.jmock.Mock; +import org.jmock.MockObjectTestCase; + +public class SameCredentialsBuilderTest extends MockObjectTestCase +{ + private SameCredentialsBuilder m_builder; + + public static void main( String[] args ) + { + junit.textui.TestRunner.run( SameCredentialsBuilderTest.class ); + } + + protected void setUp() throws Exception + { + } + + public void testBuildsSelectorLookingForEqualMatchOnAllCredentials() + { + CredentialSet creds = new CredentialSet(); + creds.add( new UsernameCredential( "joe" ) ); + creds.add( new PasswordCredential( "foo" ) ); + m_builder = new SameCredentialsBuilder( new ExpressionBuilder(), creds ); + + Mock mockSelector = new Mock( Selector.class ); + + mockSelector.expects( once() ).method( "addExpression" ).with( eq( new EqExpression( UsernameCredential.class, "joe" ) ) ); + mockSelector.expects( once() ).method( "addExpression" ).with( eq( new EqExpression( PasswordCredential.class, "foo" ) ) ); + m_builder.build( ( Selector ) mockSelector.proxy() ); + mockSelector.verify(); + } +} Added: incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java Url: http://svn.apache.org/viewcvs/incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java?view=auto&rev=107005 ============================================================================== --- (empty file) +++ incubator/directory/janus/trunk/jdbc/src/test/org/apache/janus/authentication/realm/Tables.java Tue Nov 30 05:59:19 2004 @@ -0,0 +1,30 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.janus.authentication.realm; + +public class Tables +{ + public Tables() {} + + public static Table users() + { + Table table = new Table( "USERS" ); + table.addColumn( Columns.username() ); + table.addColumn( Columns.password() ); + return table; + } +}
