WorkEvent event = new WorkEvent(workManager, WorkEvent.WORK_STARTED, work, null, duration);
workListener.workStarted(event);
}
// 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)
{
if (trace)
log.tracef("Setting security context: %s", securityContext);
try
{
// Security context
org.jboss.security.SecurityContext sc = null;
// Setup callbacks
CallbackHandler cbh = new JASPICallbackHandler();
// Subjects for execution environment
Subject executionSubject = null;
Subject serviceSubject = null;
if (trace)
log.tracef("Callback security: %s", workManager.getCallbackSecurity());
if (SecurityContextAssociation.getSecurityContext() == null ||
workManager.getCallbackSecurity().getDomain() != null)
{
String scDomain = workManager.getCallbackSecurity().getDomain();
if (trace)
log.tracef("Creating security context: %s", scDomain);
if (scDomain == null || scDomain.trim().equals(""))
{
fireWorkContextSetupFailed(ctx);
throw new WorkException(bundle.securityContextSetupFailedSinceCallbackSecurityDomainWasEmpty());
}
sc = SecurityContextFactory.createSecurityContext(scDomain);
SecurityContextAssociation.setSecurityContext(sc);
}
else
{
sc = SecurityContextAssociation.getSecurityContext();
if (trace)
log.tracef("Using security context: %s", sc);
}
executionSubject = sc.getSubjectInfo().getAuthenticatedSubject();
if (executionSubject == null)
{
if (trace)
log.tracef("Creating empty subject");
executionSubject = new Subject();
}
// Resource adapter callback
securityContext.setupSecurityContext(cbh, executionSubject, serviceSubject);
List<Callback> callbacks = new ArrayList<Callback>();
if (workManager.getCallbackSecurity().isMappingRequired())
{
// JCA 1.6: 16.4.4
}
if (workManager.getCallbackSecurity().getDefaultPrincipal() != null)
{
Principal defaultPrincipal = workManager.getCallbackSecurity().getDefaultPrincipal();
CallerPrincipalCallback cpc =
new CallerPrincipalCallback(executionSubject, defaultPrincipal);
callbacks.add(cpc);
}
if (workManager.getCallbackSecurity().getDefaultGroups() != null)
{
String[] defaultGroups = workManager.getCallbackSecurity().getDefaultGroups();
GroupPrincipalCallback gpc =
new GroupPrincipalCallback(executionSubject, defaultGroups);
callbacks.add(gpc);
}
if (callbacks.size() > 0)
{
Callback[] cb = new Callback[callbacks.size()];
cbh.handle(callbacks.toArray(cb));
}
if (trace)
log.tracef("Setting authenticated subject (%s) on security context (%s)", executionSubject, sc);
// 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());
}
if (ctx != null)
{
Xid xid = ctx.getXid();
if (xid != null)
{
workManager.getXATerminator().startWork(work, xid);
}
}