View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/server/src/java/org/astrogrid/community/server/policy/manager/AccountManagerImpl.java,v $</cvs:source>
3    * <cvs:author>$Author: jdt $</cvs:author>
4    * <cvs:date>$Date: 2005/01/07 14:14:25 $</cvs:date>
5    * <cvs:version>$Revision: 1.22 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: AccountManagerImpl.java,v $
9    *   Revision 1.22  2005/01/07 14:14:25  jdt
10   *   merged from Reg_KMB_787
11   *
12   *   Revision 1.21.4.1  2004/12/13 21:25:03  KevinBenson
13   *   added a small default null for default vospace
14   *
15   *   Revision 1.21  2004/11/22 13:03:04  jdt
16   *   Merges from Comm_KMB_585
17   *
18   *   Revision 1.20.22.1  2004/11/08 22:08:21  KevinBenson
19   *   added groupmember and permissionmanager tests.  Changed the install.xml to use eperate file names
20   *   instead of the same filename
21   *
22   *   Revision 1.20  2004/09/16 23:18:08  dave
23   *   Replaced debug logging in Community.
24   *   Added stream close() to FileStore.
25   *
26   *   Revision 1.19.12.1  2004/09/16 09:58:48  dave
27   *   Replaced debug with commons logging ....
28   *
29   *   Revision 1.19  2004/09/02 17:02:02  dave
30   *   Fixed myspace account creation problem.
31   *
32   *   Revision 1.18.70.2  2004/09/02 16:22:11  dave
33   *   Fixed create myspace account ....
34   *
35   *   Revision 1.18.70.1  2004/09/02 15:48:48  dave
36   *   Moved commit to after allocate space ....
37   *
38   *   Revision 1.18  2004/06/18 13:45:20  dave
39   *   Merged development branch, dave-dev-200406081614, into HEAD
40   *
41   *   Revision 1.17.10.2  2004/06/17 15:24:31  dave
42   *   Removed unused imports (PMD report).
43   *
44   *   Revision 1.17.10.1  2004/06/17 13:38:59  dave
45   *   Tidied up old CVS log entries
46   *
47   * </cvs:log>
48   *
49   */
50  package org.astrogrid.community.server.policy.manager ;
51  
52  import org.apache.commons.logging.Log ;
53  import org.apache.commons.logging.LogFactory ;
54  
55  import java.util.Vector ;
56  import java.util.Collection ;
57  
58  import org.exolab.castor.jdo.Database;
59  import org.exolab.castor.jdo.OQLQuery;
60  import org.exolab.castor.jdo.QueryResults;
61  import org.exolab.castor.jdo.ObjectNotFoundException ;
62  import org.exolab.castor.jdo.DuplicateIdentityException ;
63  
64  import org.astrogrid.store.Ivorn ;
65  
66  import org.astrogrid.config.Config ;
67  import org.astrogrid.config.SimpleConfig ;
68  import org.astrogrid.config.PropertyNotFoundException ;
69  
70  import org.astrogrid.community.common.policy.data.GroupData ;
71  import org.astrogrid.community.common.policy.data.AccountData ;
72  import org.astrogrid.community.common.policy.data.GroupMemberData ;
73  
74  import org.astrogrid.community.common.ivorn.CommunityIvornParser ;
75  import org.astrogrid.community.common.ivorn.CommunityAccountIvornFactory ;
76  
77  import org.astrogrid.community.common.policy.manager.AccountManager ;
78  
79  import org.astrogrid.community.server.service.CommunityServiceImpl ;
80  import org.astrogrid.community.server.database.configuration.DatabaseConfiguration ;
81  
82  import org.astrogrid.community.common.exception.CommunityPolicyException     ;
83  import org.astrogrid.community.common.exception.CommunityServiceException    ;
84  import org.astrogrid.community.common.exception.CommunityIdentifierException ;
85  
86  import org.astrogrid.store.VoSpaceClient ;
87  
88  /***
89   * Server side implmenetation of the AccountManager service.
90   *
91   */
92  public class AccountManagerImpl
93      extends CommunityServiceImpl
94      implements AccountManager
95      {
96      /***
97       * Our debug logger.
98       *
99       */
100     private static Log log = LogFactory.getLog(AccountManagerImpl.class);
101 
102     /***
103      * Our AstroGrid configuration.
104      *
105      */
106     protected static Config config = SimpleConfig.getSingleton() ;
107 
108     /***
109      * The default public group.
110      * @todo This should be in a config file, not hard coded.
111      *
112      */
113     private static String DEFAULT_GROUP_NAME = "everyone" ;
114 
115     /***
116      * The default public group.
117      * @todo This should be in a config file, not hard coded.
118      * @todo Find a better way of initialising it.
119      *
120      */
121     private static Ivorn DEFAULT_GROUP_IVORN ;
122 
123     static {
124         try {
125             DEFAULT_GROUP_IVORN = CommunityAccountIvornFactory.createLocal(
126                 DEFAULT_GROUP_NAME
127                 ) ;
128             }
129         catch (Exception ouch)
130             {
131             }
132         }
133 
134     /***
135      * Public constructor, using default database configuration.
136      *
137      */
138     public AccountManagerImpl()
139         {
140         super() ;
141         }
142 
143     /***
144      * Public constructor, using specific database configuration.
145      * @param config A specific DatabaseConfiguration.
146      *
147      */
148     public AccountManagerImpl(DatabaseConfiguration config)
149         {
150         super(config) ;
151         }
152 
153     /***
154      * Public constructor, using a parent service.
155      * @param parent A parent CommunityServiceImpl.
156      *
157      */
158     public AccountManagerImpl(CommunityServiceImpl parent)
159         {
160         super(parent) ;
161         }
162 
163     /***
164      * Add a new Account, given the Account ident.
165      * @param  ident The Account identifier.
166      * @return An AccountData for the Account.
167      * @throws CommunityIdentifierException If the identifier is not valid.
168      * @throws CommunityPolicyException If the identifier is already in the database.
169      * @throws CommunityServiceException If there is an internal error in the service.
170      *
171      */
172     public AccountData addAccount(String ident)
173         throws CommunityServiceException, CommunityIdentifierException, CommunityPolicyException
174         {
175         //
176         // Create a new Accountdata.
177         return this.addAccount(
178             new AccountData(
179                 ident
180                 )
181             ) ;
182         }
183 
184     /***
185      * Add a new Account, given the Account data.
186      * @param  account The AccountData to add.
187      * @return A new AccountData for the Account.
188      * @throws CommunityIdentifierException If the identifier is not valid.
189      * @throws CommunityPolicyException If the identifier is already in the database.
190      * @throws CommunityServiceException If there is an internal error in the service.
191      * @throws RemoteException If the WebService call fails.
192      * @todo Needs refactoring to make it more robust.
193      * @todo If the 'everyone' group does not exist, then create it.
194      * @todo If the account group already exists (as an AccountGroup) then don't throw a duplicate exception.
195      * @todo If the account already belongs to the account group then don't throw a duplicate exception.
196      * @todo If the account already belongs to the 'everyone' group then don't throw a duplicate exception.
197      * @todo If the MySpace call adds the Account, store the MySpace server and Account details.
198      * @todo Tidy up fragments of old data, e.g. groups, membership and permissions,
199      * @todo Verify that the finally gets executed, even if a new Exception is thrown.
200      *
201      */
202     public AccountData addAccount(AccountData account)
203         throws CommunityServiceException, CommunityIdentifierException, CommunityPolicyException
204         {
205         log.debug("") ;
206         log.debug("----\"----") ;
207         log.debug("AccountManagerImpl.addAccount()") ;
208         log.debug("  Account : " + ((null != account) ? account.getIdent() : null)) ;
209         //
210         // Check for null account.
211         if (null == account)
212             {
213             throw new CommunityIdentifierException(
214                 "Null account"
215                 ) ;
216             }
217         //
218         // Get the Account ident.
219         CommunityIvornParser ident = new CommunityIvornParser(
220             account.getIdent()
221             ) ;
222         //
223         // If the account is local.
224         if (ident.isLocal())
225             {
226 			//
227 			// Get the string ident.
228 			String string = ident.getAccountIdent() ;
229 			//
230 			// Set the Account ident.
231 			account.setIdent(string) ;
232             //
233             // Create the corresponding Group object.
234             GroupData group = new GroupData() ;
235             group.setIdent(string) ;
236             group.setType(GroupData.SINGLE_TYPE) ;
237             //
238             // Add the account to the group.
239             GroupMemberData groupmember = new GroupMemberData() ;
240             groupmember.setAccount(string) ;
241             groupmember.setGroup(string) ;
242             //
243             // Add the account to the guest group.
244 //
245 // TODO Need better 'default' group handling.
246 //
247             GroupMemberData guestmember = new GroupMemberData() ;
248             guestmember.setAccount(string) ;
249             guestmember.setGroup(
250                 DEFAULT_GROUP_IVORN.toString()
251                 ) ;
252             //
253             // Try performing our transaction.
254             Database database = null ;
255             try {
256                 //
257                 // Open our database connection.
258                 database = this.getDatabase() ;
259                 //
260                 // Begin a new database transaction.
261                 database.begin();
262                 //
263                 // Try creating the account in the database.
264 				log.debug("Creating community account") ;
265                 database.create(account);
266 				log.debug("Ok - Created community account") ;
267 /*
268  * Problems reported with group membership when running on windows.
269                 //
270                 // Try creating the group in the database.
271                 database.create(group);
272                 //
273                 // Try adding the account to the groups.
274                 database.create(groupmember);
275 // TODO BUG
276 // Not sure why this one fails the tests.
277 // Something about invalid primary key.
278 //                        database.create(guestmember);
279  *
280  */
281                 //
282                 // Try creating the home space for the Account.
283                 try {
284 					log.debug("Creating myspace account") ;
285                     allocateSpace(account) ;
286 					log.debug("Ok - Created myspace account") ;
287 					log.debug("Home : " + account.getHomeSpace()) ;
288                     }
289                 catch (Exception ouch)
290                     {
291                     //
292                     // Log the exception.
293                     logException(
294                         ouch,
295                         "AccountManagerImpl.addAccount.allocateSpace()"
296                         ) ;
297                     }
298                 //
299                 // Commit the transaction.
300                 database.commit() ;
301                 }
302             //
303             // If we already have an object with that ident.
304             catch (DuplicateIdentityException ouch)
305                 {
306                 //
307                 // Cancel the database transaction.
308                 rollbackTransaction(database) ;
309                 //
310                 // Throw a new Exception.
311                 throw new CommunityPolicyException(
312                     "Duplicate Account identifier",
313                     ident.toString()
314                     ) ;
315                 }
316             //
317             // If anything else went bang.
318             catch (Exception ouch)
319                 {
320                 //
321                 // Log the exception.
322                 logException(
323                     ouch,
324                     "AccountManagerImpl.addAccount()"
325                     ) ;
326                 //
327                 // Cancel the database transaction.
328                 rollbackTransaction(database) ;
329                 //
330                 // Throw a new Exception.
331                 throw new CommunityServiceException(
332                     "Database transaction failed",
333                     ident.toString(),
334                     ouch
335                     ) ;
336                 }
337             //
338             // Close our database connection.
339             finally
340                 {
341                 closeConnection(database) ;
342                 }
343             //
344             // Return the new Account details.
345             return account ;
346             }
347         //
348         // If the ident is not local.
349         else {
350             //
351             // Throw a new Exception.
352             throw new CommunityPolicyException(
353                 "Account is not local",
354                 ident.toString()
355                 ) ;
356             }
357         }
358 
359     /***
360      * Request an Account details, given the Account ident.
361      * @param  ident The Account identifier.
362      * @return An AccountData for the Account.
363      * @throws CommunityIdentifierException If the identifier is not valid.
364      * @throws CommunityPolicyException If the identifier is not in the database.
365      * @throws CommunityServiceException If there is an internal error in the service.
366      *
367      */
368     public AccountData getAccount(String ident)
369         throws CommunityServiceException, CommunityIdentifierException, CommunityPolicyException
370         {
371         return this.getAccount(
372             new CommunityIvornParser(
373                 ident
374                 )
375             ) ;
376         }
377 
378     /***
379      * Request an Account details, given the Account ident.
380      * @param  ident The Account identifier.
381      * @return An AccountData for the Account.
382      * @throws CommunityIdentifierException If the identifier is not valid.
383      * @throws CommunityPolicyException If the identifier is not in the database.
384      * @throws CommunityServiceException If there is an internal error in the service.
385      * @todo Verify that the finally gets executed, even if a new Exception is thrown.
386      *
387      */
388     public AccountData getAccount(CommunityIvornParser ident)
389         throws CommunityServiceException, CommunityIdentifierException, CommunityPolicyException
390         {
391         log.debug("") ;
392         log.debug("----\"----") ;
393         log.debug("AccountManagerImpl.getAccount()") ;
394         log.debug("  Ident : " + ident) ;
395         //
396         // Check for null ident.
397         if (null == ident)
398             {
399             throw new CommunityIdentifierException(
400                 "Null identifier"
401                 ) ;
402             }
403         //
404         // If the ident is local.
405         if (ident.isLocal())
406             {
407             //
408             // Try finding the Account.
409             Database    database = null ;
410             AccountData account  = null ;
411             try {
412 				//
413 				// Get the string ident.
414 				String string = ident.getAccountIdent() ;
415                 //
416                 // Open our database connection.
417                 database = this.getDatabase() ;
418                 //
419                 // Begin a new database transaction.
420                 database.begin();
421                 //
422                 // Load the Account from the database.
423                 account = (AccountData) database.load(AccountData.class, string) ;
424                 //
425                 // Commit the transaction.
426                 database.commit() ;
427                 }
428             //
429             // If we couldn't find the object.
430             catch (ObjectNotFoundException ouch)
431                 {
432                 //
433                 // Cancel the database transaction.
434                 rollbackTransaction(database) ;
435                 //
436                 // Throw a new Exception.
437                 throw new CommunityPolicyException(
438                     "Account not found",
439                     ident.toString()
440                     ) ;
441                 }
442             //
443             // If anything else went bang.
444             catch (Exception ouch)
445                 {
446                 //
447                 // Log the exception.
448                 logException(
449                     ouch,
450                     "AccountManagerImpl.getAccount()"
451                     ) ;
452                 //
453                 // Cancel the database transaction.
454                 rollbackTransaction(database) ;
455                 //
456                 // Throw a new Exception.
457                 throw new CommunityServiceException(
458                     "Database transaction failed",
459                     ident.toString(),
460                     ouch
461                     ) ;
462                 }
463             //
464             // Close our database connection.
465             finally
466                 {
467                 closeConnection(database) ;
468                 }
469             //
470             // Return the Account details.
471             return account ;
472             }
473         //
474         // If the ident is not local.
475         else {
476             //
477             // Throw a new Exception.
478             throw new CommunityPolicyException(
479                 "Account is not local",
480                 ident.toString()
481                 ) ;
482             }
483         }
484 
485     /***
486      * Update an Account.
487      * @param  account The new AccountData to update.
488      * @return A new AccountData for the Account.
489      * @throws CommunityIdentifierException If the identifier is not valid.
490      * @throws CommunityPolicyException If the identifier is not in the database.
491      * @throws CommunityServiceException If there is an internal error in the service.
492      * @todo Verify that the finally gets executed, even if a new Exception is thrown.
493      *
494      */
495     public AccountData setAccount(AccountData account)
496         throws CommunityServiceException, CommunityIdentifierException, CommunityPolicyException
497         {
498         log.debug("") ;
499         log.debug("----\"----") ;
500         log.debug("AccountManagerImpl.setAccount()") ;
501         log.debug("  Account : " + ((null != account) ? account.getIdent() : null)) ;
502         //
503         // Check for null account.
504         if (null == account)
505             {
506             throw new CommunityIdentifierException(
507                 "Null account"
508                 ) ;
509             }
510         //
511         // Get the Account ident.
512         CommunityIvornParser ident = new CommunityIvornParser(
513             account.getIdent()
514             ) ;
515         //
516         // If the ident is local.
517         if (ident.isLocal())
518             {
519             //
520             // Try update the database.
521             Database database = null ;
522             try {
523 				//
524 				// Get the string ident.
525 				String string = ident.getAccountIdent() ;
526                 //
527                 // Open our database connection.
528                 database = this.getDatabase() ;
529                 //
530                 // Begin a new database transaction.
531                 database.begin();
532                 //
533                 // Load the Account from the database.
534                 AccountData data = (AccountData) database.load(AccountData.class, string) ;
535                 //
536                 // Update the account data.
537                 data.setHomeSpace(account.getHomeSpace()) ;
538                 data.setDisplayName(account.getDisplayName()) ;
539                 data.setDescription(account.getDescription()) ;
540                 data.setEmailAddress(account.getEmailAddress()) ;
541                 //
542                 // Commit the transaction.
543                 database.commit() ;
544                 }
545             //
546             // If we couldn't find the object.
547             catch (ObjectNotFoundException ouch)
548                 {
549                 //
550                 // Cancel the database transaction.
551                 rollbackTransaction(database) ;
552                 //
553                 // Throw a new Exception.
554                 throw new CommunityPolicyException(
555                     "Account not found",
556                     ident.toString()
557                     ) ;
558                 }
559             //
560             // If anything else went bang.
561             catch (Exception ouch)
562                 {
563                 //
564                 // Log the exception.
565                 logException(
566                     ouch,
567                     "AccountManagerImpl.setAccount()"
568                     ) ;
569                 //
570                 // Cancel the database transaction.
571                 rollbackTransaction(database) ;
572                 //
573                 // Throw a new Exception.
574                 throw new CommunityServiceException(
575                     "Database transaction failed",
576                     ident.toString(),
577                     ouch
578                     ) ;
579                 }
580             //
581             // Close our database connection.
582             finally
583                 {
584                 closeConnection(database) ;
585                 }
586             //
587             // Return the Account details.
588             return account ;
589             }
590         //
591         // If the ident is not local.
592         else {
593             //
594             // Throw a new Exception.
595             throw new CommunityPolicyException(
596                 "Account is not local",
597                 ident.toString()
598                 ) ;
599             }
600         }
601 
602     /***
603      * Delete an Account.
604      * @param  ident The Account identifier.
605      * @return The AccountData for the old Account.
606      * @throws CommunityIdentifierException If the identifier is not valid.
607      * @throws CommunityServiceException If there is an internal error in the service.
608      *
609      */
610     public AccountData delAccount(String ident)
611         throws CommunityServiceException, CommunityIdentifierException, CommunityPolicyException
612         {
613         return this.delAccount(
614             new CommunityIvornParser(
615                 ident
616                 )
617             ) ;
618         }
619 
620     /***
621      * Delete an Account.
622      * @param  ident The Account identifier.
623      * @return The AccountData for the old Account.
624      * @throws CommunityIdentifierException If the identifier is not valid.
625      * @throws CommunityServiceException If there is an internal error in the service.
626      * @todo Need to have a mechanism for tidying up references to a remote Account.
627      * @todo Need to have a mechanism for tidying up references to an old Account.
628      * @todo Need to have a mechanism for notifying other Communities that the Account has been deleted.
629      * @todo Verify that the finally gets executed, even if a new Exception is thrown.
630      *
631      */
632     protected AccountData delAccount(CommunityIvornParser ident)
633         throws CommunityServiceException, CommunityIdentifierException, CommunityPolicyException
634         {
635         log.debug("") ;
636         log.debug("----\"----") ;
637         log.debug("AccountManagerImpl.delAccount()") ;
638         log.debug("  ident : " + ident) ;
639         //
640         // Check for null ident.
641         if (null == ident)
642             {
643             throw new CommunityIdentifierException(
644                 "Null identifier"
645                 ) ;
646             }
647 
648 	    //
649 	    // @todo Refactor this.
650 	    AccountData account  = null ;
651 
652         //
653         // If the ident is local.
654         if (ident.isLocal())
655             {
656             log.debug("  PASS : ident is local") ;
657             //
658             // Try update the database.
659             Database    database = null ;
660             try {
661 				//
662 				// Get the string ident.
663 				String string = ident.getAccountIdent() ;
664                 //
665                 // Open our database connection.
666                 database = this.getDatabase() ;
667                 //
668                 // Begin a new database transaction.
669                 database.begin();
670                 //
671                 // Load the Account from the database.
672                 account = (AccountData) database.load(AccountData.class, string) ;
673                 //
674                 // If we found the Account.
675                 if (null != account)
676                     {
677                     log.debug("  PASS : found account") ;
678                     //
679                     // Find the group for this account (if it exists).
680                     OQLQuery groupQuery = database.getOQLQuery(
681                         "SELECT groups FROM org.astrogrid.community.common.policy.data.GroupData groups WHERE groups.ident = $1"
682                         );
683                     //
684                     // Bind the query param.
685                     groupQuery.bind(string) ;
686                     //
687                     // Execute our query.
688                     QueryResults groups = groupQuery.execute();
689                     if (null != groups)
690                         {
691                         log.debug("  PASS : found groups") ;
692                         }
693                     else {
694                         log.debug("  FAIL : null groups") ;
695                         }
696                     //
697                     // Find all of the group memberships for this account.
698                     OQLQuery memberQuery = database.getOQLQuery(
699                         "SELECT members FROM org.astrogrid.community.common.policy.data.GroupMemberData members WHERE members.account = $1"
700                         );
701                     //
702                     // Bind the query param.
703                     memberQuery.bind(string) ;
704                     //
705                     // Execute our query.
706                     QueryResults members = memberQuery.execute();
707                     if (null != members)
708                         {
709                         log.debug("  PASS : found members") ;
710                         }
711                     else {
712                         log.debug("  FAIL : null members") ;
713                         }
714                     //
715                     // Load all the permissions for this group.
716                     OQLQuery permissionQuery = database.getOQLQuery(
717                         "SELECT permissions FROM org.astrogrid.community.common.policy.data.PolicyPermission permissions WHERE permissions.group = $1"
718                         );
719                     //
720                     // Bind the query param.
721                     permissionQuery.bind(string) ;
722                     //
723                     // Execute our query.
724                     QueryResults permissions = permissionQuery.execute();
725                     if (null != permissions)
726                         {
727                         log.debug("  PASS : found permissions") ;
728                         }
729                     else {
730                         log.debug("  FAIL : null permissions") ;
731                         }
732                     //
733                     // Delete the permissions.
734                     while (permissions.hasMore())
735                         {
736                         log.debug("  STEP : deleting permission") ;
737                         database.remove(permissions.next()) ;
738                         }
739                     //
740                     // Delete the group memberships.
741                     while (members.hasMore())
742                         {
743                         log.debug("  STEP : deleting membership") ;
744                         database.remove(members.next()) ;
745                         }
746                     //
747                     // Delete the Group.
748                     while (groups.hasMore())
749                         {
750                         log.debug("  STEP : deleting group") ;
751                         database.remove(groups.next()) ;
752                         }
753                     //
754                     // Delete the Account.
755                     database.remove(account) ;
756                     }
757                 log.debug("  PASS : finished deleting") ;
758                 //
759                 // Commit the transaction.
760                 database.commit() ;
761                 log.debug("  PASS : done commit") ;
762                 }
763             //
764             // If we couldn't find the object.
765             catch (ObjectNotFoundException ouch)
766                 {
767                 //
768                 // Cancel the database transaction.
769                 rollbackTransaction(database) ;
770                 //
771                 // Throw a new Exception.
772                 throw new CommunityPolicyException(
773                     "Account not found",
774                     ident.toString()
775                     ) ;
776                 }
777             //
778             // If anything else went bang.
779             catch (Exception ouch)
780                 {
781                 //
782                 // Log the exception.
783                 logException(
784                     ouch,
785                     "AccountManagerImpl.delAccount()"
786                     ) ;
787                 //
788                 // Cancel the database transaction.
789                 rollbackTransaction(database) ;
790                 //
791                 // Throw a new Exception.
792                 throw new CommunityServiceException(
793                     "Database transaction failed",
794                     ident.toString(),
795                     ouch
796                     ) ;
797                 }
798             //
799             // Close our database connection.
800             finally
801                 {
802                 closeConnection(database) ;
803                 }
804             //
805             // Return the original Account details.
806             return account ;
807             }
808         //
809         // If the ident is not local.
810         else {
811             //
812             // Throw a new Exception.
813             throw new CommunityPolicyException(
814                 "Account is not local",
815                 ident.toString()
816                 ) ;
817             }
818         }
819 
820     /***
821      * Request a list of local Accounts.
822      * @return An array of AccountData objects.
823      * @throws CommunityServiceException If there is an internal error in the service.
824      * @todo Return empty array rather than null.
825      *
826      */
827     public Object[] getLocalAccounts()
828         {
829         log.debug("") ;
830         log.debug("----\"----") ;
831         log.debug("AccountManagerImpl.getLocalAccounts()") ;
832         //
833         // Try to query the database.
834         Object[] array    = null ;
835         Database database = null ;
836         try {
837             //
838             // Open our database connection.
839             database = this.getDatabase() ;
840             //
841             // Begin a new database transaction.
842             database.begin();
843             //
844             // Create our OQL query.
845             OQLQuery query = database.getOQLQuery(
846                 "SELECT accounts FROM org.astrogrid.community.common.policy.data.AccountData accounts"
847                 );
848             //
849             // Execute our query.
850             QueryResults results = query.execute();
851             //
852             // Transfer our results to a vector.
853             Collection collection = new Vector() ;
854             while (results.hasMore())
855                 {
856                 collection.add(
857                     (AccountData)results.next()
858                     ) ;
859                 }
860             //
861             // Convert it into an array.
862             array = collection.toArray() ;
863             //
864             // Commit the transaction.
865             database.commit() ;
866             }
867         //
868         // If anything went bang.
869         catch (Exception ouch)
870             {
871             //
872             // Log the exception.
873             logException(ouch, "AccountManagerImpl.getLocalAccounts()") ;
874             //
875             // Set the response to null.
876             array = null ;
877             //
878             // Cancel the database transaction.
879             rollbackTransaction(database) ;
880             }
881         //
882         // Close our database connection.
883         finally
884             {
885             closeConnection(database) ;
886             }
887         //
888         // Return the array.
889         return array ;
890         }
891 
892     /***
893      * Allocate VoSpace space for an Account.
894      * @param account The AccountData to update.
895      * @throws CommunityIdentifierException If the identifier is not valid.
896      * @throws CommunityServiceException If the service is unable to allocate the space.
897      *
898      */
899     protected void allocateSpace(AccountData account)
900         throws CommunityServiceException, CommunityIdentifierException
901         {
902         log.debug("") ;
903         log.debug("----\"----") ;
904         log.debug("AccountManagerImpl.allocateSpace()") ;
905         log.debug("  Account : " + ((null != account) ? account.getIdent() : null)) ;
906         //
907         // Check for null account.
908         if (null == account)
909             {
910             throw new CommunityIdentifierException(
911                 "Null account"
912                 ) ;
913             }
914         //
915         // Check for null identifier.
916         if (null == account.getIdent())
917             {
918             throw new CommunityIdentifierException(
919                 "Null account identifier"
920                 ) ;
921             }
922         //
923         // If the Account home space is not set.
924         if (null == account.getHomeSpace())
925             {
926             //
927             // If we have a default home space.
928             Ivorn service = this.getDefaultVoSpace() ;
929             if (null != service)
930                 {
931                 //
932                 // Try calling the VoSpace client to create the space.
933                 try {
934                     //
935                     // Create a new VoSpaceClient.
936                     VoSpaceClient resolver = new VoSpaceClient(null) ;
937                     //
938                     // Allocate the new home space.
939                     Ivorn ivorn = resolver.createUser(
940                         service,
941                         new Ivorn(
942                             account.getIdent()
943                             )
944                         ) ;
945                     log.debug("  VoSpace ivorn : " + ivorn) ;
946                     //
947                     // Update the Account data
948                     account.setHomeSpace(
949                         ivorn.toString()
950                         ) ;
951                     }
952                 catch (Throwable ouch)
953                     {
954                     //
955                     // Log the exception.
956                     logException(ouch, "AccountManagerImpl.allocateSpace()") ;
957                     //
958                     // Throw a new Exception.
959                     throw new CommunityServiceException(
960                         "Unable to create VoSpace",
961                         ouch
962                         ) ;
963                     }
964                 }
965             }
966         }
967 
968     /***
969      * The config property key for our default VoSpace ivorn.
970      *
971      */
972     private static final String DEFAULT_VOSPACE_PROPERTY = "org.astrogrid.community.default.vospace" ;
973 
974     /***
975      * Get the default VoSpace ivorn.
976      * @return An Ivorn for the default VoSpace service.
977      * @throws CommunityServiceException If unable to get the VoSpace ivorn.
978      *
979      */
980     public Ivorn getDefaultVoSpace()
981         throws CommunityServiceException
982         {
983         log.debug("") ;
984         log.debug("----\"----") ;
985         log.debug("AccountManagerImpl.getDefaultVoSpace()") ;
986         //
987         // Get the default identifier from our config.
988         String string = null ;
989         try {
990             string = (String) config.getProperty(DEFAULT_VOSPACE_PROPERTY,null) ;
991             }
992         catch (PropertyNotFoundException ouch)
993             {
994             throw new CommunityServiceException(
995                 "Default VoSpace not configured"
996                 ) ;
997             }
998         log.debug("    Default VoSpace : " + string) ;
999         //
1000         // If we found the default identifier.
1001         if (null != string)
1002             {
1003             //
1004             // Try making it into an Ivorn.
1005             try {
1006                 return new Ivorn(string) ;
1007                 }
1008             catch (Exception ouch)
1009                 {
1010                 throw new CommunityServiceException(
1011                     "Unable to convert default VoSpace into Ivorn",
1012                     ouch
1013                     ) ;
1014                 }
1015             }
1016         //
1017         // If we didn't find the local ident.
1018         else {
1019             throw new CommunityServiceException(
1020                 "Default VoSpace not configured"
1021                 ) ;
1022             }
1023         }
1024     }