KickJava   Java API By Example, From Geeks To Geeks.

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


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.tests.web.ias;
25
26
27 import java.util.*;
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.tools.verifier.*;
30 import com.sun.enterprise.tools.verifier.tests.*;
31 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
32 import com.sun.enterprise.deployment.WebBundleDescriptor;
33 import com.sun.enterprise.tools.verifier.tests.web.*;
34 import com.sun.enterprise.tools.common.dd.ResourceRef;
35 import com.sun.enterprise.tools.common.dd.webapp.*;
36 import com.sun.enterprise.tools.common.dd.DefaultResourcePrincipal;
37
38 //<addition author="irfan@sun.com" [bug/rfe]-id="4711198" >
39
/* Changed the result messages to reflect consistency between the result messages generated
40  * for the EJB test cases for SunONE specific deployment descriptors*/

41 //</addition>
42

43
44 public class ASResourceRefName extends WebTest implements WebCheck {
45
46
47     public Result check(WebBundleDescriptor descriptor) {
48
49         String JavaDoc resrefName;
50     Result result = getInitializedResult();
51     WebComponentNameConstructor compName = new WebComponentNameConstructor(descriptor);
52 //Start Bugid:4703107
53
DefaultResourcePrincipal defPrincipal;
54     boolean oneWarning = false;
55 //End Bugid:4703107
56
String JavaDoc jndiName;
57         boolean oneFailed = false;
58         boolean notApp = false;
59         ResourceRef[] resRefs = descriptor.getIasWebApp().getResourceRef();
60         //System.out.println(">>>>>>>>>>>>checking for res " +resRefs);
61
if (resRefs.length > 0) {
62
63         boolean isValidResRefName;
64             for (int rep=0; rep<resRefs.length; rep++ ) {
65
66                 isValidResRefName=false;
67                 resrefName = resRefs[rep].getResRefName();
68                 jndiName = resRefs[rep].getJndiName();
69 //Start Bugid:4703107
70
defPrincipal = resRefs[rep].getDefaultResourcePrincipal();
71 //End Bugid:4703107
72
//System.out.println("checking for res ref : "+ resrefName+" jndiname : "+ jndiName);
73
if (validResRefName(resrefName,descriptor)) {
74                 isValidResRefName=true;
75                 result.passed(smh.getLocalString
76                       (getClass().getName() + ".passed",
77                        "PASSED [AS-WEB sun-web-app] resource-ref name [ {0} ] properly defined in the war file.",
78                        new Object JavaDoc[] {resrefName}));
79                 } else {
80                     if (!oneFailed)
81                         oneFailed = true;
82                     result.failed(smh.getLocalString
83                                         (getClass().getName() + ".failed",
84                                         "FAILED [AS-WEB sun-web-app] resource-ref name [ {0} ] is not valid, either empty or not defined in web.xml.",
85                                         new Object JavaDoc[] {resrefName}));
86                 }
87
88                 if (isValidResRefName && validJndiName(jndiName, resrefName,descriptor)){
89                     result.passed(smh.getLocalString
90                       (getClass().getName() + ".passed1",
91                        "PASSED [AS-WEB resource-ref] jndi-name [ {0} ] properly defined in the war file.",
92                        new Object JavaDoc[] {jndiName}));
93                 } else {
94                     if (!oneFailed)
95                         oneFailed = true;
96                     result.failed(smh.getLocalString
97                                         (getClass().getName() + ".failed1",
98                                         "FAILED [AS-WEB resource-ref] jndi-name [ {0} ] is not valid, either empty or not starts according to its resource ref type entry defined in web.xml.",
99                                         new Object JavaDoc[] {jndiName}));
100                 }
101 //Start Bugid:4703107
102
if(isValidResRefName){
103             if(defPrincipal != null){
104                         String JavaDoc defaultname;
105                         String JavaDoc defaultpassword;
106                         boolean defResourcePrincipalValid = true;
107                         defaultname = defPrincipal.getName();
108                         defaultpassword = defPrincipal.getPassword();
109                         if((defaultname == null)||(defaultname.length() == 0)){
110                             oneWarning = true;
111                             defResourcePrincipalValid = false;
112                             result.warning(smh.getLocalString
113                                 (getClass().getName() + ".warning1",
114                                 "WARNING [AS-WEB resource-ref] name field in DefaultResourcePrincipal of ResourceRef [ {0} ] is not specified or is an empty string.",
115                                  new Object JavaDoc[] {resrefName}));
116                         }
117                         if((defaultpassword == null)||(defaultpassword.length() == 0)){
118                             oneWarning = true;
119                             defResourcePrincipalValid = false;
120                             result.warning(smh.getLocalString
121                                 (getClass().getName() + ".warning2",
122                                 "WARNING [AS-WEB resource-ref] password field in DefaultResourcePrincipal of ResourceRef [ {0} ] is not specified or is an empty string.",
123                                  new Object JavaDoc[] {resrefName}));
124                         }
125                         if(defResourcePrincipalValid){
126                            result.passed(smh.getLocalString
127                                (getClass().getName() + ".passed3",
128                                "PASSED [AS-WEB resource-ref] DefaultResourcePrincipal of ResourceRef [ {0} ] properly defined",
129                                new Object JavaDoc[] {resrefName}));
130                         }
131                     }
132     
133         }
134 //End Bugid:4703107
135

136             }
137
138         } else {
139             //System.out.println("There are no resource references defined within the ias-web archive");
140
notApp = true;
141         result.notApplicable(smh.getLocalString
142                  (getClass().getName() + ".notApplicable",
143                   "NOT APPLICABLE [AS-WEB sun-web-app] resource-ref element not defined in the web archive [ {0} ].",
144                   new Object JavaDoc[] {descriptor.getName()}));
145         }
146         if (oneFailed) {
147             result.setStatus(Result.FAILED);
148 //Start Bugid:4703107
149
} else if(oneWarning) {
150             result.setStatus(Result.WARNING);
151 //End Bugid:4703107
152
} else if(notApp) {
153             result.setStatus(Result.NOT_APPLICABLE);
154         }else {
155             result.setStatus(Result.PASSED);
156             result.passed
157             (smh.getLocalString
158              (getClass().getName() + ".passed2",
159               "PASSED [AS-WEB sun-web-app] resource-ref element(s) are valid within the web archive [ {0} ] .",
160                       new Object JavaDoc[] {descriptor.getName()} ));
161         }
162     return result;
163     }
164
165     boolean validResRefName(String JavaDoc name,WebBundleDescriptor descriptor){
166         boolean valid =true;
167         if(name !=null && name.length()!=0) {
168           try{
169             descriptor.getResourceReferenceByName(name);
170           }
171           catch(IllegalArgumentException JavaDoc e){
172             valid=false;
173           }
174         } else{
175          valid=false;
176
177         }
178
179         return valid;
180     }
181
182     boolean validJndiName(String JavaDoc refJndiName, String JavaDoc refName,WebBundleDescriptor descriptor){
183         boolean valid =true;
184         ResourceReferenceDescriptor resDesc;
185
186         if(refJndiName !=null && refJndiName.length()!=0) {
187           //descriptor.getResourceReferenceByName(name);
188
resDesc = descriptor.getResourceReferenceByName(refName);
189                         String JavaDoc type = resDesc.getType();
190                         if(type.indexOf("javax.jms")>-1) //jms resource
191
{
192                             if(!refJndiName.startsWith("jms/"))
193                               valid=false;
194
195                         }
196                         else if(type.indexOf("javax.sql")>-1) //jdbc resource
197
{
198                             if(!refJndiName.startsWith("jdbc/"))
199                                 valid=false;
200                         }
201                         else if(type.indexOf("javax.net")>-1) //url resource
202
{
203                             if(!refJndiName.startsWith("http://"))//FIX should it start with http:// or url/http://
204
valid=false;
205                         }
206                         else if(type.indexOf("javax.mail")>-1) //jms resource
207
{
208                             if(!refJndiName.startsWith("mail/"))
209                                 valid=false;
210                         }
211                         else
212                             valid=false;
213          }
214         else{
215         valid=false;
216         }
217
218         return valid;
219     }
220 }
221
222
Popular Tags