KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.XMLEncoder JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.ejbca.core.model.SecConst;
22 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile;
23 import org.ejbca.core.model.ra.raadmin.EndEntityProfile;
24
25
26
27 /**
28  * Export profiles from the databse to XML-files.
29  *
30  * @version $Id: CaExportProfilesCommand.java,v 1.1 2006/01/17 20:28:05 anatom Exp $
31  */

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

38     public CaExportProfilesCommand(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             Collection JavaDoc certprofids = getCertificateStoreSession().getAuthorizedCertificateProfileIds(administrator,0);
51             Collection JavaDoc endentityprofids = getRaAdminSession().getAuthorizedEndEntityProfileIds(administrator);
52
53             if (args.length < 2) {
54                 getOutputStream().println(
55                     "Usage: CA exportprofiles <outpath>");
56                 getOutputStream().print("\n");
57                 return;
58             }
59
60             String JavaDoc outpath = args[1];
61
62             getOutputStream().println("Exporting certificate profiles: ");
63             Iterator JavaDoc iter = certprofids.iterator();
64             while (iter.hasNext()) {
65                 int profileid = ((Integer JavaDoc) iter.next()).intValue();
66                 if (profileid == SecConst.PROFILE_NO_PROFILE) { // Certificate profile not found i database.
67
getOutputStream().println("Error : Couldn't find certificate profile '"+profileid+"' in database.");
68                 } else {
69                     String JavaDoc profilename = getCertificateStoreSession().getCertificateProfileName(administrator, profileid);
70                     CertificateProfile profile = getCertificateStoreSession().getCertificateProfile(administrator,profileid);
71                     if (profile == null) {
72                         getOutputStream().println("Error : Couldn't find certificate profile '"+profilename+"'-"+profileid+" in database.");
73                     } else {
74                         String JavaDoc outfile = outpath+"/certprofile_"+profilename+"-"+profileid+".xml";
75                         getOutputStream().println(outfile+".");
76                         XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc(new FileOutputStream JavaDoc(outfile));
77                         encoder.writeObject(profile.saveData());
78                         encoder.close();
79                     }
80                 }
81             }
82
83             getOutputStream().println("Exporting end entity profiles: ");
84             iter = endentityprofids.iterator();
85             while (iter.hasNext()){
86                 int profileid = ((Integer JavaDoc) iter.next()).intValue();
87                 if (profileid == SecConst.PROFILE_NO_PROFILE) { // Entity profile not found i database.
88
getOutputStream().println("Error : Couldn't find entity profile '"+profileid+"' in database.");
89                 } else {
90                     String JavaDoc profilename = getRaAdminSession().getEndEntityProfileName(administrator, profileid);
91                     EndEntityProfile profile = getRaAdminSession().getEndEntityProfile(administrator, profileid);
92                     if (profile == null) {
93                         getOutputStream().println("Error : Couldn't find entity profile '"+profilename+"'-"+profileid+" in database.");
94                     } else {
95                         String JavaDoc outfile = outpath+"/entityprofile_"+profilename+"-"+profileid+".xml";
96                         getOutputStream().println(outfile+".");
97                         XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc(new FileOutputStream JavaDoc(outfile));
98                         encoder.writeObject(profile.saveData());
99                         encoder.close();
100                     }
101                 }
102             }
103         } catch (Exception JavaDoc e) {
104             throw new ErrorAdminCommandException(e);
105         }
106     }
107 }
108
Popular Tags