Skip to content

Instantly share code, notes, and snippets.

@Mart-Bogdan
Created November 20, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mart-Bogdan/67ffb2d44d583495c8b272360e4d6d7f to your computer and use it in GitHub Desktop.
Save Mart-Bogdan/67ffb2d44d583495c8b272360e4d6d7f to your computer and use it in GitHub Desktop.
Guice sample
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ua.kpi.acts.samuel.agent.inject.modules;
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.MapBinder;
import java.util.Set;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ua.kpi.acts.samuel.agent.commands.api.CommandName;
import ua.kpi.acts.samuel.agent.commands.api.ICommand;
/**
*
* @author Artur
*/
public class CommandsBindingModule extends AbstractModule
{
private final Logger log = LoggerFactory.getLogger(getClass());
@Override
public void configure() {
MapBinder<String, ICommand> mapBinder = MapBinder.newMapBinder(binder(), String.class, ICommand.class);
log.info("Fetching all exiting commands implementations");
// Go through all command names
Reflections reflections = new Reflections("ua.kpi.acts.samuel.agent.commands.impl");
// load all classes-commands in project to memory
Set<Class<?>> annotated =
(Set<Class<?>>) reflections.getTypesAnnotatedWith(CommandName.class);
log.info("Found "+annotated.size()+" commands");
// fill commands classes and its names hash map
for (Class<?> aClass : annotated) {
String commandName = aClass.getAnnotation(CommandName.class).value(); // read the annotation value
@SuppressWarnings("unchecked")
Class<ICommand> command = (Class<ICommand>) aClass;
mapBinder.addBinding(commandName).to(command);
}
}
}
package ua.kpi.acts.samuel.agent;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.io.IOException;
import ua.kpi.acts.samuel.agent.inject.modules.CommandsBindingModule;
import ua.kpi.acts.samuel.agent.inject.modules.MainBindingModule;
import ua.kpi.acts.samuel.agent.inject.modules.StorageBindingModule;
/**
* Created by Anton Goi on 30.10.2014.
*
* When somebody builds WHOLE project, don't forget to COMMENT @Test ALL
* annotations in ActiveMQTest.java
*/
public class MainAgent {
// private static final Logger log = LoggerFactory.getLogger(ActiveMQ.class);
public static void main(String[] args) throws IOException, Exception {
Injector injector = Guice.createInjector(
new MainBindingModule(),
new CommandsBindingModule(),
new StorageBindingModule()
);
AgentExecutor agentExecutor = injector.getInstance(AgentExecutor.class);
//AgentExecuter agentExecuter = new AgentExecuter(SingletonInstanceCreator.getSingletonInstance(ICommunication.class),
// SingletonInstanceCreator.getSingletonInstance(IStorage.class),
// SingletonInstanceCreator.getSingletonInstance(IScheduler.class));
agentExecutor.execute(args);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ua.kpi.acts.samuel.agent.inject.modules;
import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ua.kpi.acts.samuel.agent.commands.execution.CommandFactory;
import ua.kpi.acts.samuel.agent.commands.execution.ICommandFactory;
import ua.kpi.acts.samuel.agent.communication.ICommunication;
import ua.kpi.acts.samuel.agent.communication.impl.activemq.ActiveMqCommunication;
import ua.kpi.acts.samuel.agent.monitoring.IMonitoringCommandExecutor;
import ua.kpi.acts.samuel.agent.monitoring.impl.MonitoringCommandExecutor;
import ua.kpi.acts.samuel.agent.scheduler.AgentScheduler;
import ua.kpi.acts.samuel.agent.scheduler.IScheduler;
import ua.kpi.acts.samuel.agent.config.IConfigRetriever;
import ua.kpi.acts.samuel.agent.config.JsonAgentConfigRetriever;
/**
*
* @author Serhiy Vaschilin
*/
public class MainBindingModule extends AbstractModule {
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* Configures a {@link Binder} via the exposed methods.
*/
@Override
protected void configure() {
bind(ICommunication.class).to(ActiveMqCommunication.class);
bind(IScheduler.class).to(AgentScheduler.class);
bind(IConfigRetriever.class).to(JsonAgentConfigRetriever.class);
bind(IMonitoringCommandExecutor.class).to(MonitoringCommandExecutor.class);
bind(ICommandFactory.class).to(CommandFactory.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment