View Javadoc

1   /*
2    * WildcardHostGroup.java
3    *
4    * Created on May 27, 2004, 2:51 PM
5    */
6   
7   package gov.bnl.gums;
8   
9   import java.util.*;
10  import java.util.regex.*;
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  /*** Matches a set of hosts to a list of group mapping by comparing the hostname
15   * with a wildcard. Examples of valid wildcards are 'star*.rhic.bnl.gov',
16   * '*.usatlas.bnl.gov', 'grid.*.edu', '*test*.bnl.gov'.
17   * <p>
18   * The '*' character can represent any string part of a single token of the hostname.
19   * That is, it can't repesent a string that contains '.'.
20   *
21   * @author  Gabriele Carcassi
22   */
23  public class WildcardHostGroup extends AbstractHostGroup {
24      static Log adminLog = LogFactory.getLog(GUMS.resourceAdminLog);
25      
26      private String wildcard;
27      private List regexs;
28      
29      /*** Creates a new wildcard mapping, which needs to be properly configured. */
30      public WildcardHostGroup() {
31          adminLog.warn("The use of gov.bnl.gums.WildcardHostGroup is deprecated. Please use gov.bnl.gums.CertificateHostGroup: it provides equivalent functionalities.");
32      }
33      
34      public boolean isInGroup(String hostname) {
35          Iterator iter = regexs.iterator();
36          while (iter.hasNext()) {
37              if (hostname.matches((String) iter.next()))
38                  return true;
39          }
40          return false;
41      }
42      
43      /*** Retrieves the wildcard that will be used to match the hostname.
44       * @return The wildcard (i.e. '*.mycompany.com').
45       */
46      public String getWildcard() {
47          return this.wildcard;
48      }
49      
50      /*** Changes the wildcard that will be used to match the hostname.
51       * @param wildcard The new wildcard (i.e. '*.mycompany.com').
52       */
53      public void setWildcard(String wildcard) {
54          this.wildcard = wildcard;
55          StringTokenizer tokens = new StringTokenizer(wildcard, ",");
56          regexs = new ArrayList();
57          while (tokens.hasMoreTokens()) {
58              String regex = tokens.nextToken();
59              regex = regex.replaceAll("//.", "//.");
60              regex = regex.replaceAll("//*", "[^//.]*");
61              regexs.add(regex);
62              regexs.add("(/[^=]*=[^=]*)*/CN=" + regex + "(/[^=]*=[^=]*)*");
63          }
64      }
65      
66  }