{
log.trace("Starting work " + this);
}
// Transaction setup
ExecutionContext ctx = getWorkContext(TransactionContext.class);
if (ctx == null)
{
ctx = getExecutionContext();
}
if (ctx != null)
{
Xid xid = ctx.getXid();
if (xid != null)
{
//JBAS-4002 base value is in seconds as per the API, here we convert to millis
long timeout = (ctx.getTransactionTimeout() * 1000);
workManager.getXATerminator().registerWork(work, xid, timeout);
}
}
// Security setup
javax.resource.spi.work.SecurityContext securityContext =
getWorkContext(javax.resource.spi.work.SecurityContext.class);
if (securityContext != null && workManager.getCallbackSecurity() != null)
{
try
{
org.jboss.security.SecurityContext sc =
SecurityContextFactory.createSecurityContext(workManager.getCallbackSecurity().getDomain());
SecurityContextAssociation.setSecurityContext(sc);
// Setup callbacks
CallbackHandler cbh = new JASPICallbackHandler();
List<Callback> callbacks = new ArrayList<Callback>();
Set<String> users = workManager.getCallbackSecurity().getUsers();
if (users != null && users.size() > 0)
{
for (String user : users)
{
Subject subject = new Subject();
Principal principal = new SimplePrincipal(user);
char[] cred = workManager.getCallbackSecurity().getCredential(user);
String[] roles = workManager.getCallbackSecurity().getRoles(user);
GroupPrincipalCallback gpc = new GroupPrincipalCallback(subject, roles);
CallerPrincipalCallback cpc = new CallerPrincipalCallback(subject, principal);
PasswordValidationCallback pvc = new PasswordValidationCallback(subject, principal.getName(), cred);
callbacks.add(gpc);
callbacks.add(cpc);
callbacks.add(pvc);
}
}
else
{
if (log.isDebugEnabled())
log.debug("No users defined");
}
Callback[] cb = new Callback[callbacks.size()];
cbh.handle(callbacks.toArray(cb));
// Subjects for execution environment
Subject executionSubject = new Subject();
Subject serviceSubject = null;
// Resource adapter callback
securityContext.setupSecurityContext(cbh, executionSubject, serviceSubject);
// Set the authenticated subject
sc.getSubjectInfo().setAuthenticatedSubject(executionSubject);
}
catch (Throwable t)
{
log.securityContextSetupFailed(t.getMessage(), t);
fireWorkContextSetupFailed(ctx);
throw new WorkException(bundle.securityContextSetupFailed(t.getMessage()), t);
}
}
else if (securityContext != null && workManager.getCallbackSecurity() == null)
{
log.securityContextSetupFailedCallbackSecurityNull();
fireWorkContextSetupFailed(ctx);
throw new WorkException(bundle.securityContextSetupFailedSinceCallbackSecurityWasNull());
}
//Fires Context setup complete
fireWorkContextSetupComplete(ctx);
if (ctx != null)
{
Xid xid = ctx.getXid();
if (xid != null)
{
workManager.getXATerminator().startWork(work, xid);
}
}