org.jlot.client.config.JlotClient Maven / Gradle / Ivy
package org.jlot.client.config;
import java.beans.Introspector;
import java.util.Properties;
import org.jlot.client.executor.spi.CommandExecutor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.PropertySource;
public class JlotClient
{
private AnnotationConfigApplicationContext applicationContext;
private boolean started = false;
public void start ( Properties properties )
{
applicationContext = new AnnotationConfigApplicationContext();
addPropertySource(properties);
applicationContext.register(ClientConfig.class);
applicationContext.refresh();
started = true;
}
public void stop ( )
{
applicationContext.close();
started = false;
}
public boolean isStarted ( )
{
return started;
}
private void addPropertySource ( Properties properties )
{
PropertySource ps = new PropertiesPropertySource("jlot.properties", properties);
applicationContext.getEnvironment().getPropertySources().addFirst(ps);
}
public ApplicationContext getApplicationContext ( )
{
return applicationContext;
}
public T getRestCommand ( Class restCommandClass )
{
String shortClassName = restCommandClass.getSimpleName();
String beanName = Introspector.decapitalize(shortClassName);
return applicationContext.getBean(beanName, restCommandClass);
}
public CommandExecutor getCommandExecuter ( Class extends CommandExecutor> commandExecutorClass )
{
String shortClassName = commandExecutorClass.getSimpleName();
String beanName = Introspector.decapitalize(shortClassName);
return applicationContext.getBean(beanName, commandExecutorClass);
}
}