Alter Loader to allow configuration from Properties object. Server can now be obtained from Loader.

This commit is contained in:
2015-03-21 21:09:34 +00:00
parent 54cc7dc65f
commit 5dc25b8858
9 changed files with 137 additions and 75 deletions

View File

@@ -10,59 +10,57 @@ import org.picocontainer.parameters.ConstantParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.boukefalos.ibuddy.exception.LoaderException;
import com.github.boukefalos.ibuddy.implementation.Local;
import com.github.boukefalos.ibuddy.implementation.Remote;
import com.github.boukefalos.ibuddy.server.Server;
public class Loader {
protected static Logger logger = LoggerFactory.getLogger(Loader.class);
protected static MutablePicoContainer pico;
protected static final String PROPERTIES_FILE = "ibuddy.properties";
protected Logger logger = LoggerFactory.getLogger(Loader.class);
protected MutablePicoContainer pico;
public static iBuddy getiBuddy() throws LoaderException {
if (!setup()) {
throw new LoaderException();
}
public Loader(Properties properties) {
/* Initialise container */
pico = new DefaultPicoContainer();
/* Add implementation */
if (properties.getProperty("implementation").equals("local")) {
/* Local */
pico.addComponent(Local.class);
} else {
/* Remote */
pico.addComponent(Remote.class, Remote.class, new Parameter[]{
new ConstantParameter(properties.getProperty("remote.host")),
new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))});
}
/* Add server */
if (properties.getProperty("server") != null) {
pico.addComponent(Server.class, Server.class, new Parameter[]{
new ConstantParameter(getiBuddy()),
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
}
}
public static Loader getLoader() throws IOException {
return getLoader(PROPERTIES_FILE);
}
public static Loader getLoader(String propertiesFile) throws IOException {
/* Read properties file */
Properties properties = new Properties();
properties.load(Loader.class.getClassLoader().getResourceAsStream(propertiesFile));
/* Initialise loader */
return new Loader(properties);
}
public iBuddy getiBuddy() {
return pico.getComponent(iBuddy.class);
}
public static Server getServer() throws LoaderException {
if (!setup()) {
throw new LoaderException();
}
public Server getServer() {
return pico.getComponent(Server.class);
}
protected static boolean setup() {
if (pico == null) {
/* Read properties file */
Properties properties = new Properties();
try {
properties.load(Loader.class.getClassLoader().getResourceAsStream("ibuddy.properties"));
} catch (IOException e) {
e.printStackTrace();
logger.error("Failed to load properties file", e);
return false;
}
/* Initialise container */
pico = new DefaultPicoContainer();
/* Add implementation */
if (properties.getProperty("implementation").equals("local")) {
/* Local */
pico.addComponent(Local.class);
} else {
/* Remote */
pico.addComponent(Remote.class, Remote.class, new Parameter[]{
new ConstantParameter(properties.getProperty("remote.host")),
new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))});
}
/* Add server */
pico.addComponent(Server.class, Server.class, new Parameter[]{
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
}
return true;
}
}