KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > CheckMgr


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.verifier;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.EventObject JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.LogRecord JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 import javax.xml.parsers.DocumentBuilder JavaDoc;
36 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
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     /* This class provides the event notification methods for the check managers
61     * used by EjbCheckMgrImpl, AppCheckMgrImpl, WebCheckMgrImpl, AppClientCheckMgrImpl
62     */

63     static Vector JavaDoc<VerifierEventsListener> listenerList = new Vector JavaDoc<VerifierEventsListener>();
64
65     /**
66      * <p/>
67      * Entry point for executing all tests pertinent to this architecture
68      * </p>
69      *
70      * @param descriptor <code>ConnectorDescritor</code> the deployment descriptor
71      */

72     protected void check(Descriptor descriptor) throws Exception JavaDoc {
73         logger.log(Level.FINE, "com.sun.enterprise.tools.verifier.CheckMgr.check",
74                 new Object JavaDoc[]{getClass().getName(), descriptor.getName()});
75
76         setRuntimeDDPresent(getAbstractArchiveUri(descriptor));
77
78         // Load the list of tests from the property file for this manager
79
loadTestInformationFromPropsFile();
80
81         // These temporary placeholder will keep the results of the tests
82

83         logger.log(Level.FINE, "com.sun.enterprise.tools.verifier.CheckMgr.RunAllTests",
84                 new Object JavaDoc[]{descriptor.getName()});
85         String JavaDoc 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 JavaDoc minVersion = ti.getMinimumVersion();
96             String JavaDoc maxVersion = ti.getMaximumVersion();
97             // does this test apply to the schema version implemented by
98
// this component's descriptor
99
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 JavaDoc[]{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 JavaDoc[]{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 JavaDoc[]{ti.getClassName()});
114                 continue;
115             }
116             try {
117                 Class JavaDoc c = Class.forName(ti.getClassName());
118                 VerifierCheck t = (VerifierCheck) c.newInstance();
119                 t.setVerifierContext(context);
120                 Result r = t.check(descriptor);
121                 // no need to setComponentName as it is already set in
122
// VerifierTest.getInitialisedResult(). By Sahoo
123
// r.setComponentName(getArchiveUri(descriptor));
124
setModuleName(r);
125                 resultManager.add(r);
126                 // notify listeners of test completion
127
fireTestFinishedEvent(r);
128             } catch (Throwable JavaDoc e) {
129                 LogRecord JavaDoc logRecord = new LogRecord JavaDoc(Level.SEVERE,
130                         ti.getClassName());
131                 logRecord.setThrown(e);
132                 resultManager.log(logRecord);
133             }
134         }
135
136         fireAllTestsFinishedEvent();
137         // done adding it to hastable vector.
138
}
139
140     protected abstract ComponentNameConstructor getComponentNameConstructor(Descriptor descriptor);
141
142     /**
143      * This method sets the Context object
144      */

145     public void setVerifierContext(Context context) {
146         this.context = context;
147     }
148
149     /**
150      * support notification of test completion with a ChangeEvent
151      * the ChangeEvent source is the corresponding Result object
152      *
153      * @param l change listener
154      */

155     public static void addVerifierEventsListener(VerifierEventsListener l) {
156         listenerList.add(l);
157     }
158
159     /**
160      * Remove change listener
161      *
162      * @param l change listener
163      */

164     public static void removeVerifierEventsListener(VerifierEventsListener l) {
165         listenerList.remove(l);
166     }
167
168     /**
169      * <p/>
170      *
171      * @return <code>String</code> the file name containing the list of tests
172      * to be performed by this manager on each archive file
173      * </p>
174      */

175     protected abstract String JavaDoc getTestsListFileName();
176
177     protected abstract void setModuleName(Result r);
178
179     protected abstract String JavaDoc getSchemaVersion(Descriptor descriptor);
180
181     /**
182      * Adding a new function to get the SunONE AS test lists for Ejb tests
183      */

184     protected abstract String JavaDoc getSunONETestsListFileName();
185
186     protected String JavaDoc getArchiveUri(Descriptor descriptor) {
187         String JavaDoc archiveUri = getBundleDescriptor(descriptor).getModuleDescriptor().getArchiveUri();
188         return new File JavaDoc(archiveUri).getName();
189     }
190     
191     /**
192      * This method is overridden in EjbCheckMgrImpl. This method is used to
193      * ensure that entity and mdb tests are not run for session descriptors and
194      * similarly the other way round.
195      */

