View Javadoc

1   /*
2    * AbstractHostGroup.java
3    *
4    * Created on May 10, 2005, 3:44 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.ConfigElement;
14  import gov.bnl.gums.configuration.Configuration;
15  import gov.bnl.gums.util.StringUtil;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import javax.persistence.DiscriminatorColumn;
21  import javax.persistence.DiscriminatorType;
22  import javax.persistence.Entity;
23  import javax.persistence.Inheritance;
24  import javax.persistence.InheritanceType;
25  
26  /** It defines a a group of hosts that will be using the same mappings for user
27   * authorization. An object of this class links a series of hostnames to a list
28   * of group mappings.
29   * <p>
30   * This class does not return the list of hosts that will be affected since 
31   * the group of hosts can be defined by a rule, without knowledge of which
32   * systems exist. For example, all the machines in the 130.199.*.* subnet,
33   * or all machines in the usatlas.bnl.gov subdomain.
34   *
35   * * Two ways to build the groupToAccountMappers - one with the add/remove methods, and the other with
36   * set and get methods, which Hibernate will use.
37   *
38   * @author Gabriele Carcassi, Jay Packard
39   */
40  @Entity
41  @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
42  @DiscriminatorColumn(
43      name="type",
44      discriminatorType=DiscriminatorType.STRING
45  )
46  public abstract class HostToGroupMapping extends ConfigElement {
47      protected List<String> groupToAccountMappingsList = new ArrayList<String>();
48  	
49  	//persistent variables
50  	String groupToAccountMappings;
51  	
52  	/**
53  	 * Create a new HostToGroupMapping object.
54  	 * 
55  	 * This empty constructor needed by XML Digestor
56  	 */
57      public HostToGroupMapping() {
58      }
59      
60  	/**
61  	 * Create a new HostToGroupMapping object with a configuration and a name.
62  	 * 
63  	 * @param configuration
64  	 */
65      public HostToGroupMapping(Configuration configuration, String name) {
66      	super(configuration, name);
67      }
68  
69      /** Changes the list of group mapping associated with this mapping.
70       *
71       * @param groupMapper A list of GroupMapper objects.
72       */
73      public void addGroupToAccountMapping(String groupToAccountMapping) {
74      	if (groupToAccountMappings!=null)
75      		throw new RuntimeException("Cannot call addGroupToAccountMapping when setGroupToAccountMappings has been called");
76          this.groupToAccountMappingsList.add(groupToAccountMapping);
77      }
78      
79      /** Returns the list of group mapping associated with this mapping.
80       * @return A list of GroupMapper objects.
81       */
82      public String getGroupToAccountMappings() {
83      	if (groupToAccountMappings!=null)
84      		return groupToAccountMappings;
85      	else
86      		return StringUtil.toCommaSeparatedList(groupToAccountMappingsList);
87      }
88      
89      /**
90       * Determines if hostname is matched within this host to group mapping.
91       * 
92       * @param hostname
93       * @return true if hostname matches
94       */
95      public abstract boolean isInGroup(String hostname);
96      
97      /**
98       * @param name
99       * @return
100      */
101     public boolean removeGroupToAccountMapping(String name) {
102     	if (groupToAccountMappings!=null)
103     		throw new RuntimeException("Cannot call removeGroupToAccountMapping when setGroupToAccountMappings has been called");
104     	return groupToAccountMappingsList.remove(name);
105     }
106 
107     @ConfigFieldAnnotation(label="Group To Account Mapping(s)", help="return account from first successful mapping", orderedList=true)
108 	public void setGroupToAccountMappings(String groupToAccountMappers) {
109     	this.groupToAccountMappings = groupToAccountMappers;
110 	}
111 }