KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > ra > userdatasource > LocalUserDataSourceSessionBean


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.ejb.ra.userdatasource;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Random JavaDoc;
23
24 import javax.ejb.CreateException JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.FinderException JavaDoc;
27
28 import org.ejbca.core.ejb.BaseSessionBean;
29 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal;
30 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome;
31 import org.ejbca.core.ejb.log.ILogSessionLocal;
32 import org.ejbca.core.ejb.log.ILogSessionLocalHome;
33 import org.ejbca.core.model.InternalResources;
34 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
35 import org.ejbca.core.model.authorization.AvailableAccessRules;
36 import org.ejbca.core.model.log.Admin;
37 import org.ejbca.core.model.log.LogEntry;
38 import org.ejbca.core.model.ra.userdatasource.BaseUserDataSource;
39 import org.ejbca.core.model.ra.userdatasource.UserDataSourceConnectionException;
40 import org.ejbca.core.model.ra.userdatasource.UserDataSourceException;
41 import org.ejbca.core.model.ra.userdatasource.UserDataSourceExistsException;
42
43
44 /**
45  * Stores data used by web server clients.
46  * Uses JNDI name for datasource as defined in env 'Datasource' in ejb-jar.xml.
47  *
48  * @ejb.bean description="Session bean handling interface with user data sources"
49  * display-name="UserDataSourceSessionSB"
50  * name="UserDataSourceSession"
51  * jndi-name="UserDataSourceSession"
52  * local-jndi-name="UserDataSourceSessionLocal"
53  * view-type="both"
54  * type="Stateless"
55  * transaction-type="Container"
56  *
57  * @ejb.transaction type="Required"
58  *
59  * @weblogic.enable-call-by-reference True
60  *
61  * @ejb.env-entry name="DataSource"
62  * type="java.lang.String"
63  * value="${datasource.jndi-name-prefix}${datasource.jndi-name}"
64  *
65  *
66  * @ejb.ejb-external-ref description="The UserDataSource entity bean"
67  * view-type="local"
68  * ref-name="ejb/UserDataSourceDataLocal"
69  * type="Entity"
70  * home="org.ejbca.core.ejb.ra.userdatasource.UserDataSourceDataLocalHome"
71  * business="org.ejbca.core.ejb.ra.userdatasource.UserDataSourceDataLocal"
72  * link="UserDataSourceData"
73  *
74  * @ejb.ejb-external-ref description="The Authorization Session Bean"
75  * view-type="local"
76  * ref-name="ejb/AuthorizationSessionLocal"
77  * type="Session"
78  * home="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome"
79  * business="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal"
80  * link="AuthorizationSession"
81  *
82  *
83  * @ejb.ejb-external-ref description="The log session bean"
84  * view-type="local"
85  * ref-name="ejb/LogSessionLocal"
86  * type="Session"
87  * home="org.ejbca.core.ejb.log.ILogSessionLocalHome"
88  * business="org.ejbca.core.ejb.log.ILogSessionLocal"
89  * link="LogSession"
90  *
91  * @ejb.home extends="javax.ejb.EJBHome"
92  * local-extends="javax.ejb.EJBLocalHome"
93  * local-class="org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionLocalHome"
94  * remote-class="org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionHome"
95  *
96  * @ejb.interface extends="javax.ejb.EJBObject"
97  * local-extends="javax.ejb.EJBLocalObject"
98  * local-class="org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionLocal"
99  * remote-class="org.ejbca.core.ejb.ra.userdatasource.IUserDataSourceSessionRemote"
100  *
101  * @jonas.bean ejb-name="UserDataSourceSession"
102  */

103 public class LocalUserDataSourceSessionBean extends BaseSessionBean {
104
105     /** Internal localization of logs and errors */
106     private static final InternalResources intres = InternalResources.getInstance();
107     
108     /**
109      * The local home interface of user data source entity bean.
110      */

111     private UserDataSourceDataLocalHome userdatasourcehome = null;
112
113     /**
114      * The local interface of authorization session bean
115      */

116     private IAuthorizationSessionLocal authorizationsession = null;
117
118     /**
119      * The remote interface of log session bean
120      */

121     private ILogSessionLocal logsession = null;
122
123
124     /**
125      * Default create for SessionBean without any creation Arguments.
126      *
127      * @throws CreateException if bean instance can't be created
128      */

129     public void ejbCreate() throws CreateException JavaDoc {
130         userdatasourcehome = (UserDataSourceDataLocalHome) getLocator().getLocalHome(UserDataSourceDataLocalHome.COMP_NAME);
131     }
132
133
134     /**
135      * Gets connection to log session bean
136      *
137      * @return Connection
138      */

139     private ILogSessionLocal getLogSession() {
140         if (logsession == null) {
141             try {
142                 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME);
143                 logsession = logsessionhome.create();
144             } catch (CreateException JavaDoc e) {
145                 throw new EJBException JavaDoc(e);
146             }
147         }
148         return logsession;
149     } //getLogSession
150

