KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > ra > raadmin > LocalRaAdminSessionBean


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

13
14 package org.ejbca.core.ejb.ra.raadmin;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Random JavaDoc;
23
24 import javax.ejb.CreateException JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.FinderException JavaDoc;
27
28 import org.ejbca.core.ejb.BaseSessionBean;
29 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal;
30 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome;
31 import org.ejbca.core.ejb.log.ILogSessionLocal;
32 import org.ejbca.core.ejb.log.ILogSessionLocalHome;
33 import org.ejbca.core.model.InternalResources;
34 import org.ejbca.core.model.SecConst;
35 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
36 import org.ejbca.core.model.log.Admin;
37 import org.ejbca.core.model.log.LogEntry;
38 import org.ejbca.core.model.ra.raadmin.AdminPreference;
39 import org.ejbca.core.model.ra.raadmin.EndEntityProfile;
40 import org.ejbca.core.model.ra.raadmin.EndEntityProfileExistsException;
41 import org.ejbca.core.model.ra.raadmin.GlobalConfiguration;
42
43
44 /**
45  * Stores data used by web server clients.
46  * Uses JNDI name for datasource as defined in env 'Datasource' in ejb-jar.xml.
47  *
48  * @version $Id: LocalRaAdminSessionBean.java,v 1.10 2007/01/11 09:35:07 anatom Exp $
49  *
50  * @ejb.bean description="Session bean handling core CA function,signing certificates"
51  * display-name="RaAdminSB"
52  * name="RaAdminSession"
53  * jndi-name="RaAdminSession"
54  * local-jndi-name="RaAdminSessionLocal"
55  * view-type="both"
56  * type="Stateless"
57  * transaction-type="Container"
58  *
59  * @ejb.transaction type="Required"
60  *
61  * @weblogic.enable-call-by-reference True
62  *
63  * @ejb.home
64  * extends="javax.ejb.EJBHome"
65  * remote-class="org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionHome"
66  * local-extends="javax.ejb.EJBLocalHome"
67  * local-class="org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocalHome"
68  *
69  * @ejb.interface
70  * extends="javax.ejb.EJBObject"
71  * remote-class="org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionRemote"
72  * local-extends="javax.ejb.EJBLocalObject"
73  * local-class="org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocal"
74  *
75  * @ejb.ejb-external-ref description="The log session bean"
76  * view-type="local"
77  * ref-name="ejb/LogSessionLocal"
78  * type="Session"
79  * home="org.ejbca.core.ejb.log.ILogSessionLocalHome"
80  * business="org.ejbca.core.ejb.log.ILogSessionLocal"
81  * link="LogSession"
82  *
83  * @ejb.ejb-external-ref description="The Authorization session bean"
84  * view-type="local"
85  * ref-name="ejb/AuthorizationSessionLocal"
86  * type="Session"
87  * home="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome"
88  * business="org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal"
89  * link="AuthorizationSession"
90  *
91  * @ejb.ejb-external-ref description="The AdminPreferencesData Entity bean"
92  * view-type="local"
93  * ref-name="ejb/AdminPreferencesDataLocal"
94  * type="Entity"
95  * home="org.ejbca.core.ejb.ra.raadmin.AdminPreferencesDataLocalHome"
96  * business="org.ejbca.core.ejb.ra.raadmin.AdminPreferencesDataLocal"
97  * link="AdminPreferencesData"
98  *
99  * @ejb.ejb-external-ref description="The EndEntityProfileData Entity bean"
100  * view-type="local"
101  * ref-name="ejb/EndEntityProfileDataLocal"
102  * type="Entity"
103  * home="org.ejbca.core.ejb.ra.raadmin.EndEntityProfileDataLocalHome"
104  * business="org.ejbca.core.ejb.ra.raadmin.EndEntityProfileDataLocal"
105  * link="EndEntityProfileData"
106  *
107  * @ejb.ejb-external-ref description="The GlobalConfigurationData Entity bean"
108  * view-type="local"
109  * ref-name="ejb/GlobalConfigurationDataLocal"
110  * type="Entity"
111  * home="org.ejbca.core.ejb.ra.raadmin.GlobalConfigurationDataLocalHome"
112  * business="org.ejbca.core.ejb.ra.raadmin.GlobalConfigurationDataLocal"
113  * link="GlobalConfigurationData"
114  *
115  */

