KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > inputIPFilters > ExpireBlackList


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.plugins.inputIPFilters;
22
23 import java.net.InetAddress JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 /**
32  * BlackList + temporary volatile blacklist.
33  * @author jfp
34  *
35  */

36 public class ExpireBlackList extends BlackList {
37     private Log log = LogFactory.getLog(ExpireBlackList.class);
38     private Map JavaDoc<InetAddress JavaDoc,Date JavaDoc> timedBlackList = new HashMap JavaDoc<InetAddress JavaDoc,Date JavaDoc>();
39     private int maxTable = 2000;
40     
41     
42     @Override JavaDoc
43     public boolean checkIP(InetAddress JavaDoc input) {
44         boolean fixed = super.checkIP(input);
45         if (!fixed)
46             return false;
47         
48         return true;
49     }
50     
51     public void setTimedBlackList (long time, String JavaDoc ip) {
52         expire ();
53         if (timedBlackList.size()>maxTable)
54             return;
55         long target = new Date JavaDoc().getTime()+time;
56         try {
57             log.info("Blacklisting "+ip+" for "+time+" seconds");
58             timedBlackList.put(InetAddress.getByName(ip),new Date JavaDoc(target));
59         } catch (UnknownHostException JavaDoc e) {
60             log.error("Blacklisting "+ip+" for "+time+" seconds failed",e);
61         }
62     }
63     
64     private void expire () {
65         Date JavaDoc now = new Date JavaDoc();
66         for (InetAddress JavaDoc addr : timedBlackList.keySet()) {
67             Date JavaDoc cmp = timedBlackList.get(addr);
68             if (cmp.getTime()<now.getTime())
69                 timedBlackList.remove(addr);
70         }
71     }
72 }
73
Popular Tags