1515import java .util .List ;
1616import java .util .Objects ;
1717import java .util .concurrent .CompletableFuture ;
18+ import java .util .function .Consumer ;
1819import lombok .extern .slf4j .Slf4j ;
1920import org .apache .commons .lang .StringUtils ;
2021
@@ -35,9 +36,7 @@ public static PluginManager getInstance() {
3536
3637 private final String localPluginsPath ;
3738
38- private final List <PluginJarInfo > serverPluginList = new ArrayList <>(); // 服务端插件列表
39-
40- private final List <PluginJarInfo > localPluginList = new ArrayList <>(); // 本地插件列表
39+ private final List <PluginJarInfo > pluginList = new ArrayList <>(); // 插件列表
4140
4241 public PluginManager (String localPluginsPath ) {
4342 this .localPluginsPath = localPluginsPath ;
@@ -47,14 +46,21 @@ public PluginManager(String localPluginsPath) {
4746 }
4847 }
4948
50- public List <PluginJarInfo > getLocalPluginList () {
51- return Collections .unmodifiableList (this .localPluginList );
49+ ////////////////////////////////////////////////////////////// 查询插件
50+
51+ public List <PluginJarInfo > getPluginList () {
52+ return Collections .unmodifiableList (this .pluginList );
5253 }
5354
54- public List <PluginJarInfo > getServerPluginList () {
55- return Collections .unmodifiableList (this .serverPluginList );
55+
56+ public PluginJarInfo getPlugin (String jarName ) {
57+ return this .pluginList .stream ()
58+ .filter (plugin -> Objects .equals (plugin .getJarName (), jarName ))
59+ .findFirst ().orElse (null );
5660 }
5761
62+ ////////////////////////////////////////////////////////////// 更新插件
63+
5864 public void loadLocalPlugins () {
5965 try {
6066 Path path = Paths .get (this .localPluginsPath );
@@ -63,7 +69,13 @@ public void loadLocalPlugins() {
6369 }
6470
6571 String json = new String (Files .readAllBytes (path ), DEFAULT_CHARSET );
66- this .localPluginList .addAll (JSON .parseArray (json , PluginJarInfo .class ));
72+ JSON .parseArray (json , PluginJarInfo .class ).forEach (plugin -> {
73+ this .addOrUpdatePlugin (plugin , exist -> {
74+ exist .setLocalVersionNumber (plugin .getLocalVersionNumber ());
75+ exist .setIsDownload (plugin .getIsDownload ());
76+ exist .setIsEnable (plugin .getIsEnable ());
77+ });
78+ });
6779 } catch (IOException e ) {
6880 log .error ("读取插件配置失败" , e );
6981 }
@@ -72,7 +84,15 @@ public void loadLocalPlugins() {
7284 public void loadServerPlugins () {
7385 try {
7486 String json = HttpUtil .get (SERVER_PLUGINS_URL );
75- this .serverPluginList .addAll (JSON .parseArray (json , PluginJarInfo .class ));
87+ JSON .parseArray (json , PluginJarInfo .class ).forEach (plugin -> {
88+ this .addOrUpdatePlugin (plugin , exist -> {
89+ exist .setName (plugin .getName ());
90+ exist .setSynopsis (plugin .getSynopsis ());
91+ exist .setVersion (plugin .getVersion ());
92+ exist .setVersionNumber (plugin .getVersionNumber ());
93+ exist .setDownloadUrl (plugin .getDownloadUrl ());
94+ });
95+ });
7696 } catch (Exception e ) {
7797 log .error ("下载插件列表失败" , e );
7898 }
@@ -83,25 +103,38 @@ public CompletableFuture<Void> loadServerPluginsAsync() {
83103 return CompletableFuture .runAsync (this ::loadServerPlugins );
84104 }
85105
86- public PluginJarInfo getLocalPlugin (String jarName ) {
87- return this .localPluginList .stream ()
88- .filter (plugin -> Objects .equals (plugin .getJarName (), jarName ))
89- .findFirst ().orElse (null );
106+ private void addOrUpdatePlugin (PluginJarInfo pluginJarInfo , Consumer <PluginJarInfo > ifExists ) {
107+ PluginJarInfo exists = getPlugin (pluginJarInfo .getJarName ());
108+ if (exists == null ) {
109+ this .pluginList .add (pluginJarInfo );
110+ } else {
111+ ifExists .accept (exists );
112+ }
90113 }
91114
115+ ////////////////////////////////////////////////////////////// 下载插件
116+
92117 public File downloadPlugin (PluginJarInfo pluginJarInfo ) throws IOException {
118+ PluginJarInfo plugin = getPlugin (pluginJarInfo .getJarName ());
119+ if (plugin == null ) {
120+ throw new IllegalStateException ("没有找到插件 " + pluginJarInfo .getJarName ());
121+ }
122+
93123 File file = new File ("libs/" , pluginJarInfo .getJarName () + "-" + pluginJarInfo .getVersion () + ".jar" );
94124 HttpUtil .downloadFile (pluginJarInfo .getDownloadUrl (), file );
95125
96- pluginJarInfo .setIsDownload (true );
97- this .localPluginList .add (pluginJarInfo );
98- this .saveLocalPlugins ();
126+ plugin .setIsDownload (true );
127+ plugin .setIsEnable (true );
128+ plugin .setLocalVersionNumber (plugin .getVersionNumber ());
129+ this .saveToFile ();
99130
100131 return file ;
101132 }
102133
103- public void saveLocalPlugins () throws IOException {
104- String json = JSON .toJSONString (this .localPluginList , true );
134+ ////////////////////////////////////////////////////////////// 保存配置
135+
136+ public void saveToFile () throws IOException {
137+ String json = JSON .toJSONString (this .pluginList , true );
105138 Files .write (
106139 Paths .get (this .localPluginsPath ),
107140 json .getBytes (DEFAULT_CHARSET )
0 commit comments