View Javadoc

1   /*
2    * CertificateHostGroup.java
3    *
4    * Created on May 10, 2005, 3:56 PM
5    *
6    * To change this template, choose Tools | Options and locate the template under
7    * the Source Creation and Management node. Right-click the template and choose
8    * Open. You can then make changes to the template in the Source Editor.
9    */
10  
11  package gov.bnl.gums;
12  
13  import java.util.ArrayList;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.StringTokenizer;
17  
18  /*** Represent a set of services identified by a wildcard on their CN or DN.
19   *
20   * @author Gabriele Carcassi
21   * @since 1.1.0
22   */
23  public class CertificateHostGroup extends AbstractHostGroup {
24      
25      private String cn;
26      private String dn;
27      private List regexs;
28      
29      public boolean isInGroup(String hostname) {
30          Iterator iter = regexs.iterator();
31          while (iter.hasNext()) {
32              if (hostname.matches((String) iter.next()))
33                  return true;
34          }
35          return false;
36      }
37      
38      /*** Retrieves the wildcard that will be used to match the CN.
39       * @return The wildcard (i.e. '*.mycompany.com').
40       */
41      public String getCn() {
42          return this.cn;
43      }
44      
45      /*** Changes the wildcard that will be used to match the CN.
46       * @param cn The new wildcard (i.e. '*.mycompany.com').
47       */
48      public void setCn(String cn) {
49          this.cn = cn;
50          updateRegEx();
51      }
52      
53      /*** Retrieves the wildcard that will be used to match the DN.
54       * @return The wildcard (i.e. '/DC=org/DC=doegrids/OU=Services/CN=*.mycompany.com').
55       */
56      public String getDn() {
57          return this.dn;
58      }
59      
60      /*** Changes the wildcard that will be used to match the DN.
61       * @param wildcard The new wildcard (i.e. '/DC=org/DC=doegrids/OU=Services/CN=*.mycompany.com').
62       */
63      public void setDn(String dn) {
64          this.dn = dn;
65          updateRegEx();
66      }
67      
68      private void updateRegEx() {
69          regexs = new ArrayList();
70          if (cn != null) {
71              StringTokenizer tokens = new StringTokenizer(cn, ",");
72              while (tokens.hasMoreTokens()) {
73                  String regex = tokens.nextToken();
74                  regex = regex.replaceAll("//.", "//.");
75                  regex = regex.replaceAll("//*", "[^//./=]*");
76                  regexs.add("(/[^=]*=[^=]*)*/CN=" + regex + "(/[^=]*=[^=]*)*");
77              }
78          }
79          if (dn != null) {
80              StringTokenizer tokens = new StringTokenizer(dn, ",");
81              while (tokens.hasMoreTokens()) {
82                  String regex = tokens.nextToken();
83                  regex = regex.replaceAll("//.", "//.");
84                  regex = regex.replaceAll("//*", "[^//./=]*");
85                  regexs.add(regex);
86              }
87          }
88      }
89  }