1
2
3
4
5
6
7
8
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
21
22
23
24 public class CertificateHostToGroupMapping extends HostToGroupMapping {
25 private String cn = null;
26 private String dn = null;
27 private List regexs;
28
29
30
31
32 public CertificateHostToGroupMapping() {
33 super();
34 }
35
36
37
38
39
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
62
63
64
65 public String getCn() {
66 return this.cn;
67 }
68
69
70
71
72
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
88
89
90 public void setCn(String cn) {
91 super.setName(cn);
92 this.cn = cn;
93 updateRegEx();
94 }
95
96
97
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 }