class JaasUtils {

    public static <P extends Principal> P getPrincipal(final Class<P> principalClass) {
        if (principalClass == null)
            throw new IllegalArgumentException("Principal class cannot be null.");

        final Set<P> principals = getCurrentSubject().getPrincipals(principalClass);
        if (principals.size() != 1)
            throw new IllegalArgumentException("Incorrect number of principals of type "
                    + principalClass.getCanonicalName() + " on subject: " + Integer.toString(principals.size()));

        return principals.iterator().next();
    }

}