1 23 24 package com.sun.enterprise.tools.verifier; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.util.EventObject ; 30 import java.util.Vector ; 31 import java.util.logging.Level ; 32 import java.util.logging.LogRecord ; 33 import java.util.logging.Logger ; 34 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 import org.xml.sax.SAXException ; 44 import com.sun.enterprise.deployment.archivist.Archivist; 45 import com.sun.enterprise.deployment.archivist.ArchivistFactory; 46 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 47 import com.sun.enterprise.deployment.deploy.shared.FileArchiveFactory; 48 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 49 import com.sun.enterprise.deployment.*; 50 import com.sun.enterprise.deployment.util.ModuleDescriptor; 51 import com.sun.enterprise.logging.LogDomains; 52 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor; 53 import com.sun.enterprise.tools.verifier.tests.VerifierCheck; 54 import com.sun.enterprise.tools.verifier.webservices.WebServiceCheckMgrImpl; 55 import com.sun.enterprise.tools.verifier.persistence.PersistenceUnitCheckMgrImpl; 56 import com.sun.enterprise.util.io.FileUtils; 57 58 public abstract class CheckMgr { 59 60 63 static Vector <VerifierEventsListener> listenerList = new Vector <VerifierEventsListener>(); 64 65 72 protected void check(Descriptor descriptor) throws Exception { 73 logger.log(Level.FINE, "com.sun.enterprise.tools.verifier.CheckMgr.check", 74 new Object []{getClass().getName(), descriptor.getName()}); 75 76 setRuntimeDDPresent(getAbstractArchiveUri(descriptor)); 77 78 loadTestInformationFromPropsFile(); 80 81 83 logger.log(Level.FINE, "com.sun.enterprise.tools.verifier.CheckMgr.RunAllTests", 84 new Object []{descriptor.getName()}); 85 String schemaVersion = getSchemaVersion(descriptor); 86 context.setSchemaVersion(schemaVersion); 87 context.setJavaEEVersion(frameworkContext.getJavaEEVersion()); 88 context.setComponentNameConstructor(getComponentNameConstructor(descriptor)); 89 FileArchive moduleArchive = new FileArchive(); 90 moduleArchive.open(getAbstractArchiveUri(descriptor)); 91 context.setModuleArchive(moduleArchive); 92 ResultManager resultManager = frameworkContext.getResultManager(); 93 for (int i = 0; i < test.size(); i++) { 94 TestInformation ti = (TestInformation) test.elementAt(i); 95 String minVersion = ti.getMinimumVersion(); 96 String maxVersion = ti.getMaximumVersion(); 97 if (schemaVersion != null && minVersion != null && 100 schemaVersion.compareTo(minVersion) < 0) { 101 logger.log(Level.FINE, "com.sun.enterprise.tools.verifier.CheckMgr.version.NOT_APPLICABLE", 102 new Object []{ti.getClassName()}); 103 continue; 104 } 105 if (schemaVersion != null && maxVersion != null && 106 schemaVersion.compareTo(maxVersion) > 0) { 107 logger.log(Level.FINE, "com.sun.enterprise.tools.verifier.CheckMgr.version.NOT_APPLICABLE", 108 new Object []{ti.getClassName()}); 109 continue; 110 } 111 if(!isApplicable(ti, descriptor)) { 112 logger.log(Level.FINE, "com.sun.enterprise.tools.verifier.CheckMgr.version.NOT_APPLICABLE", 113 new Object []{ti.getClassName()}); 114 continue; 115 } 116 try { 117 Class c = Class.forName(ti.getClassName()); 118 VerifierCheck t = (VerifierCheck) c.newInstance(); 119 t.setVerifierContext(context); 120 Result r = t.check(descriptor); 121 setModuleName(r); 125 resultManager.add(r); 126 fireTestFinishedEvent(r); 128 } catch (Throwable e) { 129 LogRecord logRecord = new LogRecord (Level.SEVERE, 130 ti.getClassName()); 131 logRecord.setThrown(e); 132 resultManager.log(logRecord); 133 } 134 } 135 136 fireAllTestsFinishedEvent(); 137 } 139 140 protected abstract ComponentNameConstructor getComponentNameConstructor(Descriptor descriptor); 141 142 145 public void setVerifierContext(Context context) { 146 this.context = context; 147 } 148 149 155 public static void addVerifierEventsListener(VerifierEventsListener l) { 156 listenerList.add(l); 157 } 158 159 164 public static void removeVerifierEventsListener(VerifierEventsListener l) { 165 listenerList.remove(l); 166 } 167 168 175 protected abstract String getTestsListFileName(); 176 177 protected abstract void setModuleName(Result r); 178 179 protected abstract String getSchemaVersion(Descriptor descriptor); 180 181 184 protected abstract String getSunONETestsListFileName(); 185 186 protected String getArchiveUri(Descriptor descriptor) { 187 String archiveUri = getBundleDescriptor(descriptor).getModuleDescriptor().getArchiveUri(); 188 return new File (archiveUri).getName(); 189 } 190 191 196 protected boolean isApplicable(TestInformation test, Descriptor descriptor) { 197 return true; 198 } 199 200 protected void fireTestFinishedEvent(Result r) { 202 203 Object [] listeners; 204 synchronized (listenerList) { 205 listeners = listenerList.toArray(); 206 } 207 if (listeners == null) 208 return; 209 210 for (int i = 0; i < listeners.length; i++) { 213 if (listeners[i] instanceof VerifierEventsListener) { 214 EventObject event = new EventObject (r); 216 ((VerifierEventsListener) listeners[i]).testFinished(event); 217 } 218 } 219 } 220 221 protected void fireAllTestsFinishedEvent() { 223 224 Object [] listeners; 225 synchronized (listenerList) { 226 listeners = listenerList.toArray(); 227 } 228 if (listeners == null) 229 return; 230 231 for (int i = 0; i < listeners.length; i++) { 234 if (listeners[i] instanceof VerifierEventsListener) { 235 EventObject event = new EventObject (this); 237 ((VerifierEventsListener) listeners[i]).allTestsFinished(event); 238 } 239 } 240 } 241 242 249 private void loadTestInformationFromPropsFile() 250 throws ParserConfigurationException , SAXException , IOException { 251 252 if(!test.isEmpty()) 253 return; 254 logger.log(Level.FINE, 255 "com.sun.enterprise.tools.verifier.CheckMgr.TestnamesPropsFile"); 257 File inputFile = getTestsFileFor(getTestsListFileName()); 258 259 DocumentBuilder db = DocumentBuilderFactory.newInstance() 261 .newDocumentBuilder(); 262 Document doc = db.parse(inputFile); 263 NodeList list = doc.getElementsByTagName("test"); for (int i = 0; i < list.getLength(); i++) { 265 Element e = (Element ) list.item(i); 266 NodeList nl = e.getChildNodes(); 267 TestInformation ti = new TestInformation(); 268 for (int j = 0; j < nl.getLength(); j++) { 269 String nodeName = nl.item(j).getNodeName(); 270 if ("test-class".equals(nodeName.trim())) { Node el = nl.item(j); 272 ti.setClassName(el.getFirstChild().getNodeValue().trim()); 273 } 274 if ("minimum-version".equals(nodeName.trim())) { Node el = nl.item(j); 276 ti.setMinimumVersion( 277 el.getFirstChild().getNodeValue().trim()); 278 } 279 if ("maximum-version".equals(nodeName.trim())) { Node el = nl.item(j); 281 ti.setMaximumVersion( 282 el.getFirstChild().getNodeValue().trim()); 283 } 284 } 285 test.addElement(ti); 286 } 287 288 if ((!frameworkContext.isPortabilityMode() && 289 getRuntimeDDPresent())) 290 readSunONETests(test); 291 Vector <TestInformation> testExcluded = getTestFromExcludeList(); 293 test = getFinalTestList(test, testExcluded); 295 } 296 297 300 protected Vector <TestInformation> getTestFromExcludeList() 301 throws ParserConfigurationException , SAXException , IOException { 302 Vector <TestInformation> testExcluded = new Vector <TestInformation>(); 303 logger.log(Level.FINE, 304 "com.sun.enterprise.tools.verifier.CheckMgr.TestnamesPropsFile"); File inputFile = getTestsFileFor(excludeListFileName); 307 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 308 DocumentBuilder builder = factory.newDocumentBuilder(); 309 310 Document doc = builder.parse(inputFile); 311 NodeList list = doc.getElementsByTagName("test"); for (int i = 0; i < list.getLength(); i++) { 313 Element e = (Element ) list.item(i); 314 NodeList nl = e.getChildNodes(); 315 TestInformation ti = new TestInformation(); 316 for (int j = 0; j < nl.getLength(); j++) { 317 String nodeName = nl.item(j).getNodeName(); 318 if ("test-class".equals(nodeName.trim())) { Node el = nl.item(j); 320 ti.setClassName(el.getFirstChild().getNodeValue().trim()); 321 } 322 } 323 testExcluded.addElement(ti); 324 } 325 return testExcluded; 326 } 327 328 333 protected Vector <TestInformation> getFinalTestList( 334 Vector <TestInformation> orignalList, 335 Vector <TestInformation> excludeList) { 336 if (excludeList == null) return orignalList; 337 if (orignalList.size() != 0 && excludeList.size() != 0) { 338 for (int i = 0; i < excludeList.size(); i++) { 339 for (int j = 0; j < orignalList.size(); j++) { 340 if (((TestInformation) orignalList.elementAt(j)).getClassName() 341 .equals( 342 ((TestInformation) excludeList.elementAt(i)).getClassName())) { 343 orignalList.remove(j); 344 } 345 } 346 } 347 } 348 return orignalList; 349 } 350 351 protected void checkWebServices(Descriptor descriptor) 352 throws Exception { 353 if (frameworkContext.isPartition() && 354 !frameworkContext.isWebServices()) 355 return; 356 BundleDescriptor bundleDescriptor = (BundleDescriptor) descriptor; 357 WebServiceCheckMgrImpl webServiceCheckMgr = new WebServiceCheckMgrImpl( 358 frameworkContext); 359 if (bundleDescriptor.hasWebServices()) { 360 WebServicesDescriptor wdesc = bundleDescriptor.getWebServices(); 361 webServiceCheckMgr.setVerifierContext(context); 362 webServiceCheckMgr.check(wdesc); 363 } 364 } 365 366 protected void checkPersistenceUnits(RootDeploymentDescriptor descriptor) 367 throws Exception { 368 if (frameworkContext.isPartition() && 369 !frameworkContext.isPersistenceUnits()) 370 return; 371 CheckMgr puCheckMgr = new PersistenceUnitCheckMgrImpl( 372 frameworkContext, context); 373 for(PersistenceUnitsDescriptor pus : 374 descriptor.getPersistenceUnitsDescriptors()) { 375 for (PersistenceUnitDescriptor pu : 376 pus.getPersistenceUnitDescriptors()) { 377 puCheckMgr.check(pu); 378 } 379 } 380 } 381 382 384 protected static String getSunPrefix() { 385 return "sun-"; } 387 388 protected void setRuntimeDDPresent(String uri) { 389 InputStream is = null; 390 try { 391 AbstractArchive abstractArchive = new FileArchiveFactory().openArchive( 392 uri); 393 Archivist archivist = ArchivistFactory.getArchivistForArchive( 394 abstractArchive); 395 if(archivist != null) { 396 String ddFileEntryName = archivist.getRuntimeDeploymentDescriptorPath(); 397 is = abstractArchive.getEntry(ddFileEntryName); 398 if (is != null) { 399 isDDPresent = true; 400 } 401 } 402 403 } catch (IOException e) { 404 isDDPresent = false; 405 } finally { 406 try { 407 if(is != null) 408 is.close(); 409 } catch (Exception e) { 410 } 412 } 413 } 414 415 private boolean getRuntimeDDPresent() { 416 return isDDPresent; 417 } 418 419 private void readSunONETests(Vector <TestInformation> test) 420 throws ParserConfigurationException , SAXException , IOException { 421 String sunonetests = getSunONETestsListFileName(); 422 if (sunonetests == null) 423 return; 424 File inputFile = getTestsFileFor(sunonetests); 425 if (!inputFile.exists()) 426 return; 427 DocumentBuilder db = DocumentBuilderFactory.newInstance() 428 .newDocumentBuilder(); 429 Document doc = db.parse(inputFile); 430 NodeList list = doc.getElementsByTagName("test"); for (int i = 0; i < list.getLength(); i++) { 432 Element e = (Element ) list.item(i); 433 NodeList nl = e.getChildNodes(); 434 TestInformation ti = new TestInformation(); 435 for (int j = 0; j < nl.getLength(); j++) { 436 String nodeName = nl.item(j).getNodeName(); 437 if ("test-class".equals(nodeName.trim())) { Node el = nl.item(j); 439 ti.setClassName(el.getFirstChild().getNodeValue().trim()); 440 } 441 if ("minimum-version".equals(nodeName.trim())) { Node el = nl.item(j); 443 ti.setMinimumVersion( 444 el.getFirstChild().getNodeValue().trim()); 445 } 446 if ("maximum-version".equals(nodeName.trim())) { Node el = nl.item(j); 448 ti.setMaximumVersion( 449 el.getFirstChild().getNodeValue().trim()); 450 } 451 } 452 test.addElement(ti); 453 } 454 } 455 456 462 private File getTestsFileFor(String filename) { 463 File f = new File (frameworkContext.getConfigDirStr()); 467 f = new File (f, filename); 468 return new File (f.toString()); 469 } 470 471 protected String getAbstractArchiveUri(Descriptor descriptor) { 472 String archBase = context.getAbstractArchive().getArchiveUri(); 473 if (descriptor instanceof Application) 474 return archBase; 475 ModuleDescriptor mdesc = getBundleDescriptor(descriptor).getModuleDescriptor(); 476 if(mdesc.isStandalone()) { 477 return archBase; 478 } else { 479 return archBase + File.separator + 480 FileUtils.makeFriendlyFileName(mdesc.getArchiveUri()); 481 } 482 } 483 488 protected BundleDescriptor getBundleDescriptor(Descriptor descriptor) { 489 return (BundleDescriptor) descriptor; 490 } 491 492 protected FrameworkContext frameworkContext = null; 493 final protected boolean debug = Verifier.isDebug(); 494 protected Context context = null; 495 496 497 private static final String excludeListFileName = "TestExcludeList.xml"; private Logger logger = LogDomains.getLogger( 499 LogDomains.AVK_VERIFIER_LOGGER); 500 protected boolean isDDPresent = false; 501 private Vector <TestInformation> test = new Vector <TestInformation>(); 502 } 503 504 | Popular Tags |