View Javadoc

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