KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > runtime > ASEjbIsReadOnlyBean


1 package com.sun.enterprise.tools.verifier.tests.ejb.runtime;
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the License). You may not use this file except in
7  * compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
11  * glassfish/bootstrap/legal/CDDLv1.0.txt.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * Header Notice in each file and include the License file
17  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
18  * If applicable, add the following below the CDDL Header,
19  * with the fields enclosed by brackets [] replaced by
20  * you own identifying information:
21  * "Portions Copyrighted [year] [name of copyright owner]"
22  *
23  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24  */

25
26 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
27 import com.sun.enterprise.deployment.EjbDescriptor;
28 import com.sun.enterprise.deployment.EjbSessionDescriptor;
29 import com.sun.enterprise.tools.verifier.*;
30 import com.sun.enterprise.tools.verifier.tests.*;
31 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
32 import com.sun.enterprise.deployment.EjbEntityDescriptor;
33 import com.sun.enterprise.deployment.EjbSessionDescriptor;
34 import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
35 import java.lang.reflect.*;
36
37 import com.sun.enterprise.deployment.runtime.IASEjbExtraDescriptors;
38
39 /** ejb [0,n]
40  * is-read-only-bean ? [String]
41  *
42  * The <B>is-read-only-bean</B> should be defined for entity beans only.
43  * Also the implementation should not define ant create and remove methods.
44  * This is recommended and not required
45  * @author Irfan Ahmed
46  */

