KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > elements > EjbReferencesElement


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.ejb.elements;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
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
32 import java.util.logging.Level JavaDoc;
33
34 /**
35  * The Bean Provider must declare all enterprise bean's references to the homes
36  * of other enterprise beans as specified in section 14.3.2 of the Moscone spec.
37  * Check for one within the same jar file, can't check outside of jar file.
38  * Load/locate & check other bean's home/remote/bean, ensure they match with
39  * what the linking bean says they should be; check for pair of referencing and
40  * referenced beans exist.
41  */

42 public class EjbReferencesElement extends EjbTest implements EjbCheck {
43     
44     
45     Result result = null;
46     ComponentNameConstructor compName = null;
47     
48     // Logger to log messages
49

50     
51     /**
52      * The Bean Provider must declare all enterprise bean's references to the homes
53      * of other enterprise beans as specified in section 14.3.2 of the Moscone spec.
54      * Check for one within the same jar file, can't check outside of jar file.
55      * Load/locate & check other bean's home/remote/bean, ensure they match with
56      * what the linking bean says they should be; check for pair of referencing and
57      * referenced beans exist.
58      *
59      * @param descriptor the Enterprise Java Bean deployment descriptor
60      *
61      * @return <code>Result</code> the results for this assertion
62      */

63     public Result check(EjbDescriptor descriptor) {
64         
65         result = getInitializedResult();
66         compName = getVerifierContext().getComponentNameConstructor();
67         
68         if ((descriptor instanceof EjbEntityDescriptor) ||
69                 (descriptor instanceof EjbSessionDescriptor)) {
70             
71             // RULE: References to other beans must be declared in the form of
72
// references to other beans homes as specified in section
73
// 14.3.2 of the Moscone spec.
74

75             // check for one bean within the same jar file; can't check outside of
76
// jar file. need to load/locate and check other beans remote, home, bean
77
// match with the linking bean says they should be. i.e. check for pair
78
// of referencing & referenced bean exist, using reflection API
79

80             EjbReferenceDescriptor ejbReference;
81 // EjbAbstractDescriptor ejbDescriptor;
82
try {
83                 
84                 String JavaDoc fName = null;
85                 Set references = descriptor.getEjbReferenceDescriptors();
86                 if(references == null) {
87                     logger.log(Level.INFO,getClass().getName() + ".refnull");
88                     return result;
89                 }
90                 Iterator iterator = references.iterator();
91                 
92                 if (iterator.hasNext()) {
93                     boolean oneFailed = false;
94         // boolean foundBeanClassName = false;
95
// boolean foundHomeClassName = false;
96
// boolean foundRemoteClassName = false;
97
// File fileName = Verifier.getArchiveFile(descriptor.getEjbBundleDescriptor().getModuleDescriptor().getArchiveUri());
98
// if (fileName!=null){
99
// fName = fileName.getName();
100
// }else{
101
fName = descriptor.getEjbBundleDescriptor().getModuleDescriptor().getArchiveUri();
102 // }
103
while (iterator.hasNext()) {
104                         ejbReference = (EjbReferenceDescriptor) iterator.next();
105                         if (ejbReference.isLinked()) {
106                             // reset
107
if(ejbReference.getEjbHomeInterface() != null &&
108                                     ejbReference.getEjbInterface() != null) {
109                                 oneFailed = commonToBothInterfaces(ejbReference.getEjbHomeInterface(),ejbReference.getEjbInterface(),fName);
110                             }
111                         } else {
112                             // (e.g. external references)
113
result.addNaDetails(smh.getLocalString
114                                     ("tests.componentNameConstructor",
115                                             "For [ {0} ]",
116                                             new Object JavaDoc[] {compName.toString()}));
117                             result.notApplicable(smh.getLocalString
118                                     (getClass().getName() + ".notApplicable2",
119                                             "Not Applicable: [ {0} ] must be external reference to bean outside of [ {1} ].",
120                                             new Object JavaDoc[] {ejbReference.getName(),fName}));
121                         }
122                     }
123                     if (oneFailed) {
124                         result.setStatus(result.FAILED);
125                     } else {
126                         result.setStatus(result.PASSED);
127                     }
128                 } else {
129                     result.addNaDetails(smh.getLocalString
130                             ("tests.componentNameConstructor",
131                                     "For [ {0} ]",
132                                     new Object JavaDoc[] {compName.toString()}));
133                     result.notApplicable(smh.getLocalString
134                             (getClass().getName() + ".notApplicable1",
135                                     "There are no ejb references to other beans within this bean [ {0} ]",
136                                     new Object JavaDoc[] {descriptor.getName()}));
137                 }
138                 
139                 return result;
140             } catch (Exception JavaDoc e) {
141                 result.addErrorDetails(smh.getLocalString
142                         ("tests.componentNameConstructor",
143                                 "For [ {0} ]",
144                                 new Object JavaDoc[] {compName.toString()}));
145                 result.failed(smh.getLocalString
146                         (getClass().getName() + ".failedRef",
147                                 "Exception occurred : [ {0} ]",
148                                 new Object JavaDoc[] {e.getMessage()}));
149                 return result;
150             }
151         } else {
152             result.addNaDetails(smh.getLocalString
153                     ("tests.componentNameConstructor",
154                             "For [ {0} ]",
155                             new Object JavaDoc[] {compName.toString()}));
156             result.notApplicable(smh.getLocalString
157                     (getClass().getName() + ".notApplicable",
158                             "[ {0} ] not called \n with a Session or Entity bean.",
159                             new Object JavaDoc[] {getClass()}));
160             return result;
161         }
162     }
163     
164     /**
165      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
166      * @param home for the Home Interface of the Ejb
167      * @param remote or component for the Remote/Local interface of the Ejb.
168      * This parameter may be optional depending on the test
169      * @param fileName of the archive file.
170      * @return boolean the results for this assertion i.e if a test has failed or not
171      */

172     
173     private boolean commonToBothInterfaces(String JavaDoc home, String JavaDoc remote,String JavaDoc fileName) {
174         
175         boolean foundHomeClassName = false;
176         boolean foundRemoteClassName = false;
177         boolean oneFailed = false;
178         
179         try {
180             Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
181             if (c != null) {
182                 foundHomeClassName = true;
183                 result.addGoodDetails(smh.getLocalString
184                         ("tests.componentNameConstructor",
185                                 "For [ {0} ]",
186                                 new Object JavaDoc[] {compName.toString()}));
187                 result.passed(smh.getLocalString
188                         (getClass().getName() + ".passed2",
189                                 "The referenced bean's home interface [ {0} ] exists and is loadable within [ {1} ].",
190                                 new Object JavaDoc[] {home, fileName}));
191             } else {
192                 oneFailed = true;
193                 result.addErrorDetails(smh.getLocalString
194                         ("tests.componentNameConstructor",
195                                 "For [ {0} ]",
196                                 new Object JavaDoc[] {compName.toString()}));
197                 result.failed(smh.getLocalString
198                         (getClass().getName() + ".failed",
199                                 "Error: [ {0} ] class cannot be found within this jar [ {1} ].",
200                                 new Object JavaDoc[] {home,fileName}));
201             }
202             c = Class.forName(remote, false, getVerifierContext().getClassLoader());
203             if (c != null) {
204                 foundRemoteClassName = true;
205                 result.addGoodDetails(smh.getLocalString
206                         ("tests.componentNameConstructor",
207                                 "For [ {0} ]",
208                                 new Object JavaDoc[] {compName.toString()}));
209                 result.passed(smh.getLocalString
210                         (getClass().getName() + ".passed3",
211                                 "The referenced bean's remote interface [ {0} ] exists and is loadable within [ {1} ].",
212                                 new Object JavaDoc[] {remote,fileName}));
213             } else {
214                 oneFailed = true;
215                 result.addErrorDetails(smh.getLocalString
216                         ("tests.componentNameConstructor",
217                                 "For [ {0} ]",
218                                 new Object JavaDoc[] {compName.toString()}));
219                 result.failed(smh.getLocalString
220                         (getClass().getName() + ".failed",
221                                 "Error: [ {0} ] class cannot be found within this jar [ {1} ].",
222                                 new Object JavaDoc[] {compName,fileName}));
223             }
224             return oneFailed;
225         } catch (Exception JavaDoc e) {
226             Verifier.debug(e);
227             if (!oneFailed) {
228                 oneFailed = true;
229             }
230             String JavaDoc classStr = "";
231             if (!foundHomeClassName) {
232                 classStr = home;
233             } else if (!foundRemoteClassName) {
234                 classStr = remote;
235             }
236             result.addErrorDetails(smh.getLocalString
237                     ("tests.componentNameConstructor",
238                             "For [ {0} ]",
239                             new Object JavaDoc[] {compName.toString()}));
240             result.failed(smh.getLocalString
241                     (getClass().getName() + ".failed",
242                             "Error: class [ {0} ] cannot be found within this jar [ {1} ].",
243                             new Object JavaDoc[] {classStr, fileName}));
244             return oneFailed;
245         }
246     }
247 }
248
249
250
Popular Tags