All PLUGIN modules should implement a MODULEINTERFACE interface:
Public Interface ModuleInterface
{
??? public boolean handles (Object Key);
}
This means that all modules have a Handles method. This method provides a mechanism for selecting a module. Based on the incoming object identification module, for example, it is incoming a string, determined according to this String, this is not an applicable module and then returns True Or false.
?
Each module should describe yourself in the meta-INF directory of the JAR file. For example, in a module.mf:
Module.name = module1
Module.Type = TestInterface
Module.class = TestModuleClass
Module.Type specifies the type of this module (TestInterface is an interface or a class, indicating which module is the module, for example it is a filter, protocol, interface, or function extension, etc.), it must be imports the moduleInterface interface, Module.class is the specific class of this module. In this example, Module.Type is part of the application, and module.class is part of the JAR as Plugin.
?
ModulesCanner is used to search for one or more specified directories, find the JAR files for all modules, and obtain the description information of the module from the JAR file, then construct a modulespec object with the module's file reference and description information, so each modulespec object corresponds to A module, ModulesCanner maintains a list, saves all Modulespec objects.
Import java.io. *;
Import java.net. *;
Import java.util. *;
Import java.util.zip. *;
Import java.util.jar. *;
?
Public Class Modulescanner
{
PROTECTED LIST MODULESPECLIST = New ArrayList ();
?
Public void scan (file [] pats) throws oews {
????? for (int i = 0; i
??????? scan (Paths [i]);
?????}
?
?
PUBLIC VOID SCAN (File Path) throws ioException {
??? file [] list = path.listfiles ();
??? for (int i = 0; i
????? file = list [i];
????? if (file.isfile () && file.getname (). TOLOWERCASE (). endswith (". jar")) {
??????? system.out.println (file.getname ());
??????? properties props = readmodulemanifest (file);
??????? ing (props == null) Continue;
??????? Modulespec Spec = new modulespec (file.tourl (), prOPS;
??????? ModulespeClist.add (spec);
??????? system.out.println (SPEC);
?????}
???}
?
?
PROTECTED Properties ReadmoduleManifest (File File) THROWS IEXCEPTION {??? jarfile jar = new jarfile (file);
??? jarentry entry = findModuleEntry (jar, "meta-inf / module.mf");
??? if (entry == null) Return NULL;
??? INPUTSTREAM INPUT = JAR.GETINPUTSTREAM (Entry);
???Properties props = new profmentness ();
??? pROPS.LOAD (INPUT);
??? INPUT.CLOSE ();
??? RETURN PROPS;
?
?
PROTECTED JARENTRY FINDMODULEETRY (JARFILE JAR, STRING EXPECT) {
??? enumeration enum = jar.entries ();
??? while (enum.hasmoreElements ()) {
????? jarentry entry = (jarentry) enum.nexTelement ();
????? String name = entry.getname ();
????? f (name.equalsignorecase (expect)) {
??????? return entry;
?????}
???}
??? Return NULL;
?
?
PUBLIC MODULESPEC [] getModulesPECS () {
??? Int size = modulespeclist.size ();
??? modulespec [] specs = new modulespec [size];
??? for (int i = 0; i
????? SPECS [i] = (modulespec) modulespeclist.get (i);
???}
??? RETURN SPECS;??
?
}
?
The Modulespec class creates a module instance according to the module information, which is constructed by two parameter, one is a file reference for the JAR file, one is a Properties that contains data in Module.mf. Then create an instance through the moduleClassLoader loading class based on the information provided by these two parameters.
?
ModuleClassLoad is loaded from the JAR file, which is derived from the standard UrlclassLoader.
?
ModuleRegistry encapsulates all modules's modulespec objects that provide an efficient mapping relationship for users and a set of Modulespec objects.
?
Finally, in the ModuleFactory class, the ModulesCanner collects module, and registers it into ModuleRegistry, traversing the module instance obtained from ModuleRegistry in GetInstance, calling the Handles method of each module instance to find the appropriate module.
Import java.io. *;
Import java.net. *;
Import java.util. *;
?
Public Class ModuleFactory
{
? protected static final modulefactory factory = new modulefactory ();
?
PROTECTED MODULEGISTRY REGISTRY = New ModuleRegistry ();
? protected modecanner scanner = new modulescanner () ;?
PUBLIC VOID LOADMODULES (File Path) throws oException {
??? scanner.scan (path);
??? register (scanner.getmodulespecs ());
?
?
Public void loadingModules (file [] paths) throws oException {
??? scanner.scan (paths);
??? register (scanner.getmodulespecs ());
?
?
PUBLIC VOID Register (Modulespec Spec) {
??? registry.registermodule (SPEC);
?
?
PUBLIC VOID Register (Modulespec [] SPECS) {
??? for (int i = 0; i
????? registry.registermodule (SPECS [i]);
???}
?
?
PUBLIC Object GetInstance (Class Type, Object Key) {
??? List modules = registry.getmodulesfortype (TYPE);
??? Int count = modules.size ();
??? for (int i = 0; i
????? Modulespec Spec = (modulespec) modules.get (i);
????? ModuleInterface module = spec.getInstance ();
????? ife (module.handles (key)) Return Module;
???}
??? Return NULL;
?
?
Public static modulefactory getinstance () {
??? Return Factory;
?
?
?