KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > ca > publisher > LocalPublisherSessionBean


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.ca.publisher;
15
16 import java.security.cert.Certificate JavaDoc;
17 import java.security.cert.X509Certificate JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Random JavaDoc;
24
25 import javax.ejb.CreateException JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.FinderException JavaDoc;
28
29 import org.ejbca.core.ejb.BaseSessionBean;
30 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal;
31 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome;
32 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal;
33 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome;
34 import org.ejbca.core.ejb.log.ILogSessionLocal;
35 import org.ejbca.core.ejb.log.ILogSessionLocalHome;
36 import org.ejbca.core.model.InternalResources;
37 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
38 import org.ejbca.core.model.authorization.AvailableAccessRules;
39 import org.ejbca.core.model.ca.publisher.BasePublisher;
40 import org.ejbca.core.model.ca.publisher.PublisherConnectionException;
41 import org.ejbca.core.model.ca.publisher.PublisherException;
42 import org.ejbca.core.model.ca.publisher.PublisherExistsException;
43 import org.ejbca.core.model.log.Admin;
44 import org.ejbca.core.model.log.LogEntry;
45 import org.ejbca.core.model.ra.ExtendedInformation;
46
47
48 /**
49  * Stores data used by web server clients.
50  * Uses JNDI name for datasource as defined in env 'Datasource' in ejb-jar.xml.
51  *
52  * @ejb.bean description="Session bean handling interface with publisher data"
53  * display-name="PublisherSessionSB"
54  * name="PublisherSession"
55  * jndi-name="PublisherSession"
56  * local-jndi-name="PublisherSessionLocal"
57  * view-type="both"
58  * type="Stateless"
59  * transaction-type="Container"
60  *
61  * @ejb.transaction type="Required"
62  *
63  * @weblogic.enable-call-by-reference True
64  *
65  * @ejb.env-entry name="DataSource"
66  * type="java.lang.String"
67  * value="${datasource.jndi-name-prefix}${datasource.jndi-name}"
68  *
69  *
70  * @ejb.ejb-external-ref description="The Publisher entity bean"
71  * view-type="local"
72  * ref-name="ejb/PublisherDataLocal"
73  * type="Entity"
74  * home="org.ejbca.core.ejb.ca.publisher.PublisherDataLocalHome"
75  * business="org.ejbca.core.ejb.ca.publisher.PublisherDataLocal"
76  * link="PublisherData"
77  *
78  * @ejb.ejb-external-ref description="The Authorization Session Bean"
79  * view-type="local"
80  * ref-name="ejb/AuthorizationSessionLocal"
81  * type="Session"
82  * home="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome"
83  * business="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal"
84  * link="AuthorizationSession"
85  *
86  * @ejb.ejb-external-ref description="The CAAdmin Session Bean"
87  * view-type="local"
88  * ref-name="ejb/CAAdminSessionLocal"
89  * type="Session"
90  * home="org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome"
91  * business="org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal"
92  * link="CAAdminSession"
93  *
94  * @ejb.ejb-external-ref description="The log session bean"
95  * view-type="local"
96  * ref-name="ejb/LogSessionLocal"
97  * type="Session"
98  * home="org.ejbca.core.ejb.log.ILogSessionLocalHome"
99  * business="org.ejbca.core.ejb.log.ILogSessionLocal"
100  * link="LogSession"
101  *
102  * @ejb.home extends="javax.ejb.EJBHome"
103  * local-extends="javax.ejb.EJBLocalHome"
104  * local-class="org.ejbca.core.ejb.ca.publisher.IPublisherSessionLocalHome"
105  * remote-class="org.ejbca.core.ejb.ca.publisher.IPublisherSessionHome"
106  *
107  * @ejb.interface extends="javax.ejb.EJBObject"
108  * local-extends="javax.ejb.EJBLocalObject"
109  * local-class="org.ejbca.core.ejb.ca.publisher.IPublisherSessionLocal"
110  * remote-class="org.ejbca.core.ejb.ca.publisher.IPublisherSessionRemote"
111  *
112  * @jonas.bean ejb-name="PublisherSession"
113  */