116 public class LocalRaAdminSessionBean extends BaseSessionBean {
117
118     /** Internal localization of logs and errors */
119     private static final InternalResources intres = InternalResources.getInstance();
120
121     /** The home interface of AdminPreferences entity bean */
122     private AdminPreferencesDataLocalHome adminpreferenceshome=null;
123
124     /** The home interface of EndEntityProfileData entity bean */
125     private EndEntityProfileDataLocalHome profiledatahome=null;
126
127     /** The home interface of GlobalConfiguration entity bean */
128     private GlobalConfigurationDataLocalHome globalconfigurationhome = null;
129
130     /** Var containing the global configuration. */
131     private GlobalConfiguration globalconfiguration;
132
133     /** The local interface of log session bean */
134     private ILogSessionLocal logsession = null;
135
136     /** the local inteface of authorization session */
137     private IAuthorizationSessionLocal authorizationsession = null;
138
139
140     public static final String JavaDoc EMPTY_ENDENTITYPROFILENAME = "EMPTY";
141
142     private static final String JavaDoc DEFAULTUSERPREFERENCE = "default";
143
144     public static final String JavaDoc EMPTY_ENDENTITYPROFILE = LocalRaAdminSessionBean.EMPTY_ENDENTITYPROFILENAME;
145     public static final int EMPTY_ENDENTITYPROFILEID = SecConst.EMPTY_ENDENTITYPROFILE;
146
147     /**
148      * Default create for SessionBean without any creation Arguments.
149      * @throws CreateException if bean instance can't be created
150      * @ejb.create-method
151      */

152     public void ejbCreate() throws CreateException JavaDoc {
153       try{
154         adminpreferenceshome = (AdminPreferencesDataLocalHome)getLocator().getLocalHome(AdminPreferencesDataLocalHome.COMP_NAME);
155         profiledatahome = (EndEntityProfileDataLocalHome)getLocator().getLocalHome(EndEntityProfileDataLocalHome.COMP_NAME);
156         globalconfigurationhome = (GlobalConfigurationDataLocalHome)getLocator().getLocalHome(GlobalConfigurationDataLocalHome.COMP_NAME);
157         
158       }catch(Exception JavaDoc e){
159          throw new EJBException JavaDoc(e);
160       }
161
162     }
163
164
165     /** Gets connection to log session bean
166      */

167     private ILogSessionLocal getLogSession() {
168         if(logsession == null){
169           try{
170             ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME);
171             logsession = logsessionhome.create();
172           }catch(Exception JavaDoc e){
173              throw new EJBException JavaDoc(e);
174           }
175         }
176         return logsession;
177     } //getLogSession
178

179
180     /** Gets connection to authorization session bean
181      * @return Connection
182      */

183     private IAuthorizationSessionLocal getAuthorizationSession() {
184         if(authorizationsession == null){
185           try{
186             IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME);
187             authorizationsession = authorizationsessionhome.create();
188           }catch(Exception JavaDoc e){
189              throw new EJBException JavaDoc(e);
190           }
191         }
192         return authorizationsession;
193     } //getAuthorizationSession
194

195
196
197
198      /**
199      * Finds the admin preference belonging to a certificate serialnumber. Returns null if admin doesn't exists.
200      * @ejb.interface-method
201      */

202     public AdminPreference getAdminPreference(Admin admin, String JavaDoc certificatefingerprint){
203         debug(">getAdminPreference()");
204         AdminPreference ret =null;
205         try {
206             AdminPreferencesDataLocal apdata = adminpreferenceshome.findByPrimaryKey(certificatefingerprint);
207             ret = apdata.getAdminPreference();
208         } catch (javax.ejb.FinderException JavaDoc fe) {
209              // Create new configuration
210
ret=null;
211         } catch(Exception JavaDoc e){
212           throw new EJBException JavaDoc(e);
213         }
214         debug("<getAdminPreference()");
215         return ret;
216     } // getAdminPreference
217

218     /**
219      * Adds a admin preference to the database. Returns false if admin already exists.
220      * @ejb.interface-method
221      */

222     public boolean addAdminPreference(Admin admin, String JavaDoc certificatefingerprint, AdminPreference adminpreference){
223         debug(">addAdminPreference(fingerprint : " + certificatefingerprint + ")");
224         boolean ret = false;
225         boolean exists = false;
226         try {
227             // 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
228
AdminPreferencesDataLocal data = adminpreferenceshome.findByPrimaryKey(certificatefingerprint);
229             if (data != null) {
230                 exists = true;
231             }
232         } catch (FinderException JavaDoc e) {
233             // This is what we hope will happen
234
}
235         if (!exists) {
236             try {
237                 AdminPreferencesDataLocal apdata= adminpreferenceshome.create(certificatefingerprint, adminpreference);
238                 String JavaDoc msg = intres.getLocalizedMessage("ra.adminprefadded", apdata.getId());
239                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ADMINISTRATORPREFERENCECHANGED,msg);
240                 ret = true;
241             } catch (Exception JavaDoc e) {
242                 ret = false;
243                 String JavaDoc msg = intres.getLocalizedMessage("ra.adminprefexists");
244                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ADMINISTRATORPREFERENCECHANGED,msg);
245             }
246         } else {
247             ret = false;
248             String JavaDoc msg = intres.getLocalizedMessage("ra.adminprefexists");
249             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ADMINISTRATORPREFERENCECHANGED,msg);
250         }
251         debug("<addAdminPreference()");
252         return ret;
253     } // addAdminPreference
254

