Refactoring and package restructuring of transactions.

This commit is contained in:
Mattias Arthursson
2007-04-29 10:57:34 +00:00
parent ea3c0fd66b
commit fd0880de62
32 changed files with 342 additions and 341 deletions

View File

@@ -18,8 +18,6 @@
<property name="password" value="${password}" />
<property name="base" value="${base}" />
<property name="pooled" value="false" />
<property name="dirObjectFactory"
value="org.springframework.ldap.core.support.DefaultDirObjectFactory" />
</bean>
<bean id="dataSource"
@@ -31,7 +29,7 @@
</bean>
<bean id="contextSource"
class="org.springframework.ldap.transaction.core.TransactionAwareContextSourceProxy">
class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy">
<constructor-arg ref="contextSourceTarget" />
</bean>
@@ -45,23 +43,15 @@
<property name="dataSource" ref="dataSource" />
</bean>
<!--
<bean id="ldapTransactionManager"
class="org.springframework.ldap.transaction.core.ContextSourceTransactionManager">
<property name="contextSource" ref="contextSourceTarget" />
</bean>
-->
<bean id="transactionManager"
class="org.springframework.ldap.transaction.core.ContextSourceAndDataSourceTransactionManager">
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="contextSource" ref="contextSource" />
</bean>
<bean name="dummyDaoTarget"
class="org.springframework.ldap.transaction.core.LdapAndJdbcDummyDaoImpl">
class="org.springframework.ldap.transaction.compensating.manager.LdapAndJdbcDummyDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
@@ -77,16 +67,4 @@
</props>
</property>
</bean>
<!--
<bean name="dummyDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="dataSourceTransactionManager" />
<property name="target" ref="ldapDummyDao" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
-->
</beans>

View File

