Skip to content

Commit

Permalink
add: 插件热插拔增加jar文件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizzercn committed Dec 7, 2016
1 parent fdccba3 commit 5e44ca4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/java/cn/wizzer/common/core/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ private void initSysPlugin(NutConfig config, Dao dao) {
for (Sys_plugin sysPlugin : list) {
String name = sysPlugin.getPath().substring(sysPlugin.getPath().indexOf(".")).toLowerCase();
File file = new File(Globals.AppRoot + sysPlugin.getPath());
byte[] buf = Files.readBytes(file);
String[] p = new String[]{};
IPlugin plugin;
if (".jar".equals(name)) {
plugin = pluginMaster.buildFromJar(file, sysPlugin.getClassName(), buf);
plugin = pluginMaster.buildFromJar(file, sysPlugin.getClassName());
} else {
byte[] buf = Files.readBytes(file);
plugin = pluginMaster.build(sysPlugin.getClassName(), buf);
}
if (!Strings.isBlank(sysPlugin.getArgs())) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/wizzer/common/plugin/PluginMaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public IPlugin build(final String className, byte[] buf) {
}

@SuppressWarnings("resource")
public IPlugin buildFromJar(final File file, final String className, byte[] buf) {
public IPlugin buildFromJar(final File file, final String className) {
try {
log.debug(file.getAbsolutePath());
return (IPlugin) new URLClassLoader(new URL[]{file.toURI().toURL()}, getClass().getClassLoader(), null).loadClass(className).newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public Object addDo(@Param("code") String code,
try {
String name = tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf(".")).toLowerCase();
String p = Globals.AppRoot;
String f = Globals.AppUploadPath + "/plugin/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() + name;
String f = Globals.AppUploadPath + "/plugin/" + R.UU32() + name;
File file = new File(p + f);
Files.createFileIfNoExists(file);
Files.write(f, tf.getInputStream());
byte[] buf = Files.readBytes(file);
Files.write(file, tf.getInputStream());
IPlugin plugin;
if (".jar".equals(name)) {
plugin = pluginMaster.buildFromJar(file, className, buf);
plugin = pluginMaster.buildFromJar(file, className);
} else {
byte[] buf = Files.readBytes(file);
plugin = pluginMaster.build(className, buf);
}
pluginMaster.register(code, plugin, args);
Expand All @@ -92,7 +92,7 @@ public Object delete(String id) {
try {
Sys_plugin sysPlugin = sysPluginService.fetch(id);
pluginMaster.remove(sysPlugin.getCode());
Files.deleteFile(new File(Globals.AppRoot + sysPlugin.getPath()));
//被占用删不掉文件啊 Files.deleteFile(new File(Globals.AppRoot + sysPlugin.getPath()));
sysPluginService.delete(id);
return Result.success("system.success");
} catch (Exception e) {
Expand All @@ -108,12 +108,12 @@ public Object enable(String id) {
Sys_plugin sysPlugin = sysPluginService.fetch(id);
String name = sysPlugin.getPath().substring(sysPlugin.getPath().indexOf(".")).toLowerCase();
File file = new File(Globals.AppRoot + sysPlugin.getPath());
byte[] buf = Files.readBytes(file);
String[] p = new String[]{};
IPlugin plugin;
if (".jar".equals(name)) {
plugin = pluginMaster.buildFromJar(file, sysPlugin.getClassName(), buf);
plugin = pluginMaster.buildFromJar(file, sysPlugin.getClassName());
} else {
byte[] buf = Files.readBytes(file);
plugin = pluginMaster.build(sysPlugin.getClassName(), buf);
}
if (!Strings.isBlank(sysPlugin.getArgs())) {
Expand Down

0 comments on commit 5e44ca4

Please sign in to comment.