KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
26 import java.lang.reflect.Modifier JavaDoc;
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
29 import com.sun.enterprise.tools.verifier.*;
30
31 /**
32  * All Servlet class of an war bundle should be declared in the deployment
33  * descriptor for portability
34  *
35  * @author Jerome Dochez
36  * @version
37  */

38 public class ServletClassDeclared extends WebTest implements WebCheck {
39
40     final String JavaDoc servletClassPath = "WEB-INF/classes";
41     
42     /**
43      * All Servlet class of an war bundle should be declared in the deployment
44      *
45      * @param descriptor the Web deployment descriptor
46      *
47      * @return <code>Result</code> the results for this assertion
48      */

49     public Result check(WebBundleDescriptor descriptor) {
50         
51     Result result = getInitializedResult();
52         // See bug #6332745
53
if(getVerifierContext().getJavaEEVersion().compareTo(SpecVersionMapper.JavaEEVersion_5) >=0){
54             result.setStatus(Result.NOT_APPLICABLE);
55             return result;
56         }
57 // ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
58

59         boolean oneWarning = false;
60         boolean foundOne=false;
61         
62 // File f = Verifier.getArchiveFile(descriptor.getModuleDescriptor().getArchiveUri());
63
result = loadWarFile(descriptor);
64         
65 // ZipFile zip = null;
66
FileArchive arch = null;
67         Enumeration entries= null;
68         //ZipEntry entry;
69
Object JavaDoc entry;
70
71         try {
72 // if (f == null) {
73
String JavaDoc uri = getAbstractArchiveUri(descriptor);
74               try {
75                  arch = new FileArchive();
76                  arch.open(uri);
77                  entries = arch.entries();
78                }catch (Exception JavaDoc e) { throw e; }
79 // }
80
// else {
81
// zip = new ZipFile(f);
82
// entries = zip.entries();
83
// }
84
} catch(Exception JavaDoc e) {
85             e.printStackTrace();
86         result.failed(smh.getLocalString
87                  (getClass().getName() + ".exception",
88                                  "IOException while loading the war file [ {0} ]",
89                   new Object JavaDoc[] {descriptor.getName()}));
90             
91             return result;
92         }
93         while (entries.hasMoreElements()) {
94             entry = entries.nextElement();
95 // if (f == null) {
96
String JavaDoc name = (String JavaDoc)entry;
97 // }
98
// else {
99
// name = ((ZipEntry)entry).getName();
100
// }
101
if (name.startsWith(servletClassPath)) {
102                 if (name.endsWith(".class")) {
103                     String JavaDoc classEntryName = name.substring(0, name.length()-".class".length());
104                     classEntryName = classEntryName.substring(servletClassPath.length()+1, classEntryName.length());
105                     String JavaDoc className = classEntryName.replace('/','.');
106                     Class JavaDoc servletClass = loadClass(result, className);
107                     if (!Modifier.isAbstract(servletClass.getModifiers()) &&
108                             isImplementorOf(servletClass, "javax.servlet.Servlet")) {
109                         foundOne=true;
110                         // let's find out if this servlet has associated deployment descriptors...
111
Set servlets = descriptor.getServletDescriptors();
112                         boolean foundDD = false;
113                         for (Iterator itr = servlets.iterator();itr.hasNext();) {
114                             WebComponentDescriptor servlet = (WebComponentDescriptor)itr.next();
115                             String JavaDoc servletClassName = servlet.getWebComponentImplementation();
116                             if (servletClassName.equals(className)) {
117                                 foundDD=true;
118                                 break;
119                             }
120                         }
121                         if (foundDD) {
122                             result.addGoodDetails(smh.getLocalString
123                                 (getClass().getName() + ".passed",
124                                 "Servlet class [ {0} ] found in war file is defined in the Deployement Descriptors",
125                                 new Object JavaDoc[] {className}));
126                         } else {
127                             oneWarning=true;
128                             result.addWarningDetails(smh.getLocalString
129                                 (getClass().getName() + ".warning",
130                                 "Servlet class [ {0} ] found in war file is not defined in the Deployement Descriptors",
131                                 new Object JavaDoc[] {className}));
132                         }
133                     }
134                 }
135             }
136         }
137         if (!foundOne) {
138         result.notApplicable(smh.getLocalString
139                  (getClass().getName() + ".notApplicable",
140                   "There are no servlet implementation within the web archive [ {0} ]",
141                   new Object JavaDoc[] {descriptor.getName()}));
142         } else {
143             if (oneWarning) {
144                 result.setStatus(Result.WARNING);
145             } else {
146                 result.setStatus(Result.PASSED);
147             }
148         }
149         return result;
150     }
151 }
152
Popular Tags