151
152     /**
153      * Gets connection to authorization session bean
154      *
155      * @return IAuthorizationSessionLocal
156      */

157     private IAuthorizationSessionLocal getAuthorizationSession() {
158         if (authorizationsession == null) {
159             try {
160                 IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME);
161                 authorizationsession = authorizationsessionhome.create();
162             } catch (CreateException JavaDoc e) {
163                 throw new EJBException JavaDoc(e);
164             }
165         }
166         return authorizationsession;
167     } //getAuthorizationSession
168

169
170
171     
172
173     /**
174      * Main method used to fetch userdata from the given user data sources
175      * See BaseUserDataSource class for further documentation about function
176      *
177      * @param userdatasourceids a Collection (Integer) of userdatasource Ids.
178      * @return Collection of UserDataSourceVO, empty if no userdata could be found.
179      * @ejb.interface-method view-type="both"
180      * @see org.ejbca.core.model.ra.userdatasource.BaseUserDataSource
181      */

182     public Collection JavaDoc fetch(Admin admin, Collection JavaDoc userdatasourceids, String JavaDoc searchstring) throws UserDataSourceException{
183         Iterator JavaDoc iter = userdatasourceids.iterator();
184         ArrayList JavaDoc result = new ArrayList JavaDoc();
185         while (iter.hasNext()) {
186             Integer JavaDoc id = (Integer JavaDoc) iter.next();
187             try {
188                 UserDataSourceDataLocal pdl = userdatasourcehome.findByPrimaryKey(id);
189                 BaseUserDataSource userdatasource = pdl.getUserDataSource();
190                 if(isAuthorizedToUserDataSource(admin,userdatasource)){
191                   try {
192                     result.addAll(pdl.getUserDataSource().fetchUserDataSourceVOs(admin,searchstring));
193                     String JavaDoc msg = intres.getLocalizedMessage("userdatasource.fetcheduserdatasource", pdl.getName());
194                     getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null,
195                            null, LogEntry.EVENT_INFO_USERDATAFETCHED,msg);
196                   } catch (UserDataSourceException pe) {
197                       String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorfetchuserdatasource", pdl.getName());
198                       getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null,
199                               null, LogEntry.EVENT_ERROR_USERDATAFETCHED,msg);
200                     throw pe;
201
202                   }
203                 }else{
204                     getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,"Error, not authorized to user data source :" + pdl.getName());
205                 }
206             } catch (FinderException JavaDoc fe) {
207                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.erroruserdatasourceexist", id);
208                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null,
209                         LogEntry.EVENT_ERROR_USERDATAFETCHED, msg);
210                 throw new UserDataSourceException(msg);
211
212             }
213         }
214
215         return result;
216     }
217
218
219
220     /**
221      * Test the connection to a user data source
222      *
223      * @param userdatasourceid the id of the userdatasource to test.
224      * @ejb.interface-method view-type="both"
225      * @see org.ejbca.core.model.ra.userdatasource.BaseUserDataSource
226      */

227     public void testConnection(Admin admin, int userdatasourceid) throws UserDataSourceConnectionException {
228         debug(">testConnection(id: " + userdatasourceid + ")");
229         try {
230             UserDataSourceDataLocal pdl = userdatasourcehome.findByPrimaryKey(new Integer JavaDoc(userdatasourceid));
231             BaseUserDataSource userdatasource = pdl.getUserDataSource();
232             if(isAuthorizedToEditUserDataSource(admin,userdatasource)){
233                 try {
234                     userdatasource.testConnection(admin);
235                     String JavaDoc msg = intres.getLocalizedMessage("userdatasource.testedcon", pdl.getName());
236                     getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null,
237                             null, LogEntry.EVENT_INFO_USERDATASOURCEDATA,msg);
238                 } catch (UserDataSourceConnectionException pe) {
239                     String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errortestcon", pdl.getName());
240                     getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null,
241                             LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg, pe);
242                     throw pe;
243                 }
244             }else{
245                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errortestconauth", pdl.getName());
246                 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg);
247             }
248         } catch (FinderException JavaDoc fe) {
249             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.erroruserdatasourceexist", new Integer JavaDoc(userdatasourceid));
250             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null,
251                     LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg);
252         }
253         debug("<testConnection(id: " + userdatasourceid + ")");
254     }
255
256     /**
257      * Adds a user data source to the database.
258      *
259      * @throws UserDataSourceExistsException if user data source already exists.
260      * @throws EJBException if a communication or other error occurs.
261      * @ejb.interface-method view-type="both"
262      */