255     /**
256      * Changes the admin preference in the database. Returns false if admin doesn't exists.
257      * @ejb.interface-method
258      */

259     public boolean changeAdminPreference(Admin admin, String JavaDoc certificatefingerprint, AdminPreference adminpreference){
260        debug(">changeAdminPreference(fingerprint : " + certificatefingerprint + ")");
261        return updateAdminPreference(admin, certificatefingerprint, adminpreference, true);
262     } // changeAdminPreference
263

264     /**
265      * Changes the admin preference in the database. Returns false if admin doesn't exists.
266      * @ejb.interface-method
267      */

268     public boolean changeAdminPreferenceNoLog(Admin admin, String JavaDoc certificatefingerprint, AdminPreference adminpreference){
269        debug(">changeAdminPreferenceNoLog(fingerprint : " + certificatefingerprint + ")");
270        return updateAdminPreference(admin, certificatefingerprint, adminpreference, false);
271     } // changeAdminPreference
272

273     /**
274      * Checks if a admin preference exists in the database.
275      * @ejb.interface-method
276      * @ejb.transaction type="Supports"
277      */

278     public boolean existsAdminPreference(Admin admin, String JavaDoc certificatefingerprint){
279        debug(">existsAdminPreference(fingerprint : " + certificatefingerprint + ")");
280        boolean ret = false;
281         try {
282             AdminPreferencesDataLocal apdata = adminpreferenceshome.findByPrimaryKey(certificatefingerprint);
283             debug("Found admin preferences with id "+apdata.getId());
284             ret = true;
285         } catch (javax.ejb.FinderException JavaDoc fe) {
286              ret=false;
287         } catch(Exception JavaDoc e){
288           throw new EJBException JavaDoc(e);
289         }
290         debug("<existsAdminPreference()");
291         return ret;
292     }// existsAdminPreference
293

294     /**
295      * Function that returns the default admin preference.
296      *
297      * @throws EJBException if a communication or other error occurs.
298      * @ejb.interface-method
299      * @ejb.transaction type="Supports"
300      */

301     public AdminPreference getDefaultAdminPreference(Admin admin){
302         debug(">getDefaultAdminPreference()");
303         AdminPreference ret =null;
304         try {
305             AdminPreferencesDataLocal apdata = adminpreferenceshome.findByPrimaryKey(DEFAULTUSERPREFERENCE);
306             ret = apdata.getAdminPreference();
307         } catch (javax.ejb.FinderException JavaDoc fe) {
308             try{
309                // Create new configuration
310
AdminPreferencesDataLocal apdata = adminpreferenceshome.create(DEFAULTUSERPREFERENCE,new AdminPreference());
311               ret = apdata.getAdminPreference();
312             }catch(Exception JavaDoc e){
313               throw new EJBException JavaDoc(e);
314             }
315         } catch(Exception JavaDoc e){
316           throw new EJBException JavaDoc(e);
317         }
318         debug("<getDefaultAdminPreference()");
319         return ret;
320     } // getDefaultPreference()
321

322      /**
323      * Function that saves the default admin preference.
324      *
325      * @throws EJBException if a communication or other error occurs.
326       * @ejb.interface-method
327      */

328     public void saveDefaultAdminPreference(Admin admin, AdminPreference defaultadminpreference){
329        debug(">saveDefaultAdminPreference()");
330        try {
331           AdminPreferencesDataLocal apdata = adminpreferenceshome.findByPrimaryKey(DEFAULTUSERPREFERENCE);
332           apdata.setAdminPreference(defaultadminpreference);
333           String JavaDoc msg = intres.getLocalizedMessage("ra.defaultadminprefsaved");
334           getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ADMINISTRATORPREFERENCECHANGED,msg);
335        } catch (Exception JavaDoc e) {
336            String JavaDoc msg = intres.getLocalizedMessage("ra.errorsavedefaultadminpref");
337            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ADMINISTRATORPREFERENCECHANGED,msg);
338            throw new EJBException JavaDoc(e);
339        }
340         debug("<saveDefaultAdminPreference()");
341     } // saveDefaultAdminPreference
342

