1 23 24 package com.sun.jdo.spi.persistence.support.ejb.ejbc; 25 26 import com.sun.enterprise.deployment.EjbBundleDescriptor; 27 import com.sun.enterprise.deployment.ResourceReferenceDescriptor; 28 import com.sun.enterprise.deployment.io.DescriptorConstants; 29 import com.sun.enterprise.deployment.backend.DeploymentEventInfo; 30 import com.sun.enterprise.deployment.backend.DeploymentStatus; 31 import com.sun.enterprise.server.Constants; 32 33 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.DeploymentHelper; 34 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper; 35 import com.sun.jdo.api.persistence.support.JDOFatalUserException; 36 import com.sun.jdo.spi.persistence.utility.I18NHelper; 37 38 import java.io.File ; 39 import java.io.IOException ; 40 import java.sql.Connection ; 41 import java.sql.Statement ; 42 import java.sql.SQLException ; 43 import java.util.Collection ; 44 import java.util.Iterator ; 45 46 52 public class CMPProcessor 53 extends BaseProcessor { 54 55 67 public CMPProcessor( 68 DeploymentEventInfo info, boolean deploy, boolean redeploy, 69 String cliCreateTables, String cliDropAndCreateTables, String cliDropTables) { 70 super(info, deploy, redeploy, cliCreateTables, 71 cliDropAndCreateTables, cliDropTables); 72 } 73 74 79 protected void processApplication() { 80 Collection bundleCollection = application.getEjbBundleDescriptors(); 81 if ( bundleCollection == null) { 82 return; 83 } 84 Iterator bundleItr = bundleCollection.iterator(); 85 86 while ( bundleItr.hasNext() ) { 88 processAppBundle((EjbBundleDescriptor)bundleItr.next()); 89 } 90 } 91 92 97 private void processAppBundle(EjbBundleDescriptor bundle) { 98 if (!bundle.containsCMPEntity()) { 99 return; 100 } 101 102 ResourceReferenceDescriptor cmpResource = 103 bundle.getCMPResourceReference(); 104 105 if (!DeploymentHelper.isJavaToDatabase( 108 cmpResource.getSchemaGeneratorProperties())) { 109 return; 110 } 111 112 boolean createTables = getCreateTablesValue(cmpResource) ; 113 boolean dropTables = getDropTablesValue(cmpResource); 114 115 if (debug) { 116 logger.fine("ejb.CMPProcessor.createanddroptables", new Object [] {new Boolean (createTables), new Boolean (dropTables)}); 118 } 119 120 if (!createTables && !dropTables) { 121 return; 123 } 124 125 setApplicationLocation(); 129 setGeneratedLocation(); 130 131 constructJdbcFileNames(bundle); 132 if (debug) { 133 logger.fine("ejb.CMPProcessor.createanddropfilenames", 134 createJdbcFileName, dropJdbcFileName); } 136 dropTablesFromDB(dropTables, cmpResource.getJndiName()); 137 createTablesInDB(createTables, cmpResource.getJndiName()); 138 139 } 140 141 153 protected boolean getCreateTablesValue( 154 ResourceReferenceDescriptor cmpResource) { 155 boolean createTables = 156 deploy 157 && (cliCreateTables.equals(Constants.TRUE) 158 || (cmpResource.isCreateTablesAtDeploy() 159 && cliCreateTables.equals(Constants.UNDEFINED))); 160 return createTables; 161 } 162 163 175 protected boolean getDropTablesValue( 176 ResourceReferenceDescriptor cmpResource) { 177 boolean dropTables = 178 (redeploy 179 && (cliDropAndCreateTables.equals(Constants.TRUE) 180 || (cmpResource.isDropTablesAtUndeploy() 181 && cliDropAndCreateTables.equals(Constants.UNDEFINED)))) 182 || (!deploy 183 && (cliDropTables.equals(Constants.TRUE) 184 || (cmpResource.isDropTablesAtUndeploy() 185 && cliDropTables.equals(Constants.UNDEFINED)))); 186 return dropTables; 187 } 188 189 198 private void constructJdbcFileNames(EjbBundleDescriptor ejbBundle) { 199 String filePrefix = EJBHelper.getDDLNamePrefix(ejbBundle); 200 201 createJdbcFileName = filePrefix + CREATE_DDL_JDBC_FILE_SUFFIX; 202 dropJdbcFileName = filePrefix + DROP_DDL_JDBC_FILE_SUFFIX; 203 } 204 205 212 private boolean dropTablesFromDB( 213 boolean dropTables, String resourceName) { 214 boolean dropResult = false; 215 File dropFile = null; 216 217 if(dropTables) { 218 dropFile = getDropDDLFile(dropJdbcFileName, deploy); 219 if (dropFile.exists()) { 220 dropResult = executeDDLStatement(dropFile, resourceName); 221 } else { 222 if (debug) { 223 logger.fine("ejb.BaseProcessor.cannotdroptables", dropFile.getName()); } 225 } 226 } 227 return dropResult; 228 } 229 230 237 private boolean createTablesInDB 238 (boolean createTables, String resourceName) { 239 boolean createResult = false; 240 File createFile = null; 241 242 if(createTables) { 243 createFile = getCreateDDLFile( 244 appGeneratedLocation + createJdbcFileName); 245 if(createFile.exists()) { 246 createResult = executeDDLStatement(createFile, resourceName); 247 } else { 248 if (debug) { 249 logger.fine("ejb.BaseProcessor.cannotcreatetables", createFile.getName()); } 251 } 252 } 253 return createResult; 254 } 255 256 265 private boolean executeDDLStatement(File fileName, String resourceName) { 266 boolean result = false; 267 Connection conn = null; 268 Statement sql = null; 269 try { 270 try { 271 conn = DeploymentHelper.getConnection(resourceName); 272 sql = conn.createStatement(); 273 result = true; 274 } catch (SQLException ex) { 275 cannotConnect(resourceName, ex); 276 } catch (JDOFatalUserException ex) { 277 cannotConnect(resourceName, ex); 278 } 279 280 if(result) { 281 executeDDLs(fileName, sql); 282 } 283 } catch (IOException e) { 284 fileIOError(application.getRegistrationName(), e); 285 } finally { 286 closeConn(conn); 287 } 288 return result; 289 } 290 291 } 292 | Popular Tags |