KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > hardtoken > LocalHardTokenSessionBean


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.hardtoken;
15
16 import java.math.BigInteger JavaDoc;
17 import java.security.cert.Certificate JavaDoc;
18 import java.security.cert.X509Certificate JavaDoc;
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Random JavaDoc;
30 import java.util.TreeMap JavaDoc;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.FinderException JavaDoc;
35
36 import org.ejbca.core.ejb.BaseSessionBean;
37 import org.ejbca.core.ejb.JNDINames;
38 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal;
39 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome;
40 import org.ejbca.core.ejb.ca.store.CertificateDataBean;
41 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocal;
42 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocalHome;
43 import org.ejbca.core.ejb.log.ILogSessionLocal;
44 import org.ejbca.core.ejb.log.ILogSessionLocalHome;
45 import org.ejbca.core.model.InternalResources;
46 import org.ejbca.core.model.SecConst;
47 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
48 import org.ejbca.core.model.hardtoken.HardTokenData;
49 import org.ejbca.core.model.hardtoken.HardTokenDoesntExistsException;
50 import org.ejbca.core.model.hardtoken.HardTokenExistsException;
51 import org.ejbca.core.model.hardtoken.HardTokenIssuer;
52 import org.ejbca.core.model.hardtoken.HardTokenIssuerData;
53 import org.ejbca.core.model.hardtoken.HardTokenProfileExistsException;
54 import org.ejbca.core.model.hardtoken.UnavailableTokenException;
55 import org.ejbca.core.model.hardtoken.profiles.EIDProfile;
56 import org.ejbca.core.model.hardtoken.profiles.HardTokenProfile;
57 import org.ejbca.core.model.hardtoken.types.HardToken;
58 import org.ejbca.core.model.log.Admin;
59 import org.ejbca.core.model.log.LogConstants;
60 import org.ejbca.core.model.log.LogEntry;
61 import org.ejbca.core.model.ra.UserAdminConstants;
62 import org.ejbca.core.model.ra.UserDataVO;
63 import org.ejbca.util.CertTools;
64 import org.ejbca.util.JDBCUtil;
65
66
67
68 /**
69  * Stores data used by web server clients.
70  * Uses JNDI name for datasource as defined in env 'Datasource' in ejb-jar.xml.
71  *
72  * @ejb.bean
73  * description="Session bean handling hard token data, both about hard tokens and hard token issuers."
74  * display-name="HardTokenSessionSB"
75  * name="HardTokenSession"
76  * jndi-name="HardTokenSession"
77  * local-jndi-name="HardTokenSessionLocal"
78  * view-type="both"
79  * type="Stateless"
80  * transaction-type="Container"
81  *
82  * @ejb.transaction type="Supports"
83  *
84  * @weblogic.enable-call-by-reference True
85  *
86  * @ejb.env-entry
87  * description="The JDBC datasource to be used"
88  * name="DataSource"
89  * type="java.lang.String"
90  * value="${datasource.jndi-name-prefix}${datasource.jndi-name}"
91  *
92  * @ejb.home
93  * extends="javax.ejb.EJBHome"
94  * local-extends="javax.ejb.EJBLocalHome"
95  * local-class="org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocalHome"
96  * remote-class="org.ejbca.core.ejb.hardtoken.IHardTokenSessionHome"
97  *
98  * @ejb.interface
99  * extends="javax.ejb.EJBObject"
100  * local-extends="javax.ejb.EJBLocalObject"
101  * local-class="org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocal"
102  * remote-class="org.ejbca.core.ejb.hardtoken.IHardTokenSessionRemote"
103  *
104  * @ejb.ejb-external-ref
105  * description="The hard token profile data entity bean"
106  * view-type="local"
107  * ref-name="ejb/HardTokenProfileDataLocal"
108  * type="Entity"
109  * home="org.ejbca.core.ejb.hardtoken.HardTokenProfileDataLocalHome"
110  * business="org.ejbca.core.ejb.hardtoken.HardTokenProfileDataLocal"
111  * link="HardTokenProfileData"
112  *
113  * @ejb.ejb-external-ref
114  * description="The hard token issuers data entity bean"
115  * view-type="local"
116  * ref-name="ejb/HardTokenIssuerDataLocal"
117  * type="Entity"
118  * home="org.ejbca.core.ejb.hardtoken.HardTokenIssuerDataLocalHome"
119  * business="org.ejbca.core.ejb.hardtoken.HardTokenIssuerDataLocal"
120  * link="HardTokenIssuerData"
121  *
122  * @ejb.ejb-external-ref
123  * description="The hard token data entity bean"
124  * view-type="local"
125  * ref-name="ejb/HardTokenDataLocal"
126  * type="Entity"
127  * home="org.ejbca.core.ejb.hardtoken.HardTokenDataLocalHome"
128  * business="org.ejbca.core.ejb.hardtoken.HardTokenDataLocal"
129  * link="HardTokenData"
130  *
131  * @ejb.ejb-external-ref
132  * description="The hard token property data entity bean"
133  * view-type="local"
134  * ref-name="ejb/HardTokenPropertyDataLocal"
135  * type="Entity"
136  * home="org.ejbca.core.ejb.hardtoken.HardTokenPropertyLocalHome"
137  * business="org.ejbca.core.ejb.hardtoken.HardTokenPropertyLocal"
138  * link="HardTokenPropertyData"
139  *
140  * @ejb.ejb-external-ref
141  * description="The hard token to certificate map data entity bean"
142  * view-type="local"
143  * ref-name="ejb/HardTokenCertificateMapLocal"
144  * type="Entity"
145  * home="org.ejbca.core.ejb.hardtoken.HardTokenCertificateMapLocalHome"
146  * business="org.ejbca.core.ejb.hardtoken.HardTokenCertificateMapLocal"
147  * link="HardTokenCertificateMap"
148  *
149  * @ejb.ejb-external-ref
150  * description="The Authorization session bean"
151  * view-type="local"
152  * ref-name="ejb/AuthorizationSessionLocal"
153  * type="Session"
154  * home="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome"
155  * business="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal"
156  * link="AuthorizationSession"
157  *
158  * @ejb.ejb-external-ref
159  * description="The Certificate Store session bean"
160  * view-type="local"
161  * ref-name="ejb/CertificateStoreSessionLocal"
162  * type="Session"
163  * home="org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocalHome"
164  * business="org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocal"
165  * link="CertificateStoreSession"
166  *
167  * @ejb.ejb-external-ref
168  * description="The log session bean"
169  * view-type="local"
170  * ref-name="ejb/LogSessionLocal"
171  * type="Session"
172  * home="org.ejbca.core.ejb.log.ILogSessionLocalHome"
173  * business="org.ejbca.core.ejb.log.ILogSessionLocal"
174  * link="LogSession"
175  *
176  * @jonas.bean
177  * ejb-name="HardTokenSession"
178  *
179  */

180 public class LocalHardTokenSessionBean extends BaseSessionBean {
181
182     public static final int NO_ISSUER = 0;
183
184     /** Internal localization of logs and errors */
185     private static final InternalResources intres = InternalResources.getInstance();
186     
187     /** The local home interface of hard token issuer entity bean. */
188     private HardTokenIssuerDataLocalHome hardtokenissuerhome = null;
189
190     /** The local home interface of hard token entity bean. */
191     private HardTokenDataLocalHome hardtokendatahome = null;
192
193     /** The local home interface of hard token entity bean. */
194     private HardTokenProfileDataLocalHome hardtokenprofilehome = null;
195
196     /** The local home interface of hard token certificate map entity bean. */
197     private HardTokenCertificateMapLocalHome hardtokencertificatemaphome = null;
198
199     /** The local home interface of hard token property entity bean. */
200     private HardTokenPropertyLocalHome hardtokenpropertyhome = null;
201
202     /** The local interface of authorization session bean */
203     private IAuthorizationSessionLocal authorizationsession = null;
204
205     /** The local interface of certificate store session bean */
206     private ICertificateStoreSessionLocal certificatestoresession = null;
207
208     /** The remote interface of log session bean */
209     private ILogSessionLocal logsession = null;
210
211
212
213
214      /**
215      * Default create for SessionBean without any creation Arguments.
216      * @throws CreateException if bean instance can't be created
217      */

218
219
220     public void ejbCreate() throws CreateException JavaDoc {
221       try{
222         hardtokenissuerhome = (HardTokenIssuerDataLocalHome) getLocator().getLocalHome(HardTokenIssuerDataLocalHome.COMP_NAME);
223         hardtokendatahome = (HardTokenDataLocalHome) getLocator().getLocalHome(HardTokenDataLocalHome.COMP_NAME);
224         hardtokencertificatemaphome = (HardTokenCertificateMapLocalHome) getLocator().getLocalHome(HardTokenCertificateMapLocalHome.COMP_NAME);
225         hardtokenprofilehome = (HardTokenProfileDataLocalHome) getLocator().getLocalHome(HardTokenProfileDataLocalHome.COMP_NAME);
226         hardtokenpropertyhome = (HardTokenPropertyLocalHome) getLocator().getLocalHome(HardTokenPropertyLocalHome.COMP_NAME);
227       }catch(Exception JavaDoc e){
228          throw new EJBException JavaDoc(e);
229       }
230     }
231
232
233     /** Gets connection to log session bean
234      * @return Connection
235      */

236     private ILogSessionLocal getLogSession() {
237         if(logsession == null){
238           try{
239             ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME);
240             logsession = logsessionhome.create();
241           }catch(Exception JavaDoc e){
242              throw new EJBException JavaDoc(e);
243           }
244         }
245         return logsession;
246     } //getLogSession
247

248     /** Gets connection to certificate store session bean
249      * @return Connection
250      */

251     private ICertificateStoreSessionLocal getCertificateStoreSession() {
252         if(certificatestoresession == null){
253           try{
254             ICertificateStoreSessionLocalHome certificatestoresessionhome = (ICertificateStoreSessionLocalHome) getLocator().getLocalHome(ICertificateStoreSessionLocalHome.COMP_NAME);
255             certificatestoresession = certificatestoresessionhome.create();
256           }catch(Exception JavaDoc e){
257              throw new EJBException JavaDoc(e);
258           }
259         }
260         return certificatestoresession;
261     } //getCertificateStoreSession
262

263     /** Gets connection to authorization session bean
264      * @return IAuthorizationSessionLocal
265      */

266     private IAuthorizationSessionLocal getAuthorizationSession() {
267         if(authorizationsession == null){
268           try{
269             IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME);
270             authorizationsession = authorizationsessionhome.create();
271           }catch(Exception JavaDoc e){
272              throw new EJBException JavaDoc(e);
273           }
274         }
275         return authorizationsession;
276     } //getAuthorizationSession
277

278
279
280
281
282     /**
283      * Adds a hard token profile to the database.
284      *
285      * @throws HardTokenProfileExistsException if hard token already exists.
286      * @throws EJBException if a communication or other error occurs.
287      * @ejb.interface-method view-type="both"
288      */

289     public void addHardTokenProfile(Admin admin, String JavaDoc name, HardTokenProfile profile) throws HardTokenProfileExistsException{
290        debug(">addHardTokenProfile(name: " + name + ")");
291        addHardTokenProfile(admin,findFreeHardTokenProfileId().intValue(),name,profile);
292        debug("<addHardTokenProfile()");
293     } // addHardTokenProfile
294

295
296     /**
297      * Adds a hard token profile to the database.
298      * Used for importing and exporting profiles from xml-files.
299      *
300      * @throws HardTokenProfileExistsException if hard token already exists.
301      * @throws EJBException if a communication or other error occurs.
302      * @ejb.interface-method view-type="both"
303      * @ejb.transaction type="Required"
304      */

305     public void addHardTokenProfile(Admin admin, int profileid, String JavaDoc name, HardTokenProfile profile) throws HardTokenProfileExistsException{
306         debug(">addHardTokenProfile(name: " + name + ", id: " + profileid +")");
307         boolean success=false;
308         try{
309             hardtokenprofilehome.findByName(name);
310         }catch(FinderException JavaDoc e){
311             try{
312                 hardtokenprofilehome.findByPrimaryKey(new Integer JavaDoc(profileid));
313             }catch(FinderException JavaDoc f){
314                 try{
315                     hardtokenprofilehome.create(new Integer JavaDoc(profileid), name, profile);
316                     success = true;
317                 }catch(CreateException JavaDoc g){
318                     error("Unexpected error creating new hard token profile: ", g);
319                 }
320             }
321         }
322         
323         if(success) {
324             String JavaDoc msg = intres.getLocalizedMessage("hardtoken.addedprofile", name);
325             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENPROFILEDATA, msg);
326         } else {
327             String JavaDoc msg = intres.getLocalizedMessage("hardtoken.erroraddprofile", name);
328             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENPROFILEDATA, msg);
329         }
330         
331         if(!success)
332             throw new HardTokenProfileExistsException();
333         debug("<addHardTokenProfile()");
334     } // addHardTokenProfile
335

336     /**
337      * Updates hard token profile data
338      *
339      * @throws EJBException if a communication or other error occurs.
340      * @ejb.interface-method view-type="both"
341      * @ejb.transaction type="Required"
342      */

343     public void changeHardTokenProfile(Admin admin, String JavaDoc name, HardTokenProfile profile){
344        debug(">changeHardTokenProfile(name: " + name + ")");
345        boolean success = false;
346        try{
347          HardTokenProfileDataLocal htp = hardtokenprofilehome.findByName(name);
348          htp.setHardTokenProfile(profile);
349          success = true;
350        }catch(FinderException JavaDoc e){}
351
352        if(success) {
353            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.editedprofile", name);
354            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENPROFILEDATA, msg);
355        } else {
356            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.erroreditprofile", name);
357            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENPROFILEDATA, msg);
358        }
359
360        debug("<changeHardTokenProfile()");
361     } // changeHardTokenProfile
362

363      /**
364      * Adds a hard token profile with the same content as the original profile,
365      *
366      * @throws HardTokenProfileExistsException if hard token already exists.
367      * @throws EJBException if a communication or other error occurs.
368       * @ejb.interface-method view-type="both"
369       * @ejb.transaction type="Required"
370      */

371     public void cloneHardTokenProfile(Admin admin, String JavaDoc oldname, String JavaDoc newname) throws HardTokenProfileExistsException{
372        debug(">cloneHardTokenProfile(name: " + oldname + ")");
373        HardTokenProfile profiledata = null;
374        try{
375          HardTokenProfileDataLocal htp = hardtokenprofilehome.findByName(oldname);
376          profiledata = (HardTokenProfile) htp.getHardTokenProfile().clone();
377
378          try{
379              addHardTokenProfile(admin, newname, profiledata);
380              String JavaDoc msg = intres.getLocalizedMessage("hardtoken.clonedprofile", newname, oldname);
381              getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENPROFILEDATA, msg);
382          }catch(HardTokenProfileExistsException f){
383              String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorcloneprofile", newname, oldname);
384              getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENPROFILEDATA, msg);
385              throw f;
386          }
387
388        }catch(Exception JavaDoc e){
389           throw new EJBException JavaDoc(e);
390        }
391
392        debug("<cloneHardTokenProfile()");
393     } // cloneHardTokenProfile
394

395      /**
396      * Removes a hard token profile from the database.
397      *
398      * @throws EJBException if a communication or other error occurs.
399       * @ejb.interface-method view-type="both"
400       * @ejb.transaction type="Required"
401      */

402     public void removeHardTokenProfile(Admin admin, String JavaDoc name){
403       debug(">removeHardTokenProfile(name: " + name + ")");
404       try{
405         HardTokenProfileDataLocal htp = hardtokenprofilehome.findByName(name);
406         htp.remove();
407         String JavaDoc msg = intres.getLocalizedMessage("hardtoken.removedprofile", name);
408         getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENPROFILEDATA,msg);
409       }catch(Exception JavaDoc e){
410           String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorremoveprofile", name);
411           getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENPROFILEDATA,msg,e);
412       }
413       debug("<removeHardTokenProfile()");
414     } // removeHardTokenProfile
415

416      /**
417      * Renames a hard token profile
418      *
419      * @throws HardTokenProfileExistsException if hard token already exists.
420      * @throws EJBException if a communication or other error occurs.
421       * @ejb.interface-method view-type="both"
422       * @ejb.transaction type="Required"
423      */

424     public void renameHardTokenProfile(Admin admin, String JavaDoc oldname, String JavaDoc newname) throws HardTokenProfileExistsException{
425        debug(">renameHardTokenProfile(from " + oldname + " to " + newname + ")");
426        boolean success = false;
427        try{
428           hardtokenprofilehome.findByName(newname);
429        }catch(FinderException JavaDoc e){
430           try{
431              HardTokenProfileDataLocal htp = hardtokenprofilehome.findByName(oldname);
432              htp.setName(newname);
433              success = true;
434           }catch(FinderException JavaDoc g){}
435        }
436
437        if(success) {
438            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.renamedprofile", oldname, newname);
439            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENPROFILEDATA,msg);
440        } else {
441            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorrenameprofile", oldname, newname);
442            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENPROFILEDATA, msg);
443        }
444
445        if(!success)
446          throw new HardTokenProfileExistsException();
447        debug("<renameHardTokenProfile()");
448     } // renameHardTokenProfile
449

