UI Spring Context Holder bean registration (#1026)

* register the Spring Context Holder bean for UI instead of setting the context for each UI separately
* reverted test scope for db driver

Signed-off-by: Bogdan Bondar <Bogdan.Bondar@bosch.io>
This commit is contained in:
Bondar Bogdan
2020-10-13 17:04:18 +02:00
committed by GitHub
parent a092747c99
commit 541a0129ef
8 changed files with 45 additions and 45 deletions

View File

@@ -20,6 +20,7 @@ import org.eclipse.hawkbit.ui.push.HawkbitEventPermissionChecker;
import org.eclipse.hawkbit.ui.push.HawkbitEventProvider;
import org.eclipse.hawkbit.ui.push.UIEventPermissionChecker;
import org.eclipse.hawkbit.ui.push.UIEventProvider;
import org.eclipse.hawkbit.ui.utils.SpringContextHolder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ConfigurableApplicationContext;
@@ -81,6 +82,16 @@ public class MgmtUiAutoConfiguration {
return new HawkbitEventPermissionChecker(permChecker);
}
/**
* Bean for creating a singleton instance of the {@link SpringContextHolder}
*
* @return the singleton instance of the {@link SpringContextHolder}
*/
@Bean
SpringContextHolder springContextHolder() {
return SpringContextHolder.getInstance();
}
/**
* The UI scoped event push strategy. Session scope is necessary, that every
* UI has an own strategy.

View File

@@ -35,7 +35,7 @@ public final class EventPublisherHolder {
}
/**
* @return the cache manager holder singleton instance
* @return the event publisher holder singleton instance
*/
public static EventPublisherHolder getInstance() {
return SINGLETON;

View File

@@ -18,7 +18,6 @@ import org.eclipse.hawkbit.ui.push.UIEventProvider;
import org.eclipse.hawkbit.ui.themes.HawkbitTheme;
import org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil;
import org.eclipse.hawkbit.ui.utils.SPDateTimeUtil;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.eclipse.hawkbit.ui.utils.VaadinMessageSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -104,8 +103,6 @@ public abstract class AbstractHawkbitUI extends UI implements DetachListener {
pushStrategy.clean();
clearContextListener();
}
SpringContextHelper.clearContext();
}
private void clearContextListener() {
@@ -127,7 +124,6 @@ public abstract class AbstractHawkbitUI extends UI implements DetachListener {
pushStrategy.init(getUI());
}
addDetachListener(this);
SpringContextHelper.setContext(context);
Responsive.makeResponsive(this);
addStyleName(ValoTheme.UI_WITH_MENU);

View File

@@ -33,7 +33,7 @@ import org.eclipse.hawkbit.ui.common.data.proxies.ProxySoftwareModule;
import org.eclipse.hawkbit.ui.common.event.EntityModifiedEventPayload;
import org.eclipse.hawkbit.ui.common.event.EntityModifiedEventPayload.EntityModifiedEventType;
import org.eclipse.hawkbit.ui.common.event.EventTopics;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.eclipse.hawkbit.ui.utils.SpringContextHolder;
import org.eclipse.hawkbit.ui.utils.UINotification;
import org.eclipse.hawkbit.ui.utils.VaadinMessageSource;
import org.slf4j.Logger;
@@ -78,9 +78,9 @@ public abstract class AbstractFileTransferHandler implements Serializable {
final Lock uploadLock) {
this.artifactManagement = artifactManagement;
this.i18n = i18n;
this.eventBus = SpringContextHelper.getBean(EventBus.UIEventBus.class);
this.artifactUploadState = SpringContextHelper.getBean(ArtifactUploadState.class);
this.uiNotification = SpringContextHelper.getBean(UINotification.class);
this.eventBus = SpringContextHolder.getInstance().getBean(EventBus.UIEventBus.class);
this.artifactUploadState = SpringContextHolder.getInstance().getBean(ArtifactUploadState.class);
this.uiNotification = SpringContextHolder.getInstance().getBean(UINotification.class);
this.uploadLock = uploadLock;
}
@@ -103,8 +103,9 @@ public abstract class AbstractFileTransferHandler implements Serializable {
protected void startTransferToRepositoryThread(final InputStream inputStream, final FileUploadId fileUploadId,
final String mimeType) {
SpringContextHelper.getBean("uiExecutor", Executor.class).execute(new TransferArtifactToRepositoryRunnable(
inputStream, fileUploadId, mimeType, UI.getCurrent(), uploadLock));
SpringContextHolder.getInstance().getBean("uiExecutor", Executor.class)
.execute(new TransferArtifactToRepositoryRunnable(inputStream, fileUploadId, mimeType, UI.getCurrent(),
uploadLock));
}
private void interruptUploadAndSetReason(final String failureReason) {
@@ -241,15 +242,15 @@ public abstract class AbstractFileTransferHandler implements Serializable {
* Constructor for TransferArtifactToRepositoryRunnable
*
* @param inputStream
* InputStream
* InputStream
* @param fileUploadId
* FileUploadId
* FileUploadId
* @param mimeType
* String
* String
* @param vaadinUi
* UI
* UI
* @param uploadLock
* Lock
* Lock
*/
public TransferArtifactToRepositoryRunnable(final InputStream inputStream, final FileUploadId fileUploadId,
final String mimeType, final UI vaadinUi, final Lock uploadLock) {

View File

@@ -15,7 +15,7 @@ import org.apache.commons.lang3.StringUtils;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.im.authentication.UserPrincipal;
import org.eclipse.hawkbit.repository.model.BaseEntity;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.eclipse.hawkbit.ui.utils.SpringContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.userdetails.User;
@@ -179,10 +179,10 @@ public final class UserDetailsFormatter {
public static UserDetails getCurrentUser() {
final SecurityContext context = (SecurityContext) VaadinService.getCurrentRequest().getWrappedSession()
.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
Authentication authentication = context.getAuthentication();
final Authentication authentication = context.getAuthentication();
if (authentication instanceof OAuth2AuthenticationToken) {
OidcUser oidcUser = (OidcUser) authentication.getPrincipal();
Object details = authentication.getDetails();
final OidcUser oidcUser = (OidcUser) authentication.getPrincipal();
final Object details = authentication.getDetails();
String tenant = "DEFAULT";
if (details instanceof TenantAwareAuthenticationDetails) {
tenant = ((TenantAwareAuthenticationDetails) details).getTenant();
@@ -207,7 +207,8 @@ public final class UserDetailsFormatter {
// Exception squid:S1166 - exception has to be hidden
@SuppressWarnings({ "squid:S1166" })
private static UserDetails loadUserByUsername(final String username) {
final UserDetailsService userDetailsService = SpringContextHelper.getBean(UserDetailsService.class);
final UserDetailsService userDetailsService = SpringContextHolder.getInstance()
.getBean(UserDetailsService.class);
try {
return userDetailsService.loadUserByUsername(username);
} catch (final UsernameNotFoundException e) {

View File

@@ -17,7 +17,7 @@ import javax.validation.ConstraintViolationException;
import org.eclipse.hawkbit.ui.common.notification.ParallelNotification;
import org.eclipse.hawkbit.ui.utils.SPUIStyleDefinitions;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.eclipse.hawkbit.ui.utils.SpringContextHolder;
import org.eclipse.hawkbit.ui.utils.UINotification;
import org.eclipse.hawkbit.ui.utils.VaadinMessageSource;
import org.slf4j.Logger;
@@ -106,7 +106,7 @@ public class HawkbitUIErrorHandler extends DefaultErrorHandler {
LOG.error("Error in UI: ", ex);
final String errorMessage = extractMessageFrom(ex);
final VaadinMessageSource i18n = SpringContextHelper.getBean(VaadinMessageSource.class);
final VaadinMessageSource i18n = SpringContextHolder.getInstance().getBean(VaadinMessageSource.class);
return buildErrorNotification(i18n.getMessage("caption.error"), errorMessage);
}

View File

@@ -21,7 +21,6 @@ import org.eclipse.hawkbit.ui.components.SPUIComponentProvider;
import org.eclipse.hawkbit.ui.themes.HawkbitTheme;
import org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil;
import org.eclipse.hawkbit.ui.utils.SPUIStyleDefinitions;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.eclipse.hawkbit.ui.utils.UIComponentIdProvider;
import org.eclipse.hawkbit.ui.utils.UINotification;
import org.eclipse.hawkbit.ui.utils.VaadinMessageSource;
@@ -117,7 +116,6 @@ public abstract class AbstractHawkbitLoginUI extends UI {
@Override
protected void init(final VaadinRequest request) {
HawkbitCommonUtil.initLocalization(this, uiProperties.getLocalization(), i18n);
SpringContextHelper.setContext(context);
params = UriComponentsBuilder.fromUri(Page.getCurrent().getLocation()).build().getQueryParams();

View File

@@ -8,30 +8,31 @@
*/
package org.eclipse.hawkbit.ui.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
/**
* Singleton for the spring application context.
*/
public final class SpringContextHelper {
public final class SpringContextHolder {
private static ApplicationContext context;
private static final SpringContextHolder SINGLETON = new SpringContextHolder();
@Autowired
private ApplicationContext context;
/**
* Private Constructor.
*/
private SpringContextHelper() {
private SpringContextHolder() {
// Utility class
}
/**
* method to set application context.
*
* @param context
* ApplicationContext
* @return the spring context holder singleton instance
*/
public static void setContext(final ApplicationContext context) {
SpringContextHelper.context = context;
public static SpringContextHolder getInstance() {
return SINGLETON;
}
/**
@@ -42,7 +43,7 @@ public final class SpringContextHelper {
* application context
* @return the requested bean
*/
public static Object getBean(final String beanName) {
public Object getBean(final String beanName) {
return context.getBean(beanName);
}
@@ -54,7 +55,7 @@ public final class SpringContextHelper {
* application context
* @return the requested bean
*/
public static <T> T getBean(final Class<T> beanClazz) {
public <T> T getBean(final Class<T> beanClazz) {
return context.getBean(beanClazz);
}
@@ -69,15 +70,7 @@ public final class SpringContextHelper {
* application context
* @return the requested bean
*/
public static <T> T getBean(final String beanName, final Class<T> beanClazz) {
public <T> T getBean(final String beanName, final Class<T> beanClazz) {
return context.getBean(beanName, beanClazz);
}
/**
* method to clear application context.
*
*/
public static void clearContext() {
context = null;
}
}