Skip to content

Commit

Permalink
Extension System
Browse files Browse the repository at this point in the history
tfinnm committed Feb 7, 2020
1 parent be8f2ea commit cbe42d6
Showing 5 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -22,3 +22,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.settings/org.eclipse.jdt.core.prefs
/bin/
2 changes: 1 addition & 1 deletion .project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>NetMon</name>
<name>BLIP</name>
<comment></comment>
<projects>
</projects>
2 changes: 2 additions & 0 deletions src/application/NetMon.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package application;
import java.io.IOException;

import extensions.ExtensionManager;
import services.*;
import ui.UIManager;
import settings.SettingsManager;
@@ -12,6 +13,7 @@ public static void main(String[] args) {
try {
ServiceManager.loadServices("Services.NetMon");
SettingsManager.loadSettings("settings.NetMon");
ExtensionManager.loadExtensions();
} catch (IOException e) {
}
TimerManager.startTimers();
11 changes: 11 additions & 0 deletions src/extensions/Extension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package extensions;

import services.Service;

public interface Extension {

public void onEvent(Service.status e);

public void loadExtension();

}
53 changes: 53 additions & 0 deletions src/extensions/ExtensionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package extensions;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Scanner;

import services.Service;

public class ExtensionManager {

static ArrayList<Extension> plugins = new ArrayList<Extension>();

public static void loadExtensions() {
findExtensions("Extensions/");
}

private static void findExtensions(String DIR) {
File dir = new File(DIR);
if (dir.exists()) {
File[] files = dir.listFiles((d, name) -> name.endsWith(".class"));
for (int i = 0; i < files.length; i++) {
try {
loadExtension(files[i]);
} catch (MalformedURLException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
}
}
}
}

private static void loadExtension(File dir) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
URL loadPath = dir.toURI().toURL();
URL[] classUrl = new URL[]{loadPath};

ClassLoader cl = new URLClassLoader(classUrl);

Class loadedClass = cl.loadClass(dir.getName());

plugins.add((Extension)loadedClass.newInstance());
plugins.get(plugins.size()-1).loadExtension();
}

public static void triggerEvent(Service.status e) {

for (int j = 0; j < plugins.size(); j++) {
plugins.get(j).onEvent(e);
}

}

}

0 comments on commit cbe42d6

Please sign in to comment.