修改缓存模式...

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>());
}
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;

View File

@ -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;

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

View File

@ -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<String, PluginInfo> plugins;
List<String> repos;
RepoCache repocache;
public RepositoryManager(final org.bukkit.plugin.Plugin plugin) {
this.main = plugin;
gson = new Gson();
plugins = new HashMap<String, PluginInfo>();
repos = new ArrayList<String>();
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<PluginInfo> getAllPlugin() {
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());
}
return li;
@ -102,7 +88,7 @@ public class RepositoryManager {
public List<String> getAllPluginName() {
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);
}
return li;
@ -110,30 +96,15 @@ public class RepositoryManager {
public List<String> getAllPluginsInfo() {
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;
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<String, PluginInfo> plugin : plugins.entrySet()) {
for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
if (plugin.getValue().plugin.name.equalsIgnoreCase(name)) {
return plugin.getValue();
}
@ -143,7 +114,7 @@ public class RepositoryManager {
public List<PluginInfo> getPluginInfo(final String name) {
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)) {
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<String, PluginInfo> getPlugins() {
return plugins;
return repocache.getPlugins();
}
public List<String> getRepos() {
return repos;
public Map<String, Repositories> 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<List<String>>() {
}.getType());
}
if (plugincache != null && !plugincache.isEmpty()) {
plugins = gson.fromJson(plugincache, new TypeToken<Map<String, PluginInfo>>() {
}.getType());
}
final String reposcache = config.getString("reposcache");
if (reposcache != null && !reposcache.isEmpty()) {
repocache = RepoCache.fromJson(reposcache);
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<Repository> jsonToRepositories(final String json) {
try {
return gson.fromJson(json, new TypeToken<List<Repository>>() {
}.getType());
} catch (final JsonSyntaxException e) {
return new ArrayList<Repository>();
}
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<String> keys = repos.iterator();
final Iterator<Entry<String, Repositories>> 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, Repositories> 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<Repository> 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;
}