diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java b/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java index b2b0ac5..6bfb699 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java @@ -88,7 +88,7 @@ public class CommandRepo extends BaseCommand { return StringUtil.copyPartialMatches(args[1], Arrays.asList(new String[] { "add", "list", "clean", "update", "del" }), new ArrayList()); } if (args.length == 3 && (args[1] == "add" || args[1] == "del")) { - return StringUtil.copyPartialMatches(args[2], YumManager.repo.getRepos(), new ArrayList()); + return StringUtil.copyPartialMatches(args[2], YumManager.repo.getRepos().keySet(), new ArrayList()); } } return null; diff --git a/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java b/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java index 0dd5d9e..da81788 100644 --- a/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java +++ b/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java @@ -2,8 +2,8 @@ package cn.citycraft.Yum.manager; import java.util.List; -import cn.citycraft.Yum.manager.Repositories.Plugin; -import cn.citycraft.Yum.manager.Repositories.TagInfo; +import cn.citycraft.Yum.manager.RepoSerialization.Plugin; +import cn.citycraft.Yum.manager.RepoSerialization.TagInfo; public class PluginInfo { public Plugin plugin; diff --git a/src/main/java/cn/citycraft/Yum/manager/RepoCache.java b/src/main/java/cn/citycraft/Yum/manager/RepoCache.java new file mode 100644 index 0000000..5bedcd2 --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/manager/RepoCache.java @@ -0,0 +1,68 @@ +package cn.citycraft.Yum.manager; + +import java.util.HashMap; +import java.util.Map; + +import cn.citycraft.PluginHelper.jsonresult.JsonResult; +import cn.citycraft.PluginHelper.utils.IOUtil; +import cn.citycraft.Yum.manager.RepoSerialization.Repositories; + +public class RepoCache { + protected static JsonResult jr = JsonResult.newJsonResult(); + Map plugins = new HashMap(); + Map repos = new HashMap(); + + public static RepoCache fromJson(final String json) { + return jr.fromJson(json, RepoCache.class); + } + + public void addPlugins(final String name, final PluginInfo info) { + plugins.put(name, info); + } + + public Repositories addRepo(final String repo) { + if (repos.containsKey(repo) || repo.isEmpty()) { + return null; + } + final Repositories reposes = getRepo(repo); + if (reposes == null) { + return null; + } + repos.put(repo, reposes); + return reposes; + + } + + public Map getPlugins() { + return plugins; + } + + public Repositories getRepo(final String repo) { + final String json = IOUtil.getData(repo); + if (json == null || json.isEmpty()) { + return null; + } + final Repositories reposes = jr.fromJson(json, Repositories.class); + if (reposes == null || reposes.repos.isEmpty()) { + return null; + } + return reposes; + } + + public Map getRepos() { + return repos; + } + + public boolean removeRepo(final String repo) { + if (repo.isEmpty() || !repos.containsKey(repo)) { + return false; + } + repos.remove(repo); + return true; + } + + @Override + public String toString() { + return jr.toJson(this); + } +} diff --git a/src/main/java/cn/citycraft/Yum/manager/Repositories.java b/src/main/java/cn/citycraft/Yum/manager/RepoSerialization.java similarity index 80% rename from src/main/java/cn/citycraft/Yum/manager/Repositories.java rename to src/main/java/cn/citycraft/Yum/manager/RepoSerialization.java index 0004c40..f844c17 100644 --- a/src/main/java/cn/citycraft/Yum/manager/Repositories.java +++ b/src/main/java/cn/citycraft/Yum/manager/RepoSerialization.java @@ -12,7 +12,7 @@ import java.util.List; * @author 蒋天蓓 * 2015年8月31日下午7:41:53 */ -public class Repositories { +public class RepoSerialization { public class PackageInfo { public String name; public List plugins = new ArrayList<>(); @@ -28,6 +28,11 @@ public class Repositories { public String version; } + public class Repositories { + public String name; + public List repos; + } + public class Repository { public String id; public String type; diff --git a/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java b/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java index 52d1d7c..96791dd 100644 --- a/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java @@ -3,12 +3,7 @@ */ package cn.citycraft.Yum.manager; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -18,14 +13,14 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; -import com.google.common.base.Charsets; -import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; -import com.google.gson.reflect.TypeToken; -import cn.citycraft.Yum.manager.Repositories.PackageInfo; -import cn.citycraft.Yum.manager.Repositories.Plugin; -import cn.citycraft.Yum.manager.Repositories.Repository; +import cn.citycraft.PluginHelper.jsonresult.JsonResult; +import cn.citycraft.PluginHelper.utils.IOUtil; +import cn.citycraft.Yum.manager.RepoSerialization.PackageInfo; +import cn.citycraft.Yum.manager.RepoSerialization.Plugin; +import cn.citycraft.Yum.manager.RepoSerialization.Repositories; +import cn.citycraft.Yum.manager.RepoSerialization.Repository; /** * 仓库管理类 @@ -33,21 +28,17 @@ import cn.citycraft.Yum.manager.Repositories.Repository; * @author 蒋天蓓 */ public class RepositoryManager { - Gson gson; + JsonResult jr = JsonResult.newJsonResult(); org.bukkit.plugin.Plugin main; - Map plugins; - - List repos; + RepoCache repocache; public RepositoryManager(final org.bukkit.plugin.Plugin plugin) { this.main = plugin; - gson = new Gson(); - plugins = new HashMap(); - repos = new ArrayList(); + repocache = new RepoCache(); } public boolean addPackage(final CommandSender sender, final String urlstring) { - final String json = getHtml(urlstring); + final String json = IOUtil.getData(urlstring); if (json == null || json.isEmpty()) { return false; } @@ -68,33 +59,28 @@ public class RepositoryManager { if (!url.endsWith("repo.info")) { url = url + "/repo.info"; } - if (urllength == 0 || repos.contains(url)) { + final Repositories repo = repocache.addRepo(urlstring); + if (repo == null) { return false; } - repos.add(urlstring); - return updateRepositories(sender, urlstring); + return updateRepositories(sender, repo); } public void cacheToJson(final FileConfiguration config) { - config.set("repocache", gson.toJson(repos)); - config.set("plugincache", gson.toJson(plugins)); + config.set("reposcache", repocache.toString()); } public void clean() { - plugins.clear(); + repocache.getPlugins().clear(); } public boolean delRepositories(final CommandSender sender, final String urlstring) { - if (urlstring.isEmpty() || !repos.contains(urlstring)) { - return false; - } - repos.remove(urlstring); - return true; + return repocache.removeRepo(urlstring); } public List getAllPlugin() { final List li = new ArrayList(); - for (final Entry plugin : plugins.entrySet()) { + for (final Entry plugin : repocache.getPlugins().entrySet()) { li.add(plugin.getValue()); } return li; @@ -102,7 +88,7 @@ public class RepositoryManager { public List getAllPluginName() { final List li = new ArrayList(); - for (final Entry plugin : plugins.entrySet()) { + for (final Entry plugin : repocache.getPlugins().entrySet()) { li.add(plugin.getValue().plugin.name); } return li; @@ -110,30 +96,15 @@ public class RepositoryManager { public List getAllPluginsInfo() { final List li = new ArrayList(); - for (final Entry plugin : plugins.entrySet()) { + for (final Entry plugin : repocache.getPlugins().entrySet()) { final Plugin pl = plugin.getValue().plugin; li.add(String.format("§d%s §a%s(%s) §6- §e%s", plugin.getValue().repo, pl.name, pl.version, pl.description)); } return li; } - public String getHtml(final String urlstring) { - String html = ""; - try { - final URL url = new URL(urlstring); - final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); - String line; - while ((line = br.readLine()) != null) { - html += line; - } - return html; - } catch (final IOException e) { - return null; - } - } - public PluginInfo getPlugin(final String name) { - for (final Entry plugin : plugins.entrySet()) { + for (final Entry plugin : repocache.getPlugins().entrySet()) { if (plugin.getValue().plugin.name.equalsIgnoreCase(name)) { return plugin.getValue(); } @@ -143,7 +114,7 @@ public class RepositoryManager { public List getPluginInfo(final String name) { final List li = new ArrayList(); - for (final Entry plugin : plugins.entrySet()) { + for (final Entry plugin : repocache.getPlugins().entrySet()) { if (plugin.getValue().plugin.name.equalsIgnoreCase(name)) { li.add(plugin.getValue()); } @@ -152,50 +123,39 @@ public class RepositoryManager { } public PluginInfo getPluginInfo(final String groupId, final String artifactId) { - return plugins.get(groupId + "." + artifactId); + return repocache.getPlugins().get(groupId + "." + artifactId); } public Map getPlugins() { - return plugins; + return repocache.getPlugins(); } - public List getRepos() { - return repos; + public Map getRepos() { + return repocache.getRepos(); } public boolean jsonToCache(final FileConfiguration config) { - final String repocache = config.getString("repocache"); - final String plugincache = config.getString("plugincache"); try { - if (repocache != null && !repocache.isEmpty()) { - repos = gson.fromJson(repocache, new TypeToken>() { - }.getType()); + final String reposcache = config.getString("reposcache"); + if (reposcache != null && !reposcache.isEmpty()) { + repocache = RepoCache.fromJson(reposcache); + return true; } - if (plugincache != null && !plugincache.isEmpty()) { - plugins = gson.fromJson(plugincache, new TypeToken>() { - }.getType()); - } - return true; - } catch (final JsonSyntaxException e) { - return false; + } catch (final Exception e) { } + return false; } public PackageInfo jsonToPackage(final String json) { try { - return gson.fromJson(json, PackageInfo.class); + return jr.fromJson(json, PackageInfo.class); } catch (final JsonSyntaxException e) { return null; } } - public List jsonToRepositories(final String json) { - try { - return gson.fromJson(json, new TypeToken>() { - }.getType()); - } catch (final JsonSyntaxException e) { - return new ArrayList(); - } + public Repositories jsonToRepositories(final String json) { + return jr.fromJson(json, Repositories.class); } public void updatePackage(final CommandSender sender, final PackageInfo pkg) { @@ -204,43 +164,39 @@ public class RepositoryManager { pi.plugin = plugin; pi.url = pkg.url; pi.repo = pkg.name; - plugins.put(plugin.groupId + "." + plugin.artifactId, pi); + repocache.getPlugins().put(plugin.groupId + "." + plugin.artifactId, pi); } sender.sendMessage("§6仓库: §e" + pkg.name + " §a更新成功!"); } public boolean updateRepositories(final CommandSender sender) { - plugins.clear(); - if (repos.isEmpty()) { - repos.add("http://citycraft.cn/repo/repo.info"); + repocache.getPlugins().clear(); + if (repocache.getRepos().isEmpty()) { + repocache.addRepo("http://citycraft.cn/yumcenter/repo.info"); } - final Iterator keys = repos.iterator(); + final Iterator> keys = repocache.getRepos().entrySet().iterator(); while (keys.hasNext()) { - final String string = keys.next(); - if (updateRepositories(sender, string)) { - sender.sendMessage("§6源: §e" + string + " §a更新成功!"); + final Entry string = keys.next(); + final Repositories repo = repocache.getRepo(string.getKey()); + if (updateRepositories(sender, repo)) { + sender.sendMessage("§6源: §e" + repo.name + " §a更新成功!"); } else { - sender.sendMessage("§6源: §e" + string + " §c未找到任何仓库信息 已删除!"); + sender.sendMessage("§6源: §e" + string.getKey() + " §c未找到任何仓库信息 已删除!"); keys.remove(); } } return true; } - public boolean updateRepositories(CommandSender sender, final String urlstring) { + public boolean updateRepositories(CommandSender sender, final Repositories repocenter) { if (sender == null) { sender = Bukkit.getConsoleSender(); } - final String json = getHtml(urlstring); - if (json == null || json.isEmpty()) { + if (repocenter == null || repocenter.repos.isEmpty()) { return false; } - final List lrepo = jsonToRepositories(json); - if (lrepo == null || lrepo.isEmpty()) { - return true; - } - for (final Repository repository : lrepo) { - addPackage(sender, repository.url); + for (final Repository repo : repocenter.repos) { + addPackage(sender, repo.url); } return true; }