263
264     public void addUserDataSource(Admin admin, String JavaDoc name, BaseUserDataSource userdatasource) throws UserDataSourceExistsException {
265         debug(">addUserDataSource(name: " + name + ")");
266         addUserDataSource(admin,findFreeUserDataSourceId().intValue(),name,userdatasource);
267         debug("<addUserDataSource()");
268     } // addUserDataSource
269

270
271     /**
272      * Adds a user data source to the database.
273      * Used for importing and exporting profiles from xml-files.
274      *
275      * @throws UserDataSourceExistsException if user data source already exists.
276      * @throws EJBException if a communication or other error occurs.
277      * @ejb.interface-method view-type="both"
278      */

279
280     public void addUserDataSource(Admin admin, int id, String JavaDoc name, BaseUserDataSource userdatasource) throws UserDataSourceExistsException {
281         debug(">addUserDataSource(name: " + name + ", id: " + id + ")");
282         boolean success = false;
283         if(isAuthorizedToEditUserDataSource(admin,userdatasource)){
284             try {
285                 userdatasourcehome.findByName(name);
286             } catch (FinderException JavaDoc e) {
287                 try {
288                     userdatasourcehome.findByPrimaryKey(new Integer JavaDoc(id));
289                 } catch (FinderException JavaDoc f) {
290                     try {
291                         userdatasourcehome.create(new Integer JavaDoc(id), name, userdatasource);
292                         success = true;
293                     } catch (CreateException JavaDoc g) {
294                         error("Unexpected error creating new user data source: ", g);
295                     }
296                 }
297             }
298             if (success) {
299                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.addedsource", name);
300                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg);
301             } else {
302                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.erroraddsource", name);
303                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg);
304             }
305             if (!success)
306                 throw new UserDataSourceExistsException();
307         }else{
308             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errornotauth", name);
309             getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg);
310         }
311         debug("<addUserDataSource()");
312     } // addUserDataSource
313

314     /**
315      * Updates user data source data
316      *
317      * @throws EJBException if a communication or other error occurs.
318      * @ejb.interface-method view-type="both"
319      */

320
321     public void changeUserDataSource(Admin admin, String JavaDoc name, BaseUserDataSource userdatasource) {
322         debug(">changeUserDataSource(name: " + name + ")");
323         boolean success = false;
324         if(isAuthorizedToEditUserDataSource(admin,userdatasource)){
325             try {
326                 UserDataSourceDataLocal htp = userdatasourcehome.findByName(name);
327                 htp.setUserDataSource(userdatasource);
328                 success = true;
329             } catch (FinderException JavaDoc e) {
330             }
331             
332             if (success) {
333                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.changedsource", name);
334                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg);
335             } else {
336                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorchangesource", name);
337                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg);
338             }
339         }else{
340             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errornotauth", name);
341             getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg);
342         }
343         
344         
345         debug("<changeUserDataSource()");
346     } // changeUserDataSource
347

348     /**
349      * Adds a user data source with the same content as the original.
350      * @throws UserDataSourceExistsException
351      *
352      * @throws UserDataSourceExistsException if user data source already exists.
353      * @throws EJBException if a communication or other error occurs.
354      * @ejb.interface-method view-type="both"
355      */

