View Javadoc

1   package org.jmux.app.component;
2   
3   import org.apache.commons.dbcp.ConnectionFactory;
4   import org.apache.commons.dbcp.DriverManagerConnectionFactory;
5   import org.apache.commons.dbcp.PoolableConnectionFactory;
6   import org.apache.commons.dbcp.PoolingDataSource;
7   import org.apache.commons.dbutils.QueryRunner;
8   import org.apache.commons.pool.KeyedObjectPoolFactory;
9   import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
10  import org.apache.commons.pool.impl.GenericObjectPool;
11  import org.jmux.Log;
12  import org.jmux.app.App;
13  import org.jmux.app.component.config.Config;
14  
15  import javax.sql.DataSource;
16  
17  /******************************************************************************
18   jmux - Java Modules Using XML
19   Copyright © 2006 jmux.org
20  
21   This library is free software; you can redistribute it and/or
22   modify it under the terms of the GNU Lesser General Public
23   License as published by the Free Software Foundation; either
24   version 2.1 of the License, or (at your option) any later version.
25  
26   This library is distributed in the hope that it will be useful,
27   but WITHOUT ANY WARRANTY; without even the implied warranty of
28   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29   Lesser General Public License for more details.
30  
31   You should have received a copy of the GNU Lesser General Public
32   License along with this library; if not, write to the Free Software
33   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34   *****************************************************************************/
35  
36  /***
37   * @author donaldw
38   */
39  public class Persister extends AbstractControl {
40  
41      // Load the
42      static {
43          try {
44              Class.forName("org.hsqldb.jdbcDriver");
45          } catch (ClassNotFoundException e) {
46              Log.error("JDBC Driver not found", e);
47              System.exit(-1);
48          }
49      }
50  
51      private String dbName;
52      private PoolingDataSource dataSource;
53      private QueryRunner queryRunner;
54      private GenericObjectPool connectionPool;
55  
56      public void configure(Config config, App app) {
57          super.configure(config, app);
58          dbName = config.getString("database", "database");
59      }
60  
61      public void start() {
62          ConnectionFactory connectionFactory =
63                  new DriverManagerConnectionFactory("jdbc:hsqldb:"
64                          + dbName,    // filenames
65                          "sa",                     // username
66                          "");
67  
68          connectionPool = new GenericObjectPool();
69          KeyedObjectPoolFactory stmtPool = new GenericKeyedObjectPoolFactory(null);
70          new PoolableConnectionFactory(connectionFactory, connectionPool, stmtPool, null, false, true);
71          dataSource = new PoolingDataSource(connectionPool);
72          queryRunner = new QueryRunner(dataSource);
73      }
74  
75      public DataSource getDataSource() {
76          return dataSource;
77      }
78  
79      public QueryRunner getQueryRunner() {
80          return queryRunner;
81      }
82  
83      public void stop() {
84          /***
85           * Write out to files and perform clean shuts down
86           * otherwise there might be an unclean shutdown
87           * when program ends.
88           */
89          try {
90              queryRunner.update("shutdown");
91              connectionPool.close();
92              connectionPool = null;
93              dataSource = null;
94              queryRunner = null;
95          } catch (Exception ex) {
96              logError("Error closing database: " + ex);
97          }
98      }
99  }