修改缓存模式...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-11-14 10:19:38 +08:00
parent 1d29be976d
commit 418089a50f
5 changed files with 125 additions and 96 deletions

View File

@ -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<String>()); return StringUtil.copyPartialMatches(args[1], Arrays.asList(new String[] { "add", "list", "clean", "update", "del" }), new ArrayList<String>());
} }
if (args.length == 3 && (args[1] == "add" || args[1] == "del")) { if (args.length == 3 && (args[1] == "add" || args[1] == "del")) {
return StringUtil.copyPartialMatches(args[2], YumManager.repo.getRepos(), new ArrayList<String>()); return StringUtil.copyPartialMatches(args[2], YumManager.repo.getRepos().keySet(), new ArrayList<String>());
} }
} }
return null; return null;

View File

@ -2,8 +2,8 @@ package cn.citycraft.Yum.manager;
import java.util.List; import java.util.List;
import cn.citycraft.Yum.manager.Repositories.Plugin; import cn.citycraft.Yum.manager.RepoSerialization.Plugin;
import cn.citycraft.Yum.manager.Repositories.TagInfo; import cn.citycraft.Yum.manager.RepoSerialization.TagInfo;
public class PluginInfo { public class PluginInfo {
public Plugin plugin; public Plugin plugin;

View File

@ -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<String, PluginInfo> plugins = new HashMap<String, PluginInfo>();
Map<String, Repositories> repos = new HashMap<String, Repositories>();
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<String, PluginInfo> 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<String, Repositories> 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);
}
}

View File