343     /**
344       * Adds a profile to the database.
345       *
346       * @param admin administrator performing task
347       * @param profilename readable profile name
348       * @param profile profile to be added
349      * @ejb.interface-method
350       *
351       */

352      public void addEndEntityProfile(Admin admin, String JavaDoc profilename, EndEntityProfile profile) throws EndEntityProfileExistsException {
353          addEndEntityProfile(admin,findFreeEndEntityProfileId(),profilename,profile);
354      } // addEndEntityProfile
355

356      /**
357       * Adds a profile to the database.
358       *
359       * @param admin administrator performing task
360       * @param profileid internal ID of new profile, use only if you know it's right.
361       * @param profilename readable profile name
362       * @param profile profile to be added
363       * @ejb.interface-method
364       *
365       */

366      public void addEndEntityProfile(Admin admin, int profileid, String JavaDoc profilename, EndEntityProfile profile) throws EndEntityProfileExistsException{
367         if(profilename.trim().equalsIgnoreCase(EMPTY_ENDENTITYPROFILENAME)){
368             String JavaDoc msg = intres.getLocalizedMessage("ra.erroraddprofile", profilename);
369             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
370             throw new EndEntityProfileExistsException();
371         }
372         if (isFreeEndEntityProfileId(profileid) == false) {
373             String JavaDoc msg = intres.getLocalizedMessage("ra.erroraddprofile", profilename);
374             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
375             throw new EndEntityProfileExistsException();
376         }
377         try {
378             profiledatahome.findByProfileName(profilename);
379             String JavaDoc msg = intres.getLocalizedMessage("ra.erroraddprofile", profilename);
380             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
381             throw new EndEntityProfileExistsException();
382         } catch (FinderException JavaDoc e) {
383             try {
384                 profiledatahome.create(new Integer JavaDoc(profileid), profilename, profile);
385                 String JavaDoc msg = intres.getLocalizedMessage("ra.addedprofile", profilename);
386                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null,
387                         LogEntry.EVENT_INFO_ENDENTITYPROFILE,msg);
388             } catch (Exception JavaDoc f) {
389                 String JavaDoc msg = intres.getLocalizedMessage("ra.erroraddprofile", profilename);
390                 error(msg, e);
391                 logsession.log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(), null, null,
392                         LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
393             }
394          }
395      } // addEndEntityProfile
396

397      /**
398      * Adds a end entity profile to a group with the same content as the original profile.
399       * @ejb.interface-method
400      */

401     public void cloneEndEntityProfile(Admin admin, String JavaDoc originalprofilename, String JavaDoc newprofilename) throws EndEntityProfileExistsException{
402        EndEntityProfile profile = null;
403
404        if(newprofilename.trim().equalsIgnoreCase(EMPTY_ENDENTITYPROFILENAME)){
405            String JavaDoc msg = intres.getLocalizedMessage("ra.errorcloneprofile", newprofilename, originalprofilename);
406            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
407            throw new EndEntityProfileExistsException();
408        }
409        try{
410          EndEntityProfileDataLocal pdl = profiledatahome.findByProfileName(originalprofilename);
411          profile = (EndEntityProfile) pdl.getProfile().clone();
412          try{
413            profiledatahome.findByProfileName(newprofilename);
414            String JavaDoc msg = intres.getLocalizedMessage("ra.errorcloneprofile", newprofilename, originalprofilename);
415            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
416            throw new EndEntityProfileExistsException();
417          }catch(FinderException JavaDoc e){
418             profiledatahome.create(new Integer JavaDoc(findFreeEndEntityProfileId()),newprofilename,profile);
419             String JavaDoc msg = intres.getLocalizedMessage("ra.clonedprofile", newprofilename, originalprofilename);
420             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ENDENTITYPROFILE,msg);
421          }
422        }catch(Exception JavaDoc e){
423            String JavaDoc msg = intres.getLocalizedMessage("ra.errorcloneprofile", newprofilename, originalprofilename);
424            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
425        }
426
427     } // cloneEndEntityProfile
428

429      /**
430      * Removes an end entity profile from the database.
431      * @throws EJBException if a communication or other error occurs.
432       * @ejb.interface-method
433      */

434     public void removeEndEntityProfile(Admin admin, String JavaDoc profilename) {
435         try{
436             EndEntityProfileDataLocal pdl = profiledatahome.findByProfileName(profilename);
437             pdl.remove();
438             String JavaDoc msg = intres.getLocalizedMessage("ra.removedprofile", profilename);
439             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ENDENTITYPROFILE,msg);
440         }catch(Exception JavaDoc e){
441             String JavaDoc msg = intres.getLocalizedMessage("ra.errorremoveprofile", profilename);
442             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
443         }
444     } // removeEndEntityProfile
445

