1 23 package com.sun.enterprise.tools.verifier; 24 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.text.SimpleDateFormat ; 29 import java.util.*; 30 import java.util.logging.Level ; 31 import java.util.logging.LogRecord ; 32 import java.util.logging.Logger ; 33 34 import org.xml.sax.SAXParseException ; 35 import javax.enterprise.deploy.shared.ModuleType ; 36 37 import com.sun.enterprise.deployment.archivist.AppClientArchivist; 38 import com.sun.enterprise.deployment.archivist.ApplicationArchivist; 39 import com.sun.enterprise.deployment.archivist.Archivist; 40 import com.sun.enterprise.deployment.archivist.ArchivistFactory; 41 import com.sun.enterprise.deployment.archivist.ConnectorArchivist; 42 import com.sun.enterprise.deployment.archivist.EjbArchivist; 43 import com.sun.enterprise.deployment.archivist.PluggableArchivistsHelper; 44 import com.sun.enterprise.deployment.archivist.WebArchivist; 45 import com.sun.enterprise.deployment.backend.J2EEModuleExploder; 46 import com.sun.enterprise.deployment.backend.OptionalPkgDependency; 47 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 48 import com.sun.enterprise.deployment.deploy.shared.FileArchiveFactory; 49 import com.sun.enterprise.deployment.util.ApplicationValidator; 50 import com.sun.enterprise.deployment.*; 51 import com.sun.enterprise.logging.LogDomains; 52 import com.sun.enterprise.tools.verifier.apiscan.stdapis.APIRepository; 53 import com.sun.enterprise.tools.verifier.app.ApplicationVerifier; 54 import com.sun.enterprise.tools.verifier.appclient.AppClientVerifier; 55 import com.sun.enterprise.tools.verifier.connector.ConnectorVerifier; 56 import com.sun.enterprise.tools.verifier.ejb.EjbVerifier; 57 import com.sun.enterprise.tools.verifier.web.WebVerifier; 58 import com.sun.enterprise.util.io.FileUtils; 59 import com.sun.enterprise.util.LocalStringManagerImpl; 60 import com.sun.enterprise.util.FileUtil; 61 import com.sun.enterprise.util.JarClassLoader; 62 import com.sun.enterprise.loader.EJBClassPathUtils; 63 import com.sun.enterprise.server.PELaunch; 64 65 66 74 public class VerificationHandler { 75 76 private final String TMPDIR = System.getProperty("java.io.tmpdir"); 77 private SimpleDateFormat dateFormatter = new SimpleDateFormat ( 78 "yyyyMMddhhmmss"); private String explodeDir = TMPDIR + File.separator + "exploded" + dateFormatter.format(new Date()); 81 private LocalStringManagerImpl smh = StringManagerHelper.getLocalStringsManager(); 82 private FrameworkContext frameworkContext = null; 83 private Logger logger = LogDomains.getLogger( 84 LogDomains.AVK_VERIFIER_LOGGER); 85 private Application application = null; 86 private ResultManager resultManager = null; 87 private Archivist archivist = null; 88 private boolean isBackend = false; 89 private List<String > classPath = null; 90 private final int MAX_WINDOWS_PATH_LIMIT = 248; 91 92 public VerificationHandler(FrameworkContext frameworkContext) 93 throws IOException { 94 this.frameworkContext = frameworkContext; 95 try { 96 initStandalone(); 97 } catch(IOException ioe) { 98 cleanup(); 99 throw ioe; 100 } catch(RuntimeException re) { 101 cleanup(); 102 throw re; 103 } 104 } 105 106 public VerificationHandler(FrameworkContext frameworkContext, 107 Application application, 108 AbstractArchive abstractArchive, 109 List<String > classPath) { 110 this.frameworkContext = frameworkContext; 111 init(); 112 this.application = application; 113 this.frameworkContext.setClassPath(classPath); 114 this.frameworkContext.setJarFileName(application.getRegistrationName()); 115 isBackend = true; 116 this.frameworkContext.setApplication(application); 117 this.frameworkContext.setAbstractArchive(abstractArchive); 118 } 119 120 public ResultManager verifyArchive() { if(!application.isVirtual()) { runVerifier(new ApplicationVerifier(frameworkContext, application)); 123 } 124 125 for (Iterator itr = application.getEjbBundleDescriptors().iterator(); 126 itr.hasNext();) { 127 EjbBundleDescriptor ejbd = (EjbBundleDescriptor) itr.next(); 128 runVerifier(new EjbVerifier(frameworkContext, ejbd)); 129 } 130 131 for (Iterator itr = application.getWebBundleDescriptors().iterator(); 132 itr.hasNext();) { 133 WebBundleDescriptor webd = (WebBundleDescriptor) itr.next(); 134 runVerifier(new WebVerifier(frameworkContext, webd)); 135 } 136 137 for (Iterator itr = application.getApplicationClientDescriptors() 138 .iterator(); 139 itr.hasNext();) { 140 ApplicationClientDescriptor appClientDescriptor = 141 (ApplicationClientDescriptor) itr.next(); 142 runVerifier(new AppClientVerifier(frameworkContext,appClientDescriptor)); 143 } 144 145 for (Iterator itr = application.getRarDescriptors().iterator(); 146 itr.hasNext();) { 147 ConnectorDescriptor cond = (ConnectorDescriptor) itr.next(); 148 runVerifier(new ConnectorVerifier(frameworkContext, cond)); 149 } 150 151 return resultManager; 152 } 153 157 private void init() { 158 frameworkContext.setResultManager(resultManager = new ResultManager()); 159 try { 160 APIRepository.Initialize( 161 frameworkContext.getConfigDirStr() + File.separator + 162 "standard-apis.xml"); } catch (Exception e) { 164 throw new RuntimeException (e); 165 } 166 } 167 171 private void initStandalone() throws IOException { 172 init(); 173 logger.log(Level.FINE, getClass().getName() + ".debug.startingLoadJar"); 174 if (!frameworkContext.isPortabilityMode()) { 175 String as_config_dir = 176 System.getProperty("com.sun.aas.installRoot")+File.separator+"config"; 177 classPath = PELaunch.getServerClassPath(as_config_dir, 178 frameworkContext.getDomainDir()); 179 } 180 181 initVerifierTmpDirs(); 183 String jarFile = frameworkContext.getJarFileName(); 184 OptionalPkgDependency.satisfyOptionalPackageDependencies(); 191 archivist = ArchivistFactory.getArchivistForArchive(new File (jarFile)); 192 if(archivist == null) { 193 throw new RuntimeException ( 194 smh.getLocalString(getClass().getName() + ".notAJavaEEArchive", "[ {0} ] is not a valid Java EE archive", new Object []{jarFile})); 197 } 198 explodeArchive(new File (jarFile)); 199 checkAndExplodeArchiveInWindowsPlatform(jarFile); 200 Descriptor.setBoundsChecking(false); 201 202 try { 203 createApplicationDescriptor(); 204 } catch (IOException e) { 205 log("Problem in creating application descriptor", e); 206 throw e; 207 } catch (SAXParseException se) { 208 log("Problem in parsing the xml file. For " + 209 se.getLocalizedMessage() + 210 ", error at line " + 211 se.getLineNumber() + 212 ", column " + 213 se.getColumnNumber(), se); 214 IOException ioe = new IOException (); 215 ioe.initCause(se); 216 throw ioe; 217 } 218 ((Descriptor) application).visit(new ApplicationValidator()); 219 } 220 221 private void runVerifier(BaseVerifier baseVerifier) { 222 try { 223 baseVerifier.verify(); 224 } catch (Exception e) { 225 log("Problem in running tests for :" + 226 baseVerifier.getDescriptor().getName(), 227 e); 228 } 229 } 230 231 private void createApplicationDescriptor() throws IOException , 232 SAXParseException { 233 PluggableArchivistsHelper defaultArchivists = new PluggableArchivistsHelper(); 235 defaultArchivists.registerArchivist(new ApplicationArchivist()); 236 defaultArchivists.registerArchivist(new WebArchivist()); 237 defaultArchivists.registerArchivist(new EjbArchivist()); 238 defaultArchivists.registerArchivist(new ConnectorArchivist()); 239 defaultArchivists.registerArchivist(new AppClientArchivist()); 240 AbstractArchive abstractArchive = 241 new FileArchiveFactory().openArchive( 242 frameworkContext.getExplodedArchivePath()); 243 frameworkContext.setAbstractArchive(abstractArchive); 244 archivist.setPluggableArchivists(defaultArchivists); 245 archivist.setXMLValidationLevel("full"); 246 archivist.setRuntimeXMLValidation(true); 247 archivist.setRuntimeXMLValidationLevel("full"); 248 archivist.setAnnotationProcessingRequested(true); 249 archivist.setAnnotationErrorHandler(new VerifierErrorHandler(resultManager)); 250 251 String jarName = new File (abstractArchive.getArchiveUri()).getName(); 252 createApplicationDescriptor0(abstractArchive, jarName); 253 } 254 255 private void explodeArchive(File archiveFile) throws IOException { 256 if (archiveFile.isDirectory()) { 257 frameworkContext.setExplodedArchivePath( 258 archiveFile.getAbsolutePath()); 259 return; 260 } 261 262 String appName = FileUtils.makeFriendlyFileNameNoExtension( 263 archiveFile.getName()); 264 File appDir = new File (new File (explodeDir), appName); 265 frameworkContext.setExplodedArchivePath(appDir.getAbsolutePath()); 266 267 try { 268 ModuleType moduleType = archivist.getModuleType(); 269 if(ModuleType.EAR.equals(moduleType)) { application = J2EEModuleExploder.explodeEar(archiveFile, appDir); 273 } else if ( ModuleType.EJB.equals(moduleType) || 274 ModuleType.CAR.equals(moduleType) || 275 ModuleType.RAR.equals(moduleType) || 276 ModuleType.WAR.equals(moduleType) ) { 277 J2EEModuleExploder.explodeJar(archiveFile, appDir); 278 } else 279 throw new FileNotFoundException ( 280 "Deployment descriptor not found in " + 281 archiveFile.getName()); 282 } catch (Exception e) { 283 IOException ioe = new IOException (e.getMessage()); 284 ioe.initCause(e); 285 throw ioe; 286 } 287 } 288 289 private void checkAndExplodeArchiveInWindowsPlatform(String jarFile) 297 throws IOException { 298 if(!System.getProperty("os.name").toLowerCase().startsWith("win")) 299 return; 300 301 if(!testFileLength(new File (explodeDir))) { 302 File tempDir = new File (System.getProperty("home.drive"),"temp"); 303 if(!tempDir.exists()) 304 throw new IOException (smh.getLocalString 305 (getClass().getName() + ".exception1","Maximum Path " + 306 "Length exceeded. The application uses long file names " + 307 "which has exceeded maximum allowed path length in windows." + 308 " Please shorten the file names and then continue. Not " + 309 "able to proceed further as [{0}] does not exist", 310 new Object []{tempDir.getAbsolutePath()})); 311 if(!FileUtil.deleteDir(new File (explodeDir))) { 312 logger.log(Level.WARNING, 313 getClass().getName() + ".explodedirdeleteerror", new Object [] {explodeDir}); 315 } 316 explodeDir = tempDir.getAbsolutePath() + File.separator + "exploded" + 317 dateFormatter.format(new Date()); 318 explodeArchive(new File (jarFile)); 319 if(!testFileLength(new File (explodeDir))) 320 throw new IOException (smh.getLocalString 321 (getClass().getName() + ".exception","Maximum Path Length " + 322 "exceeded. The application uses long file names which has " + 323 "exceeded maximum allowed path length in windows. Please " + 324 "shorten the file names and then continue.")); 325 } 326 } 327 328 330 private boolean testFileLength(File file) throws IOException { 331 if(file.getAbsolutePath().length() > MAX_WINDOWS_PATH_LIMIT) { 332 logger.log(Level.WARNING, 333 getClass().getName() + ".maxlength.exceeded", new Object [] {file.getAbsolutePath(), 335 file.getAbsolutePath().length(), 336 MAX_WINDOWS_PATH_LIMIT}); 337 return false; 338 } else if(file.getCanonicalPath().length() > MAX_WINDOWS_PATH_LIMIT) { 339 logger.log(Level.WARNING, 342 getClass().getName() + ".maxlength.exceeded", new Object [] {file.getCanonicalPath(), 344 file.getCanonicalPath().length(), 345 MAX_WINDOWS_PATH_LIMIT}); 346 return false; 347 } 348 349 if(!file.isDirectory()) 350 return true; 351 352 for (File file1 : file.listFiles()) 353 if(!testFileLength(file1)) 354 return false; 355 return true; 356 } 357 358 private boolean initVerifierTmpDirs() throws IOException { 359 File test = new File (explodeDir); 361 if (!test.isDirectory() && !test.getAbsoluteFile().mkdirs()) { 362 logger.log(Level.SEVERE, getClass().getName() + 363 ".explodedircreateerror", test.getAbsolutePath()); throw new IOException (smh.getLocalString(getClass().getName() 365 + ".explodedircreateerror", test.getAbsolutePath())); } 367 return true; 368 } 369 370 public void cleanup() { 371 if(!isBackend && application!=null) 372 ((JarClassLoader)application.getClassLoader()).done(); 373 if(!isBackend && 374 !((new File (frameworkContext.getJarFileName())).isDirectory())) 375 FileUtil.deleteDir(new File (explodeDir)); 376 } 377 378 384 private void log(String message, Exception e) { 385 if (message == null) message = ""; 386 LogRecord logRecord = new LogRecord (Level.SEVERE, message); 387 logRecord.setThrown(e); 388 frameworkContext.getResultManager().log(logRecord); 389 } 390 391 private ClassLoader getEarClassLoader(Application dummyApp) throws IOException { 392 List<String > classPath = EJBClassPathUtils.getApplicationClassPath(dummyApp, 393 frameworkContext.getExplodedArchivePath()); 394 return createClassLoaderFromPath(classPath); 395 } 396 397 private ClassLoader getModuleClassLoader(ModuleType type) throws IOException { 398 String moduleRoot = frameworkContext.getExplodedArchivePath(); 399 List<String > classPath = EJBClassPathUtils.getModuleClassPath(type, moduleRoot, moduleRoot); 400 return createClassLoaderFromPath(classPath); 401 } 402 403 private ClassLoader createClassLoaderFromPath(List<String > classPath) throws IOException { 404 if(!frameworkContext.isPortabilityMode()) { 405 classPath.addAll(0, this.classPath); 406 frameworkContext.setClassPath(classPath); 407 } else { 408 String as_lib_root = System.getProperty("com.sun.aas.installRoot")+ 409 File.separator+ 410 "lib"+ File.separator; 412 classPath.add(as_lib_root + "javaee.jar"); } 414 415 JarClassLoader jcl = new JarClassLoader(); 416 for (String path : classPath) 422 jcl.appendURL(new File (path)); 423 return jcl; 424 } 425 426 434 private void createApplicationDescriptor0(AbstractArchive abstractArchive, 435 String jarName) throws IOException , SAXParseException { 436 if (archivist.getModuleType()==ModuleType.EAR) { 437 ClassLoader classLoader = getEarClassLoader(application); 438 application.setClassLoader(classLoader); 439 archivist.setClassLoader(classLoader); 440 archivist.setHandleRuntimeInfo(!frameworkContext.isPortabilityMode()); 441 archivist.readPersistenceDeploymentDescriptors(abstractArchive, application); 442 ((ApplicationArchivist) archivist).readModulesDescriptors(application, abstractArchive); 443 if(!frameworkContext.isPortabilityMode()) { 444 archivist.readRuntimeDeploymentDescriptor(abstractArchive, application); 446 } 447 application.setRegistrationName(jarName); 448 } else { 449 ClassLoader classLoader = getModuleClassLoader(archivist.getModuleType()); 450 archivist.setClassLoader(classLoader); 451 application = ApplicationArchivist.openArchive(jarName, 452 archivist, 453 abstractArchive, 454 !frameworkContext.isPortabilityMode()); 455 application.setClassLoader(classLoader); 456 } 457 } 458 } 459 | Popular Tags |