KickJava   Java API By Example, From Geeks To Geeks.

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


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.tools.verifier.tests.web.WebTest;
26 import java.util.*;
27 import java.io.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30 import com.sun.enterprise.deployment.*;
31 import com.sun.enterprise.tools.verifier.*;
32 import com.sun.enterprise.tools.verifier.tests.*;
33
34 /**
35  * Servlet Interface test.
36  * Servlets must implement the javax.servlet.Servlet interface
37  * either directly or indirectly through GenericServlet or HttpServlet
38  */

39 public class ServletInterface extends WebTest implements WebCheck {
40
41     final String JavaDoc servletClassPath = "WEB-INF/classes";
42       
43     /**
44      * Servlet Interface test.
45      * Servlets must implement the javax.servlet.Servlet interface
46      * either directly or indirectly through GenericServlet or HttpServlet
47      *
48      * @param descriptor the Web deployment descriptor
49      *
50      * @return <code>Result</code> the results for this assertion
51      */

52     public Result check(WebBundleDescriptor descriptor) {
53
54     Result result = getInitializedResult();
55     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
56
57     if (!descriptor.getServletDescriptors().isEmpty()) {
58         boolean oneFailed = false;
59             boolean notPassOrFail = true;
60         // get the servlets in this .war
61
Set servlets = descriptor.getServletDescriptors();
62         Iterator itr = servlets.iterator();
63                      
64         result = loadWarFile(descriptor);
65
66         // test the servlets in this .war
67

68             while (itr.hasNext()) {
69         WebComponentDescriptor servlet = (WebComponentDescriptor)itr.next();
70         String JavaDoc servletClassName = servlet.getWebComponentImplementation();
71         Class JavaDoc c = loadClass(result, servletClassName);
72
73                 // if the class could not be loaded we dont want to fail
74
// , it will be caught by the ServletClass test anyway
75
if (c == null) {
76                    continue;
77                 }
78                 if (isJAXRPCEndpoint(servlet)) {
79                 result.addGoodDetails(smh.getLocalString
80                        ("tests.componentNameConstructor",
81                     "For [ {0} ]",
82                     new Object JavaDoc[] {compName.toString()}));
83                 result.addGoodDetails(smh.getLocalString
84                  (getClass().getName() + ".notApplicable1",
85                   "Not Applicable since, Servlet [ {0} ] is a JAXRPC Endpoint.",
86                   new Object JavaDoc[] {servletClassName}));
87                     notPassOrFail = false;
88                 }
89         else if (isImplementorOf(c, "javax.servlet.Servlet")) {
90             result.addGoodDetails(smh.getLocalString
91                        ("tests.componentNameConstructor",
92                     "For [ {0} ]",
93                     new Object JavaDoc[] {compName.toString()}));
94             result.addGoodDetails(smh.getLocalString
95                       (getClass().getName() + ".passed",
96                        "Servlet class [ {0} ] directly or indirectly implements javax.servlet.Servlet",
97                        new Object JavaDoc[] {servletClassName}));
98                     notPassOrFail = false;
99         } else {
100             oneFailed = true;
101             result.addErrorDetails(smh.getLocalString
102                        ("tests.componentNameConstructor",
103                     "For [ {0} ]",
104                     new Object JavaDoc[] {compName.toString()}));
105             result.addErrorDetails(smh.getLocalString
106                        (getClass().getName() + ".failed",
107                         "Error: Servlet class [ {0} ] does not directly or indirectly implement javax.servlet.Servlet",
108                         new Object JavaDoc[] {servletClassName}));
109                     notPassOrFail = false;
110         }
111         }
112             // this means classloader returned null for all servlets
113
if (notPassOrFail) {
114                result.addWarningDetails(smh.getLocalString
115                                        ("tests.componentNameConstructor",
116                                         "For [ {0} ]", new Object JavaDoc[] {compName.toString()}));
117                result.warning(smh.getLocalString
118                               (getClass().getName() + ".warning",
119                                "Some servlet classes could not be loaded."));
120             }
121         else if (oneFailed) {
122         result.setStatus(Result.FAILED);
123         } else {
124         result.setStatus(Result.PASSED);
125         }
126     } else {
127         result.addNaDetails(smh.getLocalString
128                        ("tests.componentNameConstructor",
129                     "For [ {0} ]",
130                     new Object JavaDoc[] {compName.toString()}));
131         result.notApplicable(smh.getLocalString
132                  (getClass().getName() + ".notApplicable",
133                   "There are no servlet components within the web archive [ {0} ]",
134                   new Object JavaDoc[] {descriptor.getName()}));
135     }
136     return result;
137     }
138
139  private boolean isJAXRPCEndpoint(WebComponentDescriptor servlet) {
140   
141      String JavaDoc servletClassName = servlet.getWebComponentImplementation();
142
143      if (servletClassName.equals(smh.getLocalString("JAXRPCServlet","com.sun.xml.rpc.server.http.JAXRPCServlet"))) {
144         // This is a standard JAXRPC servlet
145
return true;
146      }
147
148      WebBundleDescriptor descriptor = servlet.getWebBundleDescriptor();
149      if (descriptor.hasWebServices()) {
150         WebServicesDescriptor wsdesc = descriptor.getWebServices();
151         if (wsdesc.hasEndpointsImplementedBy(servlet)) {
152            return true;
153         }
154      }
155     return false;
156  }
157
158 }
159
Popular Tags