KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > WebArchiveClassesLoadable


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 package com.sun.enterprise.tools.verifier.tests.web;
24
25 import com.sun.enterprise.deployment.WebBundleDescriptor;
26 import com.sun.enterprise.deployment.WebComponentDescriptor;
27 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
28 import com.sun.enterprise.deployment.web.ServletFilter;
29 import com.sun.enterprise.deployment.web.AppListenerDescriptor;
30 import com.sun.enterprise.deployment.web.ErrorPageDescriptor;
31 import com.sun.enterprise.tools.verifier.Result;
32 import com.sun.enterprise.tools.verifier.apiscan.classfile.ClosureCompiler;
33 import com.sun.enterprise.tools.verifier.apiscan.classfile.ClosureCompilerImpl;
34 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
35 import com.sun.enterprise.tools.verifier.tests.util.WebArchiveLoadableHelper;
36
37 import java.util.*;
38 import java.io.File JavaDoc;
39
40 /**
41  * A j2ee archive should be self sufficient and should not depend on any classes to be
42  * available at runtime.
43  * The test checks whether all the classes found in the web archive are loadable and the
44  * classes that are referenced inside their code are also loadable within the jar.
45  *
46  * @author Vikas Awasthi
47  */

48 public class WebArchiveClassesLoadable extends WebTest implements WebCheck {
49     public Result check(WebBundleDescriptor descriptor) {
50         Result result = getInitializedResult();
51         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
52         String JavaDoc archiveUri = getAbstractArchiveUri(descriptor);
53         
54         Iterator entries;
55         try{
56             entries=getClassNames(descriptor).iterator();
57         } catch(Exception JavaDoc e) {
58 // e.printStackTrace();
59
result.failed(smh.getLocalString(getClass().getName() + ".exception",
60                                              "Error: [ {0} ] exception while loading the archive [ {1} ].",
61                                               new Object JavaDoc[] {e, descriptor.getName()}));
62             return result;
63         }
64         
65         boolean allPassed = true;
66         ClosureCompiler closureCompiler=getVerifierContext().getClosureCompiler();
67         ((ClosureCompilerImpl)closureCompiler).addExcludedPattern("org.apache.jasper");
68         if(getVerifierContext().isAppserverMode())
69             ((ClosureCompilerImpl)closureCompiler).addExcludedPattern("com.sun.enterprise");
70
71         while (entries.hasNext()) {
72                 String JavaDoc className=(String JavaDoc)entries.next();
73                 boolean status=closureCompiler.buildClosure(className);
74                 allPassed=status && allPassed;
75         }
76         if (allPassed) {
77             result.setStatus(Result.PASSED);
78             addGoodDetails(result, compName);
79             result.passed(smh.getLocalString
80                 (getClass().getName() + ".passed",
81                 "All the classes are loadable within [ {0} ] without any linkage error.",
82                 new Object JavaDoc[] {archiveUri}));
83         } else {
84             result.setStatus(Result.FAILED);
85             addErrorDetails(result, compName);
86             result.addErrorDetails(WebArchiveLoadableHelper.getFailedResults(closureCompiler, getVerifierContext().getOutDir()));
87             result.addErrorDetails(smh.getLocalString
88                     ("com.sun.enterprise.tools.verifier.tests.loadableError",
89                     "Please either bundle the above mentioned classes in the application " +
90                     "or use optional packaging support for them."));
91         }
92         return result;
93     }
94     
95     /**
96      * Looks for Servlet classes, ServletFilter classes, Listener classes and
97      * Exception classes in the webBundleDescriptor. The closure is computed
98      * starting from these classes.
99      * @param descriptor
100      * @return returns a list of class names in the form that can be used in
101      * classloader.load()
102      * @throws Exception
103      */

104     private List getClassNames(WebBundleDescriptor descriptor) throws Exception JavaDoc{
105         final List<String JavaDoc> results=new LinkedList<String JavaDoc>();
106         for(Object JavaDoc obj : descriptor.getServletDescriptors()) {
107             String JavaDoc servletClassName = (WebComponentDescriptor.class.cast(obj))
108                     .getWebComponentImplementation();
109             results.add(servletClassName);
110         }
111         
112         for (Object JavaDoc obj : descriptor.getServletFilterDescriptors()) {
113             String JavaDoc filterClassName = (ServletFilter.class.cast(obj)).getClassName();
114             results.add(filterClassName);
115         }
116         
117         for (Object JavaDoc obj : descriptor.getAppListenerDescriptors()) {
118             String JavaDoc listenerClassName = (AppListenerDescriptor.class.cast(obj)).getListener();
119             results.add(listenerClassName);
120         }
121         
122         results.addAll(getVerifierContext().getFacesConfigDescriptor().getManagedBeanClasses());
123         
124         Enumeration en = descriptor.getErrorPageDescriptors();
125         while (en.hasMoreElements()) {
126             ErrorPageDescriptor errorPageDescriptor = (ErrorPageDescriptor) en.nextElement();
127             String JavaDoc exceptionType = errorPageDescriptor.getExceptionType();
128             if (exceptionType != null && !exceptionType.equals(""))
129                 results.add(exceptionType);
130         }
131         
132         File JavaDoc file = getVerifierContext().getOutDir();
133         if(!file.exists())
134             return results;
135
136         FileArchive arch= new FileArchive();
137         arch.open(file.getAbsolutePath());
138         Enumeration entries = arch.entries();
139         while(entries.hasMoreElements()){
140             String JavaDoc name=(String JavaDoc)entries.nextElement();
141             if(name.startsWith("org/apache/jsp") && name.endsWith(".class"))
142                 results.add(name.substring(0, name.lastIndexOf(".")).replace('/','.'));
143         }
144         return results;
145     }
146     
147 }
148
Popular Tags