View Javadoc

1   /*
2    * Group2AccountMapping.java
3    *
4    * Created on May 24, 2004, 2:36 PM
5    */
6   
7   package gov.bnl.gums.groupToAccount;
8   
9   import java.lang.ref.SoftReference;
10  import java.util.ArrayList;
11  import java.util.Iterator;
12  import java.util.List;
13  
14  import gov.bnl.gums.configuration.Configuration;
15  
16  /** This class defines which mapping policy should be used for the given group.
17   * It tells that a given user group (all the members of the 'usatlas'
18   * group in the 'atlas' LDAP vo) should be mapped using a given mapping policy
19   * (a composite made of a NISAccountMapper and GroupAccountMapper, meaning
20   * each user should be mapped to his account if exists, otherwise the generic
21   * group account should be used).
22   * <p>
23   * This class is used by the Hostname mapping to hold a series of group/accountmapping
24   * pairs.
25   *
26   * @author Gabriele Carcassi, Jay Packard
27   */
28  public class GroupToAccountMapping {
29      private ArrayList userGroups = new ArrayList();
30      private ArrayList accountMappers = new ArrayList();
31      private String name = "";
32  	private String description = "";
33      private String accountingVo = "";
34      private String accountingVoSubgroup = "";
35      private SoftReference configurationRef = null;
36  
37  	/**
38  	 * Creates a GroupToAccountMapping object. This empty constructor is needed by the XML Digestor.
39  	 */
40  	public GroupToAccountMapping() {
41  	}    
42      
43  	/**
44  	 * Creates a GroupToAccountMapping object with a configuration.
45  	 * 
46  	 * @param configuration
47  	 */
48  	public GroupToAccountMapping(Configuration configuration) {
49  		this.configurationRef = new SoftReference(configuration);
50  	}
51  	
52  	/**
53  	 * Creates a GroupToAccountMapping with a configuration and a name.
54  	 * 
55  	 * @param configuration
56  	 * @param name
57  	 */
58  	public GroupToAccountMapping(Configuration configuration, String name) {
59  		this.configurationRef = new SoftReference(configuration);
60  		this.name = name;
61  	}
62  
63  	/**
64       * Setter for property mapper.
65       * 
66       * @param mapper New value of property mapper.
67       */
68      public void addAccountMapper(String accountMapper) {
69          accountMappers.add(accountMapper);
70      }
71  	
72  	/**
73       * Setter for property group.
74       * 
75       * @param group New value of property group.
76       */
77      public void addUserGroup(String userGroup) {
78      	userGroups.add(userGroup);
79      }
80  	
81  	/**
82  	 * Create a clone of itself for specified configuration.
83  	 * 
84  	 * @param configuration
85  	 * @return
86  	 */
87      public GroupToAccountMapping clone(Configuration configuration) {
88      	GroupToAccountMapping groupToAccountMapping = new GroupToAccountMapping(configuration, new String(name));
89      	groupToAccountMapping.setDescription(new String(getDescription()));
90      	groupToAccountMapping.setAccountingVoSubgroup(new String(accountingVoSubgroup));
91      	groupToAccountMapping.setAccountingVo(new String(accountingVo));
92      	Iterator it = getUserGroups().iterator();
93      	while (it.hasNext())
94      		groupToAccountMapping.addUserGroup( new String((String)it.next()) );
95      	it = getAccountMappers().iterator();
96      	while (it.hasNext())
97      		groupToAccountMapping.addAccountMapper( new String((String)it.next()) );
98      	return groupToAccountMapping;
99      }
100     
101 	/**
102      * @return returns true if accountMapper is matched.
103      */
104     public boolean containsAccountMapper(String accountMapperQuery) {
105     	Iterator accountMapperIt = accountMappers.iterator();
106     	while(accountMapperIt.hasNext()) {
107     		String accountMapper = (String)accountMapperIt.next();
108     		if(accountMapper.equals(accountMapperQuery))
109     			return true;
110     	}
111     	return false;
112     }
113     
114 	/**
115      * @return returns true if userGroup is matched.
116      */
117     public boolean containsUserGroup(String userGroupQuery) {
118     	Iterator userGroupIt = userGroups.iterator();
119     	while(userGroupIt.hasNext()) {
120     		String userGroup = (String)userGroupIt.next();
121     		if(userGroup.equals(userGroupQuery))
122     			return true;
123     	}
124     	return false;
125     }
126     
127     /**
128      * Getter for property accountingVo.
129      * 
130      * @return Value of property accountingVo.
131      */
132     public String getAccountingVo() {
133 
134         return this.accountingVo;
135     }    
136     
137     /**
138      * Getter for property accountingVoSubgroup.
139      * 
140      * @return Value of property accountingVoSubgroup.
141      */
142     public String getAccountingVoSubgroup()  {
143 
144         return this.accountingVoSubgroup;
145     }    
146     
147     /**
148      * Getter for property mapper.
149      * 
150      * @return Value of property mapper.
151      */
152     public ArrayList getAccountMappers() {
153         return accountMappers;
154     }    
155     
156     /**
157      * Getter for property configuration.
158      * 
159      * @return Configuration object.
160      */
161     public Configuration getConfiguration() {
162     	if (configurationRef == null)
163     		return null;
164 		return (Configuration)configurationRef.get();
165 	}
166     
167     /**
168      * Getter for property description.
169      * 
170      * @return Description as string.
171      */
172     public String getDescription() {
173 		return description;
174 	}
175 	
176     /**
177      * Getter for property name.
178      * 
179      * @return Value of property name.
180      */
181     public String getName() {
182         return (name!=null ? name : "");
183     }
184     
185     /**
186      * Getter for property group.
187      * 
188      * @return Value of property group.
189      */
190     public ArrayList getUserGroups() {
191         return this.userGroups;
192     }
193     
194     /**
195      * Setter for property accountingVo.
196      * @param accountingVo New value of property y.
197      */
198     public void setAccountingVo(String accountingVo) {
199 
200         this.accountingVo = accountingVo;
201     }
202 
203     /**
204      * Setter for property accountingVo.
205      * 
206      * @param accountingVo New value of property accountingVo.
207      */
208     public void setAccountingVoSubgroup(java.lang.String accountingVoSubgroup)  {
209 
210         this.accountingVoSubgroup = accountingVoSubgroup;
211     }
212 
213     /**
214      * Setter for property configuration.
215      * 
216      * @param configuration.
217      */
218     public void setConfiguration(Configuration configuration) {
219 		this.configurationRef = new SoftReference(configuration);
220 	}
221     
222     /**
223      * Setter for property description.
224      * 
225      * @param description.
226      */
227     public void setDescription(String description) {
228     	this.description = description;
229     }
230     
231     /**
232      * Setter for property name.
233      * 
234      * @param name.
235      */
236     public void setName(String name) {
237 		this.name = name;
238 	}
239     
240     public String toString(String bgColor) {
241     	return "<td bgcolor=\""+bgColor+"\"><a href=\"groupToAccountMappings.jsp?command=edit&name=" + getName() + "\">" + getName() + "</a></td><td bgcolor=\""+bgColor+"\">" + getAccountingVoSubgroup() + "&nbsp;</td><td bgcolor=\""+bgColor+"\">" + getAccountingVo() + "&nbsp;</td>";
242     }
243     
244     /**
245      * Get XML representation of this object for writing to gums.config
246      * 
247      * @return xml as string
248      */
249     public String toXML() {
250     	String retStr = "\t\t<groupToAccountMapping\n"+
251 		"\t\t\tname='"+name+"'\n"+
252 		"\t\t\tdescription='"+getDescription()+"'\n"+
253 		"\t\t\taccountingVoSubgroup='"+accountingVoSubgroup+"'\n"+
254 		"\t\t\taccountingVo='"+accountingVo+"'\n"+
255 		"\t\t\tuserGroups='";
256 
257 	    List userGroups = getUserGroups();
258 		Iterator it = (Iterator)userGroups.iterator();
259 		while(it.hasNext()) {
260 			String userGroup = (String)it.next();
261 			retStr += userGroup + (it.hasNext()?", ":"");
262 		}
263 		
264 		retStr += "'\n";
265 		
266 		retStr += "\t\t\taccountMappers='";
267 	    
268 		List accountMappers = getAccountMappers();
269 		it = (Iterator)accountMappers.iterator();
270 		while(it.hasNext()) {
271 			String accountMapper = (String)it.next();
272 			retStr += accountMapper + (it.hasNext()?", ":"");
273 		}
274 		
275 		retStr += "'/>\n\n";
276 		
277 		return retStr;
278     }
279     
280 }