KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
17 import java.sql.PreparedStatement JavaDoc;
18 import java.sql.ResultSet JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import javax.ejb.CreateException JavaDoc;
23 import javax.ejb.EJBException JavaDoc;
24
25 import org.ejbca.core.ejb.BaseSessionBean;
26 import org.ejbca.core.ejb.JNDINames;
27 import org.ejbca.core.ejb.log.ILogSessionLocal;
28 import org.ejbca.core.ejb.log.ILogSessionLocalHome;
29 import org.ejbca.core.model.InternalResources;
30 import org.ejbca.core.model.SecConst;
31 import org.ejbca.core.model.hardtoken.UnavailableTokenException;
32 import org.ejbca.core.model.log.Admin;
33 import org.ejbca.core.model.log.LogEntry;
34 import org.ejbca.core.model.ra.UserDataConstants;
35 import org.ejbca.core.model.ra.UserDataVO;
36 import org.ejbca.util.JDBCUtil;
37
38
39
40 /**
41  * Remote interface for bean used by hardtoken batchprograms to retrieve users to generate from EJBCA RA.
42  *
43  * @ejb.bean
44  * description="Session bean handling userdata queue for hard token issuers"
45  * display-name="HardTokenBatchJobSessionSB"
46  * name="HardTokenBatchJobSession"
47  * jndi-name="HardTokenBatchJobSession"
48  * local-jndi-name="HardTokenBatchJobSessionLocal"
49  * view-type="both"
50  * type="Stateless"
51  * transaction-type="Container"
52  *
53  * @ejb.transaction type="Required"
54  *
55  * @weblogic.enable-call-by-reference True
56  *
57  * @ejb.env-entry
58  * description="The JDBC datasource to be used"
59  * name="DataSource"
60  * type="java.lang.String"
61  * value="${datasource.jndi-name-prefix}${datasource.jndi-name}"
62  *
63  * @ejb.home
64  * extends="javax.ejb.EJBHome"
65  * local-extends="javax.ejb.EJBLocalHome"
66  * local-class="org.ejbca.core.ejb.hardtoken.IHardTokenBatchJobSessionLocalHome"
67  * remote-class="org.ejbca.core.ejb.hardtoken.IHardTokenBatchJobSessionHome"
68  *
69  * @ejb.interface
70  * extends="javax.ejb.EJBObject"
71  * local-extends="javax.ejb.EJBLocalObject"
72  * local-class="org.ejbca.core.ejb.hardtoken.IHardTokenBatchJobSessionLocal"
73  * remote-class="org.ejbca.core.ejb.hardtoken.IHardTokenBatchJobSessionRemote"
74  *
75  * @ejb.ejb-external-ref
76  * description="The User entity bean"
77  * view-type="local"
78  * ref-name="ejb/UserDataLocal"
79  * type="Entity"
80  * home="org.ejbca.core.ejb.ra.UserDataLocalHome"
81  * business="org.ejbca.core.ejb.ra.UserDataLocal"
82  * link="UserData"
83  *
84  * @ejb.ejb-external-ref
85  * description="The Certificate Store session bean"
86  * view-type="local"
87  * ref-name="ejb/HardTokenSessionLocal"
88  * type="Session"
89  * home="org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocalHome"
90  * business="org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocal"
91  * link="HardTokenSession"
92  *
93  * @ejb.ejb-external-ref
94  * description="The log session bean"
95  * view-type="local"
96  * ref-name="ejb/LogSessionLocal"
97  * type="Session"
98  * home="org.ejbca.core.ejb.log.ILogSessionLocalHome"
99  * business="org.ejbca.core.ejb.log.ILogSessionLocal"
100  * link="LogSession"
101  *
102  * @jonas.bean
103  * ejb-name="HardTokenSession"
104  *
105  */

106 public class LocalEjbcaHardTokenBatchJobSessionBean extends BaseSessionBean {
107
108     public static final int MAX_RETURNED_QUEUE_SIZE = 300;
109
110     /** Internal localization of logs and errors */
111     private static final InternalResources intres = InternalResources.getInstance();
112     
113     /** Columns in the database used in select */
114     private static final String JavaDoc USERDATA_COL = "username, subjectDN, subjectAltName, subjectEmail, status, type, clearpassword, timeCreated, timeModified, endEntityprofileId, certificateProfileId, tokenType, hardTokenIssuerId, cAId";
115
116     /** The local interface of hard token session bean */
117     private IHardTokenSessionLocal hardtokensession = null;
118
119     /** The remote interface of log session bean */
120     private ILogSessionLocal logsession = null;
121
122
123
124     /**
125      * Default create for SessionBean without any creation Arguments.
126      * @throws CreateException if bean instance can't be created
127      */

128
129     public void ejbCreate() throws CreateException JavaDoc {
130     }
131
132
133     /** Gets connection to hard token session bean
134      * @return IHardTokenSessionLocal
135      */

136     private IHardTokenSessionLocal getHardTokenSession() {
137         if(hardtokensession == null){
138           try{
139             IHardTokenSessionLocalHome hardtokensessionhome = (IHardTokenSessionLocalHome) getLocator().getLocalHome(IHardTokenSessionLocalHome.COMP_NAME);
140             hardtokensession = hardtokensessionhome.create();
141           }catch(Exception JavaDoc e){
142              throw new EJBException JavaDoc(e);
143           }
144         }
145         return hardtokensession;
146     } //getHardTokenSession
147

148     /** Gets connection to log session bean
149      * @return Connection
150      */

151     private ILogSessionLocal getLogSession() {
152         if(logsession == null){
153           try{
154             ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME);
155             logsession = logsessionhome.create();
156           }catch(Exception JavaDoc e){
157              throw new EJBException JavaDoc(e);
158           }
159         }
160         return logsession;
161     } //getLogSession
162

163
164
165     /**
166      * Returns the next user scheduled for batch generation for the given issuer.
167      *
168      * @param admin the administrator performing the actions
169      *
170      * @return The next user to generate or NULL if there are no users i queue.
171      * @throws EJBException if a communication or other error occurs.
172      * @ejb.interface-method view-type="both"
173      */

174     public UserDataVO getNextHardTokenToGenerate(Admin admin, String JavaDoc alias) throws UnavailableTokenException{
175       debug(">getNextHardTokenToGenerate()");
176       debug("alias " + alias);
177       UserDataVO returnval=null;
178       int issuerid = getHardTokenSession().getHardTokenIssuerId(admin, alias);
179
180       debug("issuerid " + issuerid);
181
182       if(issuerid != LocalHardTokenSessionBean.NO_ISSUER){
183         Connection JavaDoc con = null;
184         ResultSet JavaDoc rs = null;
185         PreparedStatement JavaDoc ps = null;
186
187         try{
188            // Construct SQL query.
189
debug("HERE");
190             con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
191             ps = con.prepareStatement("select " + USERDATA_COL + " from UserData where hardTokenIssuerId=? and tokenType>? and (status=? or status=?)" );
192             ps.setInt(1,issuerid);
193             ps.setInt(2,SecConst.TOKEN_SOFT);
194             ps.setInt(3,UserDataConstants.STATUS_NEW);
195             ps.setInt(4,UserDataConstants.STATUS_KEYRECOVERY);
196
197             // Execute query.
198
rs = ps.executeQuery();
199
200             // Assemble result.
201

202            if(rs.next()){
203               // TODO add support for Extended Information
204
returnval = new UserDataVO(rs.getString(1), rs.getString(2), rs.getInt(14), rs.getString(3), rs.getString(4), rs.getInt(5), rs.getInt(6)
205                                                , rs.getInt(10), rs.getInt(11)
206                                                , new java.util.Date JavaDoc(rs.getLong(8)), new java.util.Date JavaDoc(rs.getLong(9))
207                                                , rs.getInt(12), rs.getInt(13),null);
208               returnval.setPassword(rs.getString(7));
209               debug("found user" + returnval.getUsername());
210             }
211             if(returnval !=null){
212               getHardTokenSession().getIsHardTokenProfileAvailableToIssuer(admin, issuerid, returnval);
213               String JavaDoc msg = intres.getLocalizedMessage("hardtoken.userdatasent", alias);
214               getLogSession().log(admin, returnval.getCAId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),returnval.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKEN_USERDATASENT, msg);
215             }
216         }catch(Exception JavaDoc e){
217             String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorsenduserdata", alias);
218             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKEN_USERDATASENT, msg);
219             throw new EJBException JavaDoc(e);
220         } finally {
221             JDBCUtil.close(con, ps, rs);
222         }
223       }
224
225       debug("<getNextHardTokenToGenerate()");
226       return returnval;
227     }// getNextHardTokenToGenerate
228

229     /**
230      * Returns a Collection of users scheduled for batch generation for the given issuer.
231      * A maximum of MAX_RETURNED_QUEUE_SIZE users will be returned by call.
232      *
233      * @param admin the administrator performing the actions
234      *
235      * @return A Collection of users to generate or NULL if there are no users i queue.
236      * @throws EJBException if a communication or other error occurs.
237      * @ejb.interface-method view-type="both"
238      */

239     public Collection JavaDoc getNextHardTokensToGenerate(Admin admin, String JavaDoc alias) throws UnavailableTokenException{
240       debug(">getNextHardTokensToGenerate()");
241       ArrayList JavaDoc returnval = new ArrayList JavaDoc();
242       int issuerid = getHardTokenSession().getHardTokenIssuerId(admin, alias);
243
244       if(issuerid != LocalHardTokenSessionBean.NO_ISSUER){
245         ResultSet JavaDoc rs = null;
246         Connection JavaDoc con = null;
247         PreparedStatement JavaDoc ps = null;
248         try{
249            // Construct SQL query.
250
con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
251             ps = con.prepareStatement("select " + USERDATA_COL + " from UserData where hardTokenIssuerId=? and tokenType>? and (status=? or status=?)" );
252             ps.setInt(1,issuerid);
253             ps.setInt(2,SecConst.TOKEN_SOFT);
254             ps.setInt(3,UserDataConstants.STATUS_NEW);
255             ps.setInt(4,UserDataConstants.STATUS_KEYRECOVERY);
256             // Assemble result.
257
while(rs.next() && returnval.size() <= MAX_RETURNED_QUEUE_SIZE){
258               // TODO add support for Extended Information
259
UserDataVO data = new UserDataVO(rs.getString(1), rs.getString(2), rs.getInt(14), rs.getString(3), rs.getString(4), rs.getInt(5), rs.getInt(6)
260                                                , rs.getInt(10), rs.getInt(11)
261                                                , new java.util.Date JavaDoc(rs.getLong(8)), new java.util.Date JavaDoc(rs.getLong(9))
262                                                , rs.getInt(12), rs.getInt(13), null);
263               data.setPassword(rs.getString(7));
264               getHardTokenSession().getIsHardTokenProfileAvailableToIssuer(admin, issuerid, data);
265               returnval.add(data);
266               String JavaDoc msg = intres.getLocalizedMessage("hardtoken.userdatasent", alias);
267               getLogSession().log(admin, data.getCAId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),data.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKEN_USERDATASENT, msg);
268             }
269         }catch(Exception JavaDoc e){
270             String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorsenduserdata", alias);
271             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKEN_USERDATASENT, msg);
272             throw new EJBException JavaDoc(e);
273         }finally{
274            JDBCUtil.close(con, ps, rs);
275         }
276       }
277
278       if(returnval.size()==0)
279         returnval=null;
280
281       debug("<getNextHardTokensToGenerate()");
282       return returnval;
283     }// getNextHardTokensToGenerate
284

285
286     /**
287      * Returns the indexed user in queue scheduled for batch generation for the given issuer.
288      *
289      * @param admin the administrator performing the actions
290      * @param index index in queue of user to retrieve.
291      *
292      * @return The next token to generate or NULL if the given user doesn't exist in queue.
293      * @throws EJBException if a communication or other error occurs.
294      * @ejb.interface-method view-type="both"
295      */

296     public UserDataVO getNextHardTokenToGenerateInQueue(Admin admin, String JavaDoc alias, int index) throws UnavailableTokenException{
297       debug(">getNextHardTokenToGenerateInQueue()");
298       UserDataVO returnval=null;
299       int issuerid = getHardTokenSession().getHardTokenIssuerId(admin, alias);
300
301       if(issuerid != LocalHardTokenSessionBean.NO_ISSUER){
302         Connection JavaDoc con = null;
303         PreparedStatement JavaDoc ps = null;
304         ResultSet JavaDoc rs = null;
305         try{
306            // Construct SQL query.
307
con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
308             ps = con.prepareStatement("select " + USERDATA_COL + " from UserData where hardTokenIssuerId=? and tokenType>? and (status=? or status=?)" );
309             ps.setInt(1,issuerid);
310             ps.setInt(2,SecConst.TOKEN_SOFT);
311             ps.setInt(3,UserDataConstants.STATUS_NEW);
312             ps.setInt(4,UserDataConstants.STATUS_KEYRECOVERY);
313
314             // Assemble result.
315
if(rs.relative(index)){
316               // TODO add support for Extended Information
317
returnval = new UserDataVO(rs.getString(1), rs.getString(2), rs.getInt(14), rs.getString(3), rs.getString(4), rs.getInt(5), rs.getInt(6)
318                                                , rs.getInt(10), rs.getInt(11)
319                                                , new java.util.Date JavaDoc(rs.getLong(8)), new java.util.Date JavaDoc(rs.getLong(9))
320                                                , rs.getInt(12), rs.getInt(13), null);
321               returnval.setPassword(rs.getString(7));
322             }
323             if(returnval !=null){
324               getHardTokenSession().getIsHardTokenProfileAvailableToIssuer(admin, issuerid, returnval);
325               String JavaDoc msg = intres.getLocalizedMessage("hardtoken.userdatasent", alias);
326               getLogSession().log(admin, returnval.getCAId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),returnval.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKEN_USERDATASENT, msg);
327             }
328         }catch(Exception JavaDoc e){
329             String JavaDoc msg = intres.getLocalizedMessage("hardtoken.errorsenduserdata", alias);
330             getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_HARDTOKEN_USERDATASENT, msg);
331             throw new EJBException JavaDoc(e);
332         }finally{
333            JDBCUtil.close(con, ps, rs);
334         }
335       }
336       debug("<getNextHardTokenToGenerateInQueue()");
337       return returnval;
338     }// getNextHardTokenToGenerateInQueue
339

340
341     /**
342      * Returns the number of users scheduled for batch generation for the given issuer.
343      *
344      * @param admin the administrator performing the actions
345      *
346      * @return the number of users to generate.
347      * @throws EJBException if a communication or other error occurs.
348      * @ejb.interface-method view-type="both"
349      */

350     public int getNumberOfHardTokensToGenerate(Admin admin, String JavaDoc alias){
351       debug(">getNumberOfHardTokensToGenerate()");
352       int count = 0;
353       int issuerid = getHardTokenSession().getHardTokenIssuerId(admin, alias);
354
355       if(issuerid != LocalHardTokenSessionBean.NO_ISSUER){
356         Connection JavaDoc con = null;
357         PreparedStatement JavaDoc ps = null;
358         ResultSet JavaDoc rs = null;
359         try{
360            // Construct SQL query.
361
con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
362             ps = con.prepareStatement("select COUNT(*) from UserData where hardTokenIssuerId=? and tokenType>? and (status=? or status=?)");
363             ps.setInt(1,issuerid);
364             ps.setInt(2,SecConst.TOKEN_SOFT);
365             ps.setInt(3,UserDataConstants.STATUS_NEW);
366             ps.setInt(4,UserDataConstants.STATUS_KEYRECOVERY);
367             // Execute query.
368
rs = ps.executeQuery();
369             // Assemble result.
370
while(rs.next()){
371               count = rs.getInt(1);
372             }
373         }catch(Exception JavaDoc e){
374           throw new EJBException JavaDoc(e);
375         }finally{
376            JDBCUtil.close(con, ps, rs);
377         }
378       }
379       debug("<getNumberOfHardTokensToGenerate()");
380       return count;
381     }// getNumberOfHardTokensToGenerate
382

383     /**
384      * Methods that checks if a user exists in the database having the given hard token issuer id. This function is mainly for avoiding
385      * desyncronisation when a hard token issuer is deleted.
386      *
387      * @param hardtokenissuerid the id of hard token issuer to look for.
388      * @return true if hardtokenissuerid exists in userdatabase.
389      * @ejb.interface-method view-type="both"
390      */

391     public boolean checkForHardTokenIssuerId(Admin admin, int hardtokenissuerid){
392         debug(">checkForHardTokenIssuerId(id: " + hardtokenissuerid + ")");
393         Connection JavaDoc con = null;
394         PreparedStatement JavaDoc ps = null;
395         ResultSet JavaDoc rs = null;
396         int count = 1; // return true as default.
397

398         try{
399            // Construct SQL query.
400
con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);
401             ps = con.prepareStatement("select COUNT(*) from UserData where hardTokenIssuerId=?");
402             ps.setInt(1,hardtokenissuerid);
403             // Execute query.
404
rs = ps.executeQuery();
405             // Assemble result.
406
if(rs.next()){
407               count = rs.getInt(1);
408             }
409             debug("<checkForHardTokenIssuerId()");
410             return count > 0;
411
412         }catch(Exception JavaDoc e){
413           throw new EJBException JavaDoc(e);
414         }finally{
415            JDBCUtil.close(con, ps, rs);
416         }
417     } // checkForHardTokenIssuerId
418

419
420
421 } // LocalRaAdminSessionBean
422

423
Popular Tags