KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.UnmarshalException JavaDoc;
17
18 import org.ejbca.core.model.SecConst;
19 import org.ejbca.core.model.ca.caadmin.CAInfo;
20 import org.ejbca.core.model.ca.catoken.HardCATokenInfo;
21 import org.ejbca.core.model.ca.catoken.IHardCAToken;
22
23
24
25
26
27 /**
28  * Activates the specified HSM CA.
29  *
30  * @version $Id: CaActivateCACommand.java,v 1.3.10.1 2007/03/16 19:59:10 herrvendil Exp $
31  */

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

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

48     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
49         try {
50             if (args.length < 3) {
51                 getOutputStream().println("Usage: CA activateca <CA name> <authorization code>");
52                 return;
53             }
54
55             String JavaDoc caname = args[1];
56             String JavaDoc authorizationcode = args[2];
57                         
58             // Get the CAs info and id
59
CAInfo cainfo = getCAAdminSessionRemote().getCAInfo(administrator, caname);
60             if(cainfo == null){
61                 getOutputStream().println("Error: CA " + caname + " cannot be found");
62                 return;
63             }
64                                     
65             // Check that ca is and hardtoken ca
66

67             if(!(cainfo.getCATokenInfo() instanceof HardCATokenInfo)){
68                 getOutputStream().println("Error: CA have a Soft CAToken and cannot be activated");
69                 return;
70             }
71             
72             // Check that CA has correct status.
73
if(cainfo.getStatus() == SecConst.CA_OFFLINE ||
74                     (cainfo.getStatus() == SecConst.CA_ACTIVE && ((HardCATokenInfo)cainfo.getCATokenInfo()).getCATokenStatus() == IHardCAToken.STATUS_OFFLINE)) {
75                 try {
76                     getCAAdminSessionRemote().activateCAToken(administrator, cainfo.getCAId(), authorizationcode);
77                 } catch (UnmarshalException JavaDoc e) {
78                     // If we gat a classnotfound we are probably getting an error back from the token,
79
// with a class we don't have here at the CLI. It is probably invalid PIN
80
getOutputStream().println("Error returned, did you enter the correct PIN?");
81                     getOutputStream().println(e.getMessage());
82                 }
83             }else{
84                 getOutputStream().println("Error: CA or CAToken must be offline to be activated.");
85             }
86             
87  
88         } catch (Exception JavaDoc e) {
89             throw new ErrorAdminCommandException(e);
90         }
91     } // execute
92
}
93
Popular Tags