356     public void cloneUserDataSource(Admin admin, String JavaDoc oldname, String JavaDoc newname) throws UserDataSourceExistsException {
357         debug(">cloneUserDataSource(name: " + oldname + ")");
358         BaseUserDataSource userdatasourcedata = null;
359         try {
360             UserDataSourceDataLocal htp = userdatasourcehome.findByName(oldname);
361             userdatasourcedata = (BaseUserDataSource) htp.getUserDataSource().clone();
362             if(isAuthorizedToEditUserDataSource(admin,userdatasourcedata)){
363                 try {
364                     addUserDataSource(admin, newname, userdatasourcedata);
365                     String JavaDoc msg = intres.getLocalizedMessage("userdatasource.clonedsource", newname, oldname);
366                     getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg);
367                 } catch (UserDataSourceExistsException f) {
368                     String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorclonesource", newname, oldname);
369                     getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg);
370                     throw f;
371                 }
372             }else{
373                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errornotauth", oldname);
374                 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg);
375             }
376         } catch (FinderException JavaDoc e) {
377             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorclonesource", newname, oldname);
378             error(msg, e);
379             throw new EJBException JavaDoc(e);
380         } catch (CloneNotSupportedException JavaDoc e) {
381             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorclonesource", newname, oldname);
382             error(msg, e);
383             throw new EJBException JavaDoc(e);
384         }
385
386         debug("<cloneUserDataSource()");
387     } // cloneUserDataSource
388

389     /**
390      * Removes a user data source from the database.
391      *
392      * @throws EJBException if a communication or other error occurs.
393      * @ejb.interface-method view-type="both"
394      */

395     public boolean removeUserDataSource(Admin admin, String JavaDoc name) {
396         debug(">removeUserDataSource(name: " + name + ")");
397         boolean retval = false;
398         try {
399             UserDataSourceDataLocal htp = userdatasourcehome.findByName(name);
400             BaseUserDataSource userdatasource = htp.getUserDataSource();
401             if(isAuthorizedToEditUserDataSource(admin,userdatasource)){
402               htp.remove();
403               String JavaDoc msg = intres.getLocalizedMessage("userdatasource.removedsource", name);
404               getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg);
405               retval = true;
406             }else{
407                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errornotauth", name);
408                 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg);
409             }
410         } catch (Exception JavaDoc e) {
411             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorremovesource", name);
412             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg, e);
413         }
414         debug("<removeUserDataSource()");
415         
416         return retval;
417     } // removeUserDataSource
418

419     /**
420      * Renames a user data source
421      *
422      * @throws UserDataSourceExistsException if user data source already exists.
423      * @throws EJBException if a communication or other error occurs.
424      * @ejb.interface-method view-type="both"
425      */

426     public void renameUserDataSource(Admin admin, String JavaDoc oldname, String JavaDoc newname) throws UserDataSourceExistsException {
427         debug(">renameUserDataSource(from " + oldname + " to " + newname + ")");
428         boolean success = false;
429         try {
430             userdatasourcehome.findByName(newname);
431         } catch (FinderException JavaDoc e) {
432             try {
433                 UserDataSourceDataLocal htp = userdatasourcehome.findByName(oldname);
434                 if(isAuthorizedToEditUserDataSource(admin,htp.getUserDataSource())){
435                   htp.setName(newname);
436                   success = true;
437                 }else{
438                     String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errornotauth", oldname);
439                     getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE, msg);
440                 }
441             } catch (FinderException JavaDoc g) {
442             }
443         }
444
445         if (success) {
446             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.renamedsource", oldname, newname);
447             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_USERDATASOURCEDATA, msg);
448         } else {
449             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorrenamesource", oldname, newname);
450             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_USERDATASOURCEDATA, msg);
451         }
452
453         if (!success)
454             throw new UserDataSourceExistsException();
455         debug("<renameUserDataSource()");
456     } // renameUserDataSource
457

458     /**
459      * Retrives a Collection of id:s (Integer) to authorized user data sources.
460      *
461      * @param indicates if sources with anyca set should be included
462      * @return Collection of id:s (Integer)
463      * @ejb.interface-method view-type="both"
464      */

