Skip to content

Instantly share code, notes, and snippets.

@Romain-P
Created June 8, 2014 17:18
Show Gist options
  • Save Romain-P/37377100057ca9b731a3 to your computer and use it in GitHub Desktop.
Save Romain-P/37377100057ca9b731a3 to your computer and use it in GitHub Desktop.
OSGi using guice
package org.heater.core;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.apache.felix.framework.Felix;
import org.heater.api.HeaterActivator;
import org.heater.gui.GuiModule;
import org.heater.gui.HeaterFrame;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
//trying to set the default looking of system
etablishingLookAndFeel();
//Initializing plugin handler
Map<String, String> properties = new HashMap<>();
properties.put(Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT, "firstInit");
Felix felix = new Felix(properties);
final Injector injector;
try {
felix.start();
File[] files = new File("plugins").listFiles();
Map<HeaterActivator, AbstractModule> modules = new HashMap<>();
for(File file: files) {
Bundle bundle = felix.getBundleContext().installBundle(file.getAbsolutePath());
HeaterActivator activator = (HeaterActivator) bundle.loadClass(bundle.getBundleContext().getProperty("plugin")).newInstance();
modules.put(activator, activator.pluginModule());
}
modules.put(null, new DefaultModule());
modules.put(null, new GuiModule());
injector = Guice.createInjector(modules.values());
for(HeaterActivator activator: modules.keySet())
if(activator != null)
injector.injectMembers(activator);
//launching gui
EventQueue.invokeLater(() -> {
//initializing frame
injector.getInstance(HeaterFrame.class).initialize().setVisible(true);
//initializing logger dialog
injector.getInstance(Logger.class).initialize();
});
} catch (Exception e) {}
}
private static void etablishingLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment