io.convergence_platform.services.initialization.ServiceInitializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.services.initialization;
import io.convergence_platform.common.database.IDatabaseMigratorService;
import io.convergence_platform.common.responses.ApiResponse;
import io.convergence_platform.services.constants.IServiceInfo;
import io.convergence_platform.services.infrastructure.*;
import io.convergence_platform.services.infrastructure.dto.RegisterServiceAuthorityRequest;
import io.convergence_platform.services.infrastructure.dto.ServiceConnectionDetailsResponse;
import io.convergence_platform.services.initialization.authorities.IServiceDeclaredAuthoritiesHandler;
import io.convergence_platform.services.initialization.db_migration.DatabaseMigratorService;
import io.convergence_platform.services.security.JwtHelper;
import io.convergence_platform.services.security.ServiceAuthorityDeclaration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
@Component
public class ServiceInitializer {
@Autowired
private IDatabaseMigratorService migrator;
@Autowired
private ServiceState serviceState;
@Autowired
private ApplicationContext appContext;
@Value("${application.convergence_core_service_url}")
private String discoveryServerUrl;
@Value("${application.mode}")
private String applicationMode;
@Value("${security.authentication.secret}")
private String secret;
@Autowired
private InfrastructureDiscoveryHelper discoveryHelper;
@EventListener
public void executeOnStartup(ApplicationStartedEvent event) {
discoveryHelper.loadServiceExposedURLs();
serviceState.state = "initializing_db";
if (migrator instanceof DatabaseMigratorService && ((DatabaseMigratorService) migrator).migrationRepository != null) {
migrator.migrateDatabase();
}
serviceState.state = "db_initialized";
saveServiceAuthority();
serviceState.state = "healthy";
}
private void saveServiceAuthority() {
serviceState.state = "initializing_authorities";
Object serviceAuthorities = null;
if (appContext.containsBean("serviceDeclaredAuthorities")) {
serviceAuthorities = appContext.getBean("serviceDeclaredAuthorities");
}
if (appContext.containsBean("serviceDeclaredAuthoritiesHandler")) {
Object serviceAuthoritiesHandler = appContext.getBean("serviceDeclaredAuthoritiesHandler");
((IServiceDeclaredAuthoritiesHandler) serviceAuthoritiesHandler).handleDeclaredAuthorities(serviceAuthorities);
} else if (applicationMode.equals("production")) {
// The service doesn't have a custom handler for the authorities, must publish them to authentication service
initializeServiceAuthorityInAuthenticationService(serviceAuthorities);
}
serviceState.state = "authorities_initialized";
}
private void initializeServiceAuthorityInAuthenticationService(Object serviceAuthorities) {
Set authorities = findAllAuthorities(serviceAuthorities);
String jwt = JwtHelper.createInterServiceJWT((IServiceInfo) appContext.getBean("serviceConstants"), secret);
AuthenticationRegisterAuthoritiesService authorityService = new AuthenticationRegisterAuthoritiesService(discoveryServerUrl, jwt);
for (ServiceAuthorityDeclaration declaration : authorities) {
RegisterServiceAuthorityRequest request = new RegisterServiceAuthorityRequest();
request.setAuthority(declaration.authority);
request.setUuid(declaration.uuid);
request.setDisplayName(declaration.displayName);
request.setTier(declaration.tier);
var response = authorityService.registerAuthority(request);
if (!response.wasSuccessful() || !response.body.getValue()) {
throw new RuntimeException(String.format("Unable to register authority with UUID = %s", declaration.uuid));
}
}
}
private Set findAllAuthorities(Object serviceAuthorities) {
Set result = new HashSet<>();
for (Field field : serviceAuthorities.getClass().getDeclaredFields()) {
try {
Object fieldValue = field.get(serviceAuthorities);
if (fieldValue instanceof ServiceAuthorityDeclaration) {
result.add((ServiceAuthorityDeclaration) fieldValue);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return result;
}
}