465     public Collection JavaDoc getAuthorizedUserDataSourceIds(Admin admin, boolean includeAnyCA) {
466         HashSet JavaDoc returnval = new HashSet JavaDoc();
467         Collection JavaDoc result = null;
468         boolean superadmin = false;
469         // If superadmin return all available user data sources
470
try {
471             try{
472               superadmin = getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.ROLE_SUPERADMINISTRATOR);
473             }catch (AuthorizationDeniedException e1) {
474                 log.debug("AuthorizationDeniedException: ", e1);
475             }
476             Collection JavaDoc authorizedcas = this.getAuthorizationSession().getAuthorizedCAIds(admin);
477             result = this.userdatasourcehome.findAll();
478             Iterator JavaDoc i = result.iterator();
479             while (i.hasNext()) {
480                 UserDataSourceDataLocal next = (UserDataSourceDataLocal) i.next();
481                 if(superadmin){
482                   returnval.add(next.getId());
483                 }else{
484                     BaseUserDataSource userdatasource = next.getUserDataSource();
485                     if(userdatasource.getApplicableCAs().contains(new Integer JavaDoc(BaseUserDataSource.ANYCA))){
486                         if(includeAnyCA){
487                             returnval.add(next.getId());
488                         }
489                     }else{
490                         if(authorizedcas.containsAll(userdatasource.getApplicableCAs())){
491                             returnval.add(next.getId());
492                         }
493                     }
494                 }
495             }
496         } catch (FinderException JavaDoc fe) {
497             String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errorfindingall");
498             log.error(msg, fe);
499         }
500
501         return returnval;
502     } // getAuthorizedUserDataSourceIds
503

504     /**
505      * Method creating a hashmap mapping user data source id (Integer) to user data source name (String).
506      *
507      * @ejb.transaction type="Supports"
508      * @ejb.interface-method view-type="both"
509      */

510     public HashMap JavaDoc getUserDataSourceIdToNameMap(Admin admin) {
511         HashMap JavaDoc returnval = new HashMap JavaDoc();
512         Collection JavaDoc result = null;
513
514         try {
515             result = userdatasourcehome.findAll();
516             Iterator JavaDoc i = result.iterator();
517             while (i.hasNext()) {
518                 UserDataSourceDataLocal next = (UserDataSourceDataLocal) i.next();
519                 returnval.put(next.getId(), next.getName());
520             }
521         } catch (FinderException JavaDoc e) {
522         }
523         return returnval;
524     } // getUserDataSourceIdToNameMap
525

526
527     /**
528      * Retrives a named user data source.
529      *
530      * @ejb.transaction type="Supports"
531      * @ejb.interface-method view-type="both"
532      */

533     public BaseUserDataSource getUserDataSource(Admin admin, String JavaDoc name) {
534         BaseUserDataSource returnval = null;
535
536         try {
537             BaseUserDataSource result = (userdatasourcehome.findByName(name)).getUserDataSource();
538             if(isAuthorizedToEditUserDataSource(admin,result)){
539                 returnval = result;
540             }else{
541                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errornotauth", name);
542                 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg);
543             }
544         } catch (FinderException JavaDoc e) {
545             // return null if we cant find it
546
}
547         return returnval;
548     } // getUserDataSource
549

550     /**
551      * Finds a user data source by id.
552      *
553      * @ejb.transaction type="Supports"
554      * @ejb.interface-method view-type="both"
555      */

556     public BaseUserDataSource getUserDataSource(Admin admin, int id) {
557         BaseUserDataSource returnval = null;
558
559         try {
560             BaseUserDataSource result = (userdatasourcehome.findByPrimaryKey(new Integer JavaDoc(id))).getUserDataSource();
561             if(isAuthorizedToEditUserDataSource(admin,result)){
562                 returnval = result;
563             }else{
564                 String JavaDoc msg = intres.getLocalizedMessage("userdatasource.errornotauth", new Integer JavaDoc(id));
565                 getLogSession().log(admin, admin.getCaId(),LogEntry.MODULE_RA,new Date JavaDoc(),null,null,LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg);
566             }
567         } catch (FinderException JavaDoc e) {
568             // return null if we cant find it
569
}
570         return returnval;
571     } // getUserDataSource
572

573     /**
574      * Help method used by user data source proxys to indicate if it is time to
575      * update it's data.
576      *
577      * @ejb.transaction type="Supports"
578      * @ejb.interface-method view-type="both"
579      */

580
581     public int getUserDataSourceUpdateCount(Admin admin, int userdatasourceid) {
582         int returnval = 0;
583
584         try {
585             returnval = (userdatasourcehome.findByPrimaryKey(new Integer JavaDoc(userdatasourceid))).getUpdateCounter();
586         } catch (FinderException JavaDoc e) {
587         }
588
589         return returnval;
590     }
591
592
593     /**
594      * Returns a user data source id, given it's user data source name
595      *
596      * @return the id or 0 if the user data source cannot be found.
597      * @ejb.transaction type="Supports"
598      * @ejb.interface-method view-type="both"
599      */