446      /**
447      * Renames a end entity profile
448       * @ejb.interface-method
449      */

450     public void renameEndEntityProfile(Admin admin, String JavaDoc oldprofilename, String JavaDoc newprofilename) throws EndEntityProfileExistsException{
451         if(newprofilename.trim().equalsIgnoreCase(EMPTY_ENDENTITYPROFILENAME) || oldprofilename.trim().equalsIgnoreCase(EMPTY_ENDENTITYPROFILENAME)){
452             String JavaDoc msg = intres.getLocalizedMessage("ra.errorrenameprofile", oldprofilename, newprofilename);
453             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
454             throw new EndEntityProfileExistsException();
455         }
456        try{
457            profiledatahome.findByProfileName(newprofilename);
458            String JavaDoc msg = intres.getLocalizedMessage("ra.errorrenameprofile", oldprofilename, newprofilename);
459            getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
460            throw new EndEntityProfileExistsException();
461        }catch(FinderException JavaDoc e){
462            try{
463                EndEntityProfileDataLocal pdl = profiledatahome.findByProfileName(oldprofilename);
464                pdl.setProfileName(newprofilename);
465                String JavaDoc msg = intres.getLocalizedMessage("ra.renamedprofile", oldprofilename, newprofilename);
466                getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ENDENTITYPROFILE,msg );
467            }catch(FinderException JavaDoc f){
468                String JavaDoc msg = intres.getLocalizedMessage("ra.errorrenameprofile", oldprofilename, newprofilename);
469                getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg );
470            }
471        }
472     } // renameProfile
473

474     /**
475      * Updates profile data
476      * @ejb.interface-method
477      */

478     public void changeEndEntityProfile(Admin admin, String JavaDoc profilename, EndEntityProfile profile){
479         try{
480             EndEntityProfileDataLocal pdl = profiledatahome.findByProfileName(profilename);
481             pdl.setProfile(profile);
482             String JavaDoc msg = intres.getLocalizedMessage("ra.changedprofile", profilename);
483             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ENDENTITYPROFILE,msg);
484         }catch(FinderException JavaDoc e){
485             String JavaDoc msg = intres.getLocalizedMessage("ra.errorchangeprofile", profilename);
486             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ENDENTITYPROFILE,msg);
487         }
488     }// changeEndEntityProfile
489

490     /**
491      * Retrives a Collection of id:s (Integer) to authorized profiles.
492      * @ejb.transaction type="Supports"
493      * @ejb.interface-method
494      */

495     public Collection JavaDoc getAuthorizedEndEntityProfileIds(Admin admin){
496       ArrayList JavaDoc returnval = new ArrayList JavaDoc();
497       Collection JavaDoc result = null;
498
499       HashSet JavaDoc authorizedcaids = new HashSet JavaDoc(getAuthorizationSession().getAuthorizedCAIds(admin));
500       //debug("Admin authorized to "+authorizedcaids.size()+" CAs.");
501
try{
502           if(getAuthorizationSession().isAuthorizedNoLog(admin, "/super_administrator"))
503               returnval.add(new Integer JavaDoc(SecConst.EMPTY_ENDENTITYPROFILE));
504         }catch(AuthorizationDeniedException e){}
505
506       try{
507           result = profiledatahome.findAll();
508           Iterator JavaDoc i = result.iterator();
509           while(i.hasNext()){
510               EndEntityProfileDataLocal next = (EndEntityProfileDataLocal) i.next();
511               // Check if all profiles available CAs exists in authorizedcaids.
512
String JavaDoc value = next.getProfile().getValue(EndEntityProfile.AVAILCAS, 0);
513               //debug("AvailCAs: "+value);
514
if (value != null) {
515                   String JavaDoc[] availablecas = value.split(EndEntityProfile.SPLITCHAR);
516                   //debug("No of available CAs: "+availablecas.length);
517
boolean allexists = true;
518                   for(int j=0; j < availablecas.length; j++){
519                       //debug("Available CA["+j+"]: "+availablecas[j]);
520
if(!authorizedcaids.contains( new Integer JavaDoc(availablecas[j]))){
521                           allexists = false;
522                           //debug("Profile "+next.getId()+" not authorized");
523
break;
524                       }
525                   }
526                   if(allexists) {
527                       //debug("Adding "+next.getId());
528
returnval.add(next.getId());
529                   }
530               }
531           }
532       }catch(Exception JavaDoc e){
533           String JavaDoc msg = intres.getLocalizedMessage("ra.errorgetids");
534           error(msg, e);
535       }
536       return returnval;
537     } // getAuthorizedEndEntityProfileNames
538

