KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 package com.sun.enterprise.tools.verifier.tests.web;
26
27 import com.sun.enterprise.deployment.InjectionTarget;
28 import com.sun.enterprise.tools.verifier.Result;
29 import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor;
30 import com.sun.enterprise.deployment.WebBundleDescriptor;
31 import java.util.Set JavaDoc;
32 import javax.servlet.Servlet JavaDoc;
33 import javax.servlet.SingleThreadModel JavaDoc;
34
35 /**
36  * Assertion
37  *
38  * EntityManager should not be injected into a web application that uses multithread model.
39  * EntityManager is not thread safe, hence it should not be injected into a web application
40  * that uses multithreaded model.
41  *
42  * @author bshankar@sun.com
43  */

44 public class EntityManagerInjection extends WebTest implements WebCheck {
45     
46     final static String JavaDoc className = EntityManagerInjection.class.getName();
47     
48     public Result check(WebBundleDescriptor descriptor) {
49         
50         Result result = getInitializedResult();
51         addWarningDetails(result,
52                 getVerifierContext().getComponentNameConstructor());
53         result.setStatus(Result.PASSED); //default status is PASSED
54

55         for(EntityManagerReferenceDescriptor emRefDesc : descriptor.getEntityManagerReferenceDescriptors()) {
56             Set JavaDoc<InjectionTarget> injectionTargets = emRefDesc.getInjectionTargets();
57             if(injectionTargets != null) {
58                 for(InjectionTarget it : injectionTargets) {
59                     String JavaDoc itClassName = it.getClassName();
60                     String JavaDoc errMsg = smh.getLocalString(className + ".warning",
61                             "Found a persistence unit by name [ {0} ] injected into [ {1} ].",
62                             new Object JavaDoc[]{emRefDesc.getUnitName(), itClassName});
63                     try {
64                         Class JavaDoc c = Class.forName(itClassName, false, getVerifierContext().getClassLoader());
65                         if(!(Servlet JavaDoc.class.isAssignableFrom(c))) {
66                             result.warning(errMsg);
67                         } else if (!(SingleThreadModel JavaDoc.class.isAssignableFrom(c))) {
68                             result.warning(errMsg);
69                         }
70                     } catch(Exception JavaDoc ex) {
71                         result.warning(errMsg);
72                     }
73                 }
74             }
75         }
76         return result;
77     }
78     
79 }
80
Popular Tags