@@ -18,12 +18,10 @@
<property name="password" value="${password}" />
<property name="base" value="${base}" />
<property name="pooled" value="false" />
<property name="dirObjectFactory"
value="org.springframework.ldap.core.support.DefaultDirObjectFactory" />
</bean>
<bean id="contextSource"
class="org.springframework.ldap.transaction.core.TransactionAwareContextSourceProxy">
class="org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy">
<constructor-arg ref="contextSourceTarget" />
</bean>
@@ -33,13 +31,13 @@
</bean>
<bean id="transactionManager"
class="org.springframework.ldap.transaction.core.ContextSourceTransactionManager">
class="org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager">
<property name="contextSource" ref="contextSource" />
</bean>
<bean name="dummyDaoTarget"
class="org.springframework.ldap.transaction.core.LdapDummyDaoImpl">
class="org.springframework.ldap.transaction.compensating.manager.LdapDummyDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -30,7 +30,7 @@ import org.springframework.ldap.LdapServerManager;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.transaction.core.ContextSourceAndDataSourceTransactionManager;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.transaction.support.TransactionSynchronizationManager;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
@@ -24,7 +24,7 @@ import org.springframework.ldap.LdapServerManager;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.transaction.core.ContextSourceAndDataSourceTransactionManager;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.transaction.support.TransactionSynchronizationManager;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
public interface DummyDao {
void createWithException(String country, String company, String fullname,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
public class DummyException extends RuntimeException {
public DummyException(String message) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
public class DummyServiceImpl {
private DummyDao dummyDaoImpl;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ldap.core.DirContextAdapter;
@@ -36,7 +36,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#createWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#createWithException(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
@@ -49,7 +49,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#create(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#create(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
@@ -73,7 +73,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#update(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#update(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void update(String dn, String fullname, String lastname,
@@ -93,7 +93,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#updateWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#updateWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateWithException(String dn, String fullname,
@@ -105,7 +105,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#updateAndRename(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#updateAndRename(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateAndRename(String dn, String newDn, String description) {
@@ -120,7 +120,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#updateAndRenameWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#updateAndRenameWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateAndRenameWithException(String dn, String newDn,
@@ -132,7 +132,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#modifyAttributes(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributes(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void modifyAttributes(String dn, String lastName, String description) {
@@ -146,7 +146,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#modifyAttributesWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributesWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void modifyAttributesWithException(String dn, String lastName,
@@ -158,7 +158,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#unbind(java.lang.String)
* @see org.springframework.ldap.transaction.support.DummyDao#unbind(java.lang.String)
*/
public void unbind(String dn, String fullname) {
ldapTemplate.unbind(dn);
@@ -169,7 +169,7 @@ public class LdapAndJdbcDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#unbindWithException(java.lang.String)
* @see org.springframework.ldap.transaction.support.DummyDao#unbindWithException(java.lang.String)
*/
public void unbindWithException(String dn, String fullname) {
unbind(dn, fullname);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
@@ -29,7 +29,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#createWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#createWithException(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
@@ -42,7 +42,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#create(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#create(java.lang.String,
* java.lang.String, java.lang.String, java.lang.String,
* java.lang.String)
*/
@@ -64,7 +64,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#update(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#update(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void update(String dn, String fullname, String lastname,
@@ -80,7 +80,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#updateWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#updateWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateWithException(String dn, String fullname,
@@ -92,7 +92,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#updateAndRename(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#updateAndRename(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateAndRename(String dn, String newDn, String description) {
@@ -107,7 +107,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#updateAndRenameWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#updateAndRenameWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void updateAndRenameWithException(String dn, String newDn,
@@ -119,7 +119,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#modifyAttributes(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributes(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void modifyAttributes(String dn, String lastName, String description) {
@@ -133,7 +133,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#modifyAttributesWithException(java.lang.String,
* @see org.springframework.ldap.transaction.support.DummyDao#modifyAttributesWithException(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void modifyAttributesWithException(String dn, String lastName,
@@ -145,7 +145,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#unbind(java.lang.String)
* @see org.springframework.ldap.transaction.support.DummyDao#unbind(java.lang.String)
*/
public void unbind(String dn, String fullname) {
ldapTemplate.unbind(dn);
@@ -154,7 +154,7 @@ public class LdapDummyDaoImpl implements DummyDao {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.transaction.core.DummyDao#unbindWithException(java.lang.String)
* @see org.springframework.ldap.transaction.support.DummyDao#unbindWithException(java.lang.String)
*/
public void unbindWithException(String dn, String fullname) {
unbind(dn, fullname);

View File

@@ -45,38 +45,53 @@ public class LdapCompensatingTransactionOperationFactory implements
private static Log log = LogFactory
.getLog(LdapCompensatingTransactionOperationFactory.class);
private LdapOperations ldapOperations;
private TempEntryRenamingStrategy renamingStrategy;
/**
* Constructor.
*
* @param ctx
* The transactional DirContext.
* @param renamingStrategy
* the {@link TempEntryRenamingStrategy} to supply to relevant
* operations.
*/
public LdapCompensatingTransactionOperationFactory(DirContext ctx,
public LdapCompensatingTransactionOperationFactory(
TempEntryRenamingStrategy renamingStrategy) {
this.ldapOperations = new LdapTemplate(new SingleContextSource(ctx));
this.renamingStrategy = renamingStrategy;
}
/*
* (non-Javadoc)
*
* @see org.springframework.transaction.compensating.CompensatingTransactionOperationFactory#createRecordingOperation(java.lang.Object,
* java.lang.String)
*/
public CompensatingTransactionOperationRecorder createRecordingOperation(
String operation) {
if (StringUtils.equals(operation, LdapTransactionUtils.BIND_METHOD_NAME)) {
Object resource, String operation) {
if (StringUtils
.equals(operation, LdapTransactionUtils.BIND_METHOD_NAME)) {
log.debug("Bind operation recorded");
return new BindOperationRecorder(ldapOperations);
} else if (StringUtils.equals(operation, LdapTransactionUtils.REBIND_METHOD_NAME)) {
return new BindOperationRecorder(
createLdapOperationsInstance((DirContext) resource));
} else if (StringUtils.equals(operation,
LdapTransactionUtils.REBIND_METHOD_NAME)) {
log.debug("Rebind operation recorded");
return new RebindOperationRecorder(ldapOperations, renamingStrategy);
} else if (StringUtils.equals(operation, LdapTransactionUtils.RENAME_METHOD_NAME)) {
return new RebindOperationRecorder(
createLdapOperationsInstance((DirContext) resource),
renamingStrategy);
} else if (StringUtils.equals(operation,
LdapTransactionUtils.RENAME_METHOD_NAME)) {
log.debug("Rename operation recorded");
return new RenameOperationRecorder(ldapOperations);
return new RenameOperationRecorder(
createLdapOperationsInstance((DirContext) resource));
} else if (StringUtils.equals(operation,
LdapTransactionUtils.MODIFY_ATTRIBUTES_METHOD_NAME)) {
return new ModifyAttributesOperationRecorder(ldapOperations);
} else if (StringUtils.equals(operation, LdapTransactionUtils.UNBIND_METHOD_NAME)) {
return new UnbindOperationRecorder(ldapOperations, renamingStrategy);
return new ModifyAttributesOperationRecorder(
createLdapOperationsInstance((DirContext) resource));
} else if (StringUtils.equals(operation,
LdapTransactionUtils.UNBIND_METHOD_NAME)) {
return new UnbindOperationRecorder(
createLdapOperationsInstance((DirContext) resource),
renamingStrategy);
}
log
@@ -85,6 +100,10 @@ public class LdapCompensatingTransactionOperationFactory implements
return new NullOperationRecorder();
}
LdapOperations createLdapOperationsInstance(DirContext ctx) {
return new LdapTemplate(new SingleContextSource(ctx));
}
/**
* A {@link ContextSource} implementation using returning
* {@link NonClosingDirContextInvocationHandler} proxies on the same
@@ -180,8 +199,4 @@ public class LdapCompensatingTransactionOperationFactory implements
}
}
}
void setLdapOperations(LdapOperations ldapOperations) {
this.ldapOperations = ldapOperations;
}
}

View File

@@ -21,12 +21,7 @@ import javax.naming.directory.DirContext;
import javax.naming.ldap.LdapContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.NamingException;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
/**
@@ -37,9 +32,6 @@ import org.springframework.util.Assert;
*/
public final class LdapTransactionUtils {
private static final Log logger = LogFactory
.getLog(LdapTransactionUtils.class);
public static final String REBIND_METHOD_NAME = "rebind";
public static final String BIND_METHOD_NAME = "bind";
@@ -57,28 +49,6 @@ public final class LdapTransactionUtils {
}
/**
* Close the given JNDI Context and ignore any thrown exception. This is
* useful for typical <code>finally</code> blocks in JNDI code.
*
* @param context
* the JNDI Context to close (may be <code>null</code>)
*/
public static void closeContext(DirContext context) {
if (context != null) {
try {
context.close();
} catch (NamingException ex) {
logger.debug("Could not close JNDI DirContext", ex);
} catch (Throwable ex) {
// We don't trust the JNDI provider: It might throw
// RuntimeException or Error.
logger.debug("Unexpected exception on closing JNDI DirContext",
ex);
}
}
}
/**
* Get the first parameter in the argument list as a Name.
*
@@ -113,31 +83,6 @@ public final class LdapTransactionUtils {
}
}
/**
* Close the supplied context, but only if it is not associated with the
* current transaction.
*
* @param context
* the DirContext to close.
* @param contextSource
* the ContextSource bound to the transaction.
* @throws NamingException
*/
public static void doCloseConnection(DirContext context,
ContextSource contextSource) throws javax.naming.NamingException {
DirContextHolder transactionContextHolder = (DirContextHolder) TransactionSynchronizationManager
.getResource(contextSource);
if (transactionContextHolder == null
|| transactionContextHolder.getCtx() != context) {
logger.debug("Closing context");
// This is not the transactional context or the transaction is
// no longer active - we should close it.
context.close();
} else {
logger.debug("Leaving transactional context open");
}
}
/**
* Check whether the supplied method is a method for which transactions is
* supported (and which should be recorded for possible rollback later).
@@ -173,5 +118,4 @@ public final class LdapTransactionUtils {
return DirContext.class;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.ldap.core.ContextSource;
@@ -30,7 +30,6 @@ import org.springframework.transaction.support.DefaultTransactionStatus;
* and thus commit and rollback may result in unexpected results.
*
* @author Mattias Arthursson
* @since 1.2
*/
public class ContextSourceAndDataSourceTransactionManager extends
DataSourceTransactionManager {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy;
@@ -104,7 +104,6 @@ import org.springframework.transaction.support.DefaultTransactionStatus;
* @see DefaultCompensatingTransactionOperationManager
* @see TempEntryRenamingStrategy
* @see TransactionAwareContextSourceProxy
* @since 1.2
*/
public class ContextSourceTransactionManager extends
AbstractPlatformTransactionManager {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
@@ -21,11 +21,12 @@ import javax.naming.directory.DirContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.transaction.compensating.DirContextHolder;
import org.springframework.ldap.transaction.compensating.LdapCompensatingTransactionOperationFactory;
import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy;
import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy;
import org.springframework.transaction.compensating.support.AbstractCompensatingTransactionManagerDelegate;
import org.springframework.transaction.compensating.support.CompensatingTransactionHolderSupport;
import org.springframework.transaction.compensating.support.DefaultCompensatingTransactionOperationManager;
/**
* This delegate performs all the work for the
@@ -36,7 +37,6 @@ import org.springframework.transaction.compensating.support.CompensatingTransact
* @author Mattias Arthursson
* @see ContextSourceTransactionManager
* @see ContextSourceAndDataSourceTransactionManager
* @since 1.2
*/
public class ContextSourceTransactionManagerDelegate extends
AbstractCompensatingTransactionManagerDelegate {
@@ -77,8 +77,10 @@ public class ContextSourceTransactionManagerDelegate extends
protected CompensatingTransactionHolderSupport getNewHolder() {
DirContext newCtx = getContextSource().getReadOnlyContext();
DirContextHolder contextHolder = new DirContextHolder(newCtx,
renamingStrategy);
DirContextHolder contextHolder = new DirContextHolder(
new DefaultCompensatingTransactionOperationManager(
new LdapCompensatingTransactionOperationFactory(
renamingStrategy)), newCtx);
return contextHolder;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.compensating;
package org.springframework.ldap.transaction.compensating.manager;
import javax.naming.directory.DirContext;
import org.springframework.transaction.compensating.CompensatingTransactionOperationFactory;
import org.springframework.transaction.compensating.CompensatingTransactionOperationManager;
import org.springframework.transaction.compensating.support.CompensatingTransactionHolderSupport;
@@ -29,24 +28,23 @@ import org.springframework.transaction.compensating.support.CompensatingTransact
* for commit or rollback.
*
* @author Mattias Arthursson
* @since 1.2
*
*/
public class DirContextHolder extends CompensatingTransactionHolderSupport {
private DirContext ctx;
private TempEntryRenamingStrategy renamingStrategy;
/**
* Constructor.
*
* @param manager
* The {@link CompensatingTransactionOperationManager}.
* @param ctx
* The DirContext associated with the current transaction.
*/
public DirContextHolder(DirContext ctx,
TempEntryRenamingStrategy renamingStrategy) {
public DirContextHolder(CompensatingTransactionOperationManager manager,
DirContext ctx) {
super(manager);
this.ctx = ctx;
this.renamingStrategy = renamingStrategy;
refreshTransactionOperationManager();
}
/**
@@ -57,12 +55,6 @@ public class DirContextHolder extends CompensatingTransactionHolderSupport {
*/
public void setCtx(DirContext ctx) {
this.ctx = ctx;
refreshTransactionOperationManager();
}
protected CompensatingTransactionOperationFactory createOperationFactory() {
return new LdapCompensatingTransactionOperationFactory(ctx,
renamingStrategy);
}
/**
@@ -72,7 +64,12 @@ public class DirContextHolder extends CompensatingTransactionHolderSupport {
return ctx;
}
public TempEntryRenamingStrategy getRenamingStrategy() {
return renamingStrategy;
/*
* (non-Javadoc)
*
* @see org.springframework.transaction.compensating.support.CompensatingTransactionHolderSupport#getTransactedResource()
*/
protected Object getTransactedResource() {
return ctx;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import java.lang.reflect.Proxy;
@@ -22,7 +22,6 @@ import javax.naming.directory.DirContext;
import org.springframework.ldap.NamingException;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextProxy;
import org.springframework.ldap.transaction.compensating.DirContextHolder;
import org.springframework.ldap.transaction.compensating.LdapTransactionUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -35,7 +34,6 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
* {@link TransactionAwareDirContextInvocationHandler}.
*
* @author Mattias Arthursson
* @since 1.2
*/
public class TransactionAwareContextSourceProxy implements ContextSource {
private ContextSource target;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
@@ -21,10 +21,13 @@ import java.lang.reflect.Method;
import javax.naming.directory.DirContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.NamingException;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.transaction.compensating.DirContextHolder;
import org.springframework.ldap.transaction.compensating.LdapTransactionUtils;
import org.springframework.transaction.compensating.support.CompensatingTransactionUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* Proxy implementation for DirContext, making sure that the instance is not
@@ -32,11 +35,13 @@ import org.springframework.transaction.compensating.support.CompensatingTransact
* storing compensating rollback operations for them.
*
* @author Mattias Arthursson
* @since 1.2
*/
public class TransactionAwareDirContextInvocationHandler implements
InvocationHandler {
private static Log log = LogFactory
.getLog(TransactionAwareDirContextInvocationHandler.class);
private DirContext target;
private ContextSource contextSource;
@@ -75,7 +80,7 @@ public class TransactionAwareDirContextInvocationHandler implements
// Use hashCode of Connection proxy.
return new Integer(proxy.hashCode());
} else if (methodName.equals("close")) {
LdapTransactionUtils.doCloseConnection(target, contextSource);
doCloseConnection(target, contextSource);
return null;
} else if (LdapTransactionUtils
.isSupportedWriteTransactionOperation(methodName)) {
@@ -91,4 +96,30 @@ public class TransactionAwareDirContextInvocationHandler implements
}
}
}
/**
* Close the supplied context, but only if it is not associated with the
* current transaction.
*
* @param context
* the DirContext to close.
* @param contextSource
* the ContextSource bound to the transaction.
* @throws NamingException
*/
void doCloseConnection(DirContext context, ContextSource contextSource)
throws javax.naming.NamingException {
DirContextHolder transactionContextHolder = (DirContextHolder) TransactionSynchronizationManager
.getResource(contextSource);
if (transactionContextHolder == null
|| transactionContextHolder.getCtx() != context) {
log.debug("Closing context");
// This is not the transactional context or the transaction is
// no longer active - we should close it.
context.close();
} else {
log.debug("Leaving transactional context open");
}
}
}

View File

@@ -31,11 +31,14 @@ public interface CompensatingTransactionOperationFactory {
* Create an appropriate {@link CompensatingTransactionOperationRecorder}
* instance corresponding to the supplied method name.
*
* @param resource
* The target transaction resource.
* @param method
* the method name to create a
* {@link CompensatingTransactionOperationRecorder} for.
*
* @return a new {@link CompensatingTransactionOperationRecorder} instance.
*/
public CompensatingTransactionOperationRecorder createRecordingOperation(
String method);
Object resource, String method);
}

View File

@@ -31,12 +31,15 @@ public interface CompensatingTransactionOperationManager {
* operation), performing the operation, and storing the necessary
* information to roll back or commit the performed operation.
*
* @param resource
* the target resource to perform the operation on.
* @param operation
* The method to be invoked.
* @param args
* Arguments supplied to the method.
*/
public void performOperation(String operation, Object[] args);
public void performOperation(Object resource, String operation,
Object[] args);
/**
* Rollback all recorded operations, by performing each of the recorded

View File

@@ -17,7 +17,6 @@ package org.springframework.transaction.compensating.support;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.transaction.core.ContextSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.DefaultTransactionStatus;
@@ -34,7 +33,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
public abstract class AbstractCompensatingTransactionManagerDelegate {
private static Log log = LogFactory
.getLog(ContextSourceTransactionManager.class);
.getLog(AbstractCompensatingTransactionManagerDelegate.class);
/**
* Close the target resource - the implementation specific resource held in

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.transaction.compensating.support;
import org.springframework.ldap.transaction.compensating.LdapCompensatingTransactionOperationFactory;
import org.springframework.transaction.compensating.CompensatingTransactionOperationFactory;
import org.springframework.transaction.compensating.CompensatingTransactionOperationManager;
import org.springframework.transaction.support.ResourceHolderSupport;
@@ -32,23 +30,23 @@ public abstract class CompensatingTransactionHolderSupport extends
private CompensatingTransactionOperationManager transactionOperationManager;
/**
* This method creates a new TransactionOperationManager instance. To be
* called by subclass in case the target object has changed.
* Constructor.
*
* @param manager
* The {@link CompensatingTransactionOperationManager} to use for
* creating Compensating operations.
*/
protected void refreshTransactionOperationManager() {
setTransactionOperationManager(new DefaultCompensatingTransactionOperationManager(
createOperationFactory()));
public CompensatingTransactionHolderSupport(
CompensatingTransactionOperationManager manager) {
this.transactionOperationManager = manager;
}
/**
* Factory method to create a
* {@link CompensatingTransactionOperationFactory} using the settings and
* current state of this object.
* Get the actual transacted resource.
*
* @return a new {@link LdapCompensatingTransactionOperationFactory}
* referncing the current transaction context.
* @return the transaction's target resource
*/
protected abstract CompensatingTransactionOperationFactory createOperationFactory();
protected abstract Object getTransactedResource();
public void clear() {
super.clear();

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.transaction.compensating.support;
import org.springframework.ldap.transaction.compensating.DirContextHolder;
import org.springframework.ldap.transaction.compensating.manager.DirContextHolder;
/**
* Transaction object for ContextSourceTransactionManager. Keeps a reference to

View File

@@ -38,13 +38,15 @@ public class CompensatingTransactionUtils {
*/
public static void performOperation(Object synchronizationKey,
Object target, Method method, Object[] args) throws Throwable {
CompensatingTransactionHolderSupport transactionContextHolder = (CompensatingTransactionHolderSupport) TransactionSynchronizationManager
CompensatingTransactionHolderSupport transactionResourceHolder = (CompensatingTransactionHolderSupport) TransactionSynchronizationManager
.getResource(synchronizationKey);
if (transactionContextHolder != null) {
if (transactionResourceHolder != null) {
CompensatingTransactionOperationManager transactionDataManager = transactionContextHolder
CompensatingTransactionOperationManager transactionOperationManager = transactionResourceHolder
.getTransactionOperationManager();
transactionDataManager.performOperation(method.getName(), args);
transactionOperationManager.performOperation(
transactionResourceHolder.getTransactedResource(), method
.getName(), args);
} else {
// Perform the target operation
try {

View File

@@ -58,12 +58,13 @@ public class DefaultCompensatingTransactionOperationManager implements
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.support.transaction.CompensatingTransactionOperationManager#operationPerformed(java.lang.String,
* java.lang.Object[])
* @see org.springframework.transaction.compensating.CompensatingTransactionOperationManager#performOperation(java.lang.Object,
* java.lang.String, java.lang.Object[])
*/
public void performOperation(String operation, Object[] args) {
public void performOperation(Object resource, String operation,
Object[] args) {
CompensatingTransactionOperationRecorder recorder = operationFactory
.createRecordingOperation(operation);
.createRecordingOperation(resource, operation);
CompensatingTransactionOperationExecutor executor = recorder
.recordOperation(args);

View File

@@ -15,17 +15,12 @@
*/
package org.springframework.ldap.transaction.compensating;
import javax.naming.directory.DirContext;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.transaction.compensating.BindOperationRecorder;
import org.springframework.ldap.transaction.compensating.LdapCompensatingTransactionOperationFactory;
import org.springframework.ldap.transaction.compensating.ModifyAttributesOperationRecorder;
import org.springframework.ldap.transaction.compensating.RebindOperationRecorder;
import org.springframework.ldap.transaction.compensating.RenameOperationRecorder;
import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy;
import org.springframework.ldap.transaction.compensating.UnbindOperationRecorder;
import org.springframework.transaction.compensating.CompensatingTransactionOperationRecorder;
public class LdapCompensatingTransactionOperationFactoryTest extends TestCase {
@@ -37,6 +32,12 @@ public class LdapCompensatingTransactionOperationFactoryTest extends TestCase {
private TempEntryRenamingStrategy renamingStrategyMock;
private MockControl dirContextControl;
private DirContext dirContextMock;
private LdapCompensatingTransactionOperationFactory tested;
protected void setUp() throws Exception {
ldapOperationsControl = MockControl.createControl(LdapOperations.class);
ldapOperationsMock = (LdapOperations) ldapOperationsControl.getMock();
@@ -46,6 +47,17 @@ public class LdapCompensatingTransactionOperationFactoryTest extends TestCase {
renamingStrategyMock = (TempEntryRenamingStrategy) renamingStrategyControl
.getMock();
dirContextControl = MockControl.createControl(DirContext.class);
dirContextMock = (DirContext) dirContextControl.getMock();
tested = new LdapCompensatingTransactionOperationFactory(
renamingStrategyMock) {
LdapOperations createLdapOperationsInstance(DirContext ctx) {
assertEquals(dirContextMock, ctx);
return ldapOperationsMock;
}
};
}
protected void tearDown() throws Exception {
@@ -54,25 +66,29 @@ public class LdapCompensatingTransactionOperationFactoryTest extends TestCase {
renamingStrategyControl = null;
renamingStrategyMock = null;
dirContextControl = null;
dirContextMock = null;
tested = null;
}
protected void replay() {
ldapOperationsControl.replay();
renamingStrategyControl.replay();
dirContextControl.replay();
}
protected void verify() {
ldapOperationsControl.verify();
renamingStrategyControl.verify();
dirContextControl.verify();
}
public void testGetRecordingOperation_Bind() throws Exception {
LdapCompensatingTransactionOperationFactory tested = new LdapCompensatingTransactionOperationFactory(
null, renamingStrategyMock);
tested.setLdapOperations(ldapOperationsMock);
CompensatingTransactionOperationRecorder result = tested
.createRecordingOperation("bind");
.createRecordingOperation(dirContextMock, "bind");
assertTrue(result instanceof BindOperationRecorder);
BindOperationRecorder bindOperationRecorder = (BindOperationRecorder) result;
assertSame(ldapOperationsMock, bindOperationRecorder
@@ -80,12 +96,8 @@ public class LdapCompensatingTransactionOperationFactoryTest extends TestCase {
}
public void testGetRecordingOperation_Rebind() throws Exception {
LdapCompensatingTransactionOperationFactory tested = new LdapCompensatingTransactionOperationFactory(
null, renamingStrategyMock);
tested.setLdapOperations(ldapOperationsMock);
CompensatingTransactionOperationRecorder result = tested
.createRecordingOperation("rebind");
.createRecordingOperation(dirContextMock, "rebind");
assertTrue(result instanceof RebindOperationRecorder);
RebindOperationRecorder rebindOperationRecorder = (RebindOperationRecorder) result;
assertSame(ldapOperationsMock, rebindOperationRecorder
@@ -95,41 +107,28 @@ public class LdapCompensatingTransactionOperationFactoryTest extends TestCase {
}
public void testGetRecordingOperation_Rename() throws Exception {
LdapCompensatingTransactionOperationFactory tested = new LdapCompensatingTransactionOperationFactory(
null, renamingStrategyMock);
tested.setLdapOperations(ldapOperationsMock);
CompensatingTransactionOperationRecorder result = tested
.createRecordingOperation("rename");
.createRecordingOperation(dirContextMock, "rename");
assertTrue(result instanceof RenameOperationRecorder);
RenameOperationRecorder recordingOperation = (RenameOperationRecorder) result;
assertSame(ldapOperationsMock, recordingOperation.getLdapOperations());
}
public void testGetRecordingOperation_ModifyAttributes() throws Exception {
LdapCompensatingTransactionOperationFactory tested = new LdapCompensatingTransactionOperationFactory(
null, renamingStrategyMock);
tested.setLdapOperations(ldapOperationsMock);
CompensatingTransactionOperationRecorder result = tested
.createRecordingOperation("modifyAttributes");
.createRecordingOperation(dirContextMock, "modifyAttributes");
assertTrue(result instanceof ModifyAttributesOperationRecorder);
ModifyAttributesOperationRecorder recordingOperation = (ModifyAttributesOperationRecorder) result;
assertSame(ldapOperationsMock, recordingOperation.getLdapOperations());
}
public void testGetRecordingOperation_Unbind() throws Exception {
LdapCompensatingTransactionOperationFactory tested = new LdapCompensatingTransactionOperationFactory(
null, renamingStrategyMock);
tested.setLdapOperations(ldapOperationsMock);
CompensatingTransactionOperationRecorder result = tested
.createRecordingOperation("unbind");
.createRecordingOperation(dirContextMock, "unbind");
assertTrue(result instanceof UnbindOperationRecorder);
UnbindOperationRecorder recordingOperation = (UnbindOperationRecorder) result;
assertSame(ldapOperationsMock, recordingOperation.getLdapOperations());
assertSame(renamingStrategyMock, recordingOperation
.getRenamingStrategy());
}
}

View File

@@ -21,11 +21,7 @@ import javax.naming.directory.DirContext;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.ldap.transaction.compensating.DirContextHolder;
import org.springframework.ldap.transaction.compensating.LdapTransactionUtils;
import org.springframework.transaction.compensating.support.CompensatingTransactionHolderSupport;
import org.springframework.transaction.support.TransactionSynchronizationManager;
public class LdapTransactionUtilsTest extends TestCase {
@@ -34,17 +30,10 @@ public class LdapTransactionUtilsTest extends TestCase {
private DirContext dirContextMock;
private MockControl contextSourceControl;
private ContextSource contextSourceMock;
protected void setUp() throws Exception {
dirContextControl = MockControl.createControl(DirContext.class);
dirContextMock = (DirContext) dirContextControl.getMock();
contextSourceControl = MockControl.createControl(ContextSource.class);
contextSourceMock = (ContextSource) contextSourceControl.getMock();
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.clearSynchronization();
}
@@ -53,19 +42,14 @@ public class LdapTransactionUtilsTest extends TestCase {
protected void tearDown() throws Exception {
dirContextControl = null;
dirContextMock = null;
contextSourceControl = null;
contextSourceMock = null;
}
protected void replay() {
dirContextControl.replay();
contextSourceControl.replay();
}
protected void verify() {
dirContextControl.verify();
contextSourceControl.verify();
}
public void testCloseContext() throws NamingException {
@@ -82,51 +66,6 @@ public class LdapTransactionUtilsTest extends TestCase {
verify();
}
public void testDoCloseConnection_NoTransaction() throws NamingException {
dirContextMock.close();
replay();
LdapTransactionUtils.doCloseConnection(dirContextMock,
contextSourceMock);
verify();
}
public void testDoCloseConnection_ActiveTransaction()
throws NamingException {
CompensatingTransactionHolderSupport holder = new DirContextHolder(
dirContextMock, null);
TransactionSynchronizationManager.bindResource(contextSourceMock,
holder);
// Context should not be closed.
replay();
LdapTransactionUtils.doCloseConnection(dirContextMock,
contextSourceMock);
verify();
}
public void testDoCloseConnection_NotTransactionalContext()
throws NamingException {
CompensatingTransactionHolderSupport holder = new DirContextHolder(
dirContextMock, null);
TransactionSynchronizationManager.bindResource(contextSourceMock,
holder);
MockControl dirContextControl2 = MockControl
.createControl(DirContext.class);
DirContext dirContextMock2 = (DirContext) dirContextControl2.getMock();
dirContextMock2.close();
dirContextControl2.replay();
replay();
LdapTransactionUtils.doCloseConnection(dirContextMock2,
contextSourceMock);
verify();
dirContextControl2.verify();
}
public void testIsSupportedWriteTransactionOperation() {
assertTrue(LdapTransactionUtils
.isSupportedWriteTransactionOperation("bind"));

View File

@@ -1,19 +1,4 @@
/*
* Copyright 2005-2007 the original author or authors.
*
* 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.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import javax.naming.directory.DirContext;
@@ -21,10 +6,9 @@ import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.transaction.compensating.DirContextHolder;
import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy;
import org.springframework.ldap.transaction.core.ContextSourceTransactionManager;
import org.springframework.ldap.transaction.core.TransactionAwareContextSourceProxy;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager;
import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.compensating.CompensatingTransactionOperationManager;
import org.springframework.transaction.compensating.support.CompensatingTransactionHolderSupport;
@@ -121,8 +105,8 @@ public class ContextSourceTransactionManagerTest extends TestCase {
}
public void testDoGetTransactionTransactionActive() {
CompensatingTransactionHolderSupport expectedContextHolder = new DirContextHolder(null,
null);
CompensatingTransactionHolderSupport expectedContextHolder = new DirContextHolder(
null, null);
TransactionSynchronizationManager.bindResource(contextSourceMock,
expectedContextHolder);
Object result = tested.doGetTransaction();
@@ -145,8 +129,6 @@ public class ContextSourceTransactionManagerTest extends TestCase {
DirContextHolder foundContextHolder = (DirContextHolder) TransactionSynchronizationManager
.getResource(contextSourceMock);
assertSame(contextMock, foundContextHolder.getCtx());
assertSame(renamingStrategyMock, foundContextHolder
.getRenamingStrategy());
}
public void testDoCommit() {
@@ -154,8 +136,8 @@ public class ContextSourceTransactionManagerTest extends TestCase {
public void testDoRollback() {
DirContextHolder expectedContextHolder = new DirContextHolder(
contextMock, renamingStrategyMock);
DirContextHolder expectedContextHolder = new DirContextHolder(null,
contextMock);
expectedContextHolder
.setTransactionOperationManager(transactionDataManagerMock);
TransactionSynchronizationManager.bindResource(contextSourceMock,
@@ -172,8 +154,8 @@ public class ContextSourceTransactionManagerTest extends TestCase {
}
public void testDoCleanupAfterCompletion() throws Exception {
DirContextHolder expectedContextHolder = new DirContextHolder(
contextMock, renamingStrategyMock);
DirContextHolder expectedContextHolder = new DirContextHolder(null,
contextMock);
TransactionSynchronizationManager.bindResource(contextSourceMock,
expectedContextHolder);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2007 the original author or authors.
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.transaction.core;
package org.springframework.ldap.transaction.compensating.manager;
import javax.naming.directory.DirContext;
import javax.naming.ldap.LdapContext;
@@ -23,7 +23,7 @@ import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextProxy;
import org.springframework.ldap.transaction.core.TransactionAwareContextSourceProxy;
import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy;
/**
* Tests for {@link TransactionAwareContextSourceProxy}.

View File

@@ -0,0 +1,113 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* 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.springframework.ldap.transaction.compensating.manager;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.transaction.compensating.manager.TransactionAwareDirContextInvocationHandler;
import org.springframework.transaction.support.TransactionSynchronizationManager;
public class TransactionAwareDirContextInvocationHandlerTest extends TestCase {
private MockControl contextSourceControl;
private ContextSource contextSourceMock;
private MockControl dirContextControl;
private DirContext dirContextMock;
private TransactionAwareDirContextInvocationHandler tested;
private DirContextHolder holder;
protected void setUp() throws Exception {
dirContextControl = MockControl.createControl(DirContext.class);
dirContextMock = (DirContext) dirContextControl.getMock();
contextSourceControl = MockControl.createControl(ContextSource.class);
contextSourceMock = (ContextSource) contextSourceControl.getMock();
holder = new DirContextHolder(null, dirContextMock);
tested = new TransactionAwareDirContextInvocationHandler(null, null);
}
protected void tearDown() throws Exception {
dirContextControl = null;
dirContextMock = null;
contextSourceControl = null;
contextSourceMock = null;
holder = null;
tested = null;
}
protected void replay() {
dirContextControl.replay();
contextSourceControl.replay();
}
protected void verify() {
dirContextControl.verify();
contextSourceControl.verify();
}
public void testDoCloseConnection_NoTransaction() throws NamingException {
dirContextMock.close();
replay();
tested.doCloseConnection(dirContextMock, contextSourceMock);
verify();
}
public void testDoCloseConnection_ActiveTransaction()
throws NamingException {
TransactionSynchronizationManager.bindResource(contextSourceMock,
holder);
// Context should not be closed.
replay();
tested.doCloseConnection(dirContextMock, contextSourceMock);
verify();
}
public void testDoCloseConnection_NotTransactionalContext()
throws NamingException {
TransactionSynchronizationManager.bindResource(contextSourceMock,
holder);
MockControl dirContextControl2 = MockControl
.createControl(DirContext.class);
DirContext dirContextMock2 = (DirContext) dirContextControl2.getMock();
dirContextMock2.close();
dirContextControl2.replay();
replay();
tested.doCloseConnection(dirContextMock2, contextSourceMock);
verify();
dirContextControl2.verify();
}
}

View File

@@ -23,7 +23,7 @@ import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.transaction.compensating.DirContextHolder;
import org.springframework.ldap.transaction.compensating.manager.DirContextHolder;
import org.springframework.transaction.compensating.CompensatingTransactionOperationManager;
import org.springframework.transaction.compensating.support.CompensatingTransactionHolderSupport;
import org.springframework.transaction.compensating.support.CompensatingTransactionUtils;
@@ -83,20 +83,21 @@ public class CompensatingTransactionUtilsTest extends TestCase {
operationManagerControl.verify();
}
public void testPerformOperation() throws Throwable {
CompensatingTransactionHolderSupport holder = new DirContextHolder(dirContextMock, null);
CompensatingTransactionHolderSupport holder = new DirContextHolder(
null, dirContextMock);
holder.setTransactionOperationManager(operationManagerMock);
TransactionSynchronizationManager.bindResource(contextSourceMock,
holder);
Object[] expectedArgs = new Object[] { "someDn" };
operationManagerMock.performOperation("unbind", expectedArgs);
operationManagerMock.performOperation(dirContextMock, "unbind",
expectedArgs);
replay();
CompensatingTransactionUtils.performOperation(contextSourceMock, dirContextMock,
getUnbindMethod(), expectedArgs);
CompensatingTransactionUtils.performOperation(contextSourceMock,
dirContextMock, getUnbindMethod(), expectedArgs);
verify();
}
@@ -105,8 +106,8 @@ public class CompensatingTransactionUtilsTest extends TestCase {
dirContextMock.unbind("someDn");
replay();
CompensatingTransactionUtils.performOperation(contextSourceMock, dirContextMock,
getUnbindMethod(), expectedArgs);
CompensatingTransactionUtils.performOperation(contextSourceMock,
dirContextMock, getUnbindMethod(), expectedArgs);
verify();
}

View File

@@ -86,11 +86,11 @@ public class DefaultCompensatingTransactionOperationManagerTest extends
public void testPerformOperation() {
Object[] expectedArgs = new Object[0];
Object expectedResource = new Object();
operationFactoryControl
.expectAndReturn(operationFactoryMock
.createRecordingOperation("some method"),
operationRecorderMock);
operationFactoryControl.expectAndReturn(operationFactoryMock
.createRecordingOperation(expectedResource, "some method"),
operationRecorderMock);
operationRecorderControl.expectAndReturn(operationRecorderMock
.recordOperation(expectedArgs), operationExecutorMock);
operationExecutorMock.performOperation();
@@ -98,7 +98,7 @@ public class DefaultCompensatingTransactionOperationManagerTest extends
DefaultCompensatingTransactionOperationManager tested = new DefaultCompensatingTransactionOperationManager(
operationFactoryMock);
replay();
tested.performOperation("some method", expectedArgs);
tested.performOperation(expectedResource, "some method", expectedArgs);
verify();
Stack result = tested.getOperationExecutors();