450     /**
451      * Retrives a Collection of id:s (Integer) to authorized profiles.
452      *
453      * Authorized hard token profiles are profiles containing only authorized certificate profiles and caids.
454      *
455      * @return Collection of id:s (Integer)
456      * @ejb.interface-method view-type="both"
457      */

458     public Collection JavaDoc getAuthorizedHardTokenProfileIds(Admin admin){
459       ArrayList JavaDoc returnval = new ArrayList JavaDoc();
460       Collection JavaDoc result = null;
461
462       HashSet JavaDoc authorizedcertprofiles = new HashSet JavaDoc(getCertificateStoreSession().getAuthorizedCertificateProfileIds(admin, CertificateDataBean.CERTTYPE_HARDTOKEN));
463       HashSet JavaDoc authorizedcaids = new HashSet JavaDoc(this.getAuthorizationSession().getAuthorizedCAIds(admin));
464       
465       try{
466         result = this.hardtokenprofilehome.findAll();
467         Iterator JavaDoc i = result.iterator();
468         while(i.hasNext()){
469           HardTokenProfileDataLocal next = (HardTokenProfileDataLocal) i.next();
470           HardTokenProfile profile = next.getHardTokenProfile();
471
472           if(profile instanceof EIDProfile){
473             if(authorizedcertprofiles.containsAll(((EIDProfile) profile).getAllCertificateProfileIds()) &&
474                authorizedcaids.containsAll(((EIDProfile) profile).getAllCAIds())){
475               returnval.add(next.getId());
476             }
477           }else{
478             //Implement for other profile types
479
}
480         }
481       }catch(FinderException JavaDoc e){}
482
483
484
485       return returnval;
486     } // getAuthorizedHardTokenProfileIds
487

488     /**
489      * Method creating a hashmap mapping profile id (Integer) to profile name (String).
490      * @ejb.interface-method view-type="both"
491      */

492     public HashMap JavaDoc getHardTokenProfileIdToNameMap(Admin admin){
493       HashMap JavaDoc returnval = new HashMap JavaDoc();
494       Collection JavaDoc result = null;
495
496       try{
497         result = hardtokenprofilehome.findAll();
498         Iterator JavaDoc i = result.iterator();
499         while(i.hasNext()){
500           HardTokenProfileDataLocal next = (HardTokenProfileDataLocal) i.next();
501           returnval.put(next.getId(),next.getName());
502         }
503       }catch(FinderException JavaDoc e){}
504       return returnval;
505     } // getHardTokenProfileIdToNameMap
506

507
508     /**
509      * Retrives a named hard token profile.
510      * @ejb.interface-method view-type="both"
511      */

512     public HardTokenProfile getHardTokenProfile(Admin admin, String JavaDoc name){
513       HardTokenProfile returnval=null;
514
515        try{
516          returnval = (hardtokenprofilehome.findByName(name)).getHardTokenProfile();
517        } catch(FinderException JavaDoc e){
518            // return null if we cant find it
519
}
520        return returnval;
521     } // getCertificateProfile
522

523      /**
524       * Finds a hard token profile by id.
525       * @ejb.interface-method view-type="both"
526       */

527     public HardTokenProfile getHardTokenProfile(Admin admin, int id){
528        HardTokenProfile returnval=null;
529
530        try{
531            returnval = (hardtokenprofilehome.findByPrimaryKey(new Integer JavaDoc(id))).getHardTokenProfile();
532        } catch(FinderException JavaDoc e){
533              // return null if we cant find it
534
}
535        return returnval;
536     } // getHardTokenProfile
537

538     /**
539      * Help method used by hard token profile proxys to indicate if it is time to
540      * update it's profile data.
541      * @ejb.interface-method view-type="both"
542      */

543     public int getHardTokenProfileUpdateCount(Admin admin, int hardtokenprofileid){
544       int returnval = 0;
545
546       try{
547         returnval = (hardtokenprofilehome.findByPrimaryKey(new Integer JavaDoc(hardtokenprofileid))).getUpdateCounter();
548       }catch(FinderException JavaDoc e){}
549
550       return returnval;
551     }
552
553
554      /**
555      * Returns a hard token profile id, given it's hard token profile name
556      *
557      *
558      * @return the id or 0 if hardtokenprofile cannot be found.
559       * @ejb.interface-method view-type="both"
560      */

561     public int getHardTokenProfileId(Admin admin, String JavaDoc name){
562       int returnval = 0;
563
564       try{
565         Integer JavaDoc id = (hardtokenprofilehome.findByName(name)).getId();
566         returnval = id.intValue();
567       }catch(FinderException JavaDoc e){}
568
569       return returnval;
570     } // getHardTokenProfileId
571

572      /**
573       * Returns a hard token profile name given its id.
574       *
575       * @return the name or null if id noesnt exists
576       * @throws EJBException if a communication or other error occurs.
577       * @ejb.interface-method view-type="both"
578       */

579     public String JavaDoc getHardTokenProfileName(Admin admin, int id){
580       debug(">getHardTokenProfileName(id: " + id + ")");
581       String JavaDoc returnval = null;
582       HardTokenProfileDataLocal htp = null;
583       try{
584         htp = hardtokenprofilehome.findByPrimaryKey(new Integer JavaDoc(id));
585         if(htp != null){
586           returnval = htp.getName();
587         }
588       }catch(FinderException JavaDoc e){}
589
590       debug("<getHardTokenProfileName()");
591       return returnval;
592     } // getHardTokenProfileName
593

594
595     /**
596      * Adds a hard token issuer to the database.
597      *
598      * @return false if hard token issuer already exists.
599      * @throws EJBException if a communication or other error occurs.
600      * @ejb.interface-method view-type="both"
601      * @ejb.transaction type="Required"
602      */

603
604     public boolean addHardTokenIssuer(Admin admin, String JavaDoc alias, int admingroupid, HardTokenIssuer issuerdata){
605        debug(">addHardTokenIssuer(alias: " + alias + ")");
606        boolean returnval=false;
607        try{
608           hardtokenissuerhome.findByAlias(alias);
609        }catch(FinderException JavaDoc e){
610          try{
611            hardtokenissuerhome.create(findFreeHardTokenIssuerId(), alias, admingroupid, issuerdata);
612            returnval = true;
613          }catch(CreateException JavaDoc g){}
614        }
615
616        if(returnval) {
617            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.addedissuer", alias);
618            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENISSUERDATA,msg);
619        } else {
620            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.erroraddissuer", alias);
621            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENISSUERDATA,msg);
622        }
623
624        debug("<addHardTokenIssuer()");
625        return returnval;
626     } // addHardTokenIssuer
627

628     /**
629      * Updates hard token issuer data
630      *
631      * @return false if alias doesn't exists
632      * @throws EJBException if a communication or other error occurs.
633      * @ejb.interface-method view-type="both"
634      * @ejb.transaction type="Required"
635      */

636
637     public boolean changeHardTokenIssuer(Admin admin, String JavaDoc alias, HardTokenIssuer issuerdata){
638        debug(">changeHardTokenIssuer(alias: " + alias + ")");
639        boolean returnvalue = false;
640        try{
641          HardTokenIssuerDataLocal htih = hardtokenissuerhome.findByAlias(alias);
642          htih.setHardTokenIssuer(issuerdata);
643          returnvalue = true;
644        }catch(FinderException JavaDoc e){}
645
646        if(returnvalue) {
647            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.editedissuer", alias);
648            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENISSUERDATA,msg);
649        } else {
650            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.erroreditissuer", alias);
651            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENISSUERDATA,msg);
652        }
653
654        debug("<changeHardTokenIssuer()");
655        return returnvalue;
656     } // changeHardTokenIssuer
657

658      /**
659      * Adds a hard token issuer with the same content as the original issuer,
660      *
661      * @return false if the new alias or certificatesn already exists.
662      * @throws EJBException if a communication or other error occurs.
663       * @ejb.interface-method view-type="both"
664       * @ejb.transaction type="Required"
665      */