47 public class ASEjbIsReadOnlyBean extends EjbTest implements EjbCheck {
48
49     boolean oneFailed= false;
50     boolean oneWarning = false;
51
52     /**
53      *
54      * @param descriptor the Enterprise Java Bean deployment descriptor
55      * @return <code>Result</code> the results for this assertion
56      */

57     public Result check(EjbDescriptor descriptor) {
58
59     Result result = getInitializedResult();
60     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
61         
62         try{
63             IASEjbExtraDescriptors iasEjbExtraDesc = descriptor.getIASEjbExtraDescriptors();
64             String JavaDoc s1 = ("/sun-ejb-jar/enterprise-beans/ejb[ejb-name=\""+descriptor.getName()+"\"]/is-read-only-bean");
65             String JavaDoc isReadOnlyBean = getXPathValue(s1);
66             if (isReadOnlyBean!=null){
67                 if (isReadOnlyBean.equals(new String JavaDoc("true"))){
68                     testROBSpecific(descriptor,iasEjbExtraDesc,result);
69                 }else if (isReadOnlyBean.equals(new String JavaDoc("false"))){
70                     result.addGoodDetails(smh.getLocalString
71                                   ("tests.componentNameConstructor",
72                                    "For [ {0} ]",
73                                    new Object JavaDoc[] {compName.toString()}));
74                     result.passed(smh.getLocalString(getClass().getName()+".passed",
75                             "PASSED [AS-EJB ejb] is-read-only-bean is {0}",
76                             new Object JavaDoc[]{new String JavaDoc(isReadOnlyBean)}));
77                 }else{
78                     oneFailed = true;
79                     result.addErrorDetails(smh.getLocalString
80                                 ("tests.componentNameConstructor",
81                                 "For [ {0} ]",
82                                 new Object JavaDoc[] {compName.toString()}));
83                     result.failed(smh.getLocalString(getClass().getName()+".failed",
84                             "FAILED [AS-EJB ejb] value [{0}] for is-read-only-bean is incorrect. It can only be true or false",
85                             new Object JavaDoc[]{new String JavaDoc(isReadOnlyBean)}));
86                     }
87             }else
88             {
89                 result.addNaDetails(smh.getLocalString
90                               ("tests.componentNameConstructor",
91                                "For [ {0} ]",
92                                new Object JavaDoc[] {compName.toString()}));
93                 result.notApplicable(smh.getLocalString(getClass().getName()+".notApplicable",
94                             "NOT APPLICABLE [AS-EJB ejb] is-read-only-bean Element not defined"));
95             }
96             if(oneFailed)
97                 result.setStatus(Result.FAILED);
98             else if(oneWarning)
99                 result.setStatus(Result.WARNING);
100             
101         }catch(Exception JavaDoc ex){
102             result.addErrorDetails(smh.getLocalString
103                            ("tests.componentNameConstructor",
104                             "For [ {0} ]",
105                             new Object JavaDoc[] {compName.toString()}));
106             result.addErrorDetails(smh.getLocalString
107                  (getClass().getName() + ".notRun",
108                   "NOT RUN [AS-EJB] : Could not create a descriptor object"));
109         }
110         return result;
111         
112     }
113
114     public void testROBSpecific(EjbDescriptor descriptor, IASEjbExtraDescriptors iasEjbExtraDesc, Result result){
115         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
116         try{
117             //Read only Beans can only be Entity Beans
118
if(descriptor instanceof EjbEntityDescriptor){
119                 result.addGoodDetails(smh.getLocalString
120                               ("tests.componentNameConstructor",
121                                "For [ {0} ]",
122                                new Object JavaDoc[] {compName.toString()}));
123                 result.passed(smh.getLocalString(getClass().getName()+".passed1",
124                     "PASSED [AS-EJB ejb] : Read Only Beans can only be Entity Beans"));
125             }else if(descriptor instanceof EjbSessionDescriptor){
126                 oneFailed = true;
127                 result.addErrorDetails(smh.getLocalString
128                                 ("tests.componentNameConstructor",
129                                 "For [ {0} ]",
130                                 new Object JavaDoc[] {compName.toString()}));
131                 result.failed(smh.getLocalString(getClass().getName()+".failed1",
132                     "FAILED [AS-EJB ejb] : Read Only Beans cannot be Session Beans. They can only be Entity Beans"));
133                 return;
134             }else if(descriptor instanceof EjbMessageBeanDescriptor){
135                 oneFailed = true;
136                 result.addErrorDetails(smh.getLocalString
137                                 ("tests.componentNameConstructor",
138                                 "For [ {0} ]",
139                                 new Object JavaDoc[] {compName.toString()}));
140                 result.failed(smh.getLocalString(getClass().getName()+".failed2",
141                     "FAILED [AS-EJB ejb] : Read Only Beans cannot be Message Driven Beans. They can only be Entity Beans"));
142                 return;
143             }
144
145             //Only Container Managed Transactions are Allowed
146
String JavaDoc txnType = descriptor.getTransactionType();
147             if(txnType.equals(descriptor.CONTAINER_TRANSACTION_TYPE)){
148                 result.addGoodDetails(smh.getLocalString
149                               ("tests.componentNameConstructor",
150                                "For [ {0} ]",
151                                new Object JavaDoc[] {compName.toString()}));
152                 result.passed(smh.getLocalString(getClass().getName()+".passed2",
153                     "PASSED [AS-EJB ejb] : Read Only Beans can only have Container Managed Transactions"));
154             }else{
155                 oneFailed = true;
156                 result.addErrorDetails(smh.getLocalString
157                                 ("tests.componentNameConstructor",
158                                 "For [ {0} ]",
159                                 new Object JavaDoc[] {compName.toString()}));
160                 result.failed(smh.getLocalString(getClass().getName()+".failed3",
161                     "FAILED [AS-EJB ejb] : Read Only Beans cannot have Bean Managed Transactions"));
162             }
163
164             //Encourage not to have create/remove methods in Home Interface
165
String JavaDoc homeName = descriptor.getHomeClassName();
166             Class JavaDoc homeClass = null;
167             boolean foundCreateMethod = false;
168             boolean foundRemoveMethod = false;
169             homeClass = getVerifierContext().getClassLoader().loadClass(homeName);
170             Method methods[] = homeClass.getDeclaredMethods();
171             for(int i=0;i<methods.length;i++){
172                 if(methods[i].getName().startsWith("create")){
173                     foundCreateMethod = true;
174                     break;
175                 }
176             }
177
178             for(int i=0;i<methods.length;i++){
179                 if(methods[i].getName().startsWith("remove")){
180                     foundRemoveMethod = true;
181                     break;
182                 }
183             }
184             if(foundCreateMethod){
185                 oneWarning = true;
186                 result.addWarningDetails(smh.getLocalString
187                               ("tests.componentNameConstructor",
188                                "For [ {0} ]",
189                                new Object JavaDoc[] {compName.toString()}));
190                 result.addWarningDetails(smh.getLocalString(getClass().getName()+".warning",
191                     "WARNING [AS-EJB ejb] : Home interface [ {0} ] should have zero create Methods for Read Only Beans.",
192                     new Object JavaDoc[] {homeName}));
193             }else{
194                 result.addGoodDetails(smh.getLocalString
195                               ("tests.componentNameConstructor",
196                                "For [ {0} ]",
197                                new Object JavaDoc[] {compName.toString()}));
198                 result.passed(smh.getLocalString(getClass().getName()+".passed3",
199                     "PASSED [AS-EJB ejb] : Read Only Bean has zero create Methods"));
200             }
201
202             if(foundRemoveMethod){
203                 oneWarning = true;
204                 result.addWarningDetails(smh.getLocalString
205                               ("tests.componentNameConstructor",
206                                "For [ {0} ]",
207                                new Object JavaDoc[] {compName.toString()}));
208                 result.addWarningDetails(smh.getLocalString(getClass().getName()+".warning1",
209                     "WARNING [AS-EJB ejb] : Home interface [ {0} ] should have zero remove Methods for Read Only Beans.",
210                     new Object JavaDoc[] {homeName}));
211             }else{
212                 result.addGoodDetails(smh.getLocalString
213                               ("tests.componentNameConstructor",
214                                "For [ {0} ]",
215                                new Object JavaDoc[] {compName.toString()}));
216                 result.passed(smh.getLocalString(getClass().getName()+".passed4",
217                     "PASSED [AS-EJB ejb] : Read Only Bean has zero remove Methods"));
218             }
219
220             //Refresh Period Test
221
int refreshPeriod = iasEjbExtraDesc.getRefreshPeriodInSeconds();
222             if (refreshPeriod != -1){
223                 long refValue = new Integer JavaDoc(refreshPeriod).longValue();
224                 if(refValue <0 || refValue > Long.MAX_VALUE){
225                     result.addErrorDetails(smh.getLocalString
226                                     ("tests.componentNameConstructor",
227                                     "For [ {0} ]",
228                                     new Object JavaDoc[] {compName.toString()}));
229                     result.failed(smh.getLocalString(getClass().getName()+".failed4",
230                         "FAILED [AS-EJB ejb] : refresh-period-in-seconds is invalid. It should be between 0 and {0}." ,new Object JavaDoc[]{new Long JavaDoc( Long.MAX_VALUE)}));
231                 }else{
232                     result.addGoodDetails(smh.getLocalString
233                                   ("tests.componentNameConstructor",
234                                    "For [ {0} ]",
235                                    new Object JavaDoc[] {compName.toString()}));
236                     result.passed(smh.getLocalString(getClass().getName()+".passed5",
237                         "PASSED [AS-EJB ejb] : refresh-period-in-seconds is {0}",new Object JavaDoc[]{new Long JavaDoc(refValue)}));
238                 }
239             }else{
240                 result.addNaDetails(smh.getLocalString
241                               ("tests.componentNameConstructor",
242                                "For [ {0} ]",
243                                new Object JavaDoc[] {compName.toString()}));
244                 result.notApplicable(smh.getLocalString(getClass().getName()+".notApplicable1",
245                     "NOT APPLICABLE [AS-EJB ejb] refresh-period-in-seconds Element not defined"));
246             }
247             
248             if(oneFailed)
249                 result.setStatus(Result.FAILED);
250             
251         }catch(Exception JavaDoc ex){
252             oneFailed = true;
253             result.addErrorDetails(smh.getLocalString
254                             ("tests.componentNameConstructor",
255                             "For [ {0} ]",
256                             new Object JavaDoc[] {compName.toString()}));
257             result.addErrorDetails(smh.getLocalString
258                  (getClass().getName() + ".notRun",
259                   "NOT RUN [AS-EJB] : Could not create a descriptor object"));
260         }
261     }
262 }
263
Popular Tags