Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from RaspberryWallet/module-class-loader
Module class loader
- Loading branch information
Showing
12 changed files
with
230 additions
and
40 deletions.
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
30 changes: 24 additions & 6 deletions
30
Manager/src/main/java/io/raspberrywallet/manager/Main.java
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 |
---|---|---|
@@ -1,28 +1,46 @@ | ||
package io.raspberrywallet.manager; | ||
|
||
import com.google.common.util.concurrent.Service; | ||
import io.raspberrywallet.manager.bitcoin.Bitcoin; | ||
import io.raspberrywallet.manager.cli.Opts; | ||
import io.raspberrywallet.manager.linux.TemperatureMonitor; | ||
import io.raspberrywallet.manager.modules.Module; | ||
import io.raspberrywallet.manager.modules.ModuleClassLoader; | ||
import io.raspberrywallet.server.Server; | ||
import org.apache.commons.cli.CommandLine; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import static io.raspberrywallet.ktor.KtorServerKt.startKtorServer; | ||
import static io.raspberrywallet.manager.cli.CliUtils.parseArgs; | ||
|
||
public class Main { | ||
|
||
public static void main(String... args) { | ||
CommandLine cmd = parseArgs(args); | ||
|
||
Bitcoin bitcoin = new Bitcoin(); | ||
if (Opts.SYNC.isSet(cmd)) { | ||
Service blockchainSyncing = bitcoin.startBlockchainAsync(); | ||
Runtime.getRuntime().addShutdownHook(new Thread(blockchainSyncing::stopAsync)); | ||
} | ||
|
||
// Disable for now | ||
//Service blockchainSyncing = bitcoin.startBlockchainAsync(); | ||
//Runtime.getRuntime().addShutdownHook(new Thread(blockchainSyncing::stopAsync)); | ||
File modulesDir = new File(Opts.MODULES.getValue(cmd)); | ||
List<Module> modules = ModuleClassLoader.getModulesFrom(modulesDir); | ||
|
||
TemperatureMonitor temperatureMonitor = new TemperatureMonitor(); | ||
|
||
Manager manager = new ExampleMockManager(bitcoin, temperatureMonitor); | ||
Manager manager = new ExampleMockManager(modules, bitcoin, temperatureMonitor); | ||
|
||
if (args.length > 0 && args[0].equals("vertx")) | ||
if (Opts.VERTX.isSet(cmd) || Opts.SERVER.getValue(cmd).equals(Opts.VERTX.name())) | ||
new Server(manager).start(); | ||
else | ||
startKtorServer(manager); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
Manager/src/main/java/io/raspberrywallet/manager/cli/CliUtils.java
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,25 @@ | ||
package io.raspberrywallet.manager.cli; | ||
|
||
import org.apache.commons.cli.*; | ||
|
||
public class CliUtils { | ||
public static CommandLine parseArgs(String[] args) { | ||
Options options = new Options() {{ | ||
addOption(Opts.MODULES.option); | ||
addOption(Opts.SYNC.option); | ||
addOption(Opts.SERVER.option); | ||
addOption(Opts.KTOR.option); | ||
addOption(Opts.VERTX.option); | ||
}}; | ||
HelpFormatter helpFormatter = new HelpFormatter(); | ||
DefaultParser parser = new DefaultParser(); | ||
try { | ||
return parser.parse(options, args); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
helpFormatter.printHelp("java -jar Manager.jar", options, true); | ||
System.exit(0); | ||
return null; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Manager/src/main/java/io/raspberrywallet/manager/cli/Opts.java
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,32 @@ | ||
package io.raspberrywallet.manager.cli; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Option; | ||
|
||
public enum Opts { | ||
SERVER(new Option("server", true, "HTTP Server impl"), "ktor"), | ||
KTOR(new Option("ktor", "Use Ktor as HTTP Server impl")), | ||
VERTX(new Option("vertx", "Use VertX as HTTP Server impl")), | ||
MODULES(new Option("modules", true, "Modules classes directory path"), "modules"), | ||
SYNC(new Option("sync", "Sync bitcoin blockchain")); | ||
|
||
public final Option option; | ||
public final String def; | ||
|
||
Opts(Option option) { | ||
this(option, null); | ||
} | ||
|
||
Opts(Option option, String def) { | ||
this.option = option; | ||
this.def = def; | ||
} | ||
|
||
public boolean isSet(CommandLine cmd) { | ||
return cmd.hasOption(this.option.getOpt()); | ||
} | ||
|
||
public String getValue(CommandLine cmd) { | ||
return cmd.getOptionValue(this.option.getOpt(), def); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
Manager/src/main/java/io/raspberrywallet/manager/modules/ModuleClassLoader.java
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,76 @@ | ||
package io.raspberrywallet.manager.modules; | ||
|
||
import com.stasbar.Logger; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class ModuleClassLoader { | ||
|
||
/** | ||
* @param modulesDir - directory where module classes are located | ||
* @return objects of instantiated Modules {@link Module} | ||
*/ | ||
@NotNull | ||
public static List<Module> getModulesFrom(File modulesDir) { | ||
if (!modulesDir.exists()) { | ||
System.out.println("Modules folder not found, skipping"); | ||
return Collections.emptyList(); | ||
} | ||
File[] files = Objects.requireNonNull(modulesDir.listFiles(), "moduleDir files can not be null"); | ||
try { | ||
URL url = modulesDir.toURI().toURL(); | ||
URLClassLoader classLoader = new URLClassLoader(new URL[]{url}); | ||
List<Class<?>> classes = getClasses(files, classLoader); | ||
List<Module> modules = instaniateModulesObjects(classes); | ||
printLoadedModules(modules); | ||
return modules; | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
private static List<Class<?>> getClasses(File[] files, final URLClassLoader classLoader) { | ||
return Arrays.stream(files).map(file -> | ||
{ | ||
try { | ||
String className = "io.raspberrywallet.manager.modules." + file.getName().replace(".class", ""); | ||
return classLoader.loadClass(className); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
).filter(Objects::nonNull).collect(Collectors.toList()); | ||
} | ||
|
||
private static List<Module> instaniateModulesObjects(List<Class<?>> classes) { | ||
return classes.stream().map(clazz -> { | ||
try { | ||
return (Module) clazz.newInstance(); | ||
} catch (InstantiationException | IllegalAccessException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
}).filter(Objects::nonNull).collect(Collectors.toList()); | ||
} | ||
|
||
private static void printLoadedModules(List<Module> modules) { | ||
Logger.info("ModuleClassLoader", String.format("Loaded %d modules", modules.size())); | ||
modules.forEach(module -> | ||
Logger.info(String.format("Module {\n\tname: %s \n\tid: %s \n\tdescription: %s \n}", | ||
module.getClass().getSimpleName(), | ||
module.getId(), | ||
module.getDescription())) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.