Created
June 8, 2014 19:11
-
-
Save Romain-P/bcf099d8c5dbda559e70 to your computer and use it in GitHub Desktop.
no comment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.heater.core.plugin; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Inject; | |
import com.google.inject.multibindings.Multibinder; | |
import org.heater.api.HeaterActivator; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
public class PluginManager { | |
private final List<HeaterActivator> instances; | |
private AbstractModule module; | |
@Inject Set<HeaterActivator> plugins; | |
public PluginManager() { | |
this.instances = new ArrayList<>(); | |
} | |
public PluginManager initialize() { | |
//TODO: insert modules main class | |
return this; | |
} | |
public void finalize() { | |
if(plugins == null) return; | |
for(HeaterActivator plugin: plugins) | |
plugin.stop(); | |
} | |
private AbstractModule buildModule() { | |
return new AbstractModule() { | |
@Override | |
protected void configure() { | |
bind(PluginManager.class).toInstance(PluginManager.this); | |
Multibinder<HeaterActivator> binder = Multibinder.newSetBinder(binder(), HeaterActivator.class); | |
for(HeaterActivator instance: instances) { | |
binder.addBinding().toInstance(instance); | |
install(instance.pluginModule()); | |
} | |
} | |
}; | |
} | |
public AbstractModule getModule() { | |
return module != null ? module : (module = buildModule()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment