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.hostToGroup;
12  
13  import gov.bnl.gums.configuration.Configuration;
14  
15  import java.util.ArrayList;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.StringTokenizer;
19  
20  /** Represent a set of services identified by a wildcard on their CN or DN.
21   *
22   * @author Gabriele Carcassi, Jay Packard
23   */
24  public class CertificateHostToGroupMapping extends HostToGroupMapping {
25      private String cn = null;
26      private String dn = null;
27      private List regexs;
28      
29      /**
30       * Create a new CertificateHostToGroupMapping object.
31       */
32      public CertificateHostToGroupMapping() {
33      	super();
34      }
35      
36      /**
37       * Create a new CertificateHostToGroupMapping object.
38       * 
39       * @param configuration
40       */
41      public CertificateHostToGroupMapping(Configuration configuration) {
42      	super(configuration);
43      }
44  
45      public HostToGroupMapping clone(Configuration configuration) {
46      	CertificateHostToGroupMapping hostToGroupMapping = new CertificateHostToGroupMapping(configuration);
47      	hostToGroupMapping.setDescription(new String(getDescription()));
48      	if (getDn()!=null)
49      		hostToGroupMapping.setDn(new String(getDn()));
50      	if (getCn()!=null)
51      		hostToGroupMapping.setCn(new String(getCn()));
52      	Iterator it = getGroupToAccountMappings().iterator();
53      	while (it.hasNext()) {
54      		String groupToAccountMapping = (String)it.next();
55      		hostToGroupMapping.addGroupToAccountMapping(new String(groupToAccountMapping));
56      	}
57      	return hostToGroupMapping;
58      }
59      
60      /** 
61       * Retrieves the wildcard that will be used to match the CN.
62       * 
63       * @return The wildcard (i.e. '*.mycompany.com').
64       */
65      public String getCn() {
66          return this.cn;
67      }
68      
69      /** 
70       * Retrieves the wildcard that will be used to match the DN.
71       * 
72       * @return The wildcard (i.e. '/DC=org/DC=doegrids/OU=Services/CN=*.mycompany.com').
73       */
74      public String getDn() {
75          return this.dn;
76      }
77      
78      public boolean isInGroup(String hostname) {
79          Iterator iter = regexs.iterator();
80          while (iter.hasNext()) {
81              if (hostname.matches((String) iter.next()))
82                  return true;
83          }
84          return false;
85      }
86      
87      /** Changes the wildcard that will be used to match the CN(s).
88       * @param cn The new wildcard (i.e. '*.mycompany.com, *othercompany.com').
89       */
90      public void setCn(String cn) {
91          super.setName(cn);
92          this.cn = cn;
93          updateRegEx();
94      }
95      
96      /** Changes the wildcard that will be used to match the DN(s).
97       * @param wildcard The new wildcard (i.e. '/DC=org/DC=doegrids/OU=Services/CN=*.mycompany.com, /DC=org/DC=doegrids/OU=Services/CN=*.othercompany.com').
98       */
99      public void setDn(String dn) {
100         super.setName(dn);
101         this.dn = dn;
102         updateRegEx();
103     }
104     
105     public void setName(String name) {
106     	throw new RuntimeException("Call setCn or SetDn rather than setName");
107     }
108     
109     public String toXML() {
110     	String retStr = "\t\t<hostToGroupMapping\n"+
111 		"\t\t\tgroupToAccountMappings='";
112 		Iterator it = getGroupToAccountMappings().iterator();
113 		while(it.hasNext()) {
114 			String groupToAccountMapping = (String)it.next();
115 			retStr += groupToAccountMapping + (it.hasNext()?", ":"");
116 		}
117 		retStr += "'\n"+
118     	"\t\t\tdescription='"+getDescription()+"'\n";
119     	if (dn != null)
120     		retStr += "\t\t\tdn='" + dn + "'";
121     	if (cn != null)
122     		retStr += "\t\t\tcn='" + (cn!=null?cn:"") + "'";
123     	retStr += "/>\n\n";
124     	return retStr;
125     }      
126     
127     private void updateRegEx() {
128         regexs = new ArrayList();
129         if (cn != null) {
130             StringTokenizer tokens = new StringTokenizer(cn, ",");
131             while (tokens.hasMoreTokens()) {
132                 String regex = tokens.nextToken();
133 		regex = regex.trim();
134                 regex = regex.replaceAll("\\.", "\\.");
135                 regex = regex.replaceAll("\\*", "[^\\./=]*");
136                 regexs.add("(/[^=]*=[^=]*)*/CN=" + regex + "(/[^=]*=[^=]*)*");
137             }
138         }
139         if (dn != null) {
140             StringTokenizer tokens = new StringTokenizer(dn, ",");
141             while (tokens.hasMoreTokens()) {
142                 String regex = tokens.nextToken();
143 		regex = regex.trim();
144                 regex = regex.replaceAll("\\.", "\\.");
145                 regex = regex.replaceAll("\\*", "[^\\./=]*");
146                 regexs.add(regex);
147             }
148         }
149     }
150 }