| 1 | /** |
| 2 | * Copyright 2009 ATG DUST Project Licensed under the Apache License, Version |
| 3 | * 2.0 (the "License"); you may not use this file except in compliance with the |
| 4 | * License. You may obtain a copy of the License at |
| 5 | * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law |
| 6 | * or agreed to in writing, software distributed under the License is |
| 7 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 8 | * KIND, either express or implied. See the License for the specific language |
| 9 | * governing permissions and limitations under the License. |
| 10 | */ |
| 11 | |
| 12 | package atg.service.jdbc; |
| 13 | |
| 14 | import java.sql.Connection; |
| 15 | import java.sql.SQLException; |
| 16 | import java.sql.Statement; |
| 17 | import java.util.Properties; |
| 18 | |
| 19 | import atg.nucleus.ServiceException; |
| 20 | import atg.test.util.DBUtils; |
| 21 | |
| 22 | /** |
| 23 | * This datasource is used for testing. It starts up an HSQLDB in memory |
| 24 | * instance on localhost automatically. The database will be named "testdb" by |
| 25 | * default. If you need to name it something else set the "databaseName" |
| 26 | * property on this component. You may want to change the name if your test |
| 27 | * requires running two databases at the same time. |
| 28 | * |
| 29 | * @author adamb |
| 30 | * @version $Id:n HSQLDB |
| 31 | * //test/UnitTests/base/main/src/Java/atg/service/jdbc/HSQLDBDataSource |
| 32 | * .java#2 $ |
| 33 | */ |
| 34 | public class HSQLDBDataSource extends InitializingDataSourceBase { |
| 35 | |
| 36 | // Don't shutdown HSQLDB by default. It might stop before other components |
| 37 | // that require it. |
| 38 | public boolean mShutdownHSQLDB = false; |
| 39 | |
| 40 | /** |
| 41 | * Returns true if the "SHUTDOWN" sql statment should be sent to HSQLDB |
| 42 | * when doStopService is called on this component. |
| 43 | * @return |
| 44 | */ |
| 45 | public boolean isShutdownHSQLDB() { |
| 46 | return mShutdownHSQLDB; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Sets the boolean which controls if HSQLDB should be shutdown when doStopService |
| 51 | * is called on this component. |
| 52 | * @param shouldShutdownHSQLDB |
| 53 | */ |
| 54 | public void setShutdownHSQLDB(boolean shouldShutdownHSQLDB) { |
| 55 | mShutdownHSQLDB = shouldShutdownHSQLDB; |
| 56 | } |
| 57 | |
| 58 | // -------------------------- |
| 59 | /** |
| 60 | * Starts this DataSource. Since the datasource uses an in memory HSQL |
| 61 | * database, the database actually is started on the first call to |
| 62 | * getConnection(). |
| 63 | */ |
| 64 | @Override |
| 65 | public void doStartService() throws ServiceException { |
| 66 | Properties props = DBUtils.getHSQLDBInMemoryDBConnection(getName()); |
| 67 | // set our properties from this object |
| 68 | this.setDriver(props.getProperty("driver")); |
| 69 | this.setURL(props.getProperty("URL")); |
| 70 | this.setUser(props.getProperty("user")); |
| 71 | this.setPassword(props.getProperty("password")); |
| 72 | if (isLoggingInfo()) |
| 73 | logInfo("HSQLDB DataSource starting with properties " + props.toString()); |
| 74 | super.doStartService(); |
| 75 | } |
| 76 | |
| 77 | // -------------------------- |
| 78 | /** |
| 79 | * Called when Nucleus is shutdown. Issues the "SHUTDOWN" command to the |
| 80 | * HSQLDB database. |
| 81 | */ |
| 82 | @Override |
| 83 | public void doStopService() { |
| 84 | if (mShutdownHSQLDB) { |
| 85 | if (isLoggingInfo()) |
| 86 | logInfo("HSQLDB DataSource shutting down."); |
| 87 | Connection connection = null; |
| 88 | try { |
| 89 | connection = this.getDriverManagerConnection(); |
| 90 | Statement st = connection.createStatement(); |
| 91 | st.execute("SHUTDOWN"); |
| 92 | } catch (SQLException e) { |
| 93 | if (isLoggingError()) |
| 94 | logError(e.getMessage()); |
| 95 | } finally { |
| 96 | if (connection != null) { |
| 97 | try { |
| 98 | connection.close(); |
| 99 | } catch (SQLException e) { |
| 100 | ; // eat it |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | } |