Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

extracted core function of di to new class #474

Merged
merged 2 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ public Core() {
void start(String[] args) {
this.registerAllCoreServices();
this.init();
CommandLineArgumentHandlerService cmaHandler = this.di.getService(new ServiceConsumerImpl<>(this.getClass(),
new ServiceDescriptionImpl<>(CommandLineArgumentHandlerService.class))).getService();
CommandLineArgumentHandlerService cmaHandler = this.di.getServiceLocator()
.getService(new ServiceConsumerImpl<>(this.getClass(),
new ServiceDescriptionImpl<>(CommandLineArgumentHandlerService.class)))
.getService();
cmaHandler.load(args, System.out::println);
if (cmaHandler.shouldProgramContinue()) {
CommandLineArgumentInfo cmaInfo = cmaHandler.getInfo();
this.di.register(new SingletonServiceProvider<>(CommandLineArgumentInfo.class, cmaInfo));
this.di.getConfiguration()
.register(new SingletonServiceProvider<>(CommandLineArgumentInfo.class, cmaHandler.getInfo()));
this.run();
} else {
System.exit(cmaHandler.areFlagsValid() ? EXIT_CODE_ALL_GOOD : EXIT_CODE_CMA_FLAGS_INVALID); // NOSONAR
Expand Down Expand Up @@ -116,7 +118,7 @@ private void init() {
}

private void loadPlugins() {
PluginManager pluginManager = this.di
PluginManager pluginManager = this.di.getServiceLocator()
.getService(
new ServiceConsumerImpl<>(this.getClass(), new ServiceDescriptionImpl<>(PluginManager.class)))
.getService();
Expand All @@ -125,7 +127,8 @@ private void loadPlugins() {
} catch (IOException e) {
throw new IllegalStateException("Could not load plugins due to an IOException.", e);
}
this.di.registerContextProvider(Context.PLUGIN, new PluginProvider(pluginManager.getPlugins()));
this.di.getConfiguration().registerContextProvider(Context.PLUGIN,
new PluginProvider(pluginManager.getPlugins()));
}

/**
Expand All @@ -144,7 +147,6 @@ private void start() {
private void doStop() {
this.logger.info("stop");
this.runnableServiceExtension.stop();
this.di.shutdown();
this.logger.info("stopped");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import de.unistuttgart.iaas.amyassist.amy.core.di.DependencyInjection;
import de.unistuttgart.iaas.amyassist.amy.core.di.ServiceDescription;
import de.unistuttgart.iaas.amyassist.amy.core.di.ServiceLocator;
import de.unistuttgart.iaas.amyassist.amy.core.di.consumer.ServiceConsumerImpl;
import de.unistuttgart.iaas.amyassist.amy.core.di.extension.Extension;

Expand All @@ -38,13 +39,13 @@
*/
public class DeploymentContainerServiceExtension implements Extension {

private DependencyInjection di;
private ServiceLocator serviceLocator;

private final Set<ServiceDescription<?>> deploymentContainerServices = new HashSet<>();

@Override
public void postConstruct(DependencyInjection dependencyInjection) {
this.di = dependencyInjection;
this.serviceLocator = dependencyInjection.getServiceLocator();
}

@Override
Expand All @@ -59,7 +60,7 @@ public <T> void onRegister(ServiceDescription<T> serviceDescription, Class<? ext
*/
public void deploy() {
for (ServiceDescription<?> deploymentContainerServiceDescription : this.deploymentContainerServices) {
DeploymentContainerService deploymentContainerService = (DeploymentContainerService) this.di
DeploymentContainerService deploymentContainerService = (DeploymentContainerService) this.serviceLocator
.getService(new ServiceConsumerImpl<>(this.getClass(), deploymentContainerServiceDescription))
.getService();
deploymentContainerService.deploy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import de.unistuttgart.iaas.amyassist.amy.core.di.DependencyInjection;
import de.unistuttgart.iaas.amyassist.amy.core.di.ServiceDescription;
import de.unistuttgart.iaas.amyassist.amy.core.di.ServiceLocator;
import de.unistuttgart.iaas.amyassist.amy.core.di.extension.Extension;

/**
Expand All @@ -37,13 +38,13 @@
*/
public class RunnableServiceExtension implements Extension {

private DependencyInjection di;
private ServiceLocator serviceLocator;

private final Set<ServiceDescription<?>> runnableServices = new HashSet<>();

@Override
public void postConstruct(DependencyInjection dependencyInjection) {
this.di = dependencyInjection;
this.serviceLocator = dependencyInjection.getServiceLocator();
}

@Override
Expand All @@ -57,22 +58,22 @@ public <T> void onRegister(ServiceDescription<T> serviceDescription, Class<? ext
* Deploy all registered RunnableServies
*/
public void deploy() {
ServiceManagerImpl service = this.di.getService(ServiceManagerImpl.class);
ServiceManagerImpl service = this.serviceLocator.getService(ServiceManagerImpl.class);
this.runnableServices.forEach(service::register);
}

/**
* Start all registered RunnableServies
*/
public void start() {
this.di.getService(ServiceManagerImpl.class).start();
this.serviceLocator.getService(ServiceManagerImpl.class).start();
}

/**
* Stop all running Services
*/
public void stop() {
this.di.getService(ServiceManagerImpl.class).stop();
this.serviceLocator.getService(ServiceManagerImpl.class).stop();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ void init() {
ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class);
this.properties = new Properties();
Mockito.when(configurationManager.getConfigurationWithDefaults("core.config")).thenReturn(this.properties);
this.dependencyInjection
this.dependencyInjection.getConfiguration()
.register(new SingletonServiceProvider<>(ConfigurationManager.class, configurationManager));
this.dependencyInjection.register(new LoggerProvider());
this.dependencyInjection.getConfiguration().register(new LoggerProvider());
}

@Test
Expand All @@ -68,8 +68,9 @@ void test() {

Mockito.when(handler.createDialog(ArgumentMatchers.any())).thenReturn(uuid);

this.dependencyInjection.register(new SingletonServiceProvider<>(DialogHandler.class, handler));
SpeechConsole console = this.dependencyInjection.createAndInitialize(SpeechConsole.class);
this.dependencyInjection.getConfiguration()
.register(new SingletonServiceProvider<>(DialogHandler.class, handler));
SpeechConsole console = this.dependencyInjection.getServiceLocator().createAndInitialize(SpeechConsole.class);

console.say(testInput);
Mockito.verify(handler).process(expected, uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,19 @@ class ServiceManagerImplTest {
public void setup() {
this.runnableServiceExtension = new RunnableServiceExtension();
this.di = new DependencyInjection(this.runnableServiceExtension);
this.di.register(ServiceManagerImpl.class);
this.di.getConfiguration().register(ServiceManagerImpl.class);
}

/**
* Test method for {@link de.unistuttgart.iaas.amyassist.amy.core.service.ServiceManagerImpl#start()}.
*/
@Test
void testStart() {

this.di.register(TestRunnableService.class);
this.di.getConfiguration().register(TestRunnableService.class);
this.runnableServiceExtension.deploy();
this.runnableServiceExtension.start();

TestRunnableService service = this.di.getService(TestRunnableService.class);
TestRunnableService service = this.di.getServiceLocator().getService(TestRunnableService.class);
assertThat(service.run, is(true));
this.runnableServiceExtension.stop();
assertThat(service.run, is(false));
Expand All @@ -80,12 +79,11 @@ void testStartDouble() {
*/
@Test
void testStartRunnableServiceFromInterface() {

this.di.register(SimpleServiceImpl.class);
this.di.getConfiguration().register(SimpleServiceImpl.class);
this.runnableServiceExtension.deploy();
this.runnableServiceExtension.start();

SimpleService service = this.di.getService(SimpleService.class);
SimpleService service = this.di.getServiceLocator().getService(SimpleService.class);
assertThat(service.getRun(), is(true));
this.runnableServiceExtension.stop();
assertThat(service.getRun(), is(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
public interface SimpleServiceLocator {
/**
* Get a service for the given service consumer. This Method track the caller to prevent the service get created
* Get a service for the given service consumer. This method track the caller to prevent the service get created
* twice and to run in a dependency circle.
*
* @param serviceConsumer
Expand Down
Loading