1 23 24 package com.sun.jdo.spi.persistence.support.ejb.ejbc; 25 26 import com.sun.enterprise.deployment.Application; 27 import com.sun.enterprise.deployment.backend.DeploymentEventInfo; 28 import com.sun.enterprise.deployment.backend.DeploymentRequest; 29 import com.sun.enterprise.deployment.backend.DeploymentStatus; 30 import com.sun.enterprise.deployment.backend.IASDeploymentException; 31 import com.sun.enterprise.util.io.FileUtils; 32 33 import com.sun.jdo.spi.persistence.utility.I18NHelper; 34 import com.sun.jdo.spi.persistence.utility.database.DatabaseConstants; 35 import com.sun.jdo.spi.persistence.utility.logging.Logger; 36 37 import java.io.BufferedReader ; 38 import java.io.File ; 39 import java.io.FileReader ; 40 import java.io.IOException ; 41 import java.sql.Connection ; 42 import java.sql.Statement ; 43 import java.sql.SQLException ; 44 import java.util.logging.Level ; 45 import java.util.ResourceBundle ; 46 47 51 abstract public class BaseProcessor 52 implements DatabaseConstants { 53 54 55 protected static final Logger logger = LogHelperEJBCompiler.getLogger(); 56 57 58 protected final static ResourceBundle messages = I18NHelper.loadBundle( 59 "com.sun.jdo.spi.persistence.support.ejb.ejbc.Bundle", BaseProcessor.class.getClassLoader()); 61 62 protected Application application; 63 protected DeploymentEventInfo info; 64 protected DeploymentRequest request; 65 protected DeploymentStatus status; 66 67 70 protected boolean deploy; 71 protected boolean redeploy; 72 protected String cliCreateTables; 73 protected String cliDropAndCreateTables; 74 protected String cliDropTables; 75 78 protected String appRegisteredName; 79 80 protected String appDeployedLocation; 81 protected String appGeneratedLocation; 82 85 protected String createJdbcFileName; 86 89 protected String dropJdbcFileName; 90 91 protected boolean debug = logger.isLoggable(logger.FINE); 92 93 105 public BaseProcessor(DeploymentEventInfo info, 106 boolean deploy, boolean redeploy, String cliCreateTables, 107 String cliDropAndCreateTables, String cliDropTables) { 108 initializeVariables(info, deploy, redeploy, cliCreateTables, 109 cliDropAndCreateTables, cliDropTables); 110 } 111 112 private void initializeVariables( 113 DeploymentEventInfo info, boolean deploy, 114 boolean redeploy, String cliCreateTables, 115 String cliDropAndCreateTables, String cliDropTables) { 116 this.info = info; 117 this.application = this.info.getApplicationDescriptor(); 118 this.appRegisteredName = this.application.getRegistrationName(); 119 this.status = 120 this.info.getDeploymentRequest().getCurrentDeploymentStatus(); 121 122 this.deploy = deploy; 123 this.redeploy = redeploy; 124 this.cliCreateTables = cliCreateTables; 125 this.cliDropAndCreateTables = cliDropAndCreateTables; 126 this.cliDropTables = cliDropTables; 127 } 128 129 abstract protected void processApplication(); 130 131 132 137 protected File getCreateDDLFile( String createJdbcFileName) { 138 File createFile = null; 139 try { 140 createFile = new File (createJdbcFileName); 141 if (!createFile.exists()) { 142 logI18NWarnMessage( 143 "ejb.BaseProcessor.cannotcreatetables", 144 appRegisteredName, createFile.getName(), null); 145 } 146 147 if (debug) { 148 logger.fine("ejb.BaseProcessor.createfilename", createFile.getName()); 150 } 151 } catch (Exception e) { 152 logI18NWarnMessage( 153 "Exception caught in BaseProcessor.getCreateDDLFile()", 154 appRegisteredName, null, e); 155 } 156 return createFile; 157 } 158 159 165 protected File getDropDDLFile(String dropJdbcFileName, boolean deploy) { 166 File dropFile = null; 167 try { 168 169 File dir = (deploy)? info.getOldStubsDir() : info.getStubsDir(); 170 if (dir == null) { 171 dir = info.getStubsDir(); 173 } 174 if(null != dropJdbcFileName) { 175 dropFile = new File (dir, dropJdbcFileName); 176 } 177 if(!dropFile.exists()) { 178 logI18NWarnMessage( 179 "ejb.BaseProcessor.cannotdroptables", 180 appRegisteredName, dropFile.getName(), null); 181 } 182 183 if (debug) { 184 logger.fine("ejb.BaseProcessor.dropfilename", dropFile.getName()); 186 } 187 188 } catch (Exception e) { 189 logI18NWarnMessage( 190 "Exception caught in BaseProcessor.getDropDDLFile()", 191 appRegisteredName, null, e); 192 } 193 return dropFile; 194 } 195 196 203 protected void executeDDLs(File f, Statement sql) 204 throws IOException { 205 206 BufferedReader reader = null; 207 StringBuffer warningBuf = new StringBuffer (); 208 209 try { 210 reader = new BufferedReader (new FileReader (f)); 211 String s; 212 while ((s = reader.readLine()) != null) { 213 try { 214 if (debug) { 215 logger.fine("ejb.BaseProcessor.executestatement", s); } 217 sql.execute(s); 218 219 } catch(SQLException ex) { 220 String msg = getI18NMessage("ejb.BaseProcessor.sqlexception", 221 s, null, ex); 222 logger.warning(msg); 223 warningBuf.append("\n\t").append(msg); } 225 } 226 } finally { 227 if (reader != null) { 228 try { 229 reader.close(); 230 } catch(IOException ex) { 231 } 233 } 234 if (warningBuf.length() > 0) { 235 String warning = 236 getI18NMessage("ejb.BaseProcessor.tablewarning"); 237 warnUser(warning + warningBuf.toString()); 238 } 239 } 240 } 241 242 248 protected void warnUser(String msg) { 249 status.setStageStatus(DeploymentStatus.WARNING); 250 status.setStageStatusMessage( 251 status.getStageStatusMessage() + "\n" + msg); } 253 254 262 protected void cannotConnect(String connName, 263 Throwable ex) { 264 logI18NWarnMessage( "ejb.BaseProcessor.cannotConnect", 265 connName, null, ex); 266 } 267 268 protected void fileIOError(String regName, 269 Throwable ex) { 270 logI18NWarnMessage("ejb.BaseProcessor.ioexception", 271 regName, null, ex); 272 } 273 274 279 protected static void closeConn(Connection conn) { 280 if (conn != null) { 281 try { 282 conn.close(); 283 } catch(SQLException ex) { 284 } 286 } 287 } 288 289 protected void logI18NInfoMessage( 290 String errorCode, String regName, 291 String fileName, Throwable ex) { 292 String msg = getI18NMessage(errorCode, 293 regName, fileName, ex); 294 logger.info(msg); 295 } 296 297 protected void logI18NWarnMessage( 298 String errorCode, String regName, 299 String fileName, Throwable ex) { 300 String msg = getI18NMessage(errorCode, 301 regName, fileName, ex); 302 logger.warning(msg); 303 warnUser(msg); 304 } 305 306 311 protected String getI18NMessage(String errorCode) { 312 return getI18NMessage( 313 errorCode, null, null, null); 314 } 315 protected String getI18NMessage( 316 String errorCode, String regName, 317 String fileName, Throwable ex) { 318 String msg = null; 319 if(null != ex) 320 msg = I18NHelper.getMessage( 321 messages, errorCode, regName, ex.toString()); 322 else if(null != fileName ) 323 msg = I18NHelper.getMessage( 324 messages, errorCode, regName, fileName); 325 else 326 msg = I18NHelper.getMessage(messages, errorCode); 327 328 return msg; 329 } 330 331 332 337 protected void setApplicationLocation() { 338 if(null != this.appDeployedLocation) 339 return; 340 341 this.appDeployedLocation = 342 info.getDeploymentRequest().getDeployedDirectory().getAbsolutePath() 343 + File.separator; 344 } 345 346 352 protected void setGeneratedLocation() { 353 if(null != this.appGeneratedLocation) 354 return; 355 this.appGeneratedLocation = 356 info.getStubsDir().getAbsolutePath() + File.separator; 357 } 358 } 359 | Popular Tags |