KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb;
25
26 import com.sun.enterprise.tools.verifier.Result;
27 import com.sun.enterprise.tools.verifier.Verifier;
28 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
29 import com.sun.enterprise.deployment.*;
30
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Set JavaDoc;
35
36 /**
37  * Local interface should not be exposed through remote interface
38  *
39  * @author Sheetal Vartak
40  * @version
41  */

42 public class LocalInterfaceExposed extends EjbTest implements EjbCheck {
43     
44     /**
45      * Bean interface type test.
46      * The bean provider must provide either Local or Remote or Both interfaces
47      *
48      * @param descriptor the Enterprise Java Bean deployment descriptor
49      *
50      * @return <code>Result</code> the results for this assertion
51      */

52     public Result check(EjbDescriptor descriptor) {
53         
54         Result result = getInitializedResult();
55         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
56         
57         if (!(descriptor instanceof EjbSessionDescriptor) &&
58                 !(descriptor instanceof EjbEntityDescriptor)) {
59             addNaDetails(result, compName);
60             result.notApplicable(smh.getLocalString
61                             (getClass().getName()+".notApplicable1",
62                             "Test apply only to session or entity beans."));
63             return result;
64         }
65         
66         EjbBundleDescriptor bundle = descriptor.getEjbBundleDescriptor();
67         Iterator JavaDoc iterator = (bundle.getEjbs()).iterator();
68         Set JavaDoc<String JavaDoc> localInterfaces = new HashSet JavaDoc<String JavaDoc>();
69         while(iterator.hasNext()) {
70             EjbAbstractDescriptor entity = (EjbAbstractDescriptor) iterator.next();
71             if (entity.getLocalClassName() != null)
72                 localInterfaces.add(entity.getLocalClassName());
73             localInterfaces.addAll(entity.getLocalBusinessClassNames());
74         }
75         ClassLoader JavaDoc jcl = getVerifierContext().getClassLoader();
76         try {
77             Set JavaDoc<String JavaDoc> remoteInterfaces = new HashSet JavaDoc<String JavaDoc>();
78             if(descriptor.getRemoteClassName()!=null)
79                 remoteInterfaces.add(descriptor.getRemoteClassName());
80             remoteInterfaces.addAll(descriptor.getRemoteBusinessClassNames());
81             
82             for (String JavaDoc intf : remoteInterfaces) {
83                 Class JavaDoc c = Class.forName(intf, false, getVerifierContext().getClassLoader());
84                 Method JavaDoc[] methods = c.getDeclaredMethods();
85                 for(int i=0; i<methods.length; i++) {
86                     //check all the local interfaces in the ejb bundle
87
for(Iterator JavaDoc itr = localInterfaces.iterator();itr.hasNext();) {
88                         String JavaDoc localIntf = (String JavaDoc) itr.next();
89                         Class JavaDoc returnType = methods[i].getReturnType();
90                         if((getBaseComponentType(returnType).getName()).equals(localIntf) ||
91                                 (contains(methods[i].getParameterTypes(), localIntf))) {
92                             addErrorDetails(result, compName);
93                             result.failed(smh.getLocalString
94                                     (getClass().getName() + ".failed",
95                                     "Error : Local Interface [ {0} ] has been " +
96                                     "exposed in remote interface [ {1} ]",
97                                     new Object JavaDoc[] {localIntf, c.getName()}));
98                             return result;
99                         }
100                     }
101                 }
102             }
103             addGoodDetails(result, compName);
104             result.passed(smh.getLocalString
105                             (getClass().getName() + ".passed",
106                             "Valid Remote interface."));
107         } catch (ClassNotFoundException JavaDoc e) {
108             Verifier.debug(e);
109             addErrorDetails(result, compName);
110             result.failed(smh.getLocalString
111                             (getClass().getName() + ".failedException",
112                             "Error: [ {0} ] class not found.",
113                             new Object JavaDoc[] {descriptor.getRemoteClassName()}));
114         }
115         return result;
116     }
117     /** returns true if intf is contained in this args array */
118     private boolean contains(Class JavaDoc[] args, String JavaDoc intf) {
119         for (int i = 0; i < args.length; i++)
120             if(getBaseComponentType(args[i]).getName().equals(intf))
121                 return true;
122         
123         return false;
124     }
125
126     /** This api recursively looks for class.getComponentType. This handles
127      * cases where array of arrays are used. */

128     private Class JavaDoc getBaseComponentType(Class JavaDoc cls) {
129         if(!cls.isArray())
130             return cls;
131         return getBaseComponentType(cls.getComponentType());
132     }
133 }
134
135
Popular Tags