114 public class LocalPublisherSessionBean extends BaseSessionBean {
115
116     /** Internal localization of logs and errors */
117     private static final InternalResources intres = InternalResources.getInstance();
118     
119     /**
120      * The local home interface of publisher entity bean.
121      */

122     private PublisherDataLocalHome publisherhome = null;
123
124     /**
125      * The local interface of ca admin session bean
126      */

127     private ICAAdminSessionLocal caadminsession = null;
128
129     /**
130      * The local interface of authorization session bean
131      */

132     private IAuthorizationSessionLocal authorizationsession = null;
133
134     /**
135      * The remote interface of log session bean
136      */

137     private ILogSessionLocal logsession = null;
138
139
140     /**
141      * Default create for SessionBean without any creation Arguments.
142      *
143      * @throws CreateException if bean instance can't be created
144      */

145     public void ejbCreate() throws CreateException JavaDoc {
146         publisherhome = (PublisherDataLocalHome) getLocator().getLocalHome(PublisherDataLocalHome.COMP_NAME);
147     }
148
149
150     /**
151      * Gets connection to log session bean
152      *
153      * @return Connection
154      */

155     private ILogSessionLocal getLogSession() {
156         if (logsession == null) {
157             try {
158                 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME);
159                 logsession = logsessionhome.create();
160             } catch (CreateException JavaDoc e) {
161                 throw new EJBException JavaDoc(e);
162             }
163         }
164         return logsession;
165     } //getLogSession
166

167
168     /**
169      * Gets connection to authorization session bean
170      *
171      * @return IAuthorizationSessionLocal
172      */

173     private IAuthorizationSessionLocal getAuthorizationSession() {
174         if (authorizationsession == null) {
175             try {
176                 IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME);
177                 authorizationsession = authorizationsessionhome.create();
178             } catch (CreateException JavaDoc e) {
179                 throw new EJBException JavaDoc(e);
180             }
181         }
182         return authorizationsession;
183     } //getAuthorizationSession
184

185     /**
186      * Gets connection to caadmin session bean
187      *
188      * @return ICAAdminSessionLocal
189      */

190     private ICAAdminSessionLocal getCAAdminSession() {
191         if (caadminsession == null) {
192             try {
193                 ICAAdminSessionLocalHome caadminsessionhome = (ICAAdminSessionLocalHome) getLocator().getLocalHome(ICAAdminSessionLocalHome.COMP_NAME);
194                 caadminsession = caadminsessionhome.create();
195             } catch (CreateException JavaDoc e) {
196                 throw new EJBException JavaDoc(e);
197             }
198         }
199         return caadminsession;
200     } //getCAAdminSession
201

202
203     /**
204      * Stores the certificate to the given collection of publishers.
205      * See BasePublisher class for further documentation about function
206      *
207      * @param publisherids a Collection (Integer) of publisherids.
208      * @return true if sucessfull result on all given publishers
209      * @ejb.interface-method view-type="both"
210      * @see org.ejbca.core.model.ca.publisher.BasePublisher
211      */

212     public boolean storeCertificate(Admin admin, Collection JavaDoc publisherids, Certificate JavaDoc incert, String JavaDoc username, String JavaDoc password, String JavaDoc cafp, int status, int type, long revocationDate, int revocationReason, ExtendedInformation extendedinformation) {
213         Iterator JavaDoc iter = publisherids.iterator();
214         boolean returnval = true;
215         while (iter.hasNext()) {
216             Integer JavaDoc id = (Integer JavaDoc) iter.next();
217             try {
218                 PublisherDataLocal pdl = publisherhome.findByPrimaryKey(id);
219                 try {
220                     returnval &= pdl.getPublisher().storeCertificate(admin, incert, username, password, cafp, status, type, revocationDate, revocationReason, extendedinformation);
221                     String JavaDoc msg = intres.getLocalizedMessage("publisher.store", ((X509Certificate JavaDoc) incert).getSubjectDN().toString(), pdl.getName());
222                     getLogSession().log(admin, (X509Certificate JavaDoc) incert, LogEntry.MODULE_CA, new java.util.Date JavaDoc(), username,
223                             (X509Certificate JavaDoc) incert, LogEntry.EVENT_INFO_STORECERTIFICATE, msg);
224                 } catch (PublisherException pe) {
225                     String JavaDoc msg = intres.getLocalizedMessage("publisher.errorstore", pdl.getName());
226                     getLogSession().log(admin, (X509Certificate JavaDoc) incert, LogEntry.MODULE_CA, new java.util.Date JavaDoc(), username, (X509Certificate JavaDoc) incert,
227                             LogEntry.EVENT_ERROR_STORECERTIFICATE, msg, pe);
228
229                 }
230             } catch (FinderException JavaDoc fe) {
231                 String JavaDoc msg = intres.getLocalizedMessage("publisher.nopublisher", id);
232                 getLogSession().log(admin, (X509Certificate JavaDoc) incert, LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, (X509Certificate JavaDoc) incert,
233                         LogEntry.EVENT_ERROR_STORECERTIFICATE, msg);
234
235             }
236         }
237
238         return returnval;
239     }
240
241     /**
242      * Stores the crl to the given collection of publishers.
243      * See BasePublisher class for further documentation about function
244      *
245      * @param publisherids a Collection (Integer) of publisherids.
246      * @return true if sucessfull result on all given publishers
247      * @ejb.interface-method view-type="both"
248      * @see org.ejbca.core.model.ca.publisher.BasePublisher
249      */

