Skip to content

Commit

Permalink
插件加载初始化bug修改
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Mar 28, 2018
1 parent ce4dad9 commit c7f61e4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 99 deletions.
74 changes: 0 additions & 74 deletions common/src/main/java/lee/study/down/io/LargeMappedByteBuffer.java

This file was deleted.

5 changes: 4 additions & 1 deletion sniff/src/main/java/lee/study/down/plug/PluginContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import lee.study.down.model.PluginBean;
import lee.study.down.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -51,7 +52,9 @@ public static void init() {
}

public static void set(String key, PluginBean value) {
content.put(key, value);
if (value != null) {
content.put(key, value);
}
}

public static PluginBean get(String key) {
Expand Down
36 changes: 13 additions & 23 deletions sniff/src/main/java/lee/study/down/plug/PluginUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

public class PluginUtil {

private static final String PLUG_PATH = PathUtil.ROOT_PATH + "plugs";
public static final String PLUG_PATH = PathUtil.ROOT_PATH + "plugs";

public static PluginBean getPluginBean(String key, InputStream inputStream, float currentVersion)
public static PluginBean getPluginBean(InputStream inputStream)
throws IOException {
PluginBean pluginBean = new PluginBean();
FileOutputStream outputStream = null;
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))
) {
Expand All @@ -29,34 +28,17 @@ public static PluginBean getPluginBean(String key, InputStream inputStream, floa
if (num == 1) {
//版本号
float newVersion = Float.parseFloat(line.substring(2));
if (newVersion > currentVersion) {
pluginBean.setVersion(newVersion);
String localFile = PLUG_PATH + File.separator + key;
FileUtil.createFileSmart(localFile);
outputStream = new FileOutputStream(localFile);
} else {
return null;
}
pluginBean.setVersion(newVersion);
num++;
}
String temp = line + "\r\n";
sb.append(temp);
outputStream.write(temp.getBytes("UTF-8"));
}
pluginBean.setContent(sb.toString());
} finally {
if (outputStream != null) {
outputStream.close();
}
}
return pluginBean;
}

private static PluginBean getPluginBean(InputStream inputStream)
throws IOException {
return getPluginBean(null, inputStream, -1F);
}

public static PluginBean checkAndUpdateLocalPlugin(String key, InputStream inputStream)
throws IOException {
String localFile = PLUG_PATH + File.separator + key;
Expand All @@ -80,16 +62,24 @@ public static PluginBean checkAndUpdateLocalPlugin(String key, InputStream input
float targetVersion = Float.parseFloat(str.substring(2));
//plug需要更新
if (targetVersion > localVersion) {
FileUtil.createFileSmart(localFile);
if (!FileUtil.exists(PLUG_PATH)) {
FileUtil.createDirSmart(PLUG_PATH);
}
if (FileUtil.exists(localFile)) {
new File(localFile).delete();
}
new File(localFile).createNewFile();
try (
FileOutputStream outputStream = new FileOutputStream(localFile);
FileOutputStream outputStream = new FileOutputStream(localFile)
) {
outputStream.write((str + "\r\n").getBytes("UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
outputStream.write((line + "\r\n").getBytes("UTF-8"));
}
}
} else {
return null;
}
}
return getPluginBean(new FileInputStream(localFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void run() {
URL url = new URL(HOST + name);
URLConnection connection = url.openConnection();
PluginBean pluginBean = PluginUtil
.getPluginBean(name, connection.getInputStream(), PluginContent.get(name).getVersion());
.checkAndUpdateLocalPlugin(name, connection.getInputStream());
if (pluginBean != null) {
PluginContent.set(name, pluginBean);
}
Expand Down

0 comments on commit c7f61e4

Please sign in to comment.