Some clean code refactorings

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-05-20 08:41:47 +02:00
parent 79d604ca0b
commit 27ae067758
2 changed files with 10 additions and 6 deletions

View File

@@ -76,4 +76,4 @@ public class DefaultProtocolProperties implements ProtocolProperties {
public void setPort(final String port) { public void setPort(final String port) {
this.port = port; this.port = port;
} }
} }

View File

@@ -29,12 +29,12 @@ import org.springframework.stereotype.Service;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
/** /**
* * A Service which provide to run system code.
*/ */
@Service @Service
public class SystemSecurityContext { public class SystemSecurityContext {
private static final Logger logger = LoggerFactory.getLogger(SystemSecurityContext.class); private static final Logger LOGGER = LoggerFactory.getLogger(SystemSecurityContext.class);
private final TenantAware tenantAware; private final TenantAware tenantAware;
@@ -67,19 +67,20 @@ public class SystemSecurityContext {
public <T> T runAsSystem(final Callable<T> callable) { public <T> T runAsSystem(final Callable<T> callable) {
final SecurityContext oldContext = SecurityContextHolder.getContext(); final SecurityContext oldContext = SecurityContextHolder.getContext();
try { try {
logger.debug("entering system code execution"); LOGGER.debug("entering system code execution");
return tenantAware.runAsTenant(tenantAware.getCurrentTenant(), () -> { return tenantAware.runAsTenant(tenantAware.getCurrentTenant(), () -> {
try { try {
setSystemContext(); setSystemContext();
return callable.call(); return callable.call();
} catch (final Exception e) { // The callable API throws a Exception ant to specific
} catch (@SuppressWarnings("squid:S2221") final Exception e) {
throw Throwables.propagate(e); throw Throwables.propagate(e);
} }
}); });
} finally { } finally {
SecurityContextHolder.setContext(oldContext); SecurityContextHolder.setContext(oldContext);
logger.debug("leaving system code execution"); LOGGER.debug("leaving system code execution");
} }
} }
@@ -97,6 +98,9 @@ public class SystemSecurityContext {
SecurityContextHolder.setContext(securityContextImpl); SecurityContextHolder.setContext(securityContextImpl);
} }
/**
* Authentication with the system role.
*/
public static class SystemCodeAuthentication implements Authentication { public static class SystemCodeAuthentication implements Authentication {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;