KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCStopCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DatabaseMetaData JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30 import javax.transaction.Transaction JavaDoc;
31 import javax.transaction.TransactionManager JavaDoc;
32
33 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractCMRFieldBridge;
34 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractEntityBridge;
35 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityMetaData;
36 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCRelationMetaData;
37 import org.jboss.logging.Logger;
38
39
40 /**
41  * JDBCStopCommand drops the table for this entity if specified in the xml.
42  *
43  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
44  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
45  * @author <a HREF="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
46  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
47  * @version $Revision: 37459 $
48  */

49 public final class JDBCStopCommand
50 {
51    private final JDBCEntityPersistenceStore manager;
52    private final JDBCAbstractEntityBridge entity;
53    private final JDBCEntityMetaData entityMetaData;
54    private final Logger log;
55
56    public JDBCStopCommand(JDBCEntityPersistenceStore manager)
57    {
58       this.manager = manager;
59       entity = manager.getEntityBridge();
60       entityMetaData = entity.getMetaData();
61
62       // Create the Log
63
log = Logger.getLogger(
64          this.getClass().getName() +
65          "." +
66          manager.getMetaData().getName());
67    }
68
69    public boolean execute()
70    {
71       boolean success = true;
72
73       // drop relation tables
74
JDBCAbstractCMRFieldBridge[] cmrFields = entity.getCMRFields();
75       for(int i = 0; i < cmrFields.length; ++i)
76       {
77          JDBCAbstractCMRFieldBridge cmrField = cmrFields[i];
78          JDBCRelationMetaData relationMetaData = cmrField.getMetaData().getRelationMetaData();
79          if(relationMetaData.isTableMappingStyle() && !relationMetaData.isTableDropped())
80          {
81             if(relationMetaData.getRemoveTable())
82             {
83                final boolean dropped = dropTable(relationMetaData.getDataSource(), cmrField.getQualifiedTableName());
84                if(!dropped)
85                {
86                   success = false;
87                }
88                else
89                {
90                   relationMetaData.setTableDropped();
91                }
92             }
93          }
94       }
95
96       if(entityMetaData.getRemoveTable())
97       {
98          boolean dropped = dropTable(entity.getDataSource(), entity.getQualifiedTableName());
99          if(!dropped)
100          {
101             success = false;
102          }
103       }
104
105       return success;
106    }
107
108    private boolean dropTable(DataSource JavaDoc dataSource, String JavaDoc qualifiedTableName)
109    {
110       boolean success;
111       Connection JavaDoc con = null;
112       ResultSet JavaDoc rs = null;
113
114       String JavaDoc schema = SQLUtil.getSchema(qualifiedTableName);
115       String JavaDoc tableName = schema != null ? SQLUtil.getTableNameWithoutSchema(qualifiedTableName) : qualifiedTableName;
116
117       // was the table already delete?
118
try
119       {
120          con = dataSource.getConnection();
121          DatabaseMetaData JavaDoc dmd = con.getMetaData();
122          rs = dmd.getTables(con.getCatalog(), schema, tableName, null);
123          if(!rs.next())
124          {
125             return true;
126          }
127       }
128       catch(SQLException JavaDoc e)
129       {
130          log.debug("Error getting database metadata for DROP TABLE command. " +
131             " DROP TABLE will not be executed. ", e);
132          return true;
133       }
134       finally
135       {
136          JDBCUtil.safeClose(rs);
137          JDBCUtil.safeClose(con);
138       }
139
140       // since we use the pools, we have to do this within a transaction
141

142       // suspend the current transaction
143
TransactionManager JavaDoc tm = manager.getContainer().getTransactionManager();
144       Transaction JavaDoc oldTransaction = null;
145       try
146       {
147          oldTransaction = tm.suspend();
148       }
149       catch(Exception JavaDoc e)
150       {
151          log.error("Could not suspend current transaction before drop table. " +
152             "'" + qualifiedTableName + "' will not be dropped.", e);
153       }
154
155       try
156       {
157          Statement JavaDoc statement = null;
158          try
159          {
160             con = dataSource.getConnection();
161             statement = con.createStatement();
162
163             // execute sql
164
String JavaDoc sql = SQLUtil.DROP_TABLE + qualifiedTableName;
165             log.debug("Executing SQL: " + sql);
166             statement.executeUpdate(sql);
167             success = true;
168          }
169          finally
170          {
171             JDBCUtil.safeClose(statement);
172             JDBCUtil.safeClose(con);
173          }
174       }
175       catch(Exception JavaDoc e)
176       {
177          log.debug("Could not drop table " + qualifiedTableName + ": " + e.getMessage());
178          success = false;
179       }
180       finally
181       {
182          try
183          {
184             // resume the old transaction
185
if(oldTransaction != null)
186             {
187                tm.resume(oldTransaction);
188             }
189          }
190          catch(Exception JavaDoc e)
191          {
192             log.error("Could not reattach original transaction after drop table");
193          }
194       }
195
196       return success;
197    }
198 }
199
Popular Tags