196     protected boolean isApplicable(TestInformation test, Descriptor descriptor) {
197         return true;
198     }
199
200     // call once per test, when r is complete
201
protected void fireTestFinishedEvent(Result r) {
202
203         Object JavaDoc[] listeners;
204         synchronized (listenerList) {
205             listeners = listenerList.toArray();
206         }
207         if (listeners == null)
208             return;
209
210         // Process the listeners last to first, notifying
211
// those that are interested in this event
212
for (int i = 0; i < listeners.length; i++) {
213             if (listeners[i] instanceof VerifierEventsListener) {
214                 // create the event:
215
EventObject JavaDoc event = new EventObject JavaDoc(r);
216                 ((VerifierEventsListener) listeners[i]).testFinished(event);
217             }
218         }
219     }
220
221     // call once per test, when all tests for the mgr are done
222
protected void fireAllTestsFinishedEvent() {
223
224         Object JavaDoc[] listeners;
225         synchronized (listenerList) {
226             listeners = listenerList.toArray();
227         }
228         if (listeners == null)
229             return;
230
231         // Process the listeners last to first, notifying
232
// those that are interested in this event
233
for (int i = 0; i < listeners.length; i++) {
234             if (listeners[i] instanceof VerifierEventsListener) {
235                 // create the event:
236
EventObject JavaDoc event = new EventObject JavaDoc(this);
237                 ((VerifierEventsListener) listeners[i]).allTestsFinished(event);
238             }
239         }
240     }
241
242     /**
243      * <p/>
244      * load all the test names from the property file. Each manager has its
245      * list of test to be performed for each archive in a property file.
246      * The tests list of a list of class name implementing a particular test
247      * </p>
248      */

249     private void loadTestInformationFromPropsFile()
250             throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
251
252         if(!test.isEmpty())
253             return;
254         logger.log(Level.FINE,
255                 "com.sun.enterprise.tools.verifier.CheckMgr.TestnamesPropsFile"); // NOI18N
256

