1 13 14 package org.ejbca.core.ejb.hardtoken; 15 16 import java.sql.Connection ; 17 import java.sql.PreparedStatement ; 18 import java.sql.ResultSet ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 22 import javax.ejb.CreateException ; 23 import javax.ejb.EJBException ; 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 106 public class LocalEjbcaHardTokenBatchJobSessionBean extends BaseSessionBean { 107 108 public static final int MAX_RETURNED_QUEUE_SIZE = 300; 109 110 111 private static final InternalResources intres = InternalResources.getInstance(); 112 113 114 private static final String USERDATA_COL = "username, subjectDN, subjectAltName, subjectEmail, status, type, clearpassword, timeCreated, timeModified, endEntityprofileId, certificateProfileId, tokenType, hardTokenIssuerId, cAId"; 115 116 117 private IHardTokenSessionLocal hardtokensession = null; 118 119 120 private ILogSessionLocal logsession = null; 121 122 123 124 128 129 public void ejbCreate() throws CreateException { 130 } 131 132 133 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 e){ 142 throw new EJBException (e); 143 } 144 } 145 return hardtokensession; 146 } 148 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 e){ 157 throw new EJBException (e); 158 } 159 } 160 return logsession; 161 } 163 164 165 174 public UserDataVO getNextHardTokenToGenerate(Admin admin, String 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 con = null; 184 ResultSet rs = null; 185 PreparedStatement ps = null; 186 187 try{ 188 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 rs = ps.executeQuery(); 199 200 202 if(rs.next()){ 203 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 (rs.getLong(8)), new java.util.Date (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 msg = intres.getLocalizedMessage("hardtoken.userdatasent", alias); 214 getLogSession().log(admin, returnval.getCAId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date (),returnval.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKEN_USERDATASENT, msg); 215 } 216 }catch(Exception e){ 217 String msg = intres.getLocalizedMessage("hardtoken.errorsenduserdata", alias); 218 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_HARDTOKEN_USERDATASENT, msg); 219 throw new EJBException (e); 220 } finally { 221 JDBCUtil.close(con, ps, rs); 222 } 223 } 224 225 debug("<getNextHardTokenToGenerate()"); 226 return returnval; 227 } 229 239 public Collection getNextHardTokensToGenerate(Admin admin, String alias) throws UnavailableTokenException{ 240 debug(">getNextHardTokensToGenerate()"); 241 ArrayList returnval = new ArrayList (); 242 int issuerid = getHardTokenSession().getHardTokenIssuerId(admin, alias); 243 244 if(issuerid != LocalHardTokenSessionBean.NO_ISSUER){ 245 ResultSet rs = null; 246 Connection con = null; 247 PreparedStatement ps = null; 248 try{ 249 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 while(rs.next() && returnval.size() <= MAX_RETURNED_QUEUE_SIZE){ 258 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 (rs.getLong(8)), new java.util.Date (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 msg = intres.getLocalizedMessage("hardtoken.userdatasent", alias); 267 getLogSession().log(admin, data.getCAId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date (),data.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKEN_USERDATASENT, msg); 268 } 269 }catch(Exception e){ 270 String msg = intres.getLocalizedMessage("hardtoken.errorsenduserdata", alias); 271 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_HARDTOKEN_USERDATASENT, msg); 272 throw new EJBException (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 } 285 286 296 public UserDataVO getNextHardTokenToGenerateInQueue(Admin admin, String 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 con = null; 303 PreparedStatement ps = null; 304 ResultSet rs = null; 305 try{ 306 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 if(rs.relative(index)){ 316 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 (rs.getLong(8)), new java.util.Date (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 msg = intres.getLocalizedMessage("hardtoken.userdatasent", alias); 326 getLogSession().log(admin, returnval.getCAId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date (),returnval.getUsername(), null, LogEntry.EVENT_INFO_HARDTOKEN_USERDATASENT, msg); 327 } 328 }catch(Exception e){ 329 String msg = intres.getLocalizedMessage("hardtoken.errorsenduserdata", alias); 330 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_HARDTOKEN, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_HARDTOKEN_USERDATASENT, msg); 331 throw new EJBException (e); 332 }finally{ 333 JDBCUtil.close(con, ps, rs); 334 } 335 } 336 debug("<getNextHardTokenToGenerateInQueue()"); 337 return returnval; 338 } 340 341 350 public int getNumberOfHardTokensToGenerate(Admin admin, String alias){ 351 debug(">getNumberOfHardTokensToGenerate()"); 352 int count = 0; 353 int issuerid = getHardTokenSession().getHardTokenIssuerId(admin, alias); 354 355 if(issuerid != LocalHardTokenSessionBean.NO_ISSUER){ 356 Connection con = null; 357 PreparedStatement ps = null; 358 ResultSet rs = null; 359 try{ 360 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 rs = ps.executeQuery(); 369 while(rs.next()){ 371 count = rs.getInt(1); 372 } 373 }catch(Exception e){ 374 throw new EJBException (e); 375 }finally{ 376 JDBCUtil.close(con, ps, rs); 377 } 378 } 379 debug("<getNumberOfHardTokensToGenerate()"); 380 return count; 381 } 383 391 public boolean checkForHardTokenIssuerId(Admin admin, int hardtokenissuerid){ 392 debug(">checkForHardTokenIssuerId(id: " + hardtokenissuerid + ")"); 393 Connection con = null; 394 PreparedStatement ps = null; 395 ResultSet rs = null; 396 int count = 1; 398 try{ 399 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 401 ps = con.prepareStatement("select COUNT(*) from UserData where hardTokenIssuerId=?"); 402 ps.setInt(1,hardtokenissuerid); 403 rs = ps.executeQuery(); 405 if(rs.next()){ 407 count = rs.getInt(1); 408 } 409 debug("<checkForHardTokenIssuerId()"); 410 return count > 0; 411 412 }catch(Exception e){ 413 throw new EJBException (e); 414 }finally{ 415 JDBCUtil.close(con, ps, rs); 416 } 417 } 419 420 421 } 423 | Popular Tags |