I have a multimodule project with two projects: Core and A. The idea is to launch/ run A whenever Core is launched.
How can I customize the ServiceLoader to look up and call the modules in the Plugins folder from core?
plugin-Project
+ Core
+ src\main\java
Core.java
+ A
+ src\main\java
A.java
+ Plugins
Core
public class Core extends Application {
private ServiceLoader<View> views;
private BorderPane mainBorderPane;
@Override
public void init() {
loadViews();
}
private void loadViews() {
views = ServiceLoader.load(View.class);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Ui Application");
mainBorderPane = new BorderPane();
mainBorderPane.setTop(createMenuBar());
Scene scene = new Scene(new Group(), 800, 605);
scene.setRoot(mainBorderPane);
stage.setScene(scene);
stage.show();
}
private MenuBar createMenuBar() {
MenuBar menuBar = new MenuBar();
Menu viewMenu = new Menu("Views");
menuBar.getMenus().add(viewMenu);
ToggleGroup toggleGroup = new ToggleGroup();
views.forEach(v -> {
RadioMenuItem item = new RadioMenuItem(v.getName());
item.setToggleGroup(toggleGroup);
item.setOnAction(event -> {
Label label = new Label(v.getName());
mainBorderPane.setLeft(label);
mainBorderPane.setCenter(v.getView());
});
viewMenu.getItems().add(item);
});
return menuBar;
}
public static void main(String[] args) {
launch(args);
}
}
View.java
public interface View {
String getName();
Node getView();
}
Scenario
The application I'm working on is an multi-module stand-alone desktop application. For example, Core would hold a pane on the left (left-pane). left-pane will accept nodes
from any module that implements an interface called LeftPane
. A implements the LeftPane
interface. Whenever Core is launched, it should scan through a folder, plugins in this case and automatically start all the bundles there, including A, which would go on to populate the left pane.