257         File JavaDoc inputFile = getTestsFileFor(getTestsListFileName());
258
259         // parse the xml file
260
DocumentBuilder JavaDoc db = DocumentBuilderFactory.newInstance()
261                 .newDocumentBuilder();
262         Document JavaDoc doc = db.parse(inputFile);
263         NodeList JavaDoc list = doc.getElementsByTagName("test"); // NOI18N
264
for (int i = 0; i < list.getLength(); i++) {
265             Element JavaDoc e = (Element JavaDoc) list.item(i);
266             NodeList JavaDoc nl = e.getChildNodes();
267             TestInformation ti = new TestInformation();
268             for (int j = 0; j < nl.getLength(); j++) {
269                 String JavaDoc nodeName = nl.item(j).getNodeName();
270                 if ("test-class".equals(nodeName.trim())) { // NOI18N
271
Node JavaDoc el = nl.item(j);
272                     ti.setClassName(el.getFirstChild().getNodeValue().trim());
273                 }
274                 if ("minimum-version".equals(nodeName.trim())) { // NOI18N
275
Node JavaDoc el = nl.item(j);
276                     ti.setMinimumVersion(
277                             el.getFirstChild().getNodeValue().trim());
278                 }
279                 if ("maximum-version".equals(nodeName.trim())) { // NOI18N
280
Node JavaDoc 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         // to get the list of tests to be excluded
292
Vector JavaDoc<TestInformation> testExcluded = getTestFromExcludeList();
293         // to exclude the tests
294
test = getFinalTestList(test, testExcluded);
295     }
296
297     /**
298      * @return <code>boolean</code> successful completion of getting exclude list
299      */

300     protected Vector JavaDoc<TestInformation> getTestFromExcludeList()
301             throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
302         Vector JavaDoc<TestInformation> testExcluded = new Vector JavaDoc<TestInformation>();
303         logger.log(Level.FINE,
304                 "com.sun.enterprise.tools.verifier.CheckMgr.TestnamesPropsFile"); // NOI18N
305
// parse the xml file
306
File JavaDoc inputFile = getTestsFileFor(excludeListFileName);
307         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
308         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
309
310         Document JavaDoc doc = builder.parse(inputFile);
311         NodeList JavaDoc list = doc.getElementsByTagName("test"); // NOI18N
312
for (int i = 0; i < list.getLength(); i++) {
313             Element JavaDoc e = (Element JavaDoc) list.item(i);
314             NodeList JavaDoc nl = e.getChildNodes();
315             TestInformation ti = new TestInformation();
316             for (int j = 0; j < nl.getLength(); j++) {
317                 String JavaDoc nodeName = nl.item(j).getNodeName();
318                 if ("test-class".equals(nodeName.trim())) { // NOI18N
319
Node JavaDoc el = nl.item(j);
320                     ti.setClassName(el.getFirstChild().getNodeValue().trim());
321                 }
322             }
323             testExcluded.addElement(ti);
324         }
325         return testExcluded;
326     }
327
328     /**
329      * @param orignalList
330      * @param excludeList
331      * @return <code>vector</code> successful completion of getting exclude list
332      */

333     protected Vector JavaDoc<TestInformation> getFinalTestList(
334             Vector JavaDoc<TestInformation> orignalList,
335             Vector JavaDoc<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 JavaDoc {
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 JavaDoc {
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     // end of code added for WebServices
383

384     protected static String JavaDoc getSunPrefix() {
385         return "sun-"; // NOI18N
386
}
387
388     protected void setRuntimeDDPresent(String JavaDoc uri) {
389         InputStream JavaDoc 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 JavaDoc ddFileEntryName = archivist.getRuntimeDeploymentDescriptorPath();
397                 is = abstractArchive.getEntry(ddFileEntryName);
398                 if (is != null) {
399                     isDDPresent = true;
400                 }
401             }
402
403         } catch (IOException JavaDoc e) {
404             isDDPresent = false;
405         } finally {
406             try {
407                 if(is != null)
408                     is.close();
409             } catch (Exception JavaDoc e) {
410                 // nothing to do here
411
}
412         }
413     }
414
415     private boolean getRuntimeDDPresent() {
416         return isDDPresent;
417     }
418
419     private void readSunONETests(Vector JavaDoc<TestInformation> test)
420             throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
421         String JavaDoc sunonetests = getSunONETestsListFileName();
422         if (sunonetests == null)
423             return;
424         File JavaDoc inputFile = getTestsFileFor(sunonetests);
425         if (!inputFile.exists())
426             return;
427         DocumentBuilder JavaDoc db = DocumentBuilderFactory.newInstance()
428                 .newDocumentBuilder();
429         Document JavaDoc doc = db.parse(inputFile);
430         NodeList JavaDoc list = doc.getElementsByTagName("test"); // NOI18N
431
for (int i = 0; i < list.getLength(); i++) {
432             Element JavaDoc e = (Element JavaDoc) list.item(i);
433             NodeList JavaDoc nl = e.getChildNodes();
434             TestInformation ti = new TestInformation();
435             for (int j = 0; j < nl.getLength(); j++) {
436                 String JavaDoc nodeName = nl.item(j).getNodeName();
437                 if ("test-class".equals(nodeName.trim())) { // NOI18N
438
Node JavaDoc el = nl.item(j);
439                     ti.setClassName(el.getFirstChild().getNodeValue().trim());
440                 }
441                 if ("minimum-version".equals(nodeName.trim())) { // NOI18N
442
Node JavaDoc el = nl.item(j);
443                     ti.setMinimumVersion(
444                             el.getFirstChild().getNodeValue().trim());
445                 }
446                 if ("maximum-version".equals(nodeName.trim())) { // NOI18N
447
Node JavaDoc el = nl.item(j);
448                     ti.setMaximumVersion(
449                             el.getFirstChild().getNodeValue().trim());
450                 }
451             }
452             test.addElement(ti);
453         }
454     }
455
456     /**
457      * Retrieve Web tests from TestNamesWeb.conf file
458      *
459      * @param filename file listing all web tests to run
460      * @return <code>File</code> File handle for tests to run
461      */

462     private File JavaDoc getTestsFileFor(String JavaDoc filename) {
463         // in case of appserver the config file will be <AS>/lib/verifier and
464
// for AVK it will be <AVK>/config/verifier.
465
// Put checks if this directory is null.
466
File JavaDoc f = new File JavaDoc(frameworkContext.getConfigDirStr());
467         f = new File JavaDoc(f, filename);
468         return new File JavaDoc(f.toString());
469     }
470
471     protected String JavaDoc getAbstractArchiveUri(Descriptor descriptor) {
472         String JavaDoc 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     /**
484      * EjbCheckMgrImpl, WebServiceClientCheckMgrImpl and WebServiceCheckMgrImpl
485      * classes override this method. For each of these areas the tests are run
486      * on descriptors rather than bundle descriptors.
487      */

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     /* TestExcludeList.xml Excluded tests */
497     private static final String JavaDoc excludeListFileName = "TestExcludeList.xml"; // NOI18N
498
private Logger JavaDoc logger = LogDomains.getLogger(
499             LogDomains.AVK_VERIFIER_LOGGER);
500     protected boolean isDDPresent = false;
501     private Vector JavaDoc<TestInformation> test = new Vector JavaDoc<TestInformation>();
502 }
503
504
Popular Tags