Add exec statistics processor & derby database
[jabaws.git] / webservices / compbio / ws / execstat / StatWriter.java
1 package compbio.ws.execstat;\r
2 \r
3 import java.sql.Connection;\r
4 import java.sql.DriverManager;\r
5 import java.sql.PreparedStatement;\r
6 import java.sql.SQLException;\r
7 import java.sql.Statement;\r
8 import java.sql.Timestamp;\r
9 \r
10 import compbio.ws.execstat.StatProcessor.JobStat;\r
11 \r
12 public class StatWriter {\r
13 \r
14         /* the default framework is embedded */\r
15         // private final String framework = "embedded";\r
16         private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";\r
17         private static final String protocol = "jdbc:derby:";\r
18         private static final String statDBName = "ExecutionStatistic";\r
19 \r
20         static Connection getDBConnection() throws SQLException {\r
21                 // TODO\r
22                 System.setProperty("derby.system.home", ".");\r
23 \r
24                 Connection conn = DriverManager.getConnection(protocol + statDBName\r
25                                 + ";create=true");\r
26 \r
27                 // We want to control transactions manually. Autocommit is on by\r
28                 // default in JDBC.\r
29                 conn.setAutoCommit(true);\r
30                 return conn;\r
31         }\r
32 \r
33         // ServiceName,jobname,start,finish,inputSize,resultSize,isCancelled,isCollected\r
34         static void createStatTable() throws SQLException {\r
35                 Connection conn = getDBConnection();\r
36                 /*\r
37                  * Creating a statement object that we can use for running various SQL\r
38                  * statements commands against the database.\r
39                  */\r
40                 Statement s = conn.createStatement();\r
41                 String create = "create table exec_stat("\r
42                                 + "number INT GENERATED ALWAYS AS IDENTITY,"\r
43                                 + "service_name VARCHAR(15) NOT NULL, "\r
44                                 + "job_id VARCHAR(35) NOT NULL PRIMARY KEY, "\r
45                                 + "start TIMESTAMP," + "finish TIMESTAMP,"\r
46                                 + "inputsize BIGINT," + "resultsize BIGINT,"\r
47                                 + "isCancelled SMALLINT NOT NULL,"\r
48                                 + "isCollected SMALLINT NOT NULL)";\r
49                 // We create a table...\r
50                 System.out.println(create);\r
51                 s.execute(create);\r
52                 s.close();\r
53                 conn.close();\r
54         }\r
55 \r
56         static void insertData(JobStat jobstatus) throws SQLException {\r
57                 Connection conn = getDBConnection();\r
58                 String insert = "insert into exec_stat (service_name, job_id, start, finish, "\r
59                                 + "inputsize, resultsize, isCancelled, isCollected) "\r
60                                 + "VALUES (?, ?, ?, ?, ?, ?, ?, ? )";\r
61                 PreparedStatement pstm = conn.prepareStatement(insert);\r
62 \r
63                 pstm.setString(1, "webservice");\r
64                 pstm.setString(2, "@Clustal#980273495452357");\r
65                 pstm.setTimestamp(3, new Timestamp(190385934834l));\r
66                 pstm.setTimestamp(4, new Timestamp(190332423423l));\r
67                 pstm.setLong(5, 1232);\r
68                 pstm.setLong(6, 1432422);\r
69                 pstm.setShort(7, (short) 1);\r
70                 pstm.setShort(8, (short) 0);\r
71                 pstm.executeUpdate();\r
72                 pstm.close();\r
73                 conn.close();\r
74         }\r
75 \r
76         void shutdownDBServer() {\r
77                 // ## DATABASE SHUTDOWN SECTION ##\r
78                 /***\r
79                  * In embedded mode, an application should shut down Derby. Shutdown\r
80                  * throws the XJ015 exception to confirm success.\r
81                  ***/\r
82                 boolean gotSQLExc = false;\r
83                 try {\r
84                         DriverManager.getConnection("jdbc:derby:;shutdown=true");\r
85                 } catch (SQLException se) {\r
86                         if (se.getSQLState().equals("XJ015")) {\r
87                                 gotSQLExc = true;\r
88                         }\r
89                 }\r
90                 if (!gotSQLExc) {\r
91                         System.out.println("Database did not shut down normally");\r
92                 } else {\r
93                         System.out.println("Database shut down normally");\r
94                 }\r
95         }\r
96         public static void main(String[] args) throws SQLException {\r
97                 // createStatTable();\r
98                 insertData(null);\r
99         }\r
100 }\r