666     public boolean cloneHardTokenIssuer(Admin admin, String JavaDoc oldalias, String JavaDoc newalias, int admingroupid){
667        debug(">cloneHardTokenIssuer(alias: " + oldalias + ")");
668        HardTokenIssuer issuerdata = null;
669        boolean returnval = false;
670        try{
671          HardTokenIssuerDataLocal htih = hardtokenissuerhome.findByAlias(oldalias);
672          issuerdata = (HardTokenIssuer) htih.getHardTokenIssuer().clone();
673
674          returnval = addHardTokenIssuer(admin, newalias, admingroupid, issuerdata);
675          if(returnval) {
676              String JavaDoc msg = intres.getLocalizedMessage("hardtoken.clonedissuer", newalias, oldalias);
677              getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENISSUERDATA,msg);
678          } else {
679              String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorcloneissuer", newalias, oldalias);
680              getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENISSUERDATA,msg);
681          }
682        }catch(Exception JavaDoc e){
683           throw new EJBException JavaDoc(e);
684        }
685
686        debug("<cloneHardTokenIssuer()");
687        return returnval;
688     } // cloneHardTokenIssuer
689

690      /**
691      * Removes a hard token issuer from the database.
692      *
693      * @throws EJBException if a communication or other error occurs.
694       * @ejb.interface-method view-type="both"
695       * @ejb.transaction type="Required"
696      */

697     public void removeHardTokenIssuer(Admin admin, String JavaDoc alias){
698       debug(">removeHardTokenIssuer(alias: " + alias + ")");
699       try{
700           HardTokenIssuerDataLocal htih = hardtokenissuerhome.findByAlias(alias);
701           htih.remove();
702           String JavaDoc msg = intres.getLocalizedMessage("hardtoken.removedissuer", alias);
703           getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENISSUERDATA,msg);
704       }catch(Exception JavaDoc e){
705           String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorremoveissuer", alias);
706           getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENISSUERDATA,msg,e);
707       }
708       debug("<removeHardTokenIssuer()");
709     } // removeHardTokenIssuer
710

711      /**
712      * Renames a hard token issuer
713      *
714      * @return false if new alias or certificatesn already exists
715      * @throws EJBException if a communication or other error occurs.
716       * @ejb.interface-method view-type="both"
717       * @ejb.transaction type="Required"
718      */

719     public boolean renameHardTokenIssuer(Admin admin, String JavaDoc oldalias, String JavaDoc newalias,
720                                          int newadmingroupid){
721        debug(">renameHardTokenIssuer(from " + oldalias + " to " + newalias + ")");
722        boolean returnvalue = false;
723        try{
724           hardtokenissuerhome.findByAlias(newalias);
725        }catch(FinderException JavaDoc e){
726            try{
727              HardTokenIssuerDataLocal htih = hardtokenissuerhome.findByAlias(oldalias);
728              htih.setAlias(newalias);
729              htih.setAdminGroupId(newadmingroupid);
730              returnvalue = true;
731            }catch(FinderException JavaDoc g){}
732        }
733
734        if(returnvalue) {
735            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.renameissuer", oldalias, newalias);
736            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENISSUERDATA,msg );
737        } else {
738            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorrenameissuer", oldalias, newalias);
739            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENISSUERDATA,msg);
740        }
741
742        debug("<renameHardTokenIssuer()");
743        return returnvalue;
744     } // renameHardTokenIssuer
745

746     /**
747      * Method to check if an administrator is authorized to issue hard tokens for
748      * the given alias.
749      *
750      * @param admin administrator to check
751      * @param alias alias of hardtoken issuer.
752      * @return true if administrator is authorized to issue hardtoken with given alias.
753      * @ejb.interface-method view-type="both"
754      */

755     public boolean getAuthorizedToHardTokenIssuer(Admin admin, String JavaDoc alias){
756       debug(">getAuthorizedToHardTokenIssuer(" + alias + ")");
757       boolean returnval = false;
758         try{
759
760           int admingroupid = hardtokenissuerhome.findByAlias(alias).getAdminGroupId();
761           returnval = getAuthorizationSession().isAuthorizedNoLog(admin, "/hardtoken_functionality/issue_hardtokens");
762           returnval = returnval && authorizationsession.existsAdministratorInGroup(admin, admingroupid);
763         }catch(FinderException JavaDoc fe){}
764           catch(AuthorizationDeniedException ade){}
765           debug("<getAuthorizedToHardTokenIssuer(" + returnval + ")");
766       return returnval;
767     }
768
769       /**
770        * Returns the available hard token issuers authorized to the administrator.
771        *
772        * @return A collection of available HardTokenIssuerData.
773        * @throws EJBException if a communication or other error occurs.
774        * @ejb.interface-method view-type="both"
775        */

776     public Collection JavaDoc getHardTokenIssuerDatas(Admin admin){
777       debug(">getHardTokenIssuerDatas()");
778       ArrayList JavaDoc returnval = new ArrayList JavaDoc();
779       Collection JavaDoc result = null;
780       HardTokenIssuerDataLocal htih = null;
781       Collection JavaDoc authorizedhardtokenprofiles = this.getAuthorizedHardTokenProfileIds(admin);
782       try{
783         result = hardtokenissuerhome.findAll();
784         if(result.size()>0){
785           Iterator JavaDoc i = result.iterator();
786           while(i.hasNext()){
787             htih = (HardTokenIssuerDataLocal) i.next();
788             if(authorizedhardtokenprofiles.containsAll(htih.getHardTokenIssuer().getAvailableHardTokenProfiles()))
789               returnval.add(new HardTokenIssuerData(htih.getId().intValue(), htih.getAlias(), htih.getAdminGroupId(), htih.getHardTokenIssuer()));
790           }
791         }
792         Collections.sort(returnval);
793       }catch(FinderException JavaDoc e){}
794
795       debug("<getHardTokenIssuerDatas()");
796       return returnval;
797     } // getHardTokenIssuers
798

799       /**
800        * Returns the available hard token issuer alliases authorized to the administrator.
801        *
802        * @return A collection of available hard token issuer aliases.
803        * @throws EJBException if a communication or other error occurs.
804        * @ejb.interface-method view-type="both"
805        */

806     public Collection JavaDoc getHardTokenIssuerAliases(Admin admin){
807       debug(">getHardTokenIssuerAliases()");
808       ArrayList JavaDoc returnval = new ArrayList JavaDoc();
809       Collection JavaDoc result = null;
810       Collection JavaDoc authorizedhardtokenprofiles = this.getAuthorizedHardTokenProfileIds(admin);
811       HardTokenIssuerDataLocal htih = null;
812       try{
813         result = hardtokenissuerhome.findAll();
814         if(result.size()>0){
815           Iterator JavaDoc i = result.iterator();
816           while(i.hasNext()){
817             htih = (HardTokenIssuerDataLocal) i.next();
818             if(authorizedhardtokenprofiles.containsAll(htih.getHardTokenIssuer().getAvailableHardTokenProfiles()))
819               returnval.add(htih.getAlias());
820           }
821         }
822         Collections.sort(returnval);
823       }catch(FinderException JavaDoc e){}
824
825       debug("<getHardTokenIssuerAliases()");
826       return returnval;
827     }// getHardTokenIssuerAliases
828

829       /**
830        * Returns the available hard token issuers authorized to the administrator.
831        *
832        * @return A treemap of available hard token issuers.
833        * @throws EJBException if a communication or other error occurs.
834        * @ejb.interface-method view-type="both"
835        */

836     public TreeMap JavaDoc getHardTokenIssuers(Admin admin){
837       debug(">getHardTokenIssuers()");
838       Collection JavaDoc authorizedhardtokenprofiles = this.getAuthorizedHardTokenProfileIds(admin);
839       TreeMap JavaDoc returnval = new TreeMap JavaDoc();
840       Collection JavaDoc result = null;
841       try{
842         result = hardtokenissuerhome.findAll();
843         if(result.size()>0){
844           Iterator JavaDoc i = result.iterator();
845           while(i.hasNext()){
846             HardTokenIssuerDataLocal htih = (HardTokenIssuerDataLocal) i.next();
847             if(authorizedhardtokenprofiles.containsAll(htih.getHardTokenIssuer().getAvailableHardTokenProfiles()))
848               returnval.put(htih.getAlias(), new HardTokenIssuerData(htih.getId().intValue(), htih.getAlias(), htih.getAdminGroupId(), htih.getHardTokenIssuer()));
849           }
850         }
851       }catch(FinderException JavaDoc e){}
852
853       debug("<getHardTokenIssuers()");
854       return returnval;
855     } // getHardTokenIssuers
856

