Skip to content

Instantly share code, notes, and snippets.

@talios
Created May 4, 2010
Embed
What would you like to do?
} else if (isSuccess && method.equals(Method.DELETE)) {//doing an delete
performAfterCommit(context, new Runnable() {
@Override
public void run() {
Domain domainCM = (Domain) attributes.get("domainToDelete"); //hack todo
deleteDomain(domainCM);
}
});
}
}
public class LdapGuard extends BaseGuard {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(LdapGuard.class);
protected void performAfterCommit(@NotNull AdviceContext context, @NotNull Runnable runnable) {
try {
final String transactionName = context.getUri() + ":" + UUID.randomUUID().toString();
log.warn("Scheduling transaction resource: " + transactionName);
new UserTransactionManager().getTransaction().enlistResource(
new LdapResource(getLdapUtil(), transactionName, runnable));
} catch (java.lang.IllegalStateException e) {
log.warn(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
public class LdapResource implements XAResource {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(LdapResource.class);
private LdapUtil ldapUtil;
private String id;
private Runnable runnable;
public LdapResource(LdapUtil ldapUtil, String id, Runnable runnable) {
this.ldapUtil = ldapUtil;
this.id = id;
this.runnable = runnable;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LdapResource that = (LdapResource) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
@Override
public void commit(Xid xid, boolean b) throws XAException {
try {
runInTransaction(runnable);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new XAException(XAException.XA_RBROLLBACK);
}
}
@Override
public boolean isSameRM(XAResource res) throws XAException {
return (res instanceof LdapResource)
&& (id.equals(((LdapResource) res).id));
}
@Override
public int prepare(Xid xid) throws XAException {
if (ldapUtil == null || !ldapUtil.isLdapServerEnabled()) {
throw new XAException(XAException.XA_RBROLLBACK);
}
try {
ldapUtil.getTemplate().lookup("ou=smx,o=smx");
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new XAException(XAException.XA_RBROLLBACK);
}
return XAResource.XA_OK;
}
@Override
public void end(Xid xid, int i) throws XAException {
}
@Override
public void forget(Xid xid) throws XAException {
}
@Override
public int getTransactionTimeout() throws XAException {
return 0;
}
@Override
public Xid[] recover(int i) throws XAException {
return new Xid[0];
}
@Override
public void rollback(Xid xid) throws XAException {
}
@Override
public boolean setTransactionTimeout(int i) throws XAException {
return false;
}
@Override
public void start(Xid xid, int i) throws XAException {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment