1 23 24 package com.sun.ejb.codegen; 25 26 import java.io.File ; 27 import java.util.Vector ; 28 import java.util.Iterator ; 29 import java.util.Collection ; 30 31 import java.util.logging.Logger ; 32 import java.util.logging.Level ; 33 34 import com.sun.logging.LogDomains; 35 import com.sun.enterprise.deployment.EjbDescriptor; 36 import com.sun.enterprise.deployment.Application; 37 import com.sun.enterprise.deployment.EjbBundleDescriptor; 38 import com.sun.enterprise.deployment.EjbCMPEntityDescriptor; 39 40 import com.sun.enterprise.util.io.FileUtils; 41 import com.sun.enterprise.util.i18n.StringManager; 42 import com.sun.enterprise.deployment.runtime.IASEjbExtraDescriptors; 43 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor; 44 import com.sun.enterprise.deployment.runtime.IASPersistenceManagerDescriptor; 45 46 47 53 class CmpCompiler { 54 55 60 CmpCompiler(EjbcContext ctx) { 61 this._ejbcCtx = ctx; 62 } 63 64 70 void compile() throws CmpCompilerException, GeneratorException { 71 72 Application application = null; 74 75 EjbBundleDescriptor bundle = null; 77 78 String beanName = null; 80 81 StringBuffer generatorExceptionMsg = null; 83 84 try { 85 long time; 87 88 File stubsDir = this._ejbcCtx.getStubsDir(); 90 91 application = this._ejbcCtx.getDescriptor(); 92 93 _logger.log(Level.FINE, "ejbc.processing_cmp", 94 application.getRegistrationName()); 95 96 Vector cmpFiles = new Vector (); 97 final ClassLoader jcl = application.getClassLoader(); 98 99 Iterator bundleItr = 101 application.getEjbBundleDescriptors().iterator(); 102 103 while ( bundleItr.hasNext() ) { 104 105 bundle = (EjbBundleDescriptor)bundleItr.next(); 106 107 if (!bundle.containsCMPEntity()) { 108 continue; 109 } 110 111 String archiveUri = (!application.isVirtual()) ? 114 this._ejbcCtx.getSrcDir().getCanonicalPath() 115 + File.separator+getModuleDirName(bundle) : 116 this._ejbcCtx.getSrcDir().getCanonicalPath(); 117 118 if (com.sun.enterprise.util.logging.Debug.enabled) { 119 _logger.log(Level.FINE,"[CMPC] Module Dir name is " 120 + archiveUri); 121 } 122 123 String generatedXmlsPath = (!application.isVirtual()) ? 124 this._ejbcCtx.getDeploymentRequest(). 125 getGeneratedXMLDirectory().getCanonicalPath() 126 + File.separator + getModuleDirName(bundle) : 127 this._ejbcCtx.getDeploymentRequest(). 128 getGeneratedXMLDirectory().getCanonicalPath(); 129 130 if (com.sun.enterprise.util.logging.Debug.enabled) { 131 _logger.log(Level.FINE,"[CMPC] Generated XML Dir name is " 132 + generatedXmlsPath); 133 } 134 135 IASPersistenceManagerDescriptor pmDesc = 136 bundle.getPreferredPersistenceManager(); 137 138 String generatorName = null; 139 if (null == pmDesc) { 140 generatorName = IASPersistenceManagerDescriptor.PM_CLASS_GENERATOR_DEFAULT; 141 142 } else { 143 generatorName = pmDesc.getPersistenceManagerClassGenerator(); 144 145 if (generatorName.equals( 148 IASPersistenceManagerDescriptor.PM_CLASS_GENERATOR_DEFAULT_OLD)) { 149 150 generatorName = IASPersistenceManagerDescriptor.PM_CLASS_GENERATOR_DEFAULT; 151 } 152 } 153 154 CMPGenerator gen = null; 155 156 try { 157 Class generator = getClass().getClassLoader().loadClass(generatorName); 158 gen = (CMPGenerator)generator.newInstance(); 159 160 } catch (Throwable e) { 161 String msg = localStrings.getString("cmpc.cmp_generator_class_error", 162 application.getRegistrationName(), 163 bundle.getModuleDescriptor().getArchiveUri()); 164 _logger.log(Level.SEVERE, msg, e); 165 generatorExceptionMsg = addGeneratorExceptionMessage(msg, 166 generatorExceptionMsg); 167 168 continue; 169 } 170 171 try { 172 time = now(); 173 gen.init(bundle, _ejbcCtx, archiveUri, generatedXmlsPath); 174 this._ejbcCtx.getTiming().cmpGeneratorTime += (now() - time); 175 176 Iterator ejbs=bundle.getEjbs().iterator(); 177 178 while ( ejbs.hasNext() ) { 179 180 EjbDescriptor desc = (EjbDescriptor) ejbs.next(); 181 beanName = desc.getName(); 182 183 if (com.sun.enterprise.util.logging.Debug.enabled) { 184 _logger.log(Level.FINE,"[CMPC] Ejb Class Name: " 185 + desc.getEjbClassName()); 186 } 187 188 if ( desc instanceof IASEjbCMPEntityDescriptor ) { 189 190 IASEjbCMPEntityDescriptor entd = 192 (IASEjbCMPEntityDescriptor)desc; 193 194 if (com.sun.enterprise.util.logging.Debug.enabled) { 195 _logger.log(Level.FINE, 196 "[CMPC] Home Object Impl name is " 197 + entd.getLocalHomeImplClassName()); 198 } 199 200 entd.setClassLoader(jcl); 202 203 try { 204 time = now(); 205 gen.generate(entd, stubsDir, stubsDir); 206 this._ejbcCtx.getTiming().cmpGeneratorTime += 207 (now() - time); 208 209 } catch (GeneratorException e) { 210 String msg = e.getMessage(); 211 _logger.log(Level.WARNING, msg); 212 generatorExceptionMsg = addGeneratorExceptionMessage( 213 msg, generatorExceptionMsg); 214 } 215 216 223 224 } else if (desc instanceof EjbCMPEntityDescriptor ) { 225 } 227 228 } 230 beanName = null; 231 232 time = now(); 233 Collection col = gen.cleanup(); 234 this._ejbcCtx.getTiming().cmpGeneratorTime += (now() - time); 235 236 for (Iterator fileIter=col.iterator();fileIter.hasNext();) { 237 File file=(File )fileIter.next(); 238 String fileName=file.getPath(); 239 _logger.log(Level.FINE,"[CMPC] File name is "+fileName); 240 cmpFiles.addElement(fileName); 241 } 242 243 } catch (GeneratorException e) { 244 String msg = e.getMessage(); 245 _logger.log(Level.WARNING, msg); 246 generatorExceptionMsg = addGeneratorExceptionMessage(msg, 247 generatorExceptionMsg); 248 } 249 250 } 252 bundle = null; 253 254 if (generatorExceptionMsg == null) { 255 String classPath = 257 getClassPath(this._ejbcCtx.getClasspathUrls(), stubsDir); 258 259 time = now(); 260 IASEJBC.compileClasses(classPath, cmpFiles, stubsDir, 261 stubsDir.getCanonicalPath(), 262 this._ejbcCtx.getJavacOptions()); 263 264 this._ejbcCtx.getTiming().javaCompileTime += (now() - time); 265 266 _logger.log(Level.FINE, "ejbc.done_processing_cmp", 267 application.getRegistrationName()); 268 } 269 270 } catch (GeneratorException e) { 271 _logger.log(Level.WARNING, e.getMessage()); 272 throw e; 273 274 } catch (Throwable e) { 275 String eType = e.getClass().getName(); 276 String appName = application.getRegistrationName(); 277 String exMsg = e.getMessage(); 278 279 String msg = null; 280 if (bundle == null) { 281 msg = localStrings.getString("cmpc.cmp_app_error", 283 eType, appName, exMsg); 284 } else { 285 String bundleName = bundle.getModuleDescriptor().getArchiveUri(); 286 if (beanName == null) { 287 msg = localStrings.getString("cmpc.cmp_module_error", 289 eType, appName, bundleName, exMsg); 290 } else { 291 msg = localStrings.getString("cmpc.cmp_bean_error", 293 new Object [] {eType, beanName, appName, bundleName, exMsg}); 294 } 295 } 296 297 _logger.log(Level.SEVERE, msg, e); 298 299 throw new CmpCompilerException(msg); 300 } 301 302 if (generatorExceptionMsg != null) { 303 throw new GeneratorException(generatorExceptionMsg.toString()); 305 } 306 } 307 308 313 private long now() { 314 return System.currentTimeMillis(); 315 } 316 317 327 private static String getModuleDirName(EjbBundleDescriptor bundle) 328 { 329 String archieveuri = bundle.getModuleDescriptor().getArchiveUri(); 330 return FileUtils.makeFriendlyFilename(archieveuri); 331 } 332 333 341 private static String getClassPath(String [] paths, File other) { 342 343 StringBuffer sb = new StringBuffer (); 344 345 for (int i=0; i<paths.length; i++) { 346 sb.append(paths[i]+File.pathSeparator); 347 } 348 349 if (other != null) { 350 sb.append(other.toString()); 351 } 352 353 return sb.toString(); 354 } 355 356 362 private StringBuffer addGeneratorExceptionMessage(String msg, StringBuffer buf) { 363 StringBuffer rc = buf; 364 if (rc == null) 365 rc = new StringBuffer (msg); 366 else 367 rc.append('\n').append(msg); 368 369 return rc; 370 } 371 372 private EjbcContext _ejbcCtx = null; 374 private static final Logger _logger = 375 LogDomains.getLogger(LogDomains.DPL_LOGGER); 376 private static final StringManager localStrings = 377 StringManager.getManager(CmpCompiler.class); 378 379 } 380 | Popular Tags |