From fabd88dc2585bf6798658f34a7ee122225316fe8 Mon Sep 17 00:00:00 2001 From: snowleo Date: Sat, 6 Oct 2012 16:49:40 +0200 Subject: Less locks please --- .../ess3/geoip/EssentialsGeoIPPlayerListener.java | 135 +++++++++------------ 1 file changed, 57 insertions(+), 78 deletions(-) (limited to 'EssentialsGeoIP/src/net/ess3/geoip/EssentialsGeoIPPlayerListener.java') diff --git a/EssentialsGeoIP/src/net/ess3/geoip/EssentialsGeoIPPlayerListener.java b/EssentialsGeoIP/src/net/ess3/geoip/EssentialsGeoIPPlayerListener.java index d2a30dbd8..5ccb5487a 100644 --- a/EssentialsGeoIP/src/net/ess3/geoip/EssentialsGeoIPPlayerListener.java +++ b/EssentialsGeoIP/src/net/ess3/geoip/EssentialsGeoIPPlayerListener.java @@ -50,113 +50,91 @@ public class EssentialsGeoIPPlayerListener implements Listener, IReload { return; } - config.acquireReadLock(); - try + if (event.getPlayer().getAddress() == null || event.getPlayer().getAddress().getAddress() == null) { - if (event.getPlayer().getAddress() == null || event.getPlayer().getAddress().getAddress() == null) { - return; - } - final InetAddress address = event.getPlayer().getAddress().getAddress(); - - final StringBuilder builder = new StringBuilder(); - if (config.getData().getDatabase().isShowCities()) + return; + } + final InetAddress address = event.getPlayer().getAddress().getAddress(); + + final StringBuilder builder = new StringBuilder(); + if (config.getData().getDatabase().isShowCities()) + { + final Location loc = ls.getLocation(address); + if (loc == null) { - final Location loc = ls.getLocation(address); - if (loc == null) - { - return; - } - if (loc.city != null) - { - builder.append(loc.city).append(", "); - } - final String region = regionName.regionNameByCode(loc.countryCode, loc.region); - if (region != null) - { - builder.append(region).append(", "); - } - builder.append(loc.countryName); + return; } - else + if (loc.city != null) { - builder.append(ls.getCountry(address).getName()); + builder.append(loc.city).append(", "); } - if (config.getData().isShowOnWhois()) + final String region = regionName.regionNameByCode(loc.countryCode, loc.region); + if (region != null) { - u.acquireWriteLock(); - try - { - u.getData().setGeolocation(builder.toString()); - } - finally - { - u.unlock(); - } + builder.append(region).append(", "); } - if (config.getData().isShowOnLogin() && !u.isHidden()) + builder.append(loc.countryName); + } + else + { + builder.append(ls.getCountry(address).getName()); + } + if (config.getData().isShowOnWhois()) + { + u.getData().setGeolocation(builder.toString()); + u.queueSave(); + } + if (config.getData().isShowOnLogin() && !u.isHidden()) + { + for (Player player : event.getPlayer().getServer().getOnlinePlayers()) { - for (Player player : event.getPlayer().getServer().getOnlinePlayers()) + final IUser user = ess.getUserMap().getUser(player); + if (Permissions.GEOIP_SHOW.isAuthorized(user)) { - final IUser user = ess.getUserMap().getUser(player); - if (Permissions.GEOIP_SHOW.isAuthorized(user)) - { - user.sendMessage(_("geoipJoinFormat", user.getPlayer().getDisplayName(), builder.toString())); - } + user.sendMessage(_("geoipJoinFormat", user.getPlayer().getDisplayName(), builder.toString())); } } } - finally - { - config.unlock(); - } } @Override public final void onReload() { config.onReload(); - config.acquireReadLock(); - try + if (config.getData().getDatabase().isShowCities()) { - if (config.getData().getDatabase().isShowCities()) - { - databaseFile = new File(geoip.getDataFolder(), "GeoIPCity.dat"); - } - else - { - databaseFile = new File(geoip.getDataFolder(), "GeoIP.dat"); - } - if (!databaseFile.exists()) + databaseFile = new File(geoip.getDataFolder(), "GeoIPCity.dat"); + } + else + { + databaseFile = new File(geoip.getDataFolder(), "GeoIP.dat"); + } + if (!databaseFile.exists()) + { + if (config.getData().getDatabase().isDownloadIfMissing()) { - if (config.getData().getDatabase().isDownloadIfMissing()) + if (config.getData().getDatabase().isShowCities()) { - if (config.getData().getDatabase().isShowCities()) - { - downloadDatabase(config.getData().getDatabase().getDownloadUrlCity()); - } - else - { - downloadDatabase(config.getData().getDatabase().getDownloadUrl()); - } + downloadDatabase(config.getData().getDatabase().getDownloadUrlCity()); } else { - LOGGER.log(Level.SEVERE, _("cantFindGeoIpDB")); - return; + downloadDatabase(config.getData().getDatabase().getDownloadUrl()); } } - try - { - ls = new LookupService(databaseFile); - } - catch (IOException ex) + else { - LOGGER.log(Level.SEVERE, _("cantReadGeoIpDB"), ex); + LOGGER.log(Level.SEVERE, _("cantFindGeoIpDB")); + return; } } - finally + try + { + ls = new LookupService(databaseFile); + } + catch (IOException ex) { - config.unlock(); + LOGGER.log(Level.SEVERE, _("cantReadGeoIpDB"), ex); } } @@ -167,7 +145,8 @@ public class EssentialsGeoIPPlayerListener implements Listener, IReload LOGGER.log(Level.SEVERE, _("geoIpUrlEmpty")); return; } - if (!databaseFile.getAbsoluteFile().getParentFile().exists()) { + if (!databaseFile.getAbsoluteFile().getParentFile().exists()) + { databaseFile.getAbsoluteFile().getParentFile().mkdirs(); } InputStream input = null; -- cgit v1.2.3