539     /**
540      * Method creating a hashmap mapping profile id (Integer) to profile name (String).
541      * @ejb.transaction type="Supports"
542      * @ejb.interface-method
543      */

544     public HashMap JavaDoc getEndEntityProfileIdToNameMap(Admin admin){
545         debug(">getEndEntityProfileIdToNameMap");
546         HashMap JavaDoc returnval = new HashMap JavaDoc();
547         Collection JavaDoc result = null;
548         returnval.put(new Integer JavaDoc(SecConst.EMPTY_ENDENTITYPROFILE),EMPTY_ENDENTITYPROFILENAME);
549         try{
550             result = profiledatahome.findAll();
551             //debug("Found "+result.size()+ " end entity profiles.");
552
Iterator JavaDoc i = result.iterator();
553             while(i.hasNext()){
554                 EndEntityProfileDataLocal next = (EndEntityProfileDataLocal) i.next();
555                 //debug("Added "+next.getId()+ ", "+next.getProfileName());
556
returnval.put(next.getId(),next.getProfileName());
557             }
558         }catch(Exception JavaDoc e) {
559             String JavaDoc msg = intres.getLocalizedMessage("ra.errorreadprofiles");
560             error(msg, e);
561         }
562         debug(">getEndEntityProfileIdToNameMap");
563         return returnval;
564       } // getEndEntityProfileIdToNameMap
565

566      /**
567      * Finds a end entity profile by id.
568      * @ejb.transaction type="Supports"
569       * @ejb.interface-method
570      */

571     public EndEntityProfile getEndEntityProfile(Admin admin, int id){
572         if (log.isDebugEnabled()) {
573             debug(">getEndEntityProfile("+id+")");
574         }
575         EndEntityProfile returnval=null;
576         try{
577             if(id==SecConst.EMPTY_ENDENTITYPROFILE) {
578                 returnval = new EndEntityProfile(true);
579             }
580             if(id!=0 && id != SecConst.EMPTY_ENDENTITYPROFILE) {
581                 returnval = (profiledatahome.findByPrimaryKey(new Integer JavaDoc(id))).getProfile();
582             }
583         }catch(FinderException JavaDoc e){
584             // Ignore so we'll return null
585
}
586         if (log.isDebugEnabled()) {
587             debug("<getEndEntityProfile(id): "+(returnval == null ? "null":"not null"));
588         }
589         return returnval;
590     } // getEndEntityProfile
591

592      /**
593      * Finds a end entity profile by id.
594      * @ejb.transaction type="Supports"
595       * @ejb.interface-method
596      */

597     public EndEntityProfile getEndEntityProfile(Admin admin, String JavaDoc profilename){
598         if (log.isDebugEnabled()) {
599             debug(">getEndEntityProfile("+profilename+")");
600         }
601         EndEntityProfile returnval=null;
602         try{
603           if(profilename.equals(EMPTY_ENDENTITYPROFILENAME)) {
604               returnval = new EndEntityProfile(true);
605           } else {
606               returnval = (profiledatahome.findByProfileName(profilename)).getProfile();
607           }
608         }catch(FinderException JavaDoc e){
609             // Ignore so we'll return null
610
}
611         debug("<getEndEntityProfile(profilename)");
612         return returnval;
613     } // getEndEntityProfile
614

615      /**
616      * Returns a end entity profiles id, given it's profilename
617      *
618      * @return the id or 0 if profile cannot be found.
619      * @ejb.transaction type="Supports"
620       * @ejb.interface-method
621      */

622     public int getEndEntityProfileId(Admin admin, String JavaDoc profilename){
623         if (log.isDebugEnabled()) {
624             debug(">getEndEntityProfileId("+profilename+")");
625         }
626       int returnval = 0;
627       if(profilename.trim().equalsIgnoreCase(EMPTY_ENDENTITYPROFILENAME))
628         return SecConst.EMPTY_ENDENTITYPROFILE;
629       try{
630         Integer JavaDoc id = (profiledatahome.findByProfileName(profilename)).getId();
631         returnval = id.intValue();
632       }catch(FinderException JavaDoc e){
633           // Ignore so we'll return 0
634
}
635       debug(">getEndEntityProfileId(profilename)");
636       return returnval;
637     } // getEndEntityrofileId
638

