1
10package org.jstk.hks;
11
12import java.util.HashMap;
13import java.util.Properties;
14import java.io.FileInputStream;
15import java.sql.DriverManager;
16import java.sql.Connection;
17import java.sql.Statement;
18import org.jstk.*;
19
20public class InitHKSCommand extends JSTKCommandAdapter {
21 private static HashMap defaults = new HashMap();
22 static {
23 defaults.put("dbprops", "config/db.properties");
24 defaults.put("username", "sa");
25 defaults.put("password", "sa");
26 }
27
28 public String briefDescription(){
29 String briefDesc = "initializes database for Hosted Key Stores";
30 return briefDesc;
31 }
32
33 public String optionsDescription(){
34 String optionsDesc =
35 " -dbprops <file> : Property file to read database parameters.[" +
36 defaults.get("dbprops") + "]\n";
37 return optionsDesc;
38 }
39 public String[] useForms(){
40 String[] useForms = {
41 "[-dbprops <file>]"
42 };
43 return useForms;
44 }
45 public String[] sampleUses(){
46 String[] sampleUses = {
47 "",
48 "-dbprops test.props"
49 };
50 return sampleUses;
51 }
52
53 public Object execute(JSTKArgs args) throws JSTKException{
54 try {
55 args.setDefaults(defaults);
56 String dbpropFile = args.get("dbprops");
57 String username = args.get("username");
58 String password = args.get("password");
59
60 FileInputStream fis = new FileInputStream(dbpropFile);
61 Properties dbprops = new Properties();
62 dbprops.load(fis);
63
64 String jdbcDriver = dbprops.getProperty("jdbcdriver");
65 String dburl = dbprops.getProperty("dburl");
66 String dbuser = dbprops.getProperty("dbuser");
67 String dbpass = dbprops.getProperty("dbpass");
68 String crcmd = dbprops.getProperty("crcmd");
69
70 Class.forName(jdbcDriver);
71 if (dbpass == null)
72 dbpass = "";
73 Connection con = DriverManager.getConnection(dburl, dbuser, dbpass);
74 Statement stmt = con.createStatement();
75
76 stmt.executeUpdate(crcmd);
77 con.close();
78
79 return new JSTKResult(null, true, "HKS Database \"" + dburl + "\" initialized.");
80 } catch (Exception exc){
81 throw new JSTKException("InitHKSCommand execution failed", exc);
82 }
83 }
84
85 public static void main(String[] args) throws Exception {
86 JSTKOptions opts = new JSTKOptions();
87 opts.parse(args, 0);
88 InitHKSCommand initCmd = new InitHKSCommand();
89 JSTKResult result = (JSTKResult)initCmd.execute(opts);
90 System.out.println(result.getText());
91 System.exit(result.isSuccess()? 0 : 1);
92 }
93}
94