KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > ejb > EjbCheckMgrImpl


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.ejb;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import com.sun.ejb.codegen.GeneratorException;
31 import com.sun.enterprise.deployment.*;
32 import com.sun.enterprise.deployment.io.EjbDeploymentDescriptorFile;
33 import com.sun.enterprise.deployment.util.EjbBundleValidator;
34 import com.sun.enterprise.deployment.util.ModuleDescriptor;
35 import com.sun.enterprise.tools.verifier.*;
36 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
37 import com.sun.enterprise.tools.verifier.tests.dd.ParseDD;
38 import com.sun.enterprise.tools.verifier.wsclient.WebServiceClientCheckMgrImpl;
39 import com.sun.enterprise.util.io.FileUtils;
40 import com.sun.jdo.spi.persistence.support.ejb.ejbc.JDOCodeGenerator;
41
42 /**
43  * Ejb harness
44  */

45 public class EjbCheckMgrImpl extends CheckMgr implements JarCheck {
46
47     /**
48      * name of the file containing the list of tests for the ejb architecture
49      */

50     private static final String JavaDoc testsListFileName = "TestNamesEjb.xml"; // NOI18N
51
private static final String JavaDoc sunONETestsListFileName = getSunPrefix()
52             .concat(testsListFileName);
53     // the JDO Code generator needs to be initialized once per BundleDescriptor
54
private JDOCodeGenerator jdc = new JDOCodeGenerator();
55
56     public EjbCheckMgrImpl(FrameworkContext frameworkContext) {
57         this.frameworkContext = frameworkContext;
58     }
59
60     /**
61      * Check Ejb for spec. conformance
62      *
63      * @param descriptor Ejb descriptor
64      */

65     public void check(Descriptor descriptor) throws Exception JavaDoc {
66         // run persistence tests first.
67
checkPersistenceUnits(EjbBundleDescriptor.class.cast(descriptor));
68         // an EjbBundleDescriptor can have an WebServicesDescriptor
69
checkWebServices(descriptor);
70         // an EjbBundleDescriptor can have WebService References
71
checkWebServicesClient(descriptor);
72
73         if (frameworkContext.isPartition() &&
74                 !frameworkContext.isEjb())
75             return;
76
77         EjbBundleDescriptor bundleDescriptor = (EjbBundleDescriptor) descriptor;
78         setDescClassLoader(bundleDescriptor);
79         // DOL (jerome): is asking us to call this in some cases, like when
80
// an ejb-ref is unresolved etc.
81
try {
82             EjbBundleValidator validator = new EjbBundleValidator();
83             validator.accept(bundleDescriptor);
84         } catch (Exception JavaDoc e) {
85         } // nothing can be done
86

87         // initialize JDOC if bundle has CMP's
88
if (bundleDescriptor.containsCMPEntity()) {
89             try {
90                 // See bug #6274161. We now pass an additional boolean
91
// to indicate whether we are in portable or AS mode.
92
jdc.init(bundleDescriptor, context.getClassLoader(),
93                         getAbstractArchiveUri(bundleDescriptor),
94                         frameworkContext.isPortabilityMode());
95             } catch (Throwable JavaDoc ex) {
96                 context.setJDOException(ex);
97             }
98         }
99         // set the JDO Codegenerator into the context
100
context.setJDOCodeGenerator(jdc);
101         
102         // run the ParseDD test
103
if (bundleDescriptor.getSpecVersion().compareTo("2.1") < 0) { // NOI18N
104
EjbDeploymentDescriptorFile ddf = new EjbDeploymentDescriptorFile();
105             File JavaDoc file = new File JavaDoc(getAbstractArchiveUri(bundleDescriptor),
106                     ddf.getDeploymentDescriptorPath());
107             FileInputStream JavaDoc is = new FileInputStream JavaDoc(file);
108             try {
109                 if (is != null) {
110                     Result result = new ParseDD().validateEJBDescriptor(is);
111                     result.setComponentName(new File JavaDoc(bundleDescriptor.getModuleDescriptor().
112                             getArchiveUri()).getName());
113                     setModuleName(result);
114                     frameworkContext.getResultManager().add(result);
115                 }
116             } finally {
117                 try {
118                     if(is != null) {
119                         is.close();
120                     }
121                 } catch (Exception JavaDoc e) {}
122             }
123         }
124
125         for (Iterator JavaDoc itr = bundleDescriptor.getEjbs().iterator();
126              itr.hasNext();) {
127             EjbDescriptor ejbDescriptor = (EjbDescriptor) itr.next();
128             super.check(ejbDescriptor);
129         }
130
131         if (bundleDescriptor.containsCMPEntity() &&
132                 context.getJDOException() == null) {
133             try {
134                 jdc.cleanup();
135             } catch (GeneratorException ge) {
136             } // eat up the exception
137
context.setJDOCodeGenerator(null);
138         }
139     }
140
141     /**
142      * return the configuration file name for the list of tests pertinent to the
143      * connector architecture
144      *
145      * @return <code>String</code> filename containing the list of tests
146      */

147     protected String JavaDoc getTestsListFileName() {
148         return testsListFileName;
149     }
150
151     /**
152      * @return <code>String</code> filename containing sunone tests
153      */

154     protected String JavaDoc getSunONETestsListFileName() {
155         return sunONETestsListFileName;
156     }
157
158     protected void checkWebServicesClient(Descriptor descriptor)
159             throws Exception JavaDoc {
160         if (frameworkContext.isPartition() &&
161                 !frameworkContext.isWebServicesClient())
162             return;
163         EjbBundleDescriptor desc = (EjbBundleDescriptor) descriptor;
164         WebServiceClientCheckMgrImpl webServiceClientCheckMgr = new WebServiceClientCheckMgrImpl(
165                 frameworkContext);
166         if (desc.hasWebServiceClients()) {
167             Set JavaDoc ejbdescs = desc.getEjbs();
168             Iterator JavaDoc ejbIt = ejbdescs.iterator();
169
170             while (ejbIt.hasNext()) {
171                 EjbDescriptor ejbDesc = (EjbDescriptor) ejbIt.next();
172                 context.setEjbDescriptorForServiceRef(ejbDesc);
173                 Set JavaDoc serviceRefDescriptors = ejbDesc.getServiceReferenceDescriptors();
174                 Iterator JavaDoc it = serviceRefDescriptors.iterator();
175                 while (it.hasNext()) {
176                     webServiceClientCheckMgr.setVerifierContext(context);
177                     webServiceClientCheckMgr.check(
178                             (ServiceReferenceDescriptor) it.next());
179                 }
180             }
181             context.setEjbDescriptorForServiceRef(null);
182         }
183     }
184
185     protected String JavaDoc getSchemaVersion(Descriptor descriptor) {
186         return getBundleDescriptor(descriptor).getSpecVersion();
187     }
188
189     protected void setModuleName(Result r) {
190         r.setModuleName(Result.EJB);
191     }
192
193     protected EjbBundleDescriptor getBundleDescriptor(Descriptor descriptor) {
194         return ((EjbDescriptor) descriptor).getEjbBundleDescriptor();
195     }
196
197     /**
198      * entity and mdb assertions should not be run for session descriptors and
199      * similarly the other way round.
200      */

201     protected boolean isApplicable(TestInformation test, Descriptor descriptor) {
202         String JavaDoc testName = test.getClassName();
203         if(descriptor instanceof EjbSessionDescriptor &&
204                 (testName.indexOf("tests.ejb.entity")>=0 || // NOI18N
205
testName.indexOf("tests.ejb.messagebean")>=0)) // NOI18N
206
return false;
207         if(descriptor instanceof EjbEntityDescriptor &&
208                 (testName.indexOf("tests.ejb.session")>=0 || // NOI18N
209
testName.indexOf("tests.ejb.messagebean")>=0)) // NOI18N
210
return false;
211         if(descriptor instanceof EjbMessageBeanDescriptor &&
212                 (testName.indexOf("tests.ejb.session")>=0 || // NOI18N
213
testName.indexOf("tests.ejb.entity")>=0)) // NOI18N
214
return false;
215         return true;
216     }
217
218     private String JavaDoc getAbstractArchiveUri(EjbBundleDescriptor desc) {
219         String JavaDoc archBase = context.getAbstractArchive().getArchiveUri();
220         ModuleDescriptor mdesc = desc.getModuleDescriptor();
221         if(mdesc.isStandalone()) {
222             return archBase;
223         } else {
224             return archBase + File.separator +
225                     FileUtils.makeFriendlyFileName(mdesc.getArchiveUri());
226         }
227     }
228
229     private void setDescClassLoader(EjbBundleDescriptor bundleDescriptor) {
230         Iterator JavaDoc bundleItr = bundleDescriptor.getEjbs().iterator();
231         while (bundleItr.hasNext()) {
232             EjbDescriptor descriptor = (EjbDescriptor) bundleItr.next();
233             if (descriptor instanceof IASEjbCMPEntityDescriptor) {
234                 ((IASEjbCMPEntityDescriptor) (descriptor)).setClassLoader(
235                         context.getClassLoader());
236             }
237         }
238     }
239
240     protected ComponentNameConstructor getComponentNameConstructor(
241             Descriptor descriptor) {
242         return new ComponentNameConstructor((EjbDescriptor)descriptor);
243     }
244
245 }
246
Popular Tags