857       /**
858        * Returns the specified hard token issuer.
859        *
860        * @return the hard token issuer data or null if hard token issuer doesn't exists.
861        * @throws EJBException if a communication or other error occurs.
862        * @ejb.interface-method view-type="both"
863        */

864     public HardTokenIssuerData getHardTokenIssuerData(Admin admin, String JavaDoc alias){
865       debug(">getHardTokenIssuerData(alias: " + alias + ")");
866       HardTokenIssuerData returnval = null;
867       HardTokenIssuerDataLocal htih = null;
868       try{
869         htih = hardtokenissuerhome.findByAlias(alias);
870         if(htih != null){
871           returnval = new HardTokenIssuerData(htih.getId().intValue(), htih.getAlias(), htih.getAdminGroupId(), htih.getHardTokenIssuer());
872         }
873       }catch(FinderException JavaDoc e){}
874
875       debug("<getHardTokenIssuerData()");
876       return returnval;
877     } // getHardTokenIssuerData
878

879        /**
880        * Returns the specified hard token issuer.
881        *
882        * @return the hard token issuer data or null if hard token issuer doesn't exists.
883        * @throws EJBException if a communication or other error occurs.
884         * @ejb.interface-method view-type="both"
885        */

886     public HardTokenIssuerData getHardTokenIssuerData(Admin admin, int id){
887       debug(">getHardTokenIssuerData(id: " + id +")" );
888       HardTokenIssuerData returnval = null;
889       HardTokenIssuerDataLocal htih = null;
890       try{
891         htih = hardtokenissuerhome.findByPrimaryKey(new Integer JavaDoc(id));
892         if(htih != null){
893           returnval = new HardTokenIssuerData(htih.getId().intValue(), htih.getAlias(), htih.getAdminGroupId(), htih.getHardTokenIssuer());
894         }
895       }catch(FinderException JavaDoc e){}
896
897       debug("<getHardTokenIssuerData()");
898       return returnval;
899     } // getHardTokenIssuerData
900

901
902       /**
903        * Returns the number of available hard token issuer.
904        *
905        * @return the number of available hard token issuer.
906        * @throws EJBException if a communication or other error occurs.
907        * @ejb.interface-method view-type="both"
908        */

909     public int getNumberOfHardTokenIssuers(Admin admin){
910       debug(">getNumberOfHardTokenIssuers()");
911       int returnval =0;
912       try{
913         returnval = (hardtokenissuerhome.findAll()).size();
914       }catch(FinderException JavaDoc e){}
915
916       debug("<getNumberOfHardTokenIssuers()");
917       return returnval;
918     } // getNumberOfHardTokenIssuers
919

920       /**
921        * Returns a hard token issuer id given its alias.
922        *
923        * @return id number of hard token issuer.
924        * @throws EJBException if a communication or other error occurs.
925        * @ejb.interface-method view-type="both"
926        */

927     public int getHardTokenIssuerId(Admin admin, String JavaDoc alias){
928       debug(">getHardTokenIssuerId(alias: " + alias + ")");
929       int returnval = NO_ISSUER;
930       HardTokenIssuerDataLocal htih = null;
931       try{
932         htih = hardtokenissuerhome.findByAlias(alias);
933         if(htih != null){
934           returnval = htih.getId().intValue();
935         }
936       }catch(FinderException JavaDoc e){}
937
938       debug("<getHardTokenIssuerId()");
939       return returnval;
940     } // getNumberOfHardTokenIssuersId
941

942        /**
943        * Returns a hard token issuer alias given its id.
944        *
945        * @return the alias or null if id noesnt exists
946        * @throws EJBException if a communication or other error occurs.
947         * @ejb.interface-method view-type="both"
948        */

949     public String JavaDoc getHardTokenIssuerAlias(Admin admin, int id){
950       debug(">getHardTokenIssuerAlias(id: " + id + ")");
951       String JavaDoc returnval = null;
952       HardTokenIssuerDataLocal htih = null;
953       try{
954         htih = hardtokenissuerhome.findByPrimaryKey(new Integer JavaDoc(id));
955         if(htih != null){
956           returnval = htih.getAlias();
957         }
958       }catch(FinderException JavaDoc e){}
959
960       debug("<getHardTokenIssuerAlias()");
961       return returnval;
962     } // getHardTokenIssuerAlias
963

964         /**
965        * Checks if a hard token profile is among a hard tokens issuers available token types.
966        *
967        * @param admin the administrator calling the function
968        * @param issuerid the id of the issuer to check.
969        * @param userdata the data of user about to be generated
970        *
971        * @throws UnavailableTokenException if users tokentype isn't among hard token issuers available tokentypes.
972        * @throws EJBException if a communication or other error occurs.
973          * @ejb.interface-method view-type="both"
974        */

975
976     public void getIsHardTokenProfileAvailableToIssuer(Admin admin, int issuerid, UserDataVO userdata) throws UnavailableTokenException{
977         debug(">getIsTokenTypeAvailableToIssuer(issuerid: " + issuerid + ", tokentype: " + userdata.getTokenType()+ ")");
978         boolean returnval = false;
979         ArrayList JavaDoc availabletokentypes = getHardTokenIssuerData(admin, issuerid).getHardTokenIssuer().getAvailableHardTokenProfiles();
980
981         for(int i=0; i < availabletokentypes.size(); i++){
982           if(((Integer JavaDoc) availabletokentypes.get(i)).intValue() == userdata.getTokenType())
983             returnval = true;
984         }
985
986         if(!returnval) {
987             String JavaDoc msg = intres.getLocalizedMessage("hardtoken.unavailabletoken", userdata.getUsername());
988             throw new UnavailableTokenException(msg);
989         }
990         debug("<getIsTokenTypeAvailableToIssuer()");
991     } // getIsTokenTypeAvailableToIssuer
992

993        /**
994        * Adds a hard token to the database
995        *
996        * @param admin the administrator calling the function
997        * @param tokensn The serialnumber of token.
998        * @param username the user owning the token.
999        * @param significantissuerdn indicates which CA the hard token should belong to.
1000       * @param hardtokendata the hard token data
1001       * @param certificates a collection of certificates places in the hard token
1002       * @param copyof indicates if the newly created token is a copy of an existing token. Use null if token is an original
1003       *
1004       * @throws EJBException if a communication or other error occurs.
1005       * @throws HardTokenExistsException if tokensn already exists in databas.
1006        * @ejb.interface-method view-type="both"
1007        * @ejb.transaction type="Required"
1008       */

1009    public void addHardToken(Admin admin, String JavaDoc tokensn, String JavaDoc username, String JavaDoc significantissuerdn, int tokentype, HardToken hardtokendata, Collection JavaDoc certificates, String JavaDoc copyof) throws HardTokenExistsException{
1010        debug(">addHardToken(tokensn : " + tokensn + ")");
1011        String JavaDoc bcdn = CertTools.stringToBCDNString(significantissuerdn);
1012        boolean exists = false;
1013        try {
1014            // We must actually check if there is one before we try to add it, because wls does not allow us to catch any errors if creating fails, that sux
1015
HardTokenDataLocal data = hardtokendatahome.findByPrimaryKey(tokensn);
1016            if (data != null) {
1017                exists = true;
1018            }
1019        } catch (FinderException JavaDoc e) {
1020            // This is what we hope will happen
1021
}
1022        if (!exists) {
1023            try {
1024                hardtokendatahome.create(tokensn, username,new java.util.Date JavaDoc(), new java.util.Date JavaDoc(), tokentype, bcdn, hardtokendata);
1025                if(certificates != null){
1026                    Iterator JavaDoc i = certificates.iterator();
1027                    while(i.hasNext()){
1028                        addHardTokenCertificateMapping(admin, tokensn, (X509Certificate JavaDoc) i.next());
1029                    }
1030                }
1031                if(copyof != null){
1032                    hardtokenpropertyhome.create(tokensn, HardTokenPropertyEntityBean.PROPERTY_COPYOF,copyof);
1033                }
1034                String JavaDoc msg = intres.getLocalizedMessage("hardtoken.addedtoken", tokensn);
1035                getLogSession().log(admin, bcdn.hashCode(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),username, null, LogEntry.EVENT_INFO_HARDTOKENDATA,msg);
1036            }
1037            catch (Exception JavaDoc e) {
1038                String JavaDoc msg = intres.getLocalizedMessage("hardtoken.tokenexists", tokensn);
1039                getLogSession().log(admin, bcdn.hashCode(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),username, null, LogEntry.EVENT_ERROR_HARDTOKENDATA,msg);
1040                throw new HardTokenExistsException("Tokensn : " + tokensn);
1041            }
1042        } else {
1043            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.tokenexists", tokensn);
1044            getLogSession().log(admin, bcdn.hashCode(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),username, null, LogEntry.EVENT_ERROR_HARDTOKENDATA,msg);
1045            throw new HardTokenExistsException("Tokensn : " + tokensn);
1046        }
1047        debug("<addHardToken()");
1048    } // addHardToken
1049