@ -12,7 +12,7 @@ import java.util.List;
* @author 蒋天蓓 * @author 蒋天蓓
* 2015年8月31日下午7:41:53 * 2015年8月31日下午7:41:53
*/ */
public class Repositories { public class RepoSerialization {
public class PackageInfo { public class PackageInfo {
public String name; public String name;
public List<Plugin> plugins = new ArrayList<>(); public List<Plugin> plugins = new ArrayList<>();
@ -28,6 +28,11 @@ public class Repositories {
public String version; public String version;
} }
public class Repositories {
public String name;
public List<Repository> repos;
}
public class Repository { public class Repository {
public String id; public String id;
public String type; public String type;

View File

@ -3,12 +3,7 @@
*/ */
package cn.citycraft.Yum.manager; 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.ArrayList;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -18,14 +13,14 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; 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.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import cn.citycraft.Yum.manager.Repositories.PackageInfo; import cn.citycraft.PluginHelper.jsonresult.JsonResult;
import cn.citycraft.Yum.manager.Repositories.Plugin; import cn.citycraft.PluginHelper.utils.IOUtil;
import cn.citycraft.Yum.manager.Repositories.Repository; 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 蒋天蓓 * @author 蒋天蓓
*/ */
public class RepositoryManager { public class RepositoryManager {
Gson gson; JsonResult jr = JsonResult.newJsonResult();
org.bukkit.plugin.Plugin main; org.bukkit.plugin.Plugin main;
Map<String, PluginInfo> plugins; RepoCache repocache;
List<String> repos;
public RepositoryManager(final org.bukkit.plugin.Plugin plugin) { public RepositoryManager(final org.bukkit.plugin.Plugin plugin) {
this.main = plugin; this.main = plugin;
gson = new Gson(); repocache = new RepoCache();
plugins = new HashMap<String, PluginInfo>();
repos = new ArrayList<String>();
} }
public boolean addPackage(final CommandSender sender, final String urlstring) { 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()) { if (json == null || json.isEmpty()) {
return false; return false;
} }
@ -68,33 +59,28 @@ public class RepositoryManager {
if (!url.endsWith("repo.info")) { if (!url.endsWith("repo.info")) {
url = url + "/repo.info"; url = url + "/repo.info";
} }
if (urllength == 0 || repos.contains(url)) { final Repositories repo = repocache.addRepo(urlstring);
if (repo == null) {
return false; return false;
} }
repos.add(urlstring); return updateRepositories(sender, repo);
return updateRepositories(sender, urlstring);
} }
public void cacheToJson(final FileConfiguration config) { public void cacheToJson(final FileConfiguration config) {
config.set("repocache", gson.toJson(repos)); config.set("reposcache", repocache.toString());
config.set("plugincache", gson.toJson(plugins));
} }
public void clean() { public void clean() {
plugins.clear(); repocache.getPlugins().clear();
} }
public boolean delRepositories(final CommandSender sender, final String urlstring) { public boolean delRepositories(final CommandSender sender, final String urlstring) {
if (urlstring.isEmpty() || !repos.contains(urlstring)) { return repocache.removeRepo(urlstring);
return false;
}
repos.remove(urlstring);
return true;
} }
public List<PluginInfo> getAllPlugin() { public List<PluginInfo> getAllPlugin() {
final List<PluginInfo> li = new ArrayList<PluginInfo>(); final List<PluginInfo> li = new ArrayList<PluginInfo>();
for (final Entry<String, PluginInfo> plugin : plugins.entrySet()) { for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
li.add(plugin.getValue()); li.add(plugin.getValue());
} }
return li; return li;
@ -102,7 +88,7 @@ public class RepositoryManager {
public List<String> getAllPluginName() { public List<String> getAllPluginName() {
final List<String> li = new ArrayList<String>(); final List<String> li = new ArrayList<String>();
for (final Entry<String, PluginInfo> plugin : plugins.entrySet()) { for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
li.add(plugin.getValue().plugin.name); li.add(plugin.getValue().plugin.name);
} }
return li; return li;
@ -110,30 +96,15 @@ public class RepositoryManager {
public List<String> getAllPluginsInfo() { public List<String> getAllPluginsInfo() {
final List<String> li = new ArrayList<String>(); final List<String> li = new ArrayList<String>();
for (final Entry<String, PluginInfo> plugin : plugins.entrySet()) { for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
final Plugin pl = plugin.getValue().plugin; 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)); li.add(String.format("§d%s §a%s(%s) §6- §e%s", plugin.getValue().repo, pl.name, pl.version, pl.description));
} }
return li; 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) { public PluginInfo getPlugin(final String name) {
for (final Entry<String, PluginInfo> plugin : plugins.entrySet()) { for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
if (plugin.getValue().plugin.name.equalsIgnoreCase(name)) { if (plugin.getValue().plugin.name.equalsIgnoreCase(name)) {
return plugin.getValue(); return plugin.getValue();
} }
@ -143,7 +114,7 @@ public class RepositoryManager {
public List<PluginInfo> getPluginInfo(final String name) { public List<PluginInfo> getPluginInfo(final String name) {
final List<PluginInfo> li = new ArrayList<PluginInfo>(); final List<PluginInfo> li = new ArrayList<PluginInfo>();
for (final Entry<String, PluginInfo> plugin : plugins.entrySet()) { for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
if (plugin.getValue().plugin.name.equalsIgnoreCase(name)) { if (plugin.getValue().plugin.name.equalsIgnoreCase(name)) {
li.add(plugin.getValue()); li.add(plugin.getValue());
} }
@ -152,50 +123,39 @@ public class RepositoryManager {
} }
public PluginInfo getPluginInfo(final String groupId, final String artifactId) { public PluginInfo getPluginInfo(final String groupId, final String artifactId) {
return plugins.get(groupId + "." + artifactId); return repocache.getPlugins().get(groupId + "." + artifactId);
} }
public Map<String, PluginInfo> getPlugins() { public Map<String, PluginInfo> getPlugins() {
return plugins; return repocache.getPlugins();
} }
public List<String> getRepos() { public Map<String, Repositories> getRepos() {
return repos; return repocache.getRepos();
} }
public boolean jsonToCache(final FileConfiguration config) { public boolean jsonToCache(final FileConfiguration config) {
final String repocache = config.getString("repocache");
final String plugincache = config.getString("plugincache");
try { try {
if (repocache != null && !repocache.isEmpty()) { final String reposcache = config.getString("reposcache");
repos = gson.fromJson(repocache, new TypeToken<List<String>>() { if (reposcache != null && !reposcache.isEmpty()) {
}.getType()); repocache = RepoCache.fromJson(reposcache);
return true;
} }
if (plugincache != null && !plugincache.isEmpty()) { } catch (final Exception e) {
plugins = gson.fromJson(plugincache, new TypeToken<Map<String, PluginInfo>>() {
}.getType());
}
return true;
} catch (final JsonSyntaxException e) {
return false;
} }
return false;
} }
public PackageInfo jsonToPackage(final String json) { public PackageInfo jsonToPackage(final String json) {
try { try {
return gson.fromJson(json, PackageInfo.class); return jr.fromJson(json, PackageInfo.class);
} catch (final JsonSyntaxException e) { } catch (final JsonSyntaxException e) {
return null; return null;
} }
} }
public List<Repository> jsonToRepositories(final String json) { public Repositories jsonToRepositories(final String json) {
try { return jr.fromJson(json, Repositories.class);
return gson.fromJson(json, new TypeToken<List<Repository>>() {
}.getType());
} catch (final JsonSyntaxException e) {
return new ArrayList<Repository>();
}
} }
public void updatePackage(final CommandSender sender, final PackageInfo pkg) { public void updatePackage(final CommandSender sender, final PackageInfo pkg) {
@ -204,43 +164,39 @@ public class RepositoryManager {
pi.plugin = plugin; pi.plugin = plugin;
pi.url = pkg.url; pi.url = pkg.url;
pi.repo = pkg.name; 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更新成功!"); sender.sendMessage("§6仓库: §e" + pkg.name + " §a更新成功!");
} }
public boolean updateRepositories(final CommandSender sender) { public boolean updateRepositories(final CommandSender sender) {
plugins.clear(); repocache.getPlugins().clear();
if (repos.isEmpty()) { if (repocache.getRepos().isEmpty()) {
repos.add("http://citycraft.cn/repo/repo.info"); repocache.addRepo("http://citycraft.cn/yumcenter/repo.info");
} }
final Iterator<String> keys = repos.iterator(); final Iterator<Entry<String, Repositories>> keys = repocache.getRepos().entrySet().iterator();
while (keys.hasNext()) { while (keys.hasNext()) {
final String string = keys.next(); final Entry<String, Repositories> string = keys.next();
if (updateRepositories(sender, string)) { final Repositories repo = repocache.getRepo(string.getKey());
sender.sendMessage("§6源: §e" + string + " §a更新成功!"); if (updateRepositories(sender, repo)) {
sender.sendMessage("§6源: §e" + repo.name + " §a更新成功!");
} else { } else {
sender.sendMessage("§6源: §e" + string + " §c未找到任何仓库信息 已删除!"); sender.sendMessage("§6源: §e" + string.getKey() + " §c未找到任何仓库信息 已删除!");
keys.remove(); keys.remove();
} }
} }
return true; return true;
} }
public boolean updateRepositories(CommandSender sender, final String urlstring) { public boolean updateRepositories(CommandSender sender, final Repositories repocenter) {
if (sender == null) { if (sender == null) {
sender = Bukkit.getConsoleSender(); sender = Bukkit.getConsoleSender();
} }
final String json = getHtml(urlstring); if (repocenter == null || repocenter.repos.isEmpty()) {
if (json == null || json.isEmpty()) {
return false; return false;
} }
final List<Repository> lrepo = jsonToRepositories(json); for (final Repository repo : repocenter.repos) {
if (lrepo == null || lrepo.isEmpty()) { addPackage(sender, repo.url);
return true;
}
for (final Repository repository : lrepo) {
addPackage(sender, repository.url);
} }
return true; return true;
} }