KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > cli > CaListExpiredCommand


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.ui.cli;
15
16 import java.security.cert.X509Certificate JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import javax.naming.Context JavaDoc;
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 /**
29  * List certificates that will expire within the given number of days.
30  *
31  * @version $Id: CaListExpiredCommand.java,v 1.1 2006/01/17 20:28:05 anatom Exp $
32  */

33 public class CaListExpiredCommand extends BaseCaAdminCommand {
34     /**
35      * Creates a new instance of CaListExpiredCommand
36      *
37      * @param args command line arguments
38      */

39     public CaListExpiredCommand(String JavaDoc[] args) {
40         super(args);
41     }
42
43     /**
44      * Runs the command
45      *
46      * @throws IllegalAdminCommandException Error in command args
47      * @throws ErrorAdminCommandException Error running command
48      */

49     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
50         if (args.length < 2) {
51             String JavaDoc 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 JavaDoc findDate = new Date JavaDoc();
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 JavaDoc certs = getExpiredCerts(findDate);
64             Iterator JavaDoc iter = certs.iterator();
65
66             while (iter.hasNext()) {
67                 X509Certificate JavaDoc xcert = (X509Certificate JavaDoc) iter.next();
68                 Date JavaDoc retDate = xcert.getNotAfter();
69                 String JavaDoc subjectDN = CertTools.getSubjectDN(xcert);
70                 String JavaDoc serNo = xcert.getSerialNumber().toString();
71                 getOutputStream().println("Certificate with subjectDN '" + subjectDN +
72                     "' and serialNumber '" + serNo + "' expires at " + retDate + ".");
73             }
74         } catch (Exception JavaDoc e) {
75             throw new ErrorAdminCommandException(e);
76         }
77     }
78
79     // execute
80
private Collection JavaDoc getExpiredCerts(Date JavaDoc findDate) {
81         try {
82             Context JavaDoc 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 JavaDoc certs = store.findCertificatesByExpireTime(administrator, findDate);
89             debug("Found " + certs.size() + " certs.");
90
91             return certs;
92         } catch (Exception JavaDoc e) {
93             error("Error getting list of certificates", e);
94         }
95
96         return null;
97     }
98
99     // getExpiredCerts
100
}
101
Popular Tags