600     public int getUserDataSourceId(Admin admin, String JavaDoc name) {
601         int returnval = 0;
602
603         try {
604             Integer JavaDoc id = (userdatasourcehome.findByName(name)).getId();
605             returnval = id.intValue();
606         } catch (FinderException JavaDoc e) {
607         }
608
609         return returnval;
610     } // getUserDataSourceId
611

612     /**
613      * Returns a user data source name given its id.
614      *
615      * @return the name or null if id doesnt exists
616      * @throws EJBException if a communication or other error occurs.
617      * @ejb.transaction type="Supports"
618      * @ejb.interface-method view-type="both"
619      */

620     public String JavaDoc getUserDataSourceName(Admin admin, int id) {
621         debug(">getUserDataSourceName(id: " + id + ")");
622         String JavaDoc returnval = null;
623         UserDataSourceDataLocal htp = null;
624         try {
625             htp = userdatasourcehome.findByPrimaryKey(new Integer JavaDoc(id));
626             if (htp != null) {
627                 returnval = htp.getName();
628             }
629         } catch (FinderException JavaDoc e) {
630         }
631
632         debug("<getUserDataSourceName()");
633         return returnval;
634     } // getUserDataSourceName
635

636     /**
637      * Method to check if an admin is authorized to fetch user data from userdata source
638      * The following checks are performed.
639      *
640      * 1. If the admin is an administrator
641      * 2. If the admin is authorized to all cas applicable to userdata source.
642      * or
643      * If the userdatasource have "ANYCA" set.
644      * @return true if the administrator is authorized
645      */

646     private boolean isAuthorizedToUserDataSource(Admin admin, BaseUserDataSource userdatasource) {
647         try {
648             if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_SUPERADMINISTRATOR)){
649                 return true;
650             }
651             
652         } catch (AuthorizationDeniedException e) {}
653         try {
654             if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_ADMINISTRATOR)){
655                 if(userdatasource.getApplicableCAs().contains(new Integer JavaDoc(BaseUserDataSource.ANYCA))){
656                     return true;
657                 }
658                 Collection JavaDoc authorizedcas = getAuthorizationSession().getAuthorizedCAIds(admin);
659                 if(authorizedcas.containsAll(userdatasource.getApplicableCAs())){
660                     return true;
661                 }
662             }
663         } catch (AuthorizationDeniedException e) {}
664         
665         return false;
666     }
667     
668     /**
669      * Method to check if an admin is authorized to edit an user data source
670      * The following checks are performed.
671      *
672      * 1. If the admin is an administrator
673      * 2. If tha admin is authorized AvailableAccessRules.REGULAR_EDITUSERDATASOURCES
674      * 3. Only the superadmin should have edit access to user data sources with 'ANYCA' set
675      * 4. Administrators should be authorized to all the user data source applicable cas.
676      *
677      * @return true if the administrator is authorized
678      */

679     private boolean isAuthorizedToEditUserDataSource(Admin admin, BaseUserDataSource userdatasource) {
680         try {
681             if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_SUPERADMINISTRATOR)){
682                 return true;
683             }
684         } catch (AuthorizationDeniedException e) {}
685         try {
686             if(getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.ROLE_ADMINISTRATOR) &&
687                     getAuthorizationSession().isAuthorizedNoLog(admin,AvailableAccessRules.REGULAR_EDITUSERDATASOURCES)){
688                 if(userdatasource.getApplicableCAs().contains(new Integer JavaDoc(BaseUserDataSource.ANYCA))){
689                     return false;
690                 }
691                 Collection JavaDoc authorizedcas = getAuthorizationSession().getAuthorizedCAIds(admin);
692                 if(authorizedcas.containsAll(userdatasource.getApplicableCAs())){
693                     return true;
694                 }
695             }
696         } catch (AuthorizationDeniedException e) {}
697         
698         return false;
699     }
700
701
702     private Integer JavaDoc findFreeUserDataSourceId() {
703         Random JavaDoc ran = (new Random JavaDoc((new Date JavaDoc()).getTime()));
704         int id = ran.nextInt();
705         boolean foundfree = false;
706
707         while (!foundfree) {
708             try {
709                 if (id > 1)
710                    userdatasourcehome.findByPrimaryKey(new Integer JavaDoc(id));
711                 id = ran.nextInt();
712             } catch (FinderException JavaDoc e) {
713                 foundfree = true;
714             }
715         }
716         return new Integer JavaDoc(id);
717     } // findFreeUserDataSourceId
718

719
720 } // LocalUserDataSourceSessionBean
721
Popular Tags