KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > HostMask


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

18 package net.sf.drftpd;
19
20 import java.net.InetAddress JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.apache.oro.text.GlobCompiler;
24 import org.apache.oro.text.regex.MalformedPatternException;
25 import org.apache.oro.text.regex.Pattern;
26 import org.apache.oro.text.regex.Perl5Matcher;
27
28 /**
29  * @author mog
30  * @version $Id: HostMask.java,v 1.9 2004/05/31 12:36:31 mog Exp $
31  */

32 public class HostMask {
33     private static final Logger logger = Logger.getLogger(HostMask.class);
34     private String JavaDoc _hostMask;
35     private String JavaDoc _identMask;
36
37     public HostMask(String JavaDoc string) {
38         int pos = string.indexOf('@');
39         if (pos == -1) {
40             _identMask = null;
41             _hostMask = string;
42         } else {
43             _identMask = string.substring(0, pos);
44             _hostMask = string.substring(pos + 1);
45         }
46     }
47
48     public String JavaDoc getHostMask() {
49         return _hostMask;
50     }
51
52     public String JavaDoc getIdentMask() {
53         return _identMask;
54     }
55
56     /**
57      * Is ident used?
58      * @return false is ident mask equals "*"
59      */

60     public boolean isIdentMaskSignificant() {
61         return _identMask != null && !_identMask.equals("*");
62     }
63
64     public boolean matches(String JavaDoc ident, InetAddress JavaDoc address) {
65         if(ident == null) ident = "";
66         Perl5Matcher m = new Perl5Matcher();
67
68         GlobCompiler c = new GlobCompiler();
69         try {
70             if (!isIdentMaskSignificant()
71                 || m.matches(ident, c.compile(getIdentMask()))) {
72                 Pattern p = c.compile(getHostMask());
73                 if (m.matches(address.getHostAddress(), p)
74                     || m.matches(address.getHostName(), p)) {
75                     return true;
76                 }
77             }
78             return false;
79         } catch (MalformedPatternException ex) {
80             logger.warn("", ex);
81             return false;
82         }
83     }
84 }
85
Popular Tags