1050       /**
1051       * changes a hard token data in the database
1052       *
1053       * @param admin the administrator calling the function
1054       * @param tokensn The serialnumber of token.
1055       * @param hardtokendata the hard token data
1056       *
1057       * @throws EJBException if a communication or other error occurs.
1058       * @throws HardTokenDoesntExistsException if tokensn doesn't exists in databas.
1059        * @ejb.interface-method view-type="both"
1060        * @ejb.transaction type="Required"
1061       */

1062    public void changeHardToken(Admin admin, String JavaDoc tokensn, int tokentype, HardToken hardtokendata) throws HardTokenDoesntExistsException{
1063        debug(">changeHardToken(tokensn : " + tokensn + ")");
1064        int caid = LogConstants.INTERNALCAID;
1065        try {
1066            HardTokenDataLocal htd = hardtokendatahome.findByPrimaryKey(tokensn);
1067            htd.setTokenType(tokentype);
1068            htd.setHardToken(hardtokendata);
1069            htd.setModifyTime(new java.util.Date JavaDoc());
1070            caid = htd.getSignificantIssuerDN().hashCode();
1071            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.changedtoken", tokensn);
1072            getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),htd.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKENDATA,msg);
1073        }
1074        catch (Exception JavaDoc e) {
1075            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorchangetoken", tokensn);
1076            getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENDATA,msg);
1077            throw new HardTokenDoesntExistsException("Tokensn : " + tokensn);
1078        }
1079        debug("<changeHardToken()");
1080    } // changeHardToken
1081

1082       /**
1083       * removes a hard token data from the database
1084       *
1085       * @param admin the administrator calling the function
1086       * @param tokensn The serialnumber of token.
1087       *
1088       * @throws EJBException if a communication or other error occurs.
1089       * @throws HardTokenDoesntExistsException if tokensn doesn't exists in databas.
1090        * @ejb.interface-method view-type="both"
1091        * @ejb.transaction type="Required"
1092       */

1093    public void removeHardToken(Admin admin, String JavaDoc tokensn) throws HardTokenDoesntExistsException{
1094      debug(">removeHardToken(tokensn : " + tokensn + ")");
1095      int caid = LogConstants.INTERNALCAID;
1096      try{
1097        HardTokenDataLocal htd = hardtokendatahome.findByPrimaryKey(tokensn);
1098        caid = htd.getSignificantIssuerDN().hashCode();
1099        htd.remove();
1100
1101        // Remove all certificate mappings.
1102
removeHardTokenCertificateMappings(admin, tokensn);
1103
1104
1105        // Remove all copyof references id property database.
1106
try{
1107            hardtokenpropertyhome.findByProperty(tokensn, HardTokenPropertyEntityBean.PROPERTY_COPYOF).remove();
1108        }catch(FinderException JavaDoc fe){}
1109        try{
1110          Collection JavaDoc copieslocal = hardtokenpropertyhome.findIdsByPropertyAndValue(HardTokenPropertyEntityBean.PROPERTY_COPYOF , tokensn);
1111          Iterator JavaDoc iter = copieslocal.iterator();
1112          while(iter.hasNext()){
1113             ((HardTokenPropertyLocal) iter.next()).remove();
1114           }
1115        }catch(FinderException JavaDoc fe){}
1116        String JavaDoc msg = intres.getLocalizedMessage("hardtoken.removedtoken", tokensn);
1117        getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENDATA,msg);
1118      }catch(Exception JavaDoc e){
1119          String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorremovetoken", tokensn);
1120          getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENDATA,msg);
1121          throw new HardTokenDoesntExistsException("Tokensn : " + tokensn);
1122      }
1123      debug("<removeHardToken()");
1124    } // removeHardToken
1125

1126       /**
1127       * Checks if a hard token serialnumber exists in the database
1128       *
1129       * @param admin the administrator calling the function
1130       * @param tokensn The serialnumber of token.
1131       *
1132       * @return true if it exists or false otherwise.
1133       * @throws EJBException if a communication or other error occurs.
1134        * @ejb.interface-method view-type="both"
1135       */

1136    public boolean existsHardToken(Admin admin, String JavaDoc tokensn){
1137       debug(">existsHardToken(tokensn : " + tokensn + ")");
1138       boolean ret = false;
1139        try {
1140            hardtokendatahome.findByPrimaryKey(tokensn);
1141            ret = true;
1142        } catch (javax.ejb.FinderException JavaDoc fe) {
1143             ret=false;
1144        } catch(Exception JavaDoc e){
1145          throw new EJBException JavaDoc(e);
1146        }
1147       debug("<existsHardToken()");
1148       return ret;
1149    } // existsHardToken
1150

1151      /**
1152       * returns hard token data for the specified tokensn
1153       *
1154       * @param admin the administrator calling the function
1155       * @param tokensn The serialnumber of token.
1156       *
1157       * @return the hard token data or NULL if tokensn doesnt exists in database.
1158       * @throws EJBException if a communication or other error occurs.
1159       * @ejb.interface-method view-type="both"
1160       */

1161    public HardTokenData getHardToken(Admin admin, String JavaDoc tokensn){
1162       debug("<getHardToken(tokensn :" + tokensn +")");
1163       
1164       
1165       // Check Approvals
1166

1167       HardTokenData returnval = null;
1168       HardTokenDataLocal htd = null;
1169       try{
1170         htd = hardtokendatahome.findByPrimaryKey(tokensn);
1171
1172         // Find Copyof
1173
String JavaDoc copyof = null;
1174         try{
1175            copyof = hardtokenpropertyhome.findByProperty(tokensn, HardTokenPropertyEntityBean.PROPERTY_COPYOF).getValue();
1176         }catch(FinderException JavaDoc fe){}
1177
1178         ArrayList JavaDoc copies = null;
1179         if(copyof == null){
1180           // Find Copies
1181
try{
1182             Collection JavaDoc copieslocal = hardtokenpropertyhome.findIdsByPropertyAndValue(HardTokenPropertyEntityBean.PROPERTY_COPYOF , tokensn);
1183             if(copieslocal.size() >0 ){
1184               copies = new ArrayList JavaDoc();
1185               Iterator JavaDoc iter = copieslocal.iterator();
1186               while(iter.hasNext()){
1187                  copies.add(((HardTokenPropertyLocal) iter.next()).getId());
1188               }
1189             }
1190           }catch(FinderException JavaDoc fe){}
1191         }
1192
1193         if(htd != null){
1194           returnval = new HardTokenData(htd.getTokenSN(),htd.getUsername(), htd.getCreateTime(),htd.getModifyTime(),htd.getTokenType(),htd.getHardToken(), copyof, copies);
1195           String JavaDoc msg = intres.getLocalizedMessage("hardtoken.viewedtoken", tokensn);
1196           getLogSession().log(admin, htd.getSignificantIssuerDN().hashCode(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),htd.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKENVIEWED,msg);
1197         }
1198       }catch(FinderException JavaDoc e){}
1199
1200       debug("<getHardToken()");
1201       return returnval;
1202    } // getHardToken
1203

1204      /**
1205       * returns hard token data for the specified user
1206       *
1207       * @param admin the administrator calling the function
1208       * @param username The username owning the tokens.
1209       *
1210       * @return a Collection of all hard token user data.
1211       * @throws EJBException if a communication or other error occurs.
1212       * @ejb.interface-method view-type="both"
1213       */

