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.Configuration;
14  
15  import java.lang.ref.SoftReference;
16  import java.util.*;
17  
18  /** It defines a a group of hosts that will be using the same mappings for user
19   * authorization. An object of this class links a series of hostnames to a list
20   * of group mappings.
21   * <p>
22   * This class does not return the list of hosts that will be affected since 
23   * the group of hosts can be defined by a rule, without knowledge of which
24   * systems exist. For example, all the machines in the 130.199.*.* subnet,
25   * or all machines in the usatlas.bnl.gov subdomain.
26   *
27   * @author Gabriele Carcassi, Jay Packard
28   */
29  public abstract class HostToGroupMapping {
30      private List groupToAccountMappers = new ArrayList();
31      private SoftReference configurationRef = null;
32      private String name = "";
33  	private String description = "";
34  	
35      
36  	/**
37  	 * Create a nwe HostToGroupMapping object.
38  	 * 
39  	 * This empty constructor needed by XML Digestor
40  	 */
41      public HostToGroupMapping() {
42      }
43  
44      /**
45       * Create a new HostToGroupMapping object with a configuration.
46       * 
47       * @param configuration
48       */
49      public HostToGroupMapping(Configuration configuration) {
50      	this.configurationRef = new SoftReference(configuration);
51      }
52      
53  	/**
54  	 * Create a new HostToGroupMapping object with a configuration and a name.
55  	 * 
56  	 * @param configuration
57  	 */
58      public HostToGroupMapping(Configuration configuration, String name) {
59      	this.configurationRef = new SoftReference(configuration);
60      	this.name = name;
61      }
62  
63      /** Changes the list of group mapping associated with this mapping.
64       *
65       * @param groupMapper A list of GroupMapper objects.
66       */
67      public void addGroupToAccountMapping(String groupToAccountMapping) {
68          this.groupToAccountMappers.add(groupToAccountMapping);
69      }
70      
71  	/**
72  	 * Create a clone of itself for specified configuration.
73  	 * 
74  	 * @param configuration
75  	 * @return
76  	 */
77      public abstract HostToGroupMapping clone(Configuration configuration);
78      
79      /**
80       * Determines if this host to group mapping contains a group to account mapping.
81       * 
82       * @return returns true if group to account mapping is contained.
83       */
84      public boolean containsGroupToAccountMapping(String groupToAccountMappingQuery) {
85      	Iterator groupMapperIt = groupToAccountMappers.iterator();
86      	while(groupMapperIt.hasNext()) {
87      		String groupToAccountMapping = (String)groupMapperIt.next();
88      		if(groupToAccountMapping.equals(groupToAccountMappingQuery))
89      			return true;
90      	}
91      	return false;
92      }
93      
94      /**
95       * Getter for property configuration.
96       * 
97       * @return Configuration as string.
98       */
99      public Configuration getConfiguration() {
100     	if (configurationRef==null)
101     		return null;
102     	return (Configuration)configurationRef.get();
103     }
104     
105     /**
106      * Getter for property description.
107      * 
108      * @return Description as string.
109      */
110 	public String getDescription() {
111 		return description;
112 	}
113     
114     /** Returns the list of group mapping associated with this mapping.
115      * @return A list of GroupMapper objects.
116      */
117     public List getGroupToAccountMappings() {
118         return Collections.unmodifiableList(groupToAccountMappers);
119     }
120     
121     /**
122      * Getter for property name.
123      * 
124      * @return Value of property name.
125      */
126     public String getName() {
127     	return name;
128     }
129     
130     /**
131      * Determines if hostname is matched within this host to group mapping.
132      * 
133      * @param hostname
134      * @return true if hostname matches
135      */
136     public abstract boolean isInGroup(String hostname);
137     
138     /**
139      * Setter for property configuration.
140      * 
141      * @param configuration.
142      */
143     public void setConfiguration(Configuration configuration) {
144 		this.configurationRef = new SoftReference(configuration);
145 	}
146     
147     /**
148      * Setter for property description.
149      * 
150      * @param description.
151      */
152     public void setDescription(String description) {
153     	this.description = description;
154     }
155     
156     /**
157      * Setter for property name.
158      * 
159      * @param name.
160      */
161     public void setName(String name) {
162     	this.name = name;
163     }
164     
165     /**
166      * Get XML representation of this object for writing to gums.config
167      * 
168      * @return xml as string
169      */
170     public abstract String toXML();
171 }