1 13 14 package org.ejbca.ui.cli; 15 16 import java.security.cert.X509Certificate ; 17 import java.util.Collection ; 18 import java.util.Date ; 19 import java.util.Iterator ; 20 21 import javax.naming.Context ; 22 23 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionHome; 24 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionRemote; 25 import org.ejbca.util.CertTools; 26 27 28 33 public class CaListExpiredCommand extends BaseCaAdminCommand { 34 39 public CaListExpiredCommand(String [] args) { 40 super(args); 41 } 42 43 49 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 50 if (args.length < 2) { 51 String msg = "List certificates that will expire within the given number of days.\n"; 52 msg += "Usage: CA listexpired <days>"; 53 throw new IllegalAdminCommandException(msg); 54 } 55 56 try { 57 long days = Long.parseLong(args[1]); 58 Date findDate = new Date (); 59 long millis = (days * 24 * 60 * 60 * 1000); 60 findDate.setTime(findDate.getTime() + millis); 61 getOutputStream().println("Looking for certificates that expire before " + findDate + "."); 62 63 Collection certs = getExpiredCerts(findDate); 64 Iterator iter = certs.iterator(); 65 66 while (iter.hasNext()) { 67 X509Certificate xcert = (X509Certificate ) iter.next(); 68 Date retDate = xcert.getNotAfter(); 69 String subjectDN = CertTools.getSubjectDN(xcert); 70 String serNo = xcert.getSerialNumber().toString(); 71 getOutputStream().println("Certificate with subjectDN '" + subjectDN + 72 "' and serialNumber '" + serNo + "' expires at " + retDate + "."); 73 } 74 } catch (Exception e) { 75 throw new ErrorAdminCommandException(e); 76 } 77 } 78 79 private Collection getExpiredCerts(Date findDate) { 81 try { 82 Context ctx = getInitialContext(); 83 ICertificateStoreSessionHome storehome = (ICertificateStoreSessionHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup( 84 "CertificateStoreSession"), ICertificateStoreSessionHome.class); 85 ICertificateStoreSessionRemote store = storehome.create(); 86 debug("Looking for cert with expireDate=" + findDate); 87 88 Collection certs = store.findCertificatesByExpireTime(administrator, findDate); 89 debug("Found " + certs.size() + " certs."); 90 91 return certs; 92 } catch (Exception e) { 93 error("Error getting list of certificates", e); 94 } 95 96 return null; 97 } 98 99 } 101 | Popular Tags |