| 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.idgen; |
| 13 | |
| 14 | import java.sql.DatabaseMetaData; |
| 15 | import java.sql.ResultSet; |
| 16 | import java.sql.SQLException; |
| 17 | import java.sql.Statement; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import javax.sql.DataSource; |
| 22 | |
| 23 | import org.apache.log4j.Logger; |
| 24 | |
| 25 | /* |
| 26 | * IdGeneratorInitializer contains logic used to create and drop the schema used |
| 27 | * for IdGenerators. |
| 28 | */ |
| 29 | public class IdGeneratorInitializer { |
| 30 | |
| 31 | private static final String SELECT_COUNT_FROM_TEMPLATE = "select count(*) from"; |
| 32 | |
| 33 | private static final String DROP_TABLE_TEMPLATE = "DROP TABLE"; |
| 34 | |
| 35 | private InitializingIdGenerator mGenerator; |
| 36 | Logger log = Logger |
| 37 | .getLogger(IdGeneratorInitializer.class); |
| 38 | |
| 39 | /** |
| 40 | * Creates a new IdGeneratorInitializer used for the given generator, |
| 41 | * pGenerator. |
| 42 | * |
| 43 | * @param pGenerator |
| 44 | */ |
| 45 | public IdGeneratorInitializer(InitializingIdGenerator pGenerator) { |
| 46 | mGenerator = pGenerator; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Creates a new schema for the current generator. If the schema exists, it's |
| 51 | * dropped and a new one is created. |
| 52 | * @throws SQLException |
| 53 | */ |
| 54 | public void initialize() throws SQLException { |
| 55 | if (tablesExist()) { |
| 56 | dropTables(); |
| 57 | } |
| 58 | initializeTables(); |
| 59 | } |
| 60 | |
| 61 | // -------------------------- |
| 62 | /** |
| 63 | * Drops the tables required for this component |
| 64 | * @throws SQLException |
| 65 | */ |
| 66 | void dropTables() throws SQLException { |
| 67 | executeUpdateStatement(DROP_TABLE_TEMPLATE + " " |
| 68 | + mGenerator.getTableName()); |
| 69 | } |
| 70 | |
| 71 | public Map<DataSource,DatabaseMetaData> mMetaDataMap = new HashMap<DataSource,DatabaseMetaData>(); |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Returns a cached instance of the DB metadata for the current connection |
| 76 | * @return |
| 77 | */ |
| 78 | DatabaseMetaData getDatabaseMetaData() { |
| 79 | DataSource ds = mGenerator.getDataSource(); |
| 80 | if (mMetaDataMap.get(ds) == null) |
| 81 | try { |
| 82 | mMetaDataMap.put(ds,ds.getConnection().getMetaData()); |
| 83 | } catch (SQLException e) { |
| 84 | // TODO Auto-generated catch block |
| 85 | e.printStackTrace(); |
| 86 | } |
| 87 | return mMetaDataMap.get(ds); |
| 88 | } |
| 89 | // -------------------------- |
| 90 | /** |
| 91 | * Returns true if the tables required for this component exist |
| 92 | * |
| 93 | * @return |
| 94 | */ |
| 95 | boolean tablesExist() { |
| 96 | String [] types = {"TABLE"}; |
| 97 | boolean exists = false; |
| 98 | try { |
| 99 | ResultSet rs = getDatabaseMetaData().getTables(null, null, mGenerator.getTableName(),types); |
| 100 | while (rs.next()) { |
| 101 | exists = true; |
| 102 | } |
| 103 | } catch (SQLException e) { |
| 104 | ; // eat it |
| 105 | } |
| 106 | return exists; |
| 107 | } |
| 108 | |
| 109 | // -------------------------- |
| 110 | /** |
| 111 | * Creates the table required for this component |
| 112 | * @throws SQLException |
| 113 | */ |
| 114 | void initializeTables() throws SQLException { |
| 115 | String statement = mGenerator.getCreateStatement(); |
| 116 | log.info("Creating IdGenerator tables : " + statement); |
| 117 | executeUpdateStatement(statement); |
| 118 | } |
| 119 | |
| 120 | // -------------------------- |
| 121 | /** |
| 122 | * @return TODO |
| 123 | * @throws SQLException |
| 124 | */ |
| 125 | private boolean executeUpdateStatement(String pStatement) throws SQLException { |
| 126 | boolean success = false; |
| 127 | Statement st = null; |
| 128 | try { |
| 129 | st = mGenerator.getDataSource().getConnection().createStatement(); // statements |
| 130 | int i = st.executeUpdate(pStatement); // run the query |
| 131 | if (i == -1) { |
| 132 | log.error("Error creating tables with statement" + pStatement); |
| 133 | } |
| 134 | success = true; |
| 135 | } finally { |
| 136 | try { |
| 137 | st.close(); |
| 138 | } catch (SQLException e) { |
| 139 | ; // eat it |
| 140 | } |
| 141 | } |
| 142 | return success; |
| 143 | } |
| 144 | } |