-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
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(); | ||
|
||
} |
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
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); | ||
} | ||
|
||
} | ||
|
||
} |