001
002 /*
003 * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004 *
005 * $Id: SchemaUpdatingDataSource.java 205 2012-01-06 22:43:05Z archie.cobbs $
006 */
007
008 package org.dellroad.stuff.schema;
009
010 import java.sql.SQLException;
011
012 import javax.sql.DataSource;
013
014 /**
015 * A {@link DataSource} that wraps an inner {@link DataSource} and automatically intializes and updates
016 * the database schema using a {@link SQLSchemaUpdater} on first access.
017 *
018 * @see SQLSchemaUpdater
019 */
020 public class SchemaUpdatingDataSource extends AbstractUpdatingDataSource {
021
022 private SQLSchemaUpdater schemaUpdater;
023
024 /**
025 * Configure the {@link SQLSchemaUpdater} that will initialize and update the database. Required property.
026 */
027 public void setSchemaUpdater(SQLSchemaUpdater schemaUpdater) {
028 this.schemaUpdater = schemaUpdater;
029 }
030
031 @Override
032 protected void updateDataSource(DataSource dataSource) throws SQLException {
033 if (this.schemaUpdater == null)
034 throw new IllegalArgumentException("no SchemaUpdater configured");
035 try {
036 this.schemaUpdater.initializeAndUpdateDatabase(dataSource);
037 } catch (RuntimeException e) {
038 if (e.getMessage() == null && e.getCause() instanceof SQLException) // unwrap checked exception
039 throw (SQLException)e.getCause();
040 throw e;
041 }
042 }
043 }
044