1 /*
2 * Copyright (c) 2003, Henri Yandell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * + Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * + Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * + Neither the name of OSJava nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package org.osjava.oscube.service.store;
33
34 import java.util.Iterator;
35 import javax.sql.DataSource;
36 import java.sql.*;
37 import org.apache.commons.dbutils.DbUtils;
38 import org.apache.commons.dbutils.QueryRunner;
39 import org.apache.commons.lang.StringUtils;
40
41 import org.apache.log4j.Logger;
42
43 import com.generationjava.config.Config;
44 import org.osjava.oscube.container.Session;
45 import org.osjava.oscube.container.Header;
46 import org.osjava.oscube.container.Result;
47
48 public class JdbcStore implements Store {
49
50 private static Logger logger = Logger.getLogger(JdbcStore.class);
51
52 public void store(Result result, Config cfg, Session session) throws StoringException {
53 Connection conn = null;
54 try {
55 // get DataSource from cfg
56 String dsname = cfg.getString("jdbc.DS");
57 // System.err.println("DSNAME: "+dsname);
58 // System.err.println("/DSNAME: "+cfg.getAbsolute(dsname));
59 DataSource ds = (DataSource)cfg.getAbsolute(dsname);
60 // System.err.println("DataSource: "+ds);
61 conn = ds.getConnection();
62 // System.err.println("CONN: "+conn);
63 String sql = cfg.getString("jdbc.sql");
64 // System.err.println("SQL: "+sql);
65 String table = cfg.getString("jdbc.table");
66 // System.err.println("Table: "+table);
67 if(sql == null) {
68 if(table == null) {
69 throw new StoringException(cfg.getContext()+".jdbc.sql or "+cfg.getContext()+".jdbc.table must be specified. ");
70 }
71 }
72
73 QueryRunner queryRunner = new QueryRunner();
74 Iterator iterator = result.iterateRows();
75 while(iterator.hasNext()) {
76 Object[] row = (Object[])iterator.next();
77 if(row.length == 0) {
78 logger.error("Empty row found. Skipping. ");
79 continue;
80 }
81 if(sql == null) {
82 if(table != null) {
83 sql = "INSERT INTO " + table + " VALUES(?"+ StringUtils.repeat(", ?", row.length-1) + ")";
84 }
85 }
86 int count = executeSql( conn, sql, row, queryRunner );
87 }
88
89 } catch(SQLException sqle) {
90 throw new StoringException("JDBC Storing Error: "+sqle.getMessage(), sqle);
91 } finally {
92 DbUtils.closeQuietly( conn );
93 }
94 }
95
96 protected int executeSql(Connection conn, String sql, Object[] row, QueryRunner queryRunner ) throws SQLException {
97 return queryRunner.update( conn, sql, row );
98 }
99
100 public boolean exists(Header header, Config cfg, Session session) throws StoringException {
101 // need to write some kind of checking in here
102 return false;
103 }
104 }