SecurityIdentity si = null;
SecurityContext sc = SecurityActions.getSecurityContext();
SecurityContext invSC = (SecurityContext) invocation.getMetaData("security","context");
SecurityDomain domain = (SecurityDomain)container.resolveAnnotation(SecurityDomain.class);
boolean domainExists = domain != null && domain.value() != null
&& domain.value().length() > 0;
/**
* TODO: Decide if you want to allow zero security based on non-availability
* of a security domain, as per the configuration on the container
*/
if(domainExists)
{
Principal p = null;
Object cred = null;
//There is no security context at all
if(sc == null && invSC == null)
{
sc = SecurityActions.createSecurityContext(domain.value());
SecurityActions.setSecurityContext(sc);
}
if(shelper.isLocalCall(mi))
{
if(sc == null)
throw new IllegalStateException("Security Context null on Local call");
si = sc.getUtil().getSecurityIdentity();
}
else
{
if(invSC == null && sc == null)
throw new IllegalStateException("Security Context is not available");
//If there was a SecurityContext over the invocation, that takes preference
if(invSC != null)
{
sc = invSC;
p = sc.getUtil().getUserPrincipal();
cred = sc.getUtil().getCredential();
String unprefixed = SecurityUtil.unprefixSecurityDomain(domain.value());
sc = SecurityActions.createSecurityContext(p,
cred, null, unprefixed);
//Set the security context
SecurityActions.setSecurityContext(sc);