639      /**
640      * Returns a end entity profiles name given it's id.
641      *
642      * @return profilename or null if profile id doesn't exists.
643      * @ejb.transaction type="Supports"
644       * @ejb.interface-method
645      */

646     public String JavaDoc getEndEntityProfileName(Admin admin, int id){
647       String JavaDoc returnval = null;
648       if(id == SecConst.EMPTY_ENDENTITYPROFILE)
649         return EMPTY_ENDENTITYPROFILENAME;
650       try{
651         returnval = (profiledatahome.findByPrimaryKey(new Integer JavaDoc(id))).getProfileName();
652       }catch(FinderException JavaDoc e){}
653
654       return returnval;
655     } // getEndEntityProfileName
656

657
658
659      /**
660      * Method to check if a certificateprofile exists in any of the end entity profiles. Used to avoid desyncronization of certificate profile data.
661      *
662      * @param certificateprofileid the certificatetype id to search for.
663      * @return true if certificateprofile exists in any of the end entity profiles.
664      * @ejb.transaction type="Supports"
665       * @ejb.interface-method
666      */

667     public boolean existsCertificateProfileInEndEntityProfiles(Admin admin, int certificateprofileid){
668       String JavaDoc[] availablecertprofiles=null;
669       boolean exists = false;
670       try{
671         Collection JavaDoc result = profiledatahome.findAll();
672         Iterator JavaDoc i = result.iterator();
673         while(i.hasNext() && !exists){
674           availablecertprofiles = ((EndEntityProfileDataLocal) i.next()).getProfile().getValue(EndEntityProfile.AVAILCERTPROFILES, 0).split(EndEntityProfile.SPLITCHAR);
675           for(int j=0; j < availablecertprofiles.length; j++){
676             if(Integer.parseInt(availablecertprofiles[j]) == certificateprofileid){
677               exists=true;
678               break;
679             }
680           }
681         }
682       }catch(FinderException JavaDoc e){}
683
684       return exists;
685     }
686
687      /**
688      * Method to check if a CA exists in any of the end entity profiles. Used to avoid desyncronization of CA data.
689      *
690      * @param caid the caid to search for.
691      * @return true if ca exists in any of the end entity profiles.
692      * @ejb.transaction type="Supports"
693       * @ejb.interface-method
694      */

695     public boolean existsCAInEndEntityProfiles(Admin admin, int caid){
696       String JavaDoc[] availablecas=null;
697       boolean exists = false;
698       try{
699         Collection JavaDoc result = profiledatahome.findAll();
700         Iterator JavaDoc i = result.iterator();
701         while(i.hasNext() && !exists){
702           availablecas = ((EndEntityProfileDataLocal) i.next()).getProfile().getValue(EndEntityProfile.AVAILCAS, 0).split(EndEntityProfile.SPLITCHAR);
703           for(int j=0; j < availablecas.length; j++){
704             if(Integer.parseInt(availablecas[j]) == caid){
705               exists=true;
706               break;
707             }
708           }
709         }
710       }catch(FinderException JavaDoc e){}
711
712       return exists;
713     } // existsCAProfileInEndEntityProfiles
714

715          /**
716      * Loads the global configuration from the database.
717      *
718      * @throws EJBException if a communication or other error occurs.
719      * @ejb.transaction type="Supports"
720      * @ejb.interface-method
721      */

722     public GlobalConfiguration loadGlobalConfiguration(Admin admin) {
723         debug(">loadGlobalConfiguration()");
724         if(globalconfiguration != null)
725           return globalconfiguration ;
726
727         GlobalConfiguration ret=null;
728         try{
729           GlobalConfigurationDataLocal gcdata = globalconfigurationhome.findByPrimaryKey("0");
730           if(gcdata!=null){
731             ret = gcdata.getGlobalConfiguration();
732           }
733         }catch (javax.ejb.FinderException JavaDoc fe) {
734              // Create new configuration
735
ret = new GlobalConfiguration();
736         }
737         debug("<loadGlobalConfiguration()");
738         return ret;
739     } //loadGlobalConfiguration
740

741     /**
742      * Sets the base url in the global configuration.
743      *
744      * @throws EJBException if a communication or other error occurs.
745      * @ejb.interface-method
746      */

747     public void initGlobalConfigurationBaseURL(Admin admin, String JavaDoc computername, String JavaDoc applicationpath) {
748         debug(">initGlobalConfigurationBaseURL()");
749         GlobalConfiguration gc = this.loadGlobalConfiguration(admin);
750         gc.setComputerName(computername);
751         gc.setApplicationPath(applicationpath);
752         this.saveGlobalConfiguration(admin, gc);
753         debug("<initGlobalConfigurationBaseURL()");
754      } // initGlobalConfigurationBaseURL
755

756     /**
757      * Saves the globalconfiguration
758      *
759      * @throws EJBException if a communication or other error occurs.
760      * @ejb.interface-method
761      */

762
763     public void saveGlobalConfiguration(Admin admin, GlobalConfiguration globalconfiguration) {
764         debug(">saveGlobalConfiguration()");
765         String JavaDoc pk = "0";
766         try {
767             GlobalConfigurationDataLocal gcdata = globalconfigurationhome.findByPrimaryKey(pk);
768             gcdata.setGlobalConfiguration(globalconfiguration);
769             String JavaDoc msg = intres.getLocalizedMessage("ra.savedconf", gcdata.getConfigurationId());
770             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_EDITSYSTEMCONFIGURATION,msg);
771         }catch (javax.ejb.FinderException JavaDoc fe) {
772             // Global configuration doesn't yet exists.
773
try{
774                 GlobalConfigurationDataLocal data1 = globalconfigurationhome.create(pk,globalconfiguration);
775                 String JavaDoc msg = intres.getLocalizedMessage("ra.createdconf", data1.getConfigurationId());
776                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_EDITSYSTEMCONFIGURATION, msg);
777             } catch(CreateException JavaDoc e){
778                 String JavaDoc msg = intres.getLocalizedMessage("ra.errorcreateconf");
779                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_EDITSYSTEMCONFIGURATION,msg);
780             }
781         }
782         this.globalconfiguration=globalconfiguration;
783         debug("<saveGlobalConfiguration()");
784     } // saveGlobalConfiguration
785

786
787
788     // Private methods
789

790     private int findFreeEndEntityProfileId(){
791       int id = (new Random JavaDoc((new Date JavaDoc()).getTime())).nextInt();
792       boolean foundfree = false;
793
794       while(!foundfree){
795         try{
796           if(id > 1)
797             profiledatahome.findByPrimaryKey(new Integer JavaDoc(id));
798           id++;
799         }catch(FinderException JavaDoc e){
800            foundfree = true;
801         }
802       }
803       return id;
804     } // findFreeEndEntityProfileId
805

806     private boolean isFreeEndEntityProfileId(int id) {
807             boolean foundfree = false;
808             try {
809                 if (id > 1) {
810                     profiledatahome.findByPrimaryKey(new Integer JavaDoc(id));
811                 }
812             } catch (FinderException JavaDoc e) {
813                 foundfree = true;
814             }
815             return foundfree;
816         } // isFreeEndEntityProfileId
817

818     /**
819      * Changes the admin preference in the database. Returns false if admin doesn't exist.
820      */

821     private boolean updateAdminPreference(Admin admin, String JavaDoc certificatefingerprint, AdminPreference adminpreference, boolean dolog){
822        debug(">updateAdminPreference(fingerprint : " + certificatefingerprint + ")");
823        boolean ret = false;
824         try {
825             adminpreferenceshome.findByPrimaryKey(certificatefingerprint);
826             adminpreferenceshome.remove(certificatefingerprint);
827             try{
828                 AdminPreferencesDataLocal apdata2 = adminpreferenceshome.findByPrimaryKey(certificatefingerprint);
829                 debug("Found admin preferences with id "+apdata2.getId());
830             } catch (javax.ejb.FinderException JavaDoc fe) {
831             }
832             adminpreferenceshome.create(certificatefingerprint,adminpreference);
833             try{
834                 AdminPreferencesDataLocal apdata3 = adminpreferenceshome.findByPrimaryKey(certificatefingerprint);
835                 debug("Found admin preferences with id "+apdata3.getId());
836             } catch (javax.ejb.FinderException JavaDoc fe) {
837             }
838             if (dolog) {
839                 String JavaDoc msg = intres.getLocalizedMessage("ra.changedadminpref", certificatefingerprint);
840                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_INFO_ADMINISTRATORPREFERENCECHANGED,msg);
841             }
842             ret = true;
843         } catch (javax.ejb.FinderException JavaDoc fe) {
844              ret=false;
845              if (dolog) {
846                  String JavaDoc msg = intres.getLocalizedMessage("ra.adminprefnotfound", certificatefingerprint);
847                  getLogSession().log(admin,admin.getCaId(), LogEntry.MODULE_RA, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_ADMINISTRATORPREFERENCECHANGED,msg);
848              }
849         } catch(Exception JavaDoc e){
850           throw new EJBException JavaDoc(e);
851         }
852         debug("<updateAdminPreference()");
853         return ret;
854     } // changeAdminPreference
855

856
857 } // LocalRaAdminSessionBean
858

859
Popular Tags