1214    public Collection JavaDoc getHardTokens(Admin admin, String JavaDoc username){
1215       debug("<getHardToken(username :" + username +")");
1216       ArrayList JavaDoc returnval = new ArrayList JavaDoc();
1217       HardTokenDataLocal htd = null;
1218       try{
1219         Collection JavaDoc result = hardtokendatahome.findByUsername(username);
1220         Iterator JavaDoc i = result.iterator();
1221         while(i.hasNext()){
1222           htd = (HardTokenDataLocal) i.next();
1223           // Find Copyof
1224
String JavaDoc copyof = null;
1225           try{
1226            copyof = hardtokenpropertyhome.findByProperty(htd.getTokenSN(), HardTokenPropertyEntityBean.PROPERTY_COPYOF).getValue();
1227           }catch(FinderException JavaDoc fe){}
1228
1229
1230           ArrayList JavaDoc copies = null;
1231           if(copyof == null){
1232            // Find Copies
1233
try{
1234                Collection JavaDoc copieslocal = hardtokenpropertyhome.findIdsByPropertyAndValue(HardTokenPropertyEntityBean.PROPERTY_COPYOF , htd.getTokenSN());
1235                if(copieslocal.size() >0 ){
1236                    copies = new ArrayList JavaDoc();
1237                    Iterator JavaDoc iter = copieslocal.iterator();
1238                    while(iter.hasNext()){
1239                        copies.add(((HardTokenPropertyLocal) iter.next()).getId());
1240                    }
1241                }
1242             }catch(FinderException JavaDoc fe){}
1243           }
1244
1245           returnval.add(new HardTokenData(htd.getTokenSN(),htd.getUsername(), htd.getCreateTime(),htd.getModifyTime(),htd.getTokenType(),htd.getHardToken(),copyof, copies));
1246           String JavaDoc msg = intres.getLocalizedMessage("hardtoken.viewedtoken", htd.getTokenSN());
1247           getLogSession().log(admin, htd.getSignificantIssuerDN().hashCode(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),htd.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKENVIEWED,msg);
1248         }
1249       }catch(FinderException JavaDoc e){}
1250
1251       debug("<getHardToken()");
1252       return returnval;
1253    } // getHardTokens
1254

1255    /**
1256     * Method that searches the database for a tokensn. It returns all hardtokens
1257     * with a serialnumber that begins with the given searchpattern.
1258     *
1259     * @param admin the administrator calling the function
1260     * @param searchpattern of begining of hard token sn
1261     * @return a Collection of username(String) matching the search string
1262     * @ejb.interface-method view-type="both"
1263     */

1264
1265    public Collection JavaDoc findHardTokenByTokenSerialNumber(Admin admin, String JavaDoc searchpattern){
1266        debug(">findHardTokenByTokenSerialNumber()");
1267        ArrayList JavaDoc returnval = new ArrayList JavaDoc();
1268        Connection JavaDoc con = null;
1269        PreparedStatement JavaDoc ps = null;
1270        ResultSet JavaDoc rs = null;
1271        try{
1272            // Construct SQL query.
1273
con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
1274            ps = con.prepareStatement("select distinct username from HardTokenData where tokenSN LIKE '%" + searchpattern + "%'");
1275            // Execute query.
1276
rs = ps.executeQuery();
1277            // Assemble result.
1278
while(rs.next() && returnval.size() <= UserAdminConstants.MAXIMUM_QUERY_ROWCOUNT){
1279                returnval.add(rs.getString(1));
1280            }
1281            debug("<findHardTokenByTokenSerialNumber()");
1282            return returnval;
1283
1284        }catch(Exception JavaDoc e){
1285            throw new EJBException JavaDoc(e);
1286        }finally{
1287            JDBCUtil.close(con, ps, rs);
1288        }
1289
1290    }
1291
1292       /**
1293       * Adds a mapping between a hard token and a certificate
1294       *
1295       * @param admin the administrator calling the function
1296       * @param tokensn The serialnumber of token.
1297       * @param certificate the certificate to map to.
1298       *
1299       * @throws EJBException if a communication or other error occurs.
1300        * @ejb.interface-method view-type="both"
1301        * @ejb.transaction type="Required"
1302       */

1303    public void addHardTokenCertificateMapping(Admin admin, String JavaDoc tokensn, X509Certificate JavaDoc certificate){
1304        String JavaDoc certificatesn = certificate.getSerialNumber().toString(16);
1305        debug(">addHardTokenCertificateMapping(certificatesn : "+ certificatesn +", tokensn : " + tokensn + ")");
1306        int caid = CertTools.getIssuerDN(certificate).hashCode();
1307        String JavaDoc fp = CertTools.getFingerprintAsString(certificate);
1308        boolean exists = false;
1309        try {
1310            // We must actually check if there is one before we try to add it, because wls does not allow us to catch any errors if creating fails, that sux
1311
HardTokenCertificateMapLocal data = hardtokencertificatemaphome.findByPrimaryKey(fp);
1312            if (data != null) {
1313                exists = true;
1314            }
1315        } catch (FinderException JavaDoc e) {
1316            // This is what we hope will happen
1317
}
1318        if (!exists) {
1319            try {
1320                hardtokencertificatemaphome.create(fp,tokensn);
1321                String JavaDoc msg = intres.getLocalizedMessage("hardtoken.addedtokencertmapping", certificatesn, tokensn);
1322                getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENCERTIFICATEMAP,msg);
1323            } catch (Exception JavaDoc e) {
1324                String JavaDoc msg = intres.getLocalizedMessage("hardtoken.erroraddtokencertmapping", certificatesn, tokensn);
1325                getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENCERTIFICATEMAP,msg);
1326            }
1327        } else {
1328            String JavaDoc msg = intres.getLocalizedMessage("hardtoken.erroraddtokencertmapping", certificatesn, tokensn);
1329            getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENCERTIFICATEMAP,msg);
1330        }
1331        debug("<addHardTokenCertificateMapping()");
1332    } // addHardTokenCertificateMapping
1333

1334      /**
1335       * Removes a mapping between a hard token and a certificate
1336       *
1337       * @param admin the administrator calling the function
1338       * @param certificate the certificate to map to.
1339       *
1340       *
1341       * @throws EJBException if a communication or other error occurs.
1342       * @ejb.interface-method view-type="both"
1343       * @ejb.transaction type="Required"
1344       */

1345    public void removeHardTokenCertificateMapping(Admin admin, X509Certificate JavaDoc certificate){
1346       String JavaDoc certificatesn = certificate.getSerialNumber().toString(16);
1347       debug(">removeHardTokenCertificateMapping(Certificatesn: " + certificatesn + ")");
1348       int caid = CertTools.getIssuerDN(certificate).hashCode();
1349      try{
1350        HardTokenCertificateMapLocal htcm =hardtokencertificatemaphome.findByPrimaryKey(CertTools.getFingerprintAsString(certificate));
1351        htcm.remove();
1352        String JavaDoc msg = intres.getLocalizedMessage("hardtoken.removedtokencertmappingcert", certificatesn);
1353        getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENCERTIFICATEMAP, msg);
1354      }catch(Exception JavaDoc e){
1355          try{
1356              String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorremovetokencertmappingcert", certificatesn);
1357              getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENCERTIFICATEMAP, msg);
1358          }catch(Exception JavaDoc re){
1359            throw new EJBException JavaDoc(e);
1360         }
1361      }
1362      debug("<removeHardTokenCertificateMapping()");
1363    } // removeHardTokenCertificateMapping
1364

1365
1366    /**
1367     * Removes all mappings between a hard token and a certificate
1368     *
1369     * @param admin the administrator calling the function
1370     * @param tokensn the serial number to remove.
1371     *
1372     *
1373     * @throws EJBException if a communication or other error occurs.
1374     */

1375    private void removeHardTokenCertificateMappings(Admin admin, String JavaDoc tokensn){
1376      debug(">removeHardTokenCertificateMappings(tokensn: " + tokensn + ")");
1377      int caid = admin.getCaId();
1378      try{
1379        Iterator JavaDoc result = hardtokencertificatemaphome.findByTokenSN(tokensn).iterator();
1380        while(result.hasNext()){
1381          HardTokenCertificateMapLocal htcm = (HardTokenCertificateMapLocal) result.next();
1382          htcm.remove();
1383
1384        }
1385        String JavaDoc msg = intres.getLocalizedMessage("hardtoken.removedtokencertmappingtoken", tokensn);
1386        getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_HARDTOKENCERTIFICATEMAP, msg);
1387        }catch(Exception JavaDoc e){
1388            try{
1389                String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorremovetokencertmappingtoken", tokensn);
1390                getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKENCERTIFICATEMAP, msg);
1391            }catch(Exception JavaDoc re){
1392              throw new EJBException JavaDoc(e);
1393           }
1394         }
1395         debug("<removeHardTokenCertificateMappings()");
1396     } // removeHardTokenCertificateMapping
1397

1398       /**
1399       * Returns all the X509Certificates places in a hard token.
1400       *
1401       * @param admin the administrator calling the function
1402       * @param tokensn The serialnumber of token.
1403       *
1404       * @return a collection of X509Certificates
1405       * @throws EJBException if a communication or other error occurs.
1406        * @ejb.interface-method view-type="both"
1407       */

