1
10package org.jstk.hks;
11
12import java.util.HashMap;
13import java.util.Properties;
14import java.io.FileInputStream;
15import java.io.ByteArrayOutputStream;
16import java.io.File;
17import java.sql.DriverManager;
18import java.sql.Connection;
19import java.sql.Statement;
20import java.sql.PreparedStatement;
21import java.sql.SQLException;
22import org.jstk.*;
23
24public class AddKSCommand extends JSTKCommandAdapter {
25 private static HashMap defaults = new HashMap();
26 static {
27 defaults.put("dbprops", "lib/db.properties");
28 defaults.put("type", "KS");
29 }
30
31 public String briefDescription(){
32 String briefDesc = "initializes database for Hosted Key Stores";
33 return briefDesc;
34 }
35
36 public String optionsDescription(){
37 String optionsDesc =
38 " -dbprops <file> : Property file to read database parameters.[" +
39 defaults.get("dbprops") + "]\n" +
40 " -username <user>: User name.\n" +
41 " -password <pass>: Password to authenticate the user.\n" +
42 " -type <type> : (KS|TS).[" +
43 defaults.get("type") + "]\n" +
44 " -file <filename>: KeyStore or TrustStore file.\n";
45 return optionsDesc;
46 }
47 public String[] useForms(){
48 String[] useForms = {
49 "[-dbprops <file>] -username <user> -password <pass> -file <filename> [-type <type>]"
50 };
51 return useForms;
52 }
53 public String[] sampleUses(){
54 String[] sampleUses = {
55 "-username u1 -password p1 -file test.ks",
56 "-username u1 -password p1 -file test.ks -dbprops test.props -type TS"
57 };
58 return sampleUses;
59 }
60
61 public Object execute(JSTKArgs args) throws JSTKException{
62 try {
63 args.setDefaults(defaults);
64 String dbpropFile = args.get("dbprops");
65 String type = args.get("type");
66 String username = args.get("username");
67 String password = args.get("password");
68 String ksFile = args.get("file");
69
70 if (username == null){
71 return new JSTKResult(null, true, "Username not set. User -username option.");
72 }
73
74 if (!type.equals("KS") && !type.equals("TS")){
75 return new JSTKResult(null, true, "Invalid type: " + type);
76 }
77
78 FileInputStream fis = new FileInputStream(dbpropFile);
79 Properties dbprops = new Properties();
80 dbprops.load(fis);
81
82 File f = new File(ksFile);
83 int nbytes = (int)f.length();
84 FileInputStream ksfis = new FileInputStream(ksFile);
85
86 String jdbcDriver = dbprops.getProperty("jdbcdriver");
87 String dburl = dbprops.getProperty("dburl");
88 String dbuser = dbprops.getProperty("dbuser");
89 String dbpass = dbprops.getProperty("dbpass");
90 if (dbpass == null)
91 dbpass = "";
92
93 Class.forName(jdbcDriver);
94 Connection con = DriverManager.getConnection(dburl, dbuser, dbpass);
95
96 Statement stmt = con.createStatement();
97 String inscmd = "INSERT INTO HKSTABLE(USERID) VALUES('" + username + "')";
98 try {
99 stmt.executeUpdate(inscmd);
00 } catch (SQLException sqle){
01 }
04
05 String updcmd = "UPDATE HKSTABLE SET " + type + " = ? WHERE USERID LIKE '" + username + "'";
06 PreparedStatement stmt1 = con.prepareStatement(updcmd);
07 stmt1.setBinaryStream(1, ksfis, nbytes);
08 stmt1.executeUpdate();
09 con.close();
10
11 return new JSTKResult(null, true, "Updated Entry: " + username + ", " + ksFile);
12 } catch (Exception exc){
13 throw new JSTKException("AddKSCommand execution failed", exc);
14 }
15 }
16
17 public static void main(String[] args) throws Exception {
18 JSTKOptions opts = new JSTKOptions();
19 opts.parse(args, 0);
20 AddKSCommand addCmd = new AddKSCommand();
21 JSTKResult result = (JSTKResult)addCmd.execute(opts);
22 System.out.println(result.getText());
23 System.exit(result.isSuccess()? 0 : 1);
24 }
25}
26