View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/common/src/java/org/astrogrid/community/common/policy/manager/GroupManagerMock.java,v $</cvs:source>
3    * <cvs:author>$Author: dave $</cvs:author>
4    * <cvs:date>$Date: 2004/09/16 23:18:08 $</cvs:date>
5    * <cvs:version>$Revision: 1.7 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: GroupManagerMock.java,v $
9    *   Revision 1.7  2004/09/16 23:18:08  dave
10   *   Replaced debug logging in Community.
11   *   Added stream close() to FileStore.
12   *
13   *   Revision 1.6.82.1  2004/09/16 09:58:48  dave
14   *   Replaced debug with commons logging ....
15   *
16   *   Revision 1.6  2004/06/18 13:45:20  dave
17   *   Merged development branch, dave-dev-200406081614, into HEAD
18   *
19   *   Revision 1.5.36.1  2004/06/17 13:38:59  dave
20   *   Tidied up old CVS log entries
21   *
22   * </cvs:log>
23   *
24   */
25  package org.astrogrid.community.common.policy.manager ;
26  
27  import org.apache.commons.logging.Log ;
28  import org.apache.commons.logging.LogFactory ;
29  
30  import java.util.Map ;
31  import java.util.HashMap ;
32  
33  import org.astrogrid.community.common.policy.data.GroupData ;
34  import org.astrogrid.community.common.service.CommunityServiceMock ;
35  
36  import org.astrogrid.community.common.exception.CommunityPolicyException     ;
37  import org.astrogrid.community.common.exception.CommunityIdentifierException ;
38  
39  /***
40   * Mock implementation of the GroupManager service.
41   *
42   */
43  public class GroupManagerMock
44      extends CommunityServiceMock
45      implements GroupManager
46      {
47      /***
48       * Our debug logger.
49       *
50       */
51  	private static Log log = LogFactory.getLog(GroupManagerMock.class);
52  
53      /***
54       * Public constructor.
55       *
56       */
57      public GroupManagerMock()
58          {
59          super() ;
60          }
61  
62      /***
63       * Our hash table of values.
64       *
65       */
66      private static Map map = new HashMap() ;
67  
68      /***
69       * Reset our map.
70       *
71       */
72      public static void reset()
73          {
74          log.debug("") ;
75          log.debug("----\"----") ;
76          log.debug("GroupManagerMock.reset()") ;
77          map.clear() ;
78          }
79  
80      /***
81       * Add a new Group, given the Group ident.
82       * @param  ident The Group identifier.
83       * @return An GroupData for the Group.
84       * @throws CommunityIdentifierException If the identifier is not valid.
85       * @throws CommunityPolicyException If the identifier is already in the database.
86       *
87       */
88      public GroupData addGroup(String ident)
89          throws CommunityIdentifierException, CommunityPolicyException
90          {
91          log.debug("") ;
92          log.debug("----\"----") ;
93          log.debug("GroupManagerMock.addGroup()") ;
94          log.debug("  Ident : " + ident) ;
95          //
96          // Check for null ident.
97          if (null == ident)
98              {
99              throw new CommunityIdentifierException(
100                 "Null identifier"
101                 ) ;
102             }
103         //
104         // Check if we already have an existing Group.
105         if (map.containsKey(ident))
106             {
107             throw new CommunityPolicyException(
108                 "Duplicate group",
109                 ident
110                 ) ;
111             }
112         //
113         // Create a new Group.
114         GroupData group = new GroupData(ident) ;
115         //
116         // Add it to our map.
117         map.put(group.getIdent(), group) ;
118         //
119         // Return the new Group.
120         return group ;
121         }
122 
123     /***
124      * Add a new Group, given the Group data.
125      * @param  data The GroupData to add.
126      * @return A new GroupData for the Group.
127      * @throws CommunityIdentifierException If the identifier is not valid.
128      * @throws CommunityPolicyException If the identifier is already in the database.
129      *
130      */
131     public GroupData addGroup(GroupData group)
132         throws CommunityIdentifierException, CommunityPolicyException
133         {
134         log.debug("") ;
135         log.debug("----\"----") ;
136         log.debug("GroupManagerMock.addGroup()") ;
137         log.debug("  Group : " + ((null != group) ? group.getIdent() : null)) ;
138         //
139         // Check for null group.
140         if (null == group)
141             {
142             throw new CommunityIdentifierException(
143                 "Null group"
144                 ) ;
145             }
146         //
147         // Check for null ident.
148         if (null == group.getIdent())
149             {
150             throw new CommunityIdentifierException(
151                 "Null identifier"
152                 ) ;
153             }
154         //
155         // Check if we already have an existing Group.
156         if (map.containsKey(group.getIdent()))
157             {
158             throw new CommunityPolicyException(
159                 "Duplicate group",
160                 group.getIdent()
161                 ) ;
162             }
163         //
164         // Add it to our map.
165         map.put(group.getIdent(), group) ;
166         //
167         // Return the new Group.
168         return group ;
169         }
170 
171     /***
172      * Request a Group details, given the Group ident.
173      * @param  ident The Group identifier.
174      * @return An GroupData for the Group.
175      * @throws CommunityIdentifierException If the identifier is not valid.
176      * @throws CommunityPolicyException If the identifier is not in the database.
177      *
178      */
179     public GroupData getGroup(String ident)
180         throws CommunityIdentifierException, CommunityPolicyException
181         {
182         log.debug("") ;
183         log.debug("----\"----") ;
184         log.debug("GroupManagerMock.getGroup()") ;
185         log.debug("  Ident : " + ident) ;
186         //
187         // Check for null ident.
188         if (null == ident)
189             {
190             throw new CommunityIdentifierException(
191                 "Null identifier"
192                 ) ;
193             }
194         //
195         // Lookup the Group in our map.
196         GroupData group = (GroupData) map.get(ident) ;
197         //
198         // If we found a Group.
199         if (null != group)
200             {
201             return group ;
202             }
203         //
204         // If we didn't find a Group.
205         else {
206             throw new CommunityPolicyException(
207                 "Group not found",
208                 ident
209                 ) ;
210             }
211         }
212 
213     /***
214      * Update a Group.
215      * @param  update The new Group data to update.
216      * @return A new GroupData for the Group.
217      * @throws CommunityIdentifierException If the identifier is not valid.
218      * @throws CommunityPolicyException If the identifier is not in the database.
219      *
220      */
221     public GroupData setGroup(GroupData update)
222         throws CommunityIdentifierException, CommunityPolicyException
223         {
224         log.debug("") ;
225         log.debug("----\"----") ;
226         log.debug("GroupManagerMock.setGroup()") ;
227         log.debug("  Ident : " + ((null != update) ? update.getIdent() : null)) ;
228         //
229         // Check for null data.
230         if (null == update)
231             {
232             throw new CommunityIdentifierException(
233                 "Null inout data"
234                 ) ;
235             }
236         //
237         // Check for null ident.
238         if (null == update.getIdent())
239             {
240             throw new CommunityIdentifierException(
241                 "Null inout data"
242                 ) ;
243             }
244         //
245         // Lookup the Group in our map.
246         GroupData found = this.getGroup(update.getIdent()) ;
247         //
248         // Replace the existing Group with the new data.
249         map.put(found.getIdent(), update) ;
250         //
251         // Return the new data.
252         return update ;
253         }
254 
255     /***
256      * Delete a Group.
257      * @param  ident The Group identifier.
258      * @return The GroupData for the old Group.
259      * @throws CommunityIdentifierException If the identifier is not valid.
260      * @throws CommunityPolicyException If the identifier is not in the database.
261      *
262      */
263     public GroupData delGroup(String ident)
264         throws CommunityIdentifierException, CommunityPolicyException
265         {
266         log.debug("") ;
267         log.debug("----\"----") ;
268         log.debug("GroupManagerMock.delGroup()") ;
269         log.debug("  Ident : " + ident) ;
270         //
271         // Check for null ident.
272         if (null == ident)
273             {
274             throw new CommunityIdentifierException(
275                 "Null identifier"
276                 ) ;
277             }
278         //
279         // Try to find the Group.
280         GroupData group = this.getGroup(ident) ;
281         //
282         // If we found an Group.
283         if (null != group)
284             {
285             //
286             // Remove the Group from our map.
287             map.remove(group.getIdent()) ;
288             //
289             // Return the old group.
290             return group ;
291             }
292         //
293         // If we didn't find an Group.
294         else {
295             throw new CommunityPolicyException(
296                 "Group not found",
297                 ident
298                 ) ;
299             }
300         }
301 
302     /***
303      * Request a list of local Groups.
304      * @return An array of GroupData objects.
305      *
306      */
307     public Object[] getLocalGroups()
308         {
309         log.debug("") ;
310         log.debug("----\"----") ;
311         log.debug("GroupManagerMock.getLocalGroups()") ;
312         return map.values().toArray() ;
313         }
314 
315     /***
316      * Get a list of local Groups that an Account belongs to.
317      * @return An array of GroupData objects.
318      * @todo Filter the results for matches.
319      *
320      */
321     public Object[] getLocalAccountGroups(String account)
322         {
323         log.debug("") ;
324         log.debug("----\"----") ;
325         log.debug("GroupManagerMock.getAccountGroups()") ;
326         return map.values().toArray() ;
327         }
328     }