250     public boolean storeCRL(Admin admin, Collection JavaDoc publisherids, byte[] incrl, String JavaDoc cafp, int number) {
251         Iterator JavaDoc iter = publisherids.iterator();
252         boolean returnval = true;
253         while (iter.hasNext()) {
254             Integer JavaDoc id = (Integer JavaDoc) iter.next();
255             try {
256                 PublisherDataLocal pdl = publisherhome.findByPrimaryKey(id);
257                 try {
258                     returnval &= pdl.getPublisher().storeCRL(admin, incrl, cafp, number);
259                     String JavaDoc msg = intres.getLocalizedMessage("publisher.store", "CRL", pdl.getName());
260                     getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null,
261                             null, LogEntry.EVENT_INFO_STORECRL, msg);
262                 } catch (PublisherException pe) {
263                     String JavaDoc msg = intres.getLocalizedMessage("publisher.errorstorecert", pdl.getName());
264                     getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null,
265                             LogEntry.EVENT_ERROR_STORECRL, msg, pe);
266
267                 }
268             } catch (FinderException JavaDoc fe) {
269                 String JavaDoc msg = intres.getLocalizedMessage("publisher.nopublisher", id);
270                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null,
271                         LogEntry.EVENT_ERROR_STORECRL, msg);
272
273             }
274         }
275
276         return returnval;
277     }
278
279     /**
280      * Revokes the certificate in the given collection of publishers.
281      * See BasePublisher class for further documentation about function
282      *
283      * @param publisherids a Collection (Integer) of publisherids.
284      * @ejb.interface-method view-type="both"
285      * @see org.ejbca.core.model.ca.publisher.BasePublisher
286      */

287     public void revokeCertificate(Admin admin, Collection JavaDoc publisherids, Certificate JavaDoc cert, int reason) {
288         Iterator JavaDoc iter = publisherids.iterator();
289         while (iter.hasNext()) {
290             Integer JavaDoc id = (Integer JavaDoc) iter.next();
291             try {
292                 PublisherDataLocal pdl = publisherhome.findByPrimaryKey(id);
293                 try {
294                     pdl.getPublisher().revokeCertificate(admin, cert, reason);
295                     String JavaDoc msg = intres.getLocalizedMessage("publisher.store", ((X509Certificate JavaDoc) cert).getSubjectDN().toString(), pdl.getName());
296                     getLogSession().log(admin, (X509Certificate JavaDoc) cert, LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null,
297                             (X509Certificate JavaDoc) cert, LogEntry.EVENT_INFO_REVOKEDCERT, msg);
298                 } catch (PublisherException pe) {
299                     String JavaDoc msg = intres.getLocalizedMessage("publisher.errorstore", pdl.getName());
300                     getLogSession().log(admin, (X509Certificate JavaDoc) cert, LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, (X509Certificate JavaDoc) cert,
301                             LogEntry.EVENT_ERROR_REVOKEDCERT, msg, pe);
302
303                 }
304             } catch (FinderException JavaDoc fe) {
305                 String JavaDoc msg = intres.getLocalizedMessage("publisher.nopublisher", id);
306                 getLogSession().log(admin, (X509Certificate JavaDoc) cert, LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, (X509Certificate JavaDoc) cert,
307                         LogEntry.EVENT_ERROR_REVOKEDCERT, msg);
308
309             }
310         }
311     }
312
313     /**
314      * Test the connection to of a publisher
315      *
316      * @param publisherid the id of the publisher to test.
317      * @ejb.interface-method view-type="both"
318      * @see org.ejbca.core.model.ca.publisher.BasePublisher
319      */

320     public void testConnection(Admin admin, int publisherid) throws PublisherConnectionException {
321         debug(">testConnection(id: " + publisherid + ")");
322         try {
323             PublisherDataLocal pdl = publisherhome.findByPrimaryKey(new Integer JavaDoc(publisherid));
324             try {
325                 pdl.getPublisher().testConnection(admin);
326                 String JavaDoc msg = intres.getLocalizedMessage("publisher.testedpublisher", pdl.getName());
327                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null,
328                         null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg);
329             } catch (PublisherConnectionException pe) {
330                 String JavaDoc msg = intres.getLocalizedMessage("publisher.errortestpublisher", pdl.getName());
331                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null,
332                         LogEntry.EVENT_ERROR_PUBLISHERDATA, msg, pe);
333
334                 throw new PublisherConnectionException(pe.getMessage());
335             }
336         } catch (FinderException JavaDoc fe) {
337             String JavaDoc msg = intres.getLocalizedMessage("publisher.nopublisher", new Integer JavaDoc(publisherid));
338             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null,
339                     LogEntry.EVENT_ERROR_PUBLISHERDATA, msg);
340
341         }
342         debug("<testConnection(id: " + publisherid + ")");
343     }
344
345     /**
346      * Adds a publisher to the database.
347      *
348      * @throws PublisherExistsException if hard token already exists.
349      * @throws EJBException if a communication or other error occurs.
350      * @ejb.interface-method view-type="both"
351      */

352
353     public void addPublisher(Admin admin, String JavaDoc name, BasePublisher publisher) throws PublisherExistsException {
354         debug(">addPublisher(name: " + name + ")");
355         addPublisher(admin,findFreePublisherId().intValue(),name,publisher);
356         debug("<addPublisher()");
357     } // addPublisher
358

359
360     /**
361      * Adds a publisher to the database.
362      * Used for importing and exporting profiles from xml-files.
363      *
364      * @throws PublisherExistsException if hard token already exists.
365      * @throws EJBException if a communication or other error occurs.
366      * @ejb.interface-method view-type="both"
367      */

368
369     public void addPublisher(Admin admin, int id, String JavaDoc name, BasePublisher publisher) throws PublisherExistsException {
370         debug(">addPublisher(name: " + name + ", id: " + id + ")");
371         boolean success = false;
372         try {
373             publisherhome.findByName(name);
374         } catch (FinderException JavaDoc e) {
375             try {
376                 publisherhome.findByPrimaryKey(new Integer JavaDoc(id));
377             } catch (FinderException JavaDoc f) {
378                 try {
379                     publisherhome.create(new Integer JavaDoc(id), name, publisher);
380                     success = true;
381                 } catch (CreateException JavaDoc g) {
382                     String JavaDoc msg = intres.getLocalizedMessage("publisher.erroraddpublisher", name);
383                     error(msg, g);
384                 }
385             }
386         }
387         if (success) {
388             String JavaDoc msg = intres.getLocalizedMessage("publisher.addedpublisher", name);
389             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg);
390         } else {
391             String JavaDoc msg = intres.getLocalizedMessage("publisher.erroraddpublisher", name);
392             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg);
393         }
394         if (!success)
395             throw new PublisherExistsException();
396         debug("<addPublisher()");
397     } // addPublisher
398

399     /**
400      * Updates publisher data
401      *
402      * @throws EJBException if a communication or other error occurs.
403      * @ejb.interface-method view-type="both"
404      */

405
406     public void changePublisher(Admin admin, String JavaDoc name, BasePublisher publisher) {
407         debug(">changePublisher(name: " + name + ")");
408         boolean success = false;
409         try {
410             PublisherDataLocal htp = publisherhome.findByName(name);
411             htp.setPublisher(publisher);
412             success = true;
413         } catch (FinderException JavaDoc e) {
414         }
415
416         if (success) {
417             String JavaDoc msg = intres.getLocalizedMessage("publisher.changedpublisher", name);
418             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg);
419         } else {
420             String JavaDoc msg = intres.getLocalizedMessage("publisher.errorchangepublisher", name);
421             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg);
422         }
423
424         debug("<changePublisher()");
425     } // changePublisher
426

427     /**
428      * Adds a publisher with the same content as the original.
429      *
430      * @throws PublisherExistsException if publisher already exists.
431      * @throws EJBException if a communication or other error occurs.
432      * @ejb.interface-method view-type="both"
433      */

434     public void clonePublisher(Admin admin, String JavaDoc oldname, String JavaDoc newname) {
435         debug(">clonePublisher(name: " + oldname + ")");
436         BasePublisher publisherdata = null;
437         try {
438             PublisherDataLocal htp = publisherhome.findByName(oldname);
439             publisherdata = (BasePublisher) htp.getPublisher().clone();
440             try {
441                 addPublisher(admin, newname, publisherdata);
442                 String JavaDoc msg = intres.getLocalizedMessage("publisher.clonedpublisher", newname, oldname);
443                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg);
444             } catch (PublisherExistsException f) {
445                 String JavaDoc msg = intres.getLocalizedMessage("publisher.errorclonepublisher", newname, oldname);
446                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg);
447                 throw f;
448             }
449         } catch (Exception JavaDoc e) {
450             String JavaDoc msg = intres.getLocalizedMessage("publisher.errorclonepublisher", newname, oldname);
451             error(msg, e);
452             throw new EJBException JavaDoc(e);
453         }
454
455         debug("<clonePublisher()");
456     } // clonePublisher
457

458     /**
459      * Removes a publisher from the database.
460      *
461      * @throws EJBException if a communication or other error occurs.
462      * @ejb.interface-method view-type="both"
463      */

464     public void removePublisher(Admin admin, String JavaDoc name) {
465         debug(">removePublisher(name: " + name + ")");
466         try {
467             PublisherDataLocal htp = publisherhome.findByName(name);
468             htp.remove();
469             String JavaDoc msg = intres.getLocalizedMessage("publisher.removedpublisher", name);
470             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg);
471         } catch (Exception JavaDoc e) {
472             String JavaDoc msg = intres.getLocalizedMessage("publisher.errorremovepublisher", name);
473             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg, e);
474         }
475         debug("<removePublisher()");
476     } // removePublisher
477

478     /**
479      * Renames a publisher
480      *
481      * @throws PublisherExistsException if publisher already exists.
482      * @throws EJBException if a communication or other error occurs.
483      * @ejb.interface-method view-type="both"
484      */

485     public void renamePublisher(Admin admin, String JavaDoc oldname, String JavaDoc newname) throws PublisherExistsException {
486         debug(">renamePublisher(from " + oldname + " to " + newname + ")");
487         boolean success = false;
488         try {
489             publisherhome.findByName(newname);
490         } catch (FinderException JavaDoc e) {
491             try {
492                 PublisherDataLocal htp = publisherhome.findByName(oldname);
493                 htp.setName(newname);
494                 success = true;
495             } catch (FinderException JavaDoc g) {
496             }
497         }
498
499         if (success) {
500             String JavaDoc msg = intres.getLocalizedMessage("publisher.renamedpublisher", oldname, newname);
501             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg);
502         } else {
503             String JavaDoc msg = intres.getLocalizedMessage("publisher.errorrenamepublisher", oldname, newname);
504             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg);
505         }
506         if (!success)
507             throw new PublisherExistsException();
508         debug("<renamePublisher()");
509     } // renameHardTokenProfile
510

511     /**
512      * Retrives a Collection of id:s (Integer) to authorized publishers.
513      *
514      * @return Collection of id:s (Integer)
515      * @ejb.interface-method view-type="both"
516      */

517     public Collection JavaDoc getAuthorizedPublisherIds(Admin admin) {
518         HashSet JavaDoc returnval = new HashSet JavaDoc();
519         Collection JavaDoc result = null;
520         boolean superadmin = false;
521         // If superadmin return all available publishers
522
try {
523             superadmin = getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.ROLE_SUPERADMINISTRATOR);
524             result = this.publisherhome.findAll();
525             Iterator JavaDoc i = result.iterator();
526             while (i.hasNext()) {
527                 PublisherDataLocal next = (PublisherDataLocal) i.next();
528                 returnval.add(next.getId());
529             }
530         } catch (AuthorizationDeniedException e1) {
531             log.debug("AuthorizationDeniedException: ", e1);
532         } catch (FinderException JavaDoc fe) {
533             log.error("FinderException looking for all publishers: ", fe);
534         }
535
536         // If CA-admin return publishers he is authorized to
537
if (!superadmin) {
538             Iterator JavaDoc authorizedcas = this.getAuthorizationSession().getAuthorizedCAIds(admin).iterator();
539             while (authorizedcas.hasNext()) {
540                 returnval.addAll(this.getCAAdminSession().getCAInfo(admin, ((Integer JavaDoc) authorizedcas.next()).intValue()).getCRLPublishers());
541             }
542         }
543         return returnval;
544     } // getAuthorizedPublisherIds
545

546     /**
547      * Method creating a hashmap mapping publisher id (Integer) to publisher name (String).
548      *
549      * @ejb.transaction type="Supports"
550      * @ejb.interface-method view-type="both"
551      */

552     public HashMap JavaDoc getPublisherIdToNameMap(Admin admin) {
553         HashMap JavaDoc returnval = new HashMap JavaDoc();
554         Collection JavaDoc result = null;
555
556         try {
557             result = publisherhome.findAll();
558             Iterator JavaDoc i = result.iterator();
559             while (i.hasNext()) {
560                 PublisherDataLocal next = (PublisherDataLocal) i.next();
561                 returnval.put(next.getId(), next.getName());
562             }
563         } catch (FinderException JavaDoc e) {
564         }
565         return returnval;
566     } // getPublisherIdToNameMap
567

568
569     /**
570      * Retrives a named publisher.
571      *
572      * @ejb.transaction type="Supports"
573      * @ejb.interface-method view-type="both"
574      */

575     public BasePublisher getPublisher(Admin admin, String JavaDoc name) {
576         BasePublisher returnval = null;
577
578         try {
579             returnval = (publisherhome.findByName(name)).getPublisher();
580         } catch (FinderException JavaDoc e) {
581             // return null if we cant find it
582
}
583         return returnval;
584     } // getPublisher
585

586     /**
587      * Finds a publisher by id.
588      *
589      * @ejb.transaction type="Supports"
590      * @ejb.interface-method view-type="both"
591      */

592     public BasePublisher getPublisher(Admin admin, int id) {
593         BasePublisher returnval = null;
594
595         try {
596             returnval = (publisherhome.findByPrimaryKey(new Integer JavaDoc(id))).getPublisher();
597         } catch (FinderException JavaDoc e) {
598             // return null if we cant find it
599
}
600         return returnval;
601     } // getPublisher
602

603     /**
604      * Help method used by publisher proxys to indicate if it is time to
605      * update it's data.
606      *
607      * @ejb.transaction type="Supports"
608      * @ejb.interface-method view-type="both"
609      */

610
611     public int getPublisherUpdateCount(Admin admin, int publisherid) {
612         int returnval = 0;
613
614         try {
615             returnval = (publisherhome.findByPrimaryKey(new Integer JavaDoc(publisherid))).getUpdateCounter();
616         } catch (FinderException JavaDoc e) {
617         }
618
619         return returnval;
620     }
621
622
623     /**
624      * Returns a publisher id, given it's publishers name
625      *
626      * @return the id or 0 if the publisher cannot be found.
627      * @ejb.transaction type="Supports"
628      * @ejb.interface-method view-type="both"
629      */

630     public int getPublisherId(Admin admin, String JavaDoc name) {
631         int returnval = 0;
632
633         try {
634             Integer JavaDoc id = (publisherhome.findByName(name)).getId();
635             returnval = id.intValue();
636         } catch (FinderException JavaDoc e) {
637         }
638
639         return returnval;
640     } // getPublisherId
641

642     /**
643      * Returns a publishers name given its id.
644      *
645      * @return the name or null if id doesnt exists
646      * @throws EJBException if a communication or other error occurs.
647      * @ejb.transaction type="Supports"
648      * @ejb.interface-method view-type="both"
649      */

650     public String JavaDoc getPublisherName(Admin admin, int id) {
651         debug(">getPublisherName(id: " + id + ")");
652         String JavaDoc returnval = null;
653         PublisherDataLocal htp = null;
654         try {
655             htp = publisherhome.findByPrimaryKey(new Integer JavaDoc(id));
656             if (htp != null) {
657                 returnval = htp.getName();
658             }
659         } catch (FinderException JavaDoc e) {
660         }
661
662         debug("<getPublisherName()");
663         return returnval;
664     } // getPublisherName
665

666
667     private Integer JavaDoc findFreePublisherId() {
668         Random JavaDoc ran = (new Random JavaDoc((new Date JavaDoc()).getTime()));
669         int id = ran.nextInt();
670         boolean foundfree = false;
671
672         while (!foundfree) {
673             try {
674                 if (id > 1)
675                     publisherhome.findByPrimaryKey(new Integer JavaDoc(id));
676                 id = ran.nextInt();
677             } catch (FinderException JavaDoc e) {
678                 foundfree = true;
679             }
680         }
681         return new Integer JavaDoc(id);
682     } // findFreePublisherId
683

684
685 } // LocalPublisherSessionBean
686
Popular Tags