1 18 19 package org.apache.roller.util; 20 21 import java.io.BufferedReader ; 22 import java.io.FileReader ; 23 import java.io.FileWriter ; 24 import java.io.PrintWriter ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.config.RollerConfig; 30 31 32 39 public class IPBanList { 40 41 private static Log log = LogFactory.getLog(IPBanList.class); 42 43 private Set bannedIps = new HashSet (); 45 46 private ModifiedFile bannedIpsFile = null; 48 49 private static IPBanList instance = null; 51 52 53 static { 54 instance = new IPBanList(); 55 } 56 57 58 private IPBanList() { 60 61 log.debug("INIT"); 62 63 String banIpsFilePath = RollerConfig.getProperty("ipbanlist.file"); 65 if(banIpsFilePath != null) { 66 ModifiedFile banIpsFile = new ModifiedFile(banIpsFilePath); 67 68 if(banIpsFile.exists() && banIpsFile.canRead()) { 69 this.bannedIpsFile = banIpsFile; 70 this.loadBannedIps(); 71 } 72 } 73 } 74 75 76 public static IPBanList getInstance() { 78 return instance; 79 } 80 81 82 public boolean isBanned(String ip) { 83 84 this.loadBannedIpsIfNeeded(false); 86 87 if(ip != null) { 88 return this.bannedIps.contains(ip); 89 } else { 90 return false; 91 } 92 } 93 94 95 public void addBannedIp(String ip) { 96 97 if(ip == null) { 98 return; 99 } 100 101 this.loadBannedIpsIfNeeded(false); 103 104 if(!this.bannedIps.contains(ip) && 105 (bannedIpsFile != null && bannedIpsFile.canWrite())) { 106 107 try { 108 synchronized(this) { 109 PrintWriter out = new PrintWriter (new FileWriter (this.bannedIpsFile, true)); 111 out.println(ip); 112 out.close(); 113 this.bannedIpsFile.clearChanged(); 114 115 this.bannedIps.add(ip); 117 } 118 119 log.debug("ADDED "+ip); 120 } catch(Exception e) { 121 log.error("Error adding banned ip to file", e); 122 } 123 } 124 } 125 126 127 130 private void loadBannedIpsIfNeeded(boolean forceLoad) { 131 132 if(bannedIpsFile != null && 133 (bannedIpsFile.hasChanged() || forceLoad)) { 134 135 this.loadBannedIps(); 137 } 138 } 139 140 141 145 private synchronized void loadBannedIps() { 146 147 if(bannedIpsFile != null) { 148 149 try { 150 HashSet newBannedIpList = new HashSet (); 151 152 BufferedReader in = new BufferedReader (new FileReader (this.bannedIpsFile)); 154 155 String ip = null; 156 while((ip = in.readLine()) != null) { 157 newBannedIpList.add(ip); 158 } 159 160 in.close(); 161 162 this.bannedIps = newBannedIpList; 164 this.bannedIpsFile.clearChanged(); 165 166 log.info(this.bannedIps.size()+" banned ips loaded"); 167 } catch(Exception ex) { 168 log.error("Error loading banned ips from file", ex); 169 } 170 171 } 172 } 173 174 175 private class ModifiedFile extends java.io.File { 178 179 private long myLastModified = 0; 180 181 public ModifiedFile(String filePath) { 182 super(filePath); 183 184 this.myLastModified = lastModified(); 185 } 186 187 public boolean hasChanged() { 188 if(lastModified() != myLastModified) { 189 return true; 190 } else { 191 return false; 192 } 193 } 194 195 public void clearChanged() { 196 myLastModified = lastModified(); 197 } 198 } 199 200 } 201 | Popular Tags |