1408    public Collection JavaDoc findCertificatesInHardToken(Admin admin, String JavaDoc tokensn){
1409       debug("<findCertificatesInHardToken(username :" + tokensn +")");
1410       ArrayList JavaDoc returnval = new ArrayList JavaDoc();
1411       HardTokenCertificateMapLocal htcm = null;
1412       try{
1413         Collection JavaDoc result = hardtokencertificatemaphome.findByTokenSN(tokensn);
1414         Iterator JavaDoc i = result.iterator();
1415         while(i.hasNext()){
1416           htcm = (HardTokenCertificateMapLocal) i.next();
1417           Certificate JavaDoc cert = getCertificateStoreSession().findCertificateByFingerprint(admin, htcm.getCertificateFingerprint());
1418           if (cert != null) {
1419               returnval.add(cert);
1420           }
1421         }
1422       }catch(Exception JavaDoc e){
1423          throw new EJBException JavaDoc(e);
1424       }
1425
1426       debug("<findCertificatesInHardToken()");
1427       return returnval;
1428    } // findCertificatesInHardToken
1429

1430    /**
1431     * Returns the tokensn that the have blongs to a given certificatesn and tokensn.
1432     *
1433     * @param admin the administrator calling the function
1434     * @param certificatesn The serialnumber of certificate.
1435     * @param issuerdn the issuerdn of the certificate.
1436     *
1437     * @return the serialnumber or null if no tokensn could be found.
1438     * @throws EJBException if a communication or other error occurs.
1439      * @ejb.interface-method view-type="both"
1440     */

1441  public String JavaDoc findHardTokenByCertificateSNIssuerDN(Admin admin, BigInteger JavaDoc certificatesn, String JavaDoc issuerdn){
1442     debug("<findHardTokenByCertificateSNIssuerDN(certificatesn :" + certificatesn + ", issuerdn :" + issuerdn+ ")");
1443     String JavaDoc returnval = null;
1444     HardTokenCertificateMapLocal htcm = null;
1445     try{
1446       X509Certificate JavaDoc cert = (X509Certificate JavaDoc) getCertificateStoreSession().findCertificateByIssuerAndSerno(admin,issuerdn,certificatesn);
1447       if(cert != null){
1448         htcm = hardtokencertificatemaphome.findByPrimaryKey(CertTools.getFingerprintAsString(cert));
1449         if(htcm != null){
1450           returnval = htcm.getTokenSN();
1451         }
1452       }
1453     }catch(Exception JavaDoc e){
1454        throw new EJBException JavaDoc(e);
1455     }
1456
1457     debug("<findHardTokenByCertificateSNIssuerDN()");
1458     return returnval;
1459  } // findCertificatesInHardToken
1460

1461
1462    /**
1463     * Method used to signal to the log that token was generated successfully.
1464     *
1465     * @param admin administrator performing action
1466     * @param tokensn tokensn of token generated
1467     * @param username username of user token was generated for.
1468     * @param significantissuerdn indicates which CA the hard token should belong to.
1469     * @ejb.interface-method view-type="both"
1470     * @ejb.transaction type="Required"
1471     */

1472    public void tokenGenerated(Admin admin, String JavaDoc tokensn, String JavaDoc username, String JavaDoc significantissuerdn){
1473      int caid = CertTools.stringToBCDNString(significantissuerdn).hashCode();
1474      try{
1475        String JavaDoc msg = intres.getLocalizedMessage("hardtoken.generatedtoken", tokensn);
1476        getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),username, null, LogEntry.EVENT_INFO_HARDTOKENGENERATED, msg);
1477      }catch(Exception JavaDoc e){
1478        throw new EJBException JavaDoc(e);
1479      }
1480    } // tokenGenerated
1481

1482    /**
1483     * Method used to signal to the log that error occured when generating token.
1484     *
1485     * @param admin administrator performing action
1486     * @param tokensn tokensn of token.
1487     * @param username username of user token was generated for.
1488     * @param significantissuerdn indicates which CA the hard token should belong to.
1489     * @ejb.interface-method view-type="both"
1490     * @ejb.transaction type="Required"
1491     */

1492    public void errorWhenGeneratingToken(Admin admin, String JavaDoc tokensn, String JavaDoc username, String JavaDoc significantissuerdn){
1493      int caid = CertTools.stringToBCDNString(significantissuerdn).hashCode();
1494      try{
1495          String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorgeneratetoken", tokensn);
1496          getLogSession().log(admin, caid, LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),username, null, LogEntry.EVENT_ERROR_HARDTOKENGENERATED, msg);
1497      }catch(Exception JavaDoc e){
1498        throw new EJBException JavaDoc(e);
1499      }
1500    } // errorWhenGeneratingToken
1501

1502
1503    /**
1504    * Method to check if a certificate profile exists in any of the hard token profiles.
1505    * Used to avoid desyncronization of certificate profile data.
1506    *
1507    * @param id the certificateprofileid to search for.
1508    * @return true if certificateprofileid exists in any of the hard token profiles.
1509     * @ejb.interface-method view-type="both"
1510    */

1511   public boolean existsCertificateProfileInHardTokenProfiles(Admin admin, int id){
1512     HardTokenProfile profile = null;
1513     Collection JavaDoc certprofiles=null;
1514     boolean exists = false;
1515     try{
1516       Collection JavaDoc result = hardtokenprofilehome.findAll();
1517       Iterator JavaDoc i = result.iterator();
1518       while(i.hasNext() && !exists){
1519         profile = ((HardTokenProfileDataLocal) i.next()).getHardTokenProfile();
1520         if(profile instanceof EIDProfile){
1521           certprofiles = ((EIDProfile) profile).getAllCertificateProfileIds();
1522           if(certprofiles.contains(new Integer JavaDoc(id)))
1523             exists = true;
1524         }
1525       }
1526     }catch(FinderException JavaDoc e){}
1527
1528     return exists;
1529   } // existsCertificateProfileInHardTokenProfiles
1530

1531    /**
1532    * Method to check if a hard token profile exists in any of the hard token issuers.
1533    * Used to avoid desyncronization of hard token profile data.
1534    *
1535    * @param id the hard token profileid to search for.
1536    * @return true if hard token profileid exists in any of the hard token issuers.
1537    * @ejb.interface-method view-type="both"
1538    */

1539  public boolean existsHardTokenProfileInHardTokenIssuer(Admin admin, int id){
1540     HardTokenIssuer issuer = null;
1541     Collection JavaDoc hardtokenissuers=null;
1542     boolean exists = false;
1543     try{
1544       Collection JavaDoc result = this.hardtokenissuerhome.findAll();
1545       Iterator JavaDoc i = result.iterator();
1546       while(i.hasNext() && !exists){
1547         issuer = ((HardTokenIssuerDataLocal) i.next()).getHardTokenIssuer();
1548         hardtokenissuers = issuer.getAvailableHardTokenProfiles();
1549         if(hardtokenissuers.contains(new Integer JavaDoc(id)))
1550             exists = true;
1551       }
1552     }catch(FinderException JavaDoc e){}
1553     return exists;
1554  } // existsHardTokenProfileInHardTokenIssuer
1555

1556    private Integer JavaDoc findFreeHardTokenProfileId(){
1557      Random JavaDoc ran = (new Random JavaDoc((new Date JavaDoc()).getTime()));
1558      int id = ran.nextInt();
1559      boolean foundfree = false;
1560
1561      while(!foundfree){
1562        try{
1563          if(id > SecConst.TOKEN_SOFT)
1564            hardtokenprofilehome.findByPrimaryKey(new Integer JavaDoc(id));
1565            id = ran.nextInt();
1566        }catch(FinderException JavaDoc e){
1567           foundfree = true;
1568        }
1569      }
1570      return new Integer JavaDoc(id);
1571    } // findFreeHardTokenProfileId
1572

1573    private Integer JavaDoc findFreeHardTokenIssuerId(){
1574      Random JavaDoc ran = (new Random JavaDoc((new Date JavaDoc()).getTime()));
1575      int id = ran.nextInt();
1576      boolean foundfree = false;
1577
1578      while(!foundfree){
1579        try{
1580          if(id > 1)
1581            hardtokenissuerhome.findByPrimaryKey(new Integer JavaDoc(id));
1582            id = ran.nextInt();
1583        }catch(FinderException JavaDoc e){
1584           foundfree = true;
1585        }
1586      }
1587      return new Integer JavaDoc(id);
1588    } // findFreeHardTokenIssuerId
1589

1590
1591} // LocalHardTokenSessionBean
1592
Popular Tags