KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.interfaces.RSAPublicKey JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 import org.ejbca.core.model.ca.caadmin.CAInfo;
21 import org.ejbca.util.CertTools;
22
23
24
25 /**
26  * Gets and prints info about the CA.
27  *
28  * @version $Id: CaInfoCommand.java,v 1.3 2006/12/15 15:07:10 anatom Exp $
29  */

30 public class CaInfoCommand extends BaseCaAdminCommand {
31     /**
32      * Creates a new instance of CaInfoCommand
33      *
34      * @param args command line arguments
35      */

36     public CaInfoCommand(String JavaDoc[] args) {
37         super(args);
38     }
39
40     /**
41      * Runs the command
42      *
43      * @throws IllegalAdminCommandException Error in command args
44      * @throws ErrorAdminCommandException Error running command
45      */

46     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
47         if (args.length < 2) {
48            String JavaDoc msg = "Usage: CA info <caname>";
49            throw new IllegalAdminCommandException(msg);
50         }
51         try {
52             String JavaDoc caname = args[1];
53             ArrayList JavaDoc chain = new ArrayList JavaDoc(getCertChain(caname));
54             CAInfo cainfo = getCAInfo(caname);
55                                     
56             getOutputStream().println("CA name: " + caname);
57             getOutputStream().println("CA ID: " + cainfo.getCAId());
58             getOutputStream().println("CA CRL Expiration Period: " + cainfo.getCRLPeriod());
59             getOutputStream().println("CA CRL Issue Interval: " + cainfo.getCRLIssueInterval());
60             getOutputStream().println("CA Description: " + cainfo.getDescription());
61             getOutputStream().println("\n");
62             
63             if (chain.size() < 2)
64               getOutputStream().println("This is a Root CA.");
65             else
66               getOutputStream().println("This is a subordinate CA.");
67               
68               getOutputStream().println("Size of chain: " + chain.size());
69             if (chain.size() > 0) {
70                 X509Certificate JavaDoc rootcert = (X509Certificate JavaDoc)chain.get(chain.size()-1);
71                 getOutputStream().println("Root CA DN: "+CertTools.getSubjectDN(rootcert));
72                 getOutputStream().println("Root CA id: "+CertTools.getSubjectDN(rootcert).hashCode());
73                 getOutputStream().println("Certificate valid from: "+rootcert.getNotBefore().toString());
74                 getOutputStream().println("Certificate valid to: "+rootcert.getNotAfter().toString());
75                 getOutputStream().println("Root CA keysize: "+((RSAPublicKey JavaDoc)rootcert.getPublicKey()).getModulus().bitLength());
76                 for(int i = chain.size()-2; i>=0; i--){
77                     X509Certificate JavaDoc cacert = (X509Certificate JavaDoc)chain.get(i);
78                     getOutputStream().println("CA DN: "+CertTools.getSubjectDN(cacert));
79                     getOutputStream().println("Certificate valid from: "+cacert.getNotBefore().toString());
80                     getOutputStream().println("Certificate valid to: "+cacert.getNotAfter().toString());
81                     getOutputStream().println("CA keysize: "+((RSAPublicKey JavaDoc)cacert.getPublicKey()).getModulus().bitLength());
82
83                 }
84             }
85         } catch (Exception JavaDoc e) {
86             throw new ErrorAdminCommandException(e);
87         }